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
32 #include "gnunet_common.h"
33 #include "gnunet_client_lib.h"
34 #include "gnunet_util_lib.h"
35 #include "gnunet_peer_lib.h"
36 #include "gnunet_mesh_service.h"
38 #include "mesh_protocol.h"
40 #define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
43 /******************************************************************************/
44 /************************ DATA STRUCTURES ****************************/
45 /******************************************************************************/
48 * Transmission queue to the service
50 struct GNUNET_MESH_TransmitHandle
56 struct GNUNET_MESH_TransmitHandle *next;
61 struct GNUNET_MESH_TransmitHandle *prev;
64 * Tunnel this message is sent on / for (may be NULL for control messages).
66 struct GNUNET_MESH_Tunnel *tunnel;
69 * Callback to obtain the message to transmit, or NULL if we
70 * got the message in 'data'. Notice that messages built
71 * by 'notify' need to be encapsulated with information about
74 GNUNET_CONNECTION_TransmitReadyNotify notify;
77 * Closure for 'notify'
82 * How long is this message valid. Once the timeout has been
83 * reached, the message must no longer be sent. If this
84 * is a message with a 'notify' callback set, the 'notify'
85 * function should be called with 'buf' NULL and size 0.
87 struct GNUNET_TIME_Absolute timeout;
90 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
92 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
95 * Priority of the message. The queue is sorted by priority,
96 * control messages have the maximum priority (UINT32_MAX).
101 * Target of the message, 0 for multicast. This field
102 * is only valid if 'notify' is non-NULL.
104 GNUNET_PEER_Id target;
107 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
114 * Opaque handle to the service.
116 struct GNUNET_MESH_Handle
120 * Handle to the server connection, to send messages later
122 struct GNUNET_CLIENT_Connection *client;
125 * Set of handlers used for processing incoming messages in the tunnels
127 const struct GNUNET_MESH_MessageHandler *message_handlers;
130 * Set of applications that should be claimed to be offered at this node.
131 * Note that this is just informative, the appropiate handlers must be
132 * registered independently and the mapping is up to the developer of the
133 * client application.
135 const GNUNET_MESH_ApplicationType *applications;
138 * Double linked list of the tunnels this client is connected to.
140 struct GNUNET_MESH_Tunnel *tunnels_head;
141 struct GNUNET_MESH_Tunnel *tunnels_tail;
144 * Callback for inbound tunnel creation
146 GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
149 * Callback for inbound tunnel disconnection
151 GNUNET_MESH_TunnelEndHandler *cleaner;
154 * Handle to cancel pending transmissions in case of disconnection
156 struct GNUNET_CLIENT_TransmitHandle *th;
159 * Closure for all the handlers given by the client
164 * Messages to send to the service
166 struct GNUNET_MESH_TransmitHandle *th_head;
167 struct GNUNET_MESH_TransmitHandle *th_tail;
170 * tid of the next tunnel to create (to avoid reusing IDs often)
172 MESH_TunnelNumber next_tid;
173 unsigned int n_handlers;
174 unsigned int n_applications;
175 unsigned int max_queue_size;
178 * Have we started the task to receive messages from the service
179 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
184 * Number of packets queued
186 unsigned int npackets;
189 * Configuration given by the client, in case of reconnection
191 const struct GNUNET_CONFIGURATION_Handle *cfg;
194 * Time to the next reconnect in case one reconnect fails
196 struct GNUNET_TIME_Relative reconnect_time;
199 * Task for trying to reconnect.
201 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
206 * Description of a peer
208 struct GNUNET_MESH_Peer
211 * ID of the peer in short form
216 * Tunnel this peer belongs to
218 struct GNUNET_MESH_Tunnel *t;
221 * Flag indicating whether service has informed about its connection
229 * Opaque handle to a tunnel.
231 struct GNUNET_MESH_Tunnel
237 struct GNUNET_MESH_Tunnel *next;
238 struct GNUNET_MESH_Tunnel *prev;
241 * Callback to execute when peers connect to the tunnel
243 GNUNET_MESH_PeerConnectHandler connect_handler;
246 * Callback to execute when peers disconnect from the tunnel
248 GNUNET_MESH_PeerDisconnectHandler disconnect_handler;
251 * Closure for the connect/disconnect handlers
256 * Handle to the mesh this tunnel belongs to
258 struct GNUNET_MESH_Handle *mesh;
261 * Local ID of the tunnel
263 MESH_TunnelNumber tid;
266 * Owner of the tunnel. 0 if the tunnel is the local client.
268 GNUNET_PEER_Id owner;
271 * All peers added to the tunnel
273 struct GNUNET_MESH_Peer **peers;
276 * List of application types that have been requested for this tunnel
278 GNUNET_MESH_ApplicationType *apps;
281 * Any data the caller wants to put in here
286 * Number of peers added to the tunnel
291 * Number of packets queued in this tunnel
293 unsigned int npackets;
296 * Number of applications requested this tunnel
303 /******************************************************************************/
304 /*********************** AUXILIARY FUNCTIONS *************************/
305 /******************************************************************************/
308 * Get the tunnel handler for the tunnel specified by id from the given handle
309 * @param h Mesh handle
310 * @param tid ID of the wanted tunnel
311 * @return handle to the required tunnel or NULL if not found
313 static struct GNUNET_MESH_Tunnel *
314 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
316 struct GNUNET_MESH_Tunnel *t;
330 * Create a new tunnel and insert it in the tunnel list of the mesh handle
331 * @param h Mesh handle
332 * @param tid desired tid of the tunnel, 0 to assign one automatically
333 * @return handle to the created tunnel
335 static struct GNUNET_MESH_Tunnel *
336 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
338 struct GNUNET_MESH_Tunnel *t;
340 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
341 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
345 t->tid = h->next_tid;
346 while (NULL != retrieve_tunnel (h, h->next_tid))
349 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
350 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
362 * Destroy the specified tunnel.
363 * - Destroys all peers, calling the disconnect callback on each if needed
364 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
365 * - Calls cleaner if tunnel was inbound
366 * - Frees all memory used
368 * @param t Pointer to the tunnel.
369 * @param call_cleaner Whether to call the cleaner handler.
371 * @return Handle to the required tunnel or NULL if not found.
374 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
376 struct GNUNET_MESH_Handle *h;
377 struct GNUNET_PeerIdentity pi;
378 struct GNUNET_MESH_TransmitHandle *th;
379 struct GNUNET_MESH_TransmitHandle *next;
382 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
391 /* disconnect all peers */
392 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
393 for (i = 0; i < t->npeers; i++)
395 if ( (NULL != t->disconnect_handler) && t->peers[i]->connected)
397 GNUNET_PEER_resolve (t->peers[i]->id, &pi);
398 t->disconnect_handler (t->cls, &pi);
400 GNUNET_PEER_change_rc (t->peers[i]->id, -1);
401 GNUNET_free (t->peers[i]);
404 /* signal tunnel destruction */
405 if ( (NULL != h->cleaner) && (0 != t->owner) && (GNUNET_YES == call_cleaner) )
406 h->cleaner (h->cls, t, t->ctx);
408 /* check that clients did not leave messages behind in the queue */
409 for (th = h->th_head; NULL != th; th = next)
414 /* Clients should have aborted their requests already.
415 * Management traffic should be ok, as clients can't cancel that */
416 GNUNET_break (NULL == th->notify);
417 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
419 /* clean up request */
420 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
421 GNUNET_SCHEDULER_cancel (th->timeout_task);
425 /* if there are no more pending requests with mesh service, cancel active request */
426 /* Note: this should be unnecessary... */
427 if ( (NULL == h->th_head) && (NULL != h->th))
429 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
435 GNUNET_free (t->peers);
437 GNUNET_PEER_change_rc (t->owner, -1);
438 if (0 != t->napps && t->apps)
439 GNUNET_free (t->apps);
446 * Get the peer descriptor for the peer with id from the given tunnel
447 * @param t Tunnel handle
448 * @param id Short form ID of the wanted peer
449 * @return handle to the requested peer or NULL if not found
451 static struct GNUNET_MESH_Peer *
452 retrieve_peer (struct GNUNET_MESH_Tunnel *t, GNUNET_PEER_Id id)
456 for (i = 0; i < t->npeers; i++)
457 if (t->peers[i]->id == id)
464 * Add a peer into a tunnel
465 * @param t Tunnel handle
466 * @param pi Full ID of the new peer
467 * @return handle to the newly created peer
469 static struct GNUNET_MESH_Peer *
470 add_peer_to_tunnel (struct GNUNET_MESH_Tunnel *t,
471 const struct GNUNET_PeerIdentity *pi)
473 struct GNUNET_MESH_Peer *p;
481 id = GNUNET_PEER_intern (pi);
483 p = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
486 GNUNET_array_append (t->peers, t->npeers, p);
492 * Remove a peer from a tunnel
493 * @param p Peer handle
496 remove_peer_from_tunnel (struct GNUNET_MESH_Peer *p)
500 for (i = 0; i < p->t->npeers; i++)
502 if (p->t->peers[i] == p)
505 if (i == p->t->npeers)
510 p->t->peers[i] = p->t->peers[p->t->npeers - 1];
511 GNUNET_array_grow (p->t->peers, p->t->npeers, p->t->npeers - 1);
516 * Notify client that the transmission has timed out
518 * @param tc task context
521 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
523 struct GNUNET_MESH_TransmitHandle *th = cls;
524 struct GNUNET_MESH_Handle *mesh;
526 mesh = th->tunnel->mesh;
527 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
528 if (th->notify != NULL)
529 th->notify (th->notify_cls, 0, NULL);
531 if ((NULL == mesh->th_head) && (NULL != mesh->th))
533 /* queue empty, no point in asking for transmission */
534 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
541 * Add a transmit handle to the transmission queue by priority and set the
544 * @param h mesh handle with the queue head and tail
545 * @param th handle to the packet to be transmitted
548 add_to_queue (struct GNUNET_MESH_Handle *h,
549 struct GNUNET_MESH_TransmitHandle *th)
551 struct GNUNET_MESH_TransmitHandle *p;
554 while ((NULL != p) && (th->priority <= p->priority))
560 GNUNET_CONTAINER_DLL_insert_after (h->th_head, h->th_tail, p, th);
561 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
564 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
565 (th->timeout), &timeout_transmission, th);
570 * Auxiliary function to send an already constructed packet to the service.
571 * Takes care of creating a new queue element, copying the message and
572 * calling the tmt_rdy function if necessary.
574 * @param h mesh handle
575 * @param msg message to transmit
576 * @param tunnel tunnel this send is related to (NULL if N/A)
579 send_packet (struct GNUNET_MESH_Handle *h,
580 const struct GNUNET_MessageHeader *msg,
581 struct GNUNET_MESH_Tunnel *tunnel);
585 * Reconnect callback: tries to reconnect again after a failer previous
587 * @param cls closure (mesh handle)
588 * @param tc task context
591 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
595 * Send a connect packet to the service with the applications and types
596 * requested by the user.
598 * @param h The mesh handle.
602 send_connect (struct GNUNET_MESH_Handle *h)
606 size = sizeof (struct GNUNET_MESH_ClientConnect);
607 size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
608 size += h->n_handlers * sizeof (uint16_t);
610 char buf[size] GNUNET_ALIGN;
611 struct GNUNET_MESH_ClientConnect *msg;
612 GNUNET_MESH_ApplicationType *apps;
617 /* build connection packet */
618 msg = (struct GNUNET_MESH_ClientConnect *) buf;
619 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
620 msg->header.size = htons (size);
621 apps = (GNUNET_MESH_ApplicationType *) &msg[1];
622 for (napps = 0; napps < h->n_applications; napps++)
624 apps[napps] = htonl (h->applications[napps]);
625 LOG (GNUNET_ERROR_TYPE_DEBUG, " app %u\n", h->applications[napps]);
627 types = (uint16_t *) & apps[napps];
628 for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
629 types[ntypes] = htons (h->message_handlers[ntypes].type);
630 msg->applications = htons (napps);
631 msg->types = htons (ntypes);
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "Sending %lu bytes long message %d types and %d apps\n",
634 ntohs (msg->header.size), ntypes, napps);
635 send_packet (h, &msg->header, NULL);
641 * Reconnect to the service, retransmit all infomation to try to restore the
644 * @param h handle to the mesh
646 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
649 do_reconnect (struct GNUNET_MESH_Handle *h)
651 struct GNUNET_MESH_Tunnel *t;
654 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
656 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
658 h->in_receive = GNUNET_NO;
662 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
665 if (NULL != h->client)
667 GNUNET_CLIENT_disconnect (h->client);
671 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
672 if (h->client == NULL)
674 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
677 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
678 GNUNET_TIME_relative_multiply
679 (h->reconnect_time, 2));
680 LOG (GNUNET_ERROR_TYPE_DEBUG, " Next retry in %sms\n",
681 GNUNET_TIME_relative_to_string (h->reconnect_time));
687 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
690 /* Rebuild all tunnels */
691 for (t = h->tunnels_head; NULL != t; t = t->next)
693 struct GNUNET_MESH_TunnelMessage tmsg;
694 struct GNUNET_MESH_PeerControl pmsg;
696 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
698 /* Tunnel was created by service (incoming tunnel) */
699 /* TODO: Notify service of missing tunnel, to request
700 * creator to recreate path (find a path to him via DHT?)
704 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
705 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
706 tmsg.tunnel_id = htonl (t->tid);
707 send_packet (h, &tmsg.header, t);
709 pmsg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
710 pmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
711 pmsg.tunnel_id = htonl (t->tid);
713 /* Reconnect all peers */
714 for (i = 0; i < t->npeers; i++)
716 GNUNET_PEER_resolve (t->peers[i]->id, &pmsg.peer);
717 if (NULL != t->disconnect_handler && t->peers[i]->connected)
718 t->disconnect_handler (t->cls, &pmsg.peer);
719 /* If the tunnel was "by type", dont connect individual peers */
721 send_packet (t->mesh, &pmsg.header, t);
723 /* Reconnect all types, if any */
724 for (i = 0; i < t->napps; i++)
726 struct GNUNET_MESH_ConnectPeerByType msg;
728 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
729 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
730 msg.tunnel_id = htonl (t->tid);
731 msg.type = htonl (t->apps[i]);
732 send_packet (t->mesh, &msg.header, t);
739 * Reconnect callback: tries to reconnect again after a failer previous
741 * @param cls closure (mesh handle)
742 * @param tc task context
745 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
747 struct GNUNET_MESH_Handle *h = cls;
749 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
750 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
757 * Reconnect to the service, retransmit all infomation to try to restore the
760 * @param h handle to the mesh
762 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
765 reconnect (struct GNUNET_MESH_Handle *h)
767 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
768 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
769 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
774 /******************************************************************************/
775 /*********************** RECEIVE HANDLERS ****************************/
776 /******************************************************************************/
779 * Process the new tunnel notification and add it to the tunnels in the handle
781 * @param h The mesh handle
782 * @param msg A message with the details of the new incoming tunnel
785 process_tunnel_created (struct GNUNET_MESH_Handle *h,
786 const struct GNUNET_MESH_TunnelNotification *msg)
788 struct GNUNET_MESH_Tunnel *t;
789 MESH_TunnelNumber tid;
791 tid = ntohl (msg->tunnel_id);
792 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
797 if (NULL != h->new_tunnel)
799 struct GNUNET_ATS_Information atsi;
801 t = create_tunnel (h, tid);
802 t->owner = GNUNET_PEER_intern (&msg->peer);
804 t->peers = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer *));
805 t->peers[0] = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
807 t->peers[0]->connected = 1;
808 t->peers[0]->id = t->owner;
809 GNUNET_PEER_change_rc (t->owner, 1);
814 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, &atsi);
815 LOG (GNUNET_ERROR_TYPE_DEBUG, "new incoming tunnel %X\n", t->tid);
819 struct GNUNET_MESH_TunnelMessage d_msg;
821 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
823 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
824 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
825 d_msg.tunnel_id = msg->tunnel_id;
827 send_packet (h, &d_msg.header, NULL);
834 * Process the tunnel destroy notification and free associated resources
836 * @param h The mesh handle
837 * @param msg A message with the details of the tunnel being destroyed
840 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
841 const struct GNUNET_MESH_TunnelMessage *msg)
843 struct GNUNET_MESH_Tunnel *t;
844 MESH_TunnelNumber tid;
846 tid = ntohl (msg->tunnel_id);
847 t = retrieve_tunnel (h, tid);
857 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
858 destroy_tunnel (t, GNUNET_YES);
864 * Process the new peer event and notify the upper level of it
866 * @param h The mesh handle
867 * @param msg A message with the details of the peer event
870 process_peer_event (struct GNUNET_MESH_Handle *h,
871 const struct GNUNET_MESH_PeerControl *msg)
873 struct GNUNET_MESH_Tunnel *t;
874 struct GNUNET_MESH_Peer *p;
875 struct GNUNET_ATS_Information atsi;
879 LOG (GNUNET_ERROR_TYPE_DEBUG, "processig peer event\n");
880 size = ntohs (msg->header.size);
881 if (size != sizeof (struct GNUNET_MESH_PeerControl))
886 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
892 id = GNUNET_PEER_search (&msg->peer);
893 if ((p = retrieve_peer (t, id)) == NULL)
894 p = add_peer_to_tunnel (t, &msg->peer);
895 if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD == ntohs (msg->header.type))
897 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding peer\n");
898 if (NULL != t->connect_handler)
902 t->connect_handler (t->cls, &msg->peer, &atsi);
908 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing peer\n");
909 if (NULL != t->disconnect_handler && p->connected)
911 t->disconnect_handler (t->cls, &msg->peer);
913 remove_peer_from_tunnel (p);
916 LOG (GNUNET_ERROR_TYPE_DEBUG, "processing peer event END\n");
921 * Process the incoming data packets
923 * @param h The mesh handle
924 * @param message A message encapsulating the data
926 * @return GNUNET_YES if everything went fine
927 * GNUNET_NO if client closed connection (h no longer valid)
930 process_incoming_data (struct GNUNET_MESH_Handle *h,
931 const struct GNUNET_MessageHeader *message)
933 const struct GNUNET_MessageHeader *payload;
934 const struct GNUNET_MESH_MessageHandler *handler;
935 const struct GNUNET_PeerIdentity *peer;
936 struct GNUNET_MESH_Unicast *ucast;
937 struct GNUNET_MESH_Multicast *mcast;
938 struct GNUNET_MESH_ToOrigin *to_orig;
939 struct GNUNET_MESH_Tunnel *t;
943 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
944 type = ntohs (message->type);
947 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
948 ucast = (struct GNUNET_MESH_Unicast *) message;
950 t = retrieve_tunnel (h, ntohl (ucast->tid));
951 payload = (struct GNUNET_MessageHeader *) &ucast[1];
953 LOG (GNUNET_ERROR_TYPE_DEBUG, " ucast on tunnel %s [%X]\n",
954 GNUNET_i2s (peer), ntohl (ucast->tid));
956 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
957 mcast = (struct GNUNET_MESH_Multicast *) message;
958 t = retrieve_tunnel (h, ntohl (mcast->tid));
959 payload = (struct GNUNET_MessageHeader *) &mcast[1];
961 LOG (GNUNET_ERROR_TYPE_DEBUG, " mcast on tunnel %s [%X]\n",
962 GNUNET_i2s (peer), ntohl (mcast->tid));
964 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
965 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
966 t = retrieve_tunnel (h, ntohl (to_orig->tid));
967 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
968 peer = &to_orig->sender;
969 LOG (GNUNET_ERROR_TYPE_DEBUG, " torig on tunnel %s [%X]\n",
970 GNUNET_i2s (peer), ntohl (to_orig->tid));
978 /* Tunnel was ignored, probably service didn't get it yet */
981 type = ntohs (payload->type);
982 for (i = 0; i < h->n_handlers; i++)
984 handler = &h->message_handlers[i];
985 if (handler->type == type)
987 struct GNUNET_ATS_Information atsi;
992 handler->callback (h->cls, t, &t->ctx, peer, payload, &atsi))
994 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH: callback caused disconnection\n");
995 GNUNET_MESH_disconnect (h);
1000 LOG (GNUNET_ERROR_TYPE_DEBUG,
1001 "MESH: callback completed successfully\n");
1011 * Function to process all messages received from the service
1013 * @param cls closure
1014 * @param msg message received, NULL on timeout or fatal error
1017 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1019 struct GNUNET_MESH_Handle *h = cls;
1023 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL msg\n");
1027 LOG (GNUNET_ERROR_TYPE_DEBUG, "received a message type %hu from MESH\n",
1029 switch (ntohs (msg->type))
1031 /* Notify of a new incoming tunnel */
1032 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1033 process_tunnel_created (h, (struct GNUNET_MESH_TunnelNotification *) msg);
1035 /* Notify of a tunnel disconnection */
1036 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1037 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1039 /* Notify of a new peer or a peer disconnect in the tunnel */
1040 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD:
1041 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL:
1042 process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
1044 /* Notify of a new data packet in the tunnel */
1045 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1046 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1047 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1048 if (GNUNET_NO == process_incoming_data (h, msg))
1051 /* We shouldn't get any other packages, log and ignore */
1053 LOG (GNUNET_ERROR_TYPE_WARNING,
1054 "MESH: unsolicited message form service (type %d)\n",
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1058 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1059 GNUNET_TIME_UNIT_FOREVER_REL);
1063 /******************************************************************************/
1064 /************************ SEND FUNCTIONS ****************************/
1065 /******************************************************************************/
1068 * Function called to send a message to the service.
1069 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1072 * @param cls closure, the mesh handle
1073 * @param size number of bytes available in buf
1074 * @param buf where the callee should write the connect message
1075 * @return number of bytes written to buf
1078 send_callback (void *cls, size_t size, void *buf)
1080 struct GNUNET_MESH_Handle *h = cls;
1081 struct GNUNET_MESH_TransmitHandle *th;
1086 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() Buffer %u\n", size);
1088 if ((0 == size) || (NULL == buf))
1090 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL callback\n");
1095 while ((NULL != (th = h->th_head)) && (size >= th->size))
1097 if (NULL != th->notify)
1099 if (th->tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1101 /* traffic to origin */
1102 struct GNUNET_MESH_ToOrigin to;
1103 struct GNUNET_MessageHeader *mh;
1105 GNUNET_assert (size >= th->size);
1106 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (to)];
1107 psize = th->notify (th->notify_cls, size - sizeof (to), mh);
1108 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin, type %u\n",
1112 psize += sizeof (to);
1113 GNUNET_assert (size >= psize);
1114 to.header.size = htons (psize);
1115 to.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
1116 to.tid = htonl (th->tunnel->tid);
1117 memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1118 memset (&to.sender, 0, sizeof (struct GNUNET_PeerIdentity));
1119 memcpy (cbuf, &to, sizeof (to));
1122 else if (th->target == 0)
1125 struct GNUNET_MESH_Multicast mc;
1126 struct GNUNET_MessageHeader *mh;
1128 GNUNET_assert (size >= th->size);
1129 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (mc)];
1130 psize = th->notify (th->notify_cls, size - sizeof (mc), mh);
1131 LOG (GNUNET_ERROR_TYPE_DEBUG, " multicast, type %u\n",
1135 psize += sizeof (mc);
1136 GNUNET_assert (size >= psize);
1137 mc.header.size = htons (psize);
1138 mc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1139 mc.tid = htonl (th->tunnel->tid);
1142 memset (&mc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1143 memcpy (cbuf, &mc, sizeof (mc));
1149 struct GNUNET_MESH_Unicast uc;
1150 struct GNUNET_MessageHeader *mh;
1152 GNUNET_assert (size >= th->size);
1153 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (uc)];
1154 psize = th->notify (th->notify_cls, size - sizeof (uc), mh);
1155 LOG (GNUNET_ERROR_TYPE_DEBUG, " unicast, type %u\n",
1159 psize += sizeof (uc);
1160 GNUNET_assert (size >= psize);
1161 uc.header.size = htons (psize);
1162 uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1163 uc.tid = htonl (th->tunnel->tid);
1164 memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1165 GNUNET_PEER_resolve (th->target, &uc.destination);
1166 memcpy (cbuf, &uc, sizeof (uc));
1172 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1173 LOG (GNUNET_ERROR_TYPE_DEBUG, " mesh traffic, type %u\n",
1175 memcpy (cbuf, &th[1], th->size);
1178 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1179 GNUNET_SCHEDULER_cancel (th->timeout_task);
1180 if (NULL != th->notify)
1182 th->tunnel->mesh->npackets--;
1183 th->tunnel->npackets--;
1185 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1191 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size: %u\n", tsize);
1192 if (NULL != (th = h->th_head))
1194 LOG (GNUNET_ERROR_TYPE_DEBUG, " next size: %u\n", th->size);
1197 GNUNET_CLIENT_notify_transmit_ready (h->client, th->size,
1198 GNUNET_TIME_UNIT_FOREVER_REL,
1199 GNUNET_YES, &send_callback, h);
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() END\n");
1202 if (GNUNET_NO == h->in_receive)
1204 h->in_receive = GNUNET_YES;
1205 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1206 GNUNET_TIME_UNIT_FOREVER_REL);
1213 * Auxiliary function to send an already constructed packet to the service.
1214 * Takes care of creating a new queue element, copying the message and
1215 * calling the tmt_rdy function if necessary.
1217 * @param h mesh handle
1218 * @param msg message to transmit
1219 * @param tunnel tunnel this send is related to (NULL if N/A)
1222 send_packet (struct GNUNET_MESH_Handle *h,
1223 const struct GNUNET_MessageHeader *msg,
1224 struct GNUNET_MESH_Tunnel *tunnel)
1226 struct GNUNET_MESH_TransmitHandle *th;
1229 msize = ntohs (msg->size);
1230 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1231 th->priority = UINT32_MAX;
1232 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1234 th->tunnel = tunnel;
1235 memcpy (&th[1], msg, msize);
1236 add_to_queue (h, th);
1240 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1241 GNUNET_TIME_UNIT_FOREVER_REL,
1242 GNUNET_YES, &send_callback, h);
1246 /******************************************************************************/
1247 /********************** API CALL DEFINITIONS *************************/
1248 /******************************************************************************/
1251 * Connect to the mesh service.
1253 * @param cfg configuration to use
1254 * @param queue_size size of the data message queue, shared among all tunnels
1255 * (each tunnel is guaranteed to accept at least one message,
1256 * no matter what is the status of other tunnels)
1257 * @param cls closure for the various callbacks that follow
1258 * (including handlers in the handlers array)
1259 * @param new_tunnel function called when an *inbound* tunnel is created
1260 * @param cleaner function called when an *inbound* tunnel is destroyed by the
1261 * remote peer, it is *not* called if GNUNET_MESH_tunnel_destroy
1262 * is called on the tunnel
1263 * @param handlers callbacks for messages we care about, NULL-terminated
1264 * note that the mesh is allowed to drop notifications about
1265 * inbound messages if the client does not process them fast
1266 * enough (for this notification type, a bounded queue is used)
1267 * @param stypes list of the applications that this client claims to provide
1268 * @return handle to the mesh service NULL on error
1269 * (in this case, init is never called)
1271 struct GNUNET_MESH_Handle *
1272 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1273 unsigned int queue_size, void *cls,
1274 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1275 GNUNET_MESH_TunnelEndHandler cleaner,
1276 const struct GNUNET_MESH_MessageHandler *handlers,
1277 const GNUNET_MESH_ApplicationType *stypes)
1279 struct GNUNET_MESH_Handle *h;
1281 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1282 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1284 h->max_queue_size = queue_size;
1285 h->new_tunnel = new_tunnel;
1286 h->cleaner = cleaner;
1287 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1288 if (h->client == NULL)
1296 h->applications = stypes;
1297 h->message_handlers = handlers;
1298 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1299 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1300 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1302 /* count handlers and apps, calculate size */
1303 for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
1304 for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1312 * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
1313 * disconnect callbacks will be called on any still connected peers, notifying
1314 * about their disconnection. The registered inbound tunnel cleaner will be
1315 * called should any inbound tunnels still exist.
1317 * @param handle connection to mesh to disconnect
1320 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1322 struct GNUNET_MESH_Tunnel *t;
1323 struct GNUNET_MESH_Tunnel *aux;
1324 struct GNUNET_MESH_TransmitHandle *th;
1326 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1328 t = handle->tunnels_head;
1332 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1337 destroy_tunnel (t, GNUNET_YES);
1340 while ( (th = handle->th_head) != NULL)
1342 struct GNUNET_MessageHeader *msg;
1344 /* Make sure it is an allowed packet (everything else should have been
1345 * already canceled).
1347 GNUNET_break (UINT32_MAX == th->priority);
1348 GNUNET_break (NULL == th->notify);
1349 msg = (struct GNUNET_MessageHeader *) &th[1];
1350 switch (ntohs(msg->type))
1352 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1353 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1357 LOG (GNUNET_ERROR_TYPE_DEBUG, "unexpected msg %u\n",
1361 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1365 if (NULL != handle->th)
1367 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1370 if (NULL != handle->client)
1372 GNUNET_CLIENT_disconnect (handle->client);
1373 handle->client = NULL;
1375 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1377 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1378 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1380 GNUNET_free (handle);
1385 * Announce to ther peer the availability of services described by the regex,
1386 * in order to be reachable to other peers via connect_by_string.
1388 * @param h handle to mesh.
1389 * @param regex string with the regular expression describing local services.
1392 GNUNET_MESH_announce_regex (struct GNUNET_MESH_Handle *h,
1395 struct GNUNET_MessageHeader *msg;
1399 len = strlen (regex);
1400 msgsize = sizeof(struct GNUNET_MessageHeader) + len;
1401 GNUNET_assert (UINT16_MAX > msgsize);
1403 msg = GNUNET_malloc (msgsize);
1404 msg->size = htons (msgsize);
1405 msg->type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX);
1406 memcpy (&msg[1], regex, len);
1408 send_packet(h, msg, NULL);
1412 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1413 * and to broadcast).
1415 * @param h mesh handle
1416 * @param tunnel_ctx client's tunnel context to associate with the tunnel
1417 * @param connect_handler function to call when peers are actually connected
1418 * @param disconnect_handler function to call when peers are disconnected
1419 * @param handler_cls closure for connect/disconnect handlers
1421 struct GNUNET_MESH_Tunnel *
1422 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, void *tunnel_ctx,
1423 GNUNET_MESH_PeerConnectHandler connect_handler,
1424 GNUNET_MESH_PeerDisconnectHandler disconnect_handler,
1427 struct GNUNET_MESH_Tunnel *t;
1428 struct GNUNET_MESH_TunnelMessage msg;
1430 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
1431 t = create_tunnel (h, 0);
1432 t->connect_handler = connect_handler;
1433 t->disconnect_handler = disconnect_handler;
1434 t->cls = handler_cls;
1435 t->ctx = tunnel_ctx;
1436 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1437 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1438 msg.tunnel_id = htonl (t->tid);
1439 send_packet (h, &msg.header, t);
1445 * Destroy an existing tunnel. The existing callback for the tunnel will NOT
1448 * @param tunnel tunnel handle
1451 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1453 struct GNUNET_MESH_Handle *h;
1454 struct GNUNET_MESH_TunnelMessage msg;
1455 struct GNUNET_MESH_TransmitHandle *th;
1457 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1460 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1461 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1462 msg.tunnel_id = htonl (tunnel->tid);
1466 struct GNUNET_MESH_TransmitHandle *aux;
1467 if (th->tunnel == tunnel)
1470 /* FIXME call the handler? */
1471 if (NULL != th->notify)
1472 th->notify (th->notify_cls, 0, NULL);
1473 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1481 destroy_tunnel (tunnel, GNUNET_NO);
1482 send_packet (h, &msg.header, NULL);
1487 * Request that a peer should be added to the tunnel. The existing
1488 * connect handler will be called ONCE with either success or failure.
1489 * This function should NOT be called again with the same peer before the
1490 * connect handler is called.
1492 * @param tunnel handle to existing tunnel
1493 * @param peer peer to add
1496 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
1497 const struct GNUNET_PeerIdentity *peer)
1499 struct GNUNET_MESH_PeerControl msg;
1500 GNUNET_PEER_Id peer_id;
1503 peer_id = GNUNET_PEER_intern (peer);
1504 for (i = 0; i < tunnel->npeers; i++)
1506 if (tunnel->peers[i]->id == peer_id)
1508 /* Peer already exists in tunnel */
1509 GNUNET_PEER_change_rc (peer_id, -1);
1514 if (NULL == add_peer_to_tunnel (tunnel, peer))
1517 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1518 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1519 msg.tunnel_id = htonl (tunnel->tid);
1521 send_packet (tunnel->mesh, &msg.header, tunnel);
1528 * Request that a peer should be removed from the tunnel. The existing
1529 * disconnect handler will be called ONCE if we were connected.
1531 * @param tunnel handle to existing tunnel
1532 * @param peer peer to remove
1535 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
1536 const struct GNUNET_PeerIdentity *peer)
1538 struct GNUNET_MESH_PeerControl msg;
1539 GNUNET_PEER_Id peer_id;
1542 peer_id = GNUNET_PEER_search (peer);
1548 for (i = 0; i < tunnel->npeers; i++)
1549 if (tunnel->peers[i]->id == peer_id)
1551 if (i == tunnel->npeers)
1556 if (NULL != tunnel->disconnect_handler && tunnel->peers[i]->connected == 1)
1557 tunnel->disconnect_handler (tunnel->cls, peer);
1558 GNUNET_PEER_change_rc (peer_id, -1);
1559 GNUNET_free (tunnel->peers[i]);
1560 tunnel->peers[i] = tunnel->peers[tunnel->npeers - 1];
1561 GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1);
1563 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1564 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1565 msg.tunnel_id = htonl (tunnel->tid);
1566 memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1567 send_packet (tunnel->mesh, &msg.header, tunnel);
1572 * Request that the mesh should try to connect to a peer supporting the given
1575 * @param tunnel handle to existing tunnel
1576 * @param app_type application type that must be supported by the peer (MESH
1577 * should discover peer in proximity handling this type)
1580 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
1581 GNUNET_MESH_ApplicationType app_type)
1583 struct GNUNET_MESH_ConnectPeerByType msg;
1585 GNUNET_array_append (tunnel->apps, tunnel->napps, app_type);
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, "* CONNECT BY TYPE *\n");
1588 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
1589 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
1590 msg.tunnel_id = htonl (tunnel->tid);
1591 msg.type = htonl (app_type);
1592 send_packet (tunnel->mesh, &msg.header, tunnel);
1597 * Request that the mesh should try to connect to a peer matching the
1598 * description given in the service string.
1600 * @param tunnel handle to existing tunnel
1601 * @param description string describing the destination node requirements
1604 GNUNET_MESH_peer_request_connect_by_string (struct GNUNET_MESH_Tunnel *tunnel,
1605 const char *description)
1607 struct GNUNET_MESH_ConnectPeerByString *msg;
1611 len = strlen(description);
1612 msgsize = sizeof(struct GNUNET_MESH_ConnectPeerByString) + len;
1613 GNUNET_assert (UINT16_MAX > msgsize);
1614 msg = GNUNET_malloc (msgsize);
1615 msg->header.size = htons (msgsize);
1616 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING);
1617 msg->tunnel_id = htonl (tunnel->tid);
1618 memcpy(&msg[1], description, len);
1620 send_packet (tunnel->mesh, &msg->header, tunnel);
1625 * Request that the given peer isn't added to this tunnel in calls to
1626 * connect_by_* calls, (due to misbehaviour, bad performance, ...).
1628 * @param tunnel handle to existing tunnel.
1629 * @param peer peer identity of the peer which should be blacklisted
1633 GNUNET_MESH_peer_blacklist (struct GNUNET_MESH_Tunnel *tunnel,
1634 const struct GNUNET_PeerIdentity *peer)
1636 struct GNUNET_MESH_PeerControl msg;
1638 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1639 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST);
1640 msg.tunnel_id = htonl (tunnel->tid);
1642 send_packet (tunnel->mesh, &msg.header, tunnel);
1649 * Request that the given peer isn't blacklisted anymore from this tunnel,
1650 * and therefore can be added in future calls to connect_by_*.
1651 * The peer must have been previously blacklisted for this tunnel.
1653 * @param tunnel handle to existing tunnel.
1654 * @param peer peer identity of the peer which shouldn't be blacklisted
1655 * for the tunnel anymore.
1658 GNUNET_MESH_peer_unblacklist (struct GNUNET_MESH_Tunnel *tunnel,
1659 const struct GNUNET_PeerIdentity *peer)
1661 struct GNUNET_MESH_PeerControl msg;
1663 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1664 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST);
1665 msg.tunnel_id = htonl (tunnel->tid);
1667 send_packet (tunnel->mesh, &msg.header, tunnel);
1674 * Ask the mesh to call "notify" once it is ready to transmit the
1675 * given number of bytes to the specified "target". If we are not yet
1676 * connected to the specified peer, a call to this function will cause
1677 * us to try to establish a connection.
1679 * @param tunnel tunnel to use for transmission
1680 * @param cork is corking allowed for this transmission?
1681 * @param priority how important is the message?
1682 * @param maxdelay how long can the message wait?
1683 * @param target destination for the message,
1684 * NULL for multicast to all tunnel targets
1685 * @param notify_size how many bytes of buffer space does notify want?
1686 * @param notify function to call when buffer space is available;
1687 * will be called with NULL on timeout or if the overall queue
1688 * for this peer is larger than queue_size and this is currently
1689 * the message with the lowest priority
1690 * @param notify_cls closure for notify
1691 * @return non-NULL if the notify callback was queued,
1692 * NULL if we can not even queue the request (insufficient
1693 * memory); if NULL is returned, "notify" will NOT be called.
1695 struct GNUNET_MESH_TransmitHandle *
1696 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1698 struct GNUNET_TIME_Relative maxdelay,
1699 const struct GNUNET_PeerIdentity *target,
1701 GNUNET_CONNECTION_TransmitReadyNotify notify,
1704 struct GNUNET_MESH_TransmitHandle *th;
1705 struct GNUNET_MESH_TransmitHandle *least_priority_th;
1706 uint32_t least_priority;
1709 GNUNET_assert (NULL != tunnel);
1710 LOG (GNUNET_ERROR_TYPE_DEBUG, "mesh notify transmit ready called\n");
1712 LOG (GNUNET_ERROR_TYPE_DEBUG, " target %s\n", GNUNET_i2s (target));
1714 LOG (GNUNET_ERROR_TYPE_DEBUG, " target multicast\n");
1715 GNUNET_assert (NULL != notify);
1716 if (tunnel->mesh->npackets >= tunnel->mesh->max_queue_size &&
1717 tunnel->npackets > 0)
1722 th = tunnel->mesh->th_tail;
1723 least_priority = priority;
1724 least_priority_th = NULL;
1727 if (th->priority < least_priority && th->tunnel->npackets > 1)
1729 least_priority_th = th;
1730 least_priority = th->priority;
1734 if (NULL == least_priority_th)
1736 /* Can't be a control message */
1737 GNUNET_assert (NULL != least_priority_th->notify);
1738 least_priority_th->notify (notify_cls, 0, NULL);
1739 least_priority_th->tunnel->npackets--;
1740 tunnel->mesh->npackets--;
1741 GNUNET_CONTAINER_DLL_remove (tunnel->mesh->th_head, tunnel->mesh->th_tail,
1743 if (GNUNET_SCHEDULER_NO_TASK != least_priority_th->timeout_task)
1744 GNUNET_SCHEDULER_cancel (least_priority_th->timeout_task);
1745 GNUNET_free (least_priority_th);
1748 tunnel->mesh->npackets++;
1749 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1750 th->tunnel = tunnel;
1751 th->priority = priority;
1752 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1753 th->target = GNUNET_PEER_intern (target);
1754 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1755 overhead = sizeof (struct GNUNET_MESH_ToOrigin);
1756 else if (NULL == target)
1757 overhead = sizeof (struct GNUNET_MESH_Multicast);
1759 overhead = sizeof (struct GNUNET_MESH_Unicast);
1760 th->size = notify_size + overhead;
1761 th->notify = notify;
1762 th->notify_cls = notify_cls;
1763 add_to_queue (tunnel->mesh, th);
1764 if (NULL != tunnel->mesh->th)
1767 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
1768 GNUNET_TIME_UNIT_FOREVER_REL,
1769 GNUNET_YES, &send_callback,
1776 * Cancel the specified transmission-ready notification.
1778 * @param th handle that was returned by "notify_transmit_ready".
1781 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1783 struct GNUNET_MESH_Handle *mesh;
1785 mesh = th->tunnel->mesh;
1786 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1787 GNUNET_SCHEDULER_cancel (th->timeout_task);
1788 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1790 if ((NULL == mesh->th_head) && (NULL != mesh->th))
1792 /* queue empty, no point in asking for transmission */
1793 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1800 * Transition API for tunnel ctx management
1803 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
1809 * Transition API for tunnel ctx management
1812 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)