3 This file is part of GNUnet.
4 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with GNUnet; see the file COPYING. If not, write to the
15 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA.
20 * @file mesh/mesh_api_new.c
21 * @brief mesh api: client implementation of mesh service
22 * @author Bartlomiej Polot
25 * - callbacks to client missing on certain events
26 * - processing messages from service is incomplete
31 * - AUXILIARY FUNCTIONS
34 * - API CALL DEFINITIONS
39 #if 0 /* keep Emacsens' auto-indent happy */
45 #include "gnunet_common.h"
46 #include "gnunet_client_lib.h"
47 #include "gnunet_util_lib.h"
48 #include "gnunet_peer_lib.h"
49 #include "gnunet_mesh_service_new.h"
51 #include "mesh_protocol.h"
54 /******************************************************************************/
55 /************************ DATA STRUCTURES ****************************/
56 /******************************************************************************/
59 * Transmission queue to the service
61 struct GNUNET_MESH_TransmitHandle
67 struct GNUNET_MESH_TransmitHandle *next;
72 struct GNUNET_MESH_TransmitHandle *prev;
75 * Tunnel this message is sent over (may be NULL for control messages).
77 struct GNUNET_MESH_Tunnel *tunnel;
80 * Data itself, currently points to the end of this struct if
81 * we have a message already, NULL if the message is to be
82 * obtained from the callback.
84 const struct GNUNET_MessageHeader *data;
87 * Callback to obtain the message to transmit, or NULL if we
88 * got the message in 'data'. Notice that messages built
89 * by 'notify' need to be encapsulated with information about
92 GNUNET_CONNECTION_TransmitReadyNotify notify;
95 * Closure for 'notify'
100 * How long is this message valid. Once the timeout has been
101 * reached, the message must no longer be sent. If this
102 * is a message with a 'notify' callback set, the 'notify'
103 * function should be called with 'buf' NULL and size 0.
105 struct GNUNET_TIME_Absolute timeout;
108 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
110 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
113 * Priority of the message. The queue is sorted by priority,
114 * control messages have the maximum priority (UINT32_MAX).
119 * Target of the message, 0 for broadcast. This field
120 * is only valid if 'notify' is non-NULL.
122 GNUNET_PEER_Id target;
125 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
132 * Opaque handle to the service.
134 struct GNUNET_MESH_Handle
138 * Handle to the server connection, to send messages later
140 struct GNUNET_CLIENT_Connection *client;
143 * Set of handlers used for processing incoming messages in the tunnels
145 const struct GNUNET_MESH_MessageHandler *message_handlers;
148 * Set of applications that should be claimed to be offered at this node.
149 * Note that this is just informative, the appropiate handlers must be
150 * registered independently and the mapping is up to the developer of the
151 * client application.
153 const GNUNET_MESH_ApplicationType *applications;
156 * Double linked list of the tunnels this client is connected to.
158 struct GNUNET_MESH_Tunnel *tunnels_head;
159 struct GNUNET_MESH_Tunnel *tunnels_tail;
162 * Callback for tunnel disconnection
164 GNUNET_MESH_TunnelEndHandler *cleaner;
167 * Handle to cancel pending transmissions in case of disconnection
169 struct GNUNET_CLIENT_TransmitHandle *th;
172 * Closure for all the handlers given by the client
177 * Messages to send to the service
179 struct GNUNET_MESH_TransmitHandle *th_head;
180 struct GNUNET_MESH_TransmitHandle *th_tail;
183 * tid of the next tunnel to create (to avoid reusing IDs often)
185 MESH_TunnelNumber next_tid;
186 unsigned int n_handlers;
187 unsigned int n_applications;
188 unsigned int max_queue_size;
191 * Have we started the task to receive messages from the service
192 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
197 * Number of packets queued
199 unsigned int npackets;
202 * Configuration given by the client, in case of reconnection
204 const struct GNUNET_CONFIGURATION_Handle *cfg;
209 * Description of a peer
211 struct GNUNET_MESH_Peer
214 * ID of the peer in short form
219 * Tunnel this peer belongs to
221 struct GNUNET_MESH_Tunnel *t;
224 * Flag indicating whether service has informed about its connection
232 * Opaque handle to a tunnel.
234 struct GNUNET_MESH_Tunnel
240 struct GNUNET_MESH_Tunnel *next;
241 struct GNUNET_MESH_Tunnel *prev;
244 * Callback to execute when peers connect to the tunnel
246 GNUNET_MESH_TunnelConnectHandler connect_handler;
249 * Callback to execute when peers disconnect to the tunnel
251 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
254 * All peers added to the tunnel
256 struct GNUNET_MESH_Peer **peers;
259 * Closure for the connect/disconnect handlers
264 * Handle to the mesh this tunnel belongs to
266 struct GNUNET_MESH_Handle *mesh;
269 * Local ID of the tunnel
271 MESH_TunnelNumber tid;
274 * Owner of the tunnel
276 GNUNET_PEER_Id owner;
279 * List of application types that have been requested for this tunnel
281 GNUNET_MESH_ApplicationType *apps;
284 * Number of peers added to the tunnel
289 * Number of packets queued in this tunnel
291 unsigned int npackets;
294 * Number of applications requested this tunnel
301 /******************************************************************************/
302 /*********************** AUXILIARY FUNCTIONS *************************/
303 /******************************************************************************/
306 * Get the tunnel handler for the tunnel specified by id from the given handle
307 * @param h Mesh handle
308 * @param tid ID of the wanted tunnel
309 * @return handle to the required tunnel or NULL if not found
311 static struct GNUNET_MESH_Tunnel *
312 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
314 struct GNUNET_MESH_Tunnel *t;
328 * Create a new tunnel and insert it in the tunnel list of the mesh handle
329 * @param h Mesh handle
330 * @param tid desired tid of the tunnel, 0 to assign one automatically
331 * @return handle to the created tunnel
333 static struct GNUNET_MESH_Tunnel *
334 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
336 struct GNUNET_MESH_Tunnel *t;
338 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
342 t->tid = h->next_tid++;
346 /* FIXME keep double numbering?
347 * client numbers from 0x8... anx service from 0xB... ? */
349 h->next_tid = tid + 1;
351 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK; // keep in range
352 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
358 * Get the tunnel handler for the tunnel specified by id from the given handle
359 * @param h Mesh handle
360 * @param tid ID of the wanted tunnel
361 * @return handle to the required tunnel or NULL if not found
364 destroy_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
366 struct GNUNET_MESH_Tunnel *t;
367 struct GNUNET_PeerIdentity pi;
370 t = retrieve_tunnel (h, tid);
376 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
377 for (i = 0; i < t->npeers; i++)
379 GNUNET_PEER_resolve (t->peers[i]->id, &pi);
380 t->disconnect_handler (t->cls, &pi);
381 GNUNET_PEER_change_rc (t->peers[i]->id, -1);
382 GNUNET_free (t->peers[i]);
384 h->cleaner (h->cls, t, NULL); /* FIXME ctx? */
386 GNUNET_PEER_change_rc (t->owner, -1);
387 GNUNET_free (t->peers);
394 * Get the peer descriptor for the peer with id from the given tunnel
395 * @param t Tunnel handle
396 * @param id Short form ID of the wanted peer
397 * @return handle to the requested peer or NULL if not found
399 static struct GNUNET_MESH_Peer *
400 retrieve_peer (struct GNUNET_MESH_Tunnel *t, GNUNET_PEER_Id id)
404 for (i = 0; i < t->npeers; i++)
405 if (t->peers[i]->id == id)
412 * Add a peer into a tunnel
413 * @param t Tunnel handle
414 * @param pi Full ID of the new peer
415 * @return handle to the newly created peer
417 static struct GNUNET_MESH_Peer *
418 add_peer_to_tunnel (struct GNUNET_MESH_Tunnel *t,
419 const struct GNUNET_PeerIdentity *pi)
421 struct GNUNET_MESH_Peer *p;
424 id = GNUNET_PEER_intern (pi);
426 p = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
429 GNUNET_array_append (t->peers, t->npeers, p);
435 * Remove a peer from a tunnel
436 * @param t Tunnel handle
437 * @param p Peer handle
440 remove_peer_from_tunnel (struct GNUNET_MESH_Peer *p)
444 for (i = 0; i < p->t->npeers; i++)
446 if (p->t->peers[i] == p)
449 if (i == p->t->npeers)
454 p->t->peers[i] = p->t->peers[p->t->npeers - 1];
455 GNUNET_array_grow (p->t->peers, p->t->npeers, p->t->npeers - 1);
460 * Notify client that the transmission has timed out
462 * @param tc task context
465 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
467 struct GNUNET_MESH_TransmitHandle *th = cls;
468 struct GNUNET_MESH_Handle *mesh;
470 mesh = th->tunnel->mesh;
471 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
472 if (th->notify != NULL)
473 th->notify (th->notify_cls, 0, NULL);
475 if ((NULL == mesh->th_head) && (NULL != mesh->th))
477 /* queue empty, no point in asking for transmission */
478 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
485 * Add a transmit handle to the transmission queue by priority and set the
488 * @param h mesh handle with the queue head and tail
489 * @param q handle to the packet to be transmitted
492 add_to_queue (struct GNUNET_MESH_Handle *h,
493 struct GNUNET_MESH_TransmitHandle *th)
495 struct GNUNET_MESH_TransmitHandle *p;
498 while ((NULL != p) && (th->priority <= p->priority))
504 GNUNET_CONTAINER_DLL_insert_after (h->th_head, h->th_tail, p, th);
505 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
508 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
509 (th->timeout), &timeout_transmission, th);
514 send_packet (struct GNUNET_MESH_Handle *h,
515 const struct GNUNET_MessageHeader *msg);
519 * Reconnect to the service, retransmit all infomation to try to restore the
521 * FIXME: notify user about it? (call all disconnect callbacks?)
523 * @param h handle to the mesh
525 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
528 reconnect (struct GNUNET_MESH_Handle *h)
530 struct GNUNET_MESH_Tunnel *t;
533 h->in_receive = GNUNET_NO;
537 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
539 if (NULL != h->client)
541 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
545 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
546 if (h->client == NULL)
548 /* FIXME: panic? exponential backoff retry? */
552 for (t = h->tunnels_head; NULL != t; t = t->next)
554 struct GNUNET_MESH_TunnelMessage msg;
556 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
557 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
558 msg.tunnel_id = htonl (t->tid);
559 send_packet (h, &msg.header);
560 for (i = 0; i < t->npeers; i++)
562 struct GNUNET_MESH_PeerControl msg;
564 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
565 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
566 msg.tunnel_id = htonl (t->tid);
567 GNUNET_PEER_resolve (t->peers[i]->id, &msg.peer);
568 send_packet (t->mesh, &msg.header);
570 /* FIXME what about connects by type? */
576 /******************************************************************************/
577 /*********************** RECEIVE HANDLERS ****************************/
578 /******************************************************************************/
581 * Process the new tunnel notification and add it to the tunnels in the handle
583 * @param h The mesh handle
584 * @param msg A message with the details of the new incoming tunnel
587 process_tunnel_create (struct GNUNET_MESH_Handle *h,
588 const struct GNUNET_MESH_TunnelMessage *msg)
590 struct GNUNET_MESH_Tunnel *t;
591 MESH_TunnelNumber tid;
593 tid = ntohl (msg->tunnel_id);
594 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)
599 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
603 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
609 * Process the tunnel destroy notification and free associated resources
611 * @param h The mesh handle
612 * @param msg A message with the details of the tunnel being destroyed
615 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
616 const struct GNUNET_MESH_TunnelMessage *msg)
618 struct GNUNET_MESH_Tunnel *t;
619 MESH_TunnelNumber tid;
621 tid = ntohl (msg->tunnel_id);
622 t = retrieve_tunnel (h, tid);
627 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
633 * Process the new peer event and notify the upper level of it
635 * @param h The mesh handle
636 * @param msg A message with the details of the peer event
639 process_peer_event (struct GNUNET_MESH_Handle *h,
640 const struct GNUNET_MESH_PeerControl *msg)
642 struct GNUNET_MESH_Tunnel *t;
643 struct GNUNET_MESH_Peer *p;
644 struct GNUNET_TRANSPORT_ATS_Information atsi;
648 size = ntohs (msg->header.size);
649 if (size != sizeof (struct GNUNET_MESH_PeerControl))
654 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
657 t = create_tunnel (h, msg->tunnel_id);
658 t->owner = GNUNET_PEER_intern (&msg->peer);
660 t->peers = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer *));
661 t->peers[0] = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
663 t->peers[0]->connected = 1;
664 t->peers[0]->id = t->owner;
667 id = GNUNET_PEER_search (&msg->peer);
668 if ((p = retrieve_peer (t, id)) == NULL)
669 p = add_peer_to_tunnel (t, &msg->peer);
672 if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD == msg->header.type)
674 if (NULL != t->connect_handler)
676 t->connect_handler (t->cls, &msg->peer, &atsi);
682 if (NULL != t->disconnect_handler && p->connected)
684 t->disconnect_handler (t->cls, &msg->peer);
686 remove_peer_from_tunnel (p);
693 * Process the incoming data packets
695 * @param h The mesh handle
696 * @param msh A message encapsulating the data
699 process_incoming_data (struct GNUNET_MESH_Handle *h,
700 const struct GNUNET_MessageHeader *message)
702 const struct GNUNET_MessageHeader *payload;
703 const struct GNUNET_MESH_MessageHandler *handler;
704 const struct GNUNET_PeerIdentity *peer;
705 struct GNUNET_MESH_Unicast *ucast;
706 struct GNUNET_MESH_Multicast *mcast;
707 struct GNUNET_MESH_ToOrigin *to_orig;
708 struct GNUNET_MESH_Tunnel *t;
712 type = ntohs (message->type);
715 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
716 ucast = (struct GNUNET_MESH_Unicast *) message;
717 t = retrieve_tunnel (h, ntohl (ucast->tid));
718 payload = (struct GNUNET_MessageHeader *) &ucast[1];
721 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
722 mcast = (struct GNUNET_MESH_Multicast *) message;
723 t = retrieve_tunnel (h, ntohl (mcast->tid));
724 payload = (struct GNUNET_MessageHeader *) &mcast[1];
727 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
728 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
729 t = retrieve_tunnel (h, ntohl (to_orig->tid));
730 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
731 peer = &to_orig->sender;
742 for (i = 0; i < h->n_handlers; i++)
744 handler = &h->message_handlers[i];
745 if (handler->type == type)
747 struct GNUNET_TRANSPORT_ATS_Information atsi;
753 handler->callback (h->cls, t, NULL, peer, payload, &atsi))
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "MESH: callback completed successfully\n");
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761 "MESH: callback caused disconnection\n");
762 GNUNET_MESH_disconnect (h);
770 * Function to process all messages received from the service
773 * @param msg message received, NULL on timeout or fatal error
776 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
778 struct GNUNET_MESH_Handle *h = cls;
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: received a message from MESH\n");
786 switch (ntohs (msg->type))
788 /* Notify of a new incoming tunnel */
789 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
790 process_tunnel_create (h, (struct GNUNET_MESH_TunnelMessage *) msg);
792 /* Notify of a tunnel disconnection */
793 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
794 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
796 /* Notify of a new peer or a peer disconnect in the tunnel */
797 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD:
798 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL:
799 process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
801 /* Notify of a new data packet in the tunnel */
802 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
803 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
804 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
805 process_incoming_data (h, msg);
807 /* We shouldn't get any other packages, log and ignore */
809 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
810 "MESH: unsolicited message form service (type %d)\n",
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: message processed\n");
814 GNUNET_CLIENT_receive (h->client, &msg_received, h,
815 GNUNET_TIME_UNIT_FOREVER_REL);
819 /******************************************************************************/
820 /************************ SEND FUNCTIONS ****************************/
821 /******************************************************************************/
824 * Function called to send a message to the service.
825 * "buf" will be NULL and "size" zero if the socket was closed for writing in
828 * @param cls closure, the mesh handle
829 * @param size number of bytes available in buf
830 * @param buf where the callee should write the connect message
831 * @return number of bytes written to buf
834 send_callback (void *cls, size_t size, void *buf)
836 struct GNUNET_MESH_Handle *h = cls;
837 struct GNUNET_MESH_TransmitHandle *th;
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send packet() Buffer %u\n", size);
844 if ((0 == size) || (NULL == buf))
850 while ((NULL != (th = h->th_head)) && (size >= th->size))
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: type: %u\n",
853 ntohs (th->data->type));
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: size: %u\n",
855 ntohs (th->data->size));
856 if (NULL == th->data)
858 GNUNET_assert (NULL != th->notify);
862 struct GNUNET_MESH_Multicast mc;
864 GNUNET_assert (size >= sizeof (mc) + th->size);
866 th->notify (th->notify_cls, size - sizeof (mc), &cbuf[sizeof (mc)]);
869 mc.header.size = htons (sizeof (mc) + th->size);
870 mc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
871 mc.tid = htonl (th->tunnel->tid);
872 memset (&mc.oid, 0, sizeof (struct GNUNET_PeerIdentity)); /* myself */
873 memcpy (cbuf, &mc, sizeof (mc));
874 psize = th->size + sizeof (mc);
880 struct GNUNET_MESH_Unicast uc;
882 GNUNET_assert (size >= sizeof (uc) + th->size);
884 th->notify (th->notify_cls, size - sizeof (uc), &cbuf[sizeof (uc)]);
887 uc.header.size = htons (sizeof (uc) + th->size);
888 uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
889 uc.tid = htonl (th->tunnel->tid);
890 memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity)); /* myself */
891 GNUNET_PEER_resolve (th->target, &uc.destination);
892 memcpy (cbuf, &uc, sizeof (uc));
893 psize = th->size + sizeof (uc);
899 memcpy (cbuf, th->data, th->size);
902 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
903 GNUNET_SCHEDULER_cancel (th->timeout_task);
904 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: total size: %u\n", tsize);
911 if (NULL != (th = h->th_head))
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: next size: %u\n", th->size);
915 GNUNET_CLIENT_notify_transmit_ready (h->client, th->size,
916 GNUNET_TIME_UNIT_FOREVER_REL,
917 GNUNET_YES, &send_callback, h);
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send packet() END\n");
920 if (GNUNET_NO == h->in_receive)
922 h->in_receive = GNUNET_YES;
923 GNUNET_CLIENT_receive (h->client, &msg_received, h,
924 GNUNET_TIME_UNIT_FOREVER_REL);
931 * Auxiliary function to send an already constructed packet to the service.
932 * Takes care of creating a new queue element, copying the message and
933 * calling the tmt_rdy function if necessary.
934 * @param h mesh handle
935 * @param msg message to transmit
938 send_packet (struct GNUNET_MESH_Handle *h,
939 const struct GNUNET_MessageHeader *msg)
941 struct GNUNET_MESH_TransmitHandle *th;
944 msize = ntohs (msg->size);
945 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
946 th->priority = UINT32_MAX;
947 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
949 th->data = (void *) &th[1];
950 memcpy (&th[1], msg, msize);
951 add_to_queue (h, th);
955 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
956 GNUNET_TIME_UNIT_FOREVER_REL,
957 GNUNET_YES, &send_callback, h);
961 /******************************************************************************/
962 /********************** API CALL DEFINITIONS *************************/
963 /******************************************************************************/
966 * Connect to the mesh service.
968 * @param cfg configuration to use
969 * @param cls closure for the various callbacks that follow
970 * (including handlers in the handlers array)
971 * @param queue_size size of the data message queue, shared among all tunnels
972 * (each tunnel is guaranteed to accept at least one message,
973 * no matter what is the status of other tunnels)
974 * @param cleaner function called when an *inbound* tunnel is destroyed
975 * @param handlers callbacks for messages we care about, NULL-terminated
976 * note that the mesh is allowed to drop notifications about
977 * inbound messages if the client does not process them fast
978 * enough (for this notification type, a bounded queue is used)
979 * @param stypes Application Types the client claims to offer
980 * @return handle to the mesh service
981 * NULL on error (in this case, init is never called)
983 struct GNUNET_MESH_Handle *
984 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
985 unsigned int queue_size, void *cls,
986 GNUNET_MESH_TunnelEndHandler cleaner,
987 const struct GNUNET_MESH_MessageHandler *handlers,
988 const GNUNET_MESH_ApplicationType *stypes)
990 struct GNUNET_MESH_Handle *h;
991 struct GNUNET_MESH_ClientConnect *msg;
992 GNUNET_MESH_ApplicationType *apps;
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect()\n");
999 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1001 h->max_queue_size = queue_size;
1002 h->cleaner = cleaner;
1003 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1004 if (h->client == NULL)
1011 h->message_handlers = handlers;
1012 h->applications = stypes;
1013 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_MARK;
1015 /* count handlers and apps, calculate size */
1016 for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
1017 for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
1018 size = sizeof (struct GNUNET_MESH_ClientConnect);
1019 size += h->n_handlers * sizeof (uint16_t);
1020 size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
1025 /* build connection packet */
1026 msg = (struct GNUNET_MESH_ClientConnect *) buf;
1027 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
1028 msg->header.size = htons (size);
1029 types = (uint16_t *) & msg[1];
1030 for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
1031 types[ntypes] = h->message_handlers[ntypes].type;
1032 apps = (GNUNET_MESH_ApplicationType *) &types[ntypes];
1033 for (napps = 0; napps < h->n_applications; napps++)
1034 apps[napps] = h->applications[napps];
1035 msg->applications = htons (napps);
1036 msg->types = htons (ntypes);
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "mesh: Sending %lu bytes long message %d types and %d apps\n",
1039 ntohs (msg->header.size), ntypes, napps);
1040 send_packet (h, &msg->header);
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect() END\n");
1048 * Disconnect from the mesh service.
1050 * @param handle connection to mesh to disconnect
1053 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1055 if (NULL != handle->th)
1057 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1059 if (NULL != handle->client)
1061 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1063 GNUNET_free (handle);
1068 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1069 * and to broadcast).
1071 * @param h mesh handle
1072 * @param connect_handler function to call when peers are actually connected
1073 * @param disconnect_handler function to call when peers are disconnected
1074 * @param handler_cls closure for connect/disconnect handlers
1076 struct GNUNET_MESH_Tunnel *
1077 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
1078 GNUNET_MESH_TunnelConnectHandler connect_handler,
1079 GNUNET_MESH_TunnelDisconnectHandler
1080 disconnect_handler, void *handler_cls)
1082 struct GNUNET_MESH_Tunnel *t;
1083 struct GNUNET_MESH_TunnelMessage msg;
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Creating new tunnel\n");
1086 t = create_tunnel (h, 0);
1087 t->connect_handler = connect_handler;
1088 t->disconnect_handler = disconnect_handler;
1089 t->cls = handler_cls;
1090 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1091 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1092 msg.tunnel_id = htonl (t->tid);
1093 send_packet (h, &msg.header);
1099 * Destroy an existing tunnel.
1101 * @param tun tunnel handle
1104 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tun)
1106 struct GNUNET_MESH_Handle *h;
1107 struct GNUNET_MESH_TunnelMessage msg;
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Destroying tunnel\n");
1112 if (0 != tun->owner)
1113 GNUNET_PEER_change_rc (tun->owner, -1);
1115 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1116 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1117 msg.tunnel_id = htonl (tun->tid);
1119 send_packet (h, &msg.header);
1124 * Request that a peer should be added to the tunnel. The existing
1125 * connect handler will be called ONCE with either success or failure.
1126 * This function should NOT be called again with the same peer before the
1127 * connect handler is called.
1129 * @param tunnel handle to existing tunnel
1130 * @param timeout how long to try to establish a connection
1131 * @param peer peer to add
1134 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
1135 const struct GNUNET_PeerIdentity *peer)
1137 struct GNUNET_MESH_PeerControl msg;
1138 GNUNET_PEER_Id peer_id;
1141 peer_id = GNUNET_PEER_intern (peer);
1142 for (i = 0; i < tunnel->npeers; i++)
1144 if (tunnel->peers[i]->id == peer_id)
1146 GNUNET_PEER_change_rc (peer_id, -1);
1151 add_peer_to_tunnel (tunnel, peer);
1153 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1154 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1155 msg.tunnel_id = htonl (tunnel->tid);
1157 send_packet (tunnel->mesh, &msg.header);
1164 * Request that a peer should be removed from the tunnel. The existing
1165 * disconnect handler will be called ONCE if we were connected.
1167 * @param tunnel handle to existing tunnel
1168 * @param peer peer to remove
1171 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
1172 const struct GNUNET_PeerIdentity *peer)
1174 struct GNUNET_MESH_PeerControl msg;
1175 GNUNET_PEER_Id peer_id;
1178 peer_id = GNUNET_PEER_search (peer);
1184 for (i = 0; i < tunnel->npeers; i++)
1185 if (tunnel->peers[i]->id == peer_id)
1187 if (i == tunnel->npeers)
1192 if (NULL != tunnel->disconnect_handler && tunnel->peers[i]->connected == 1)
1193 tunnel->disconnect_handler (tunnel->cls, peer);
1194 GNUNET_PEER_change_rc (peer_id, -1);
1195 GNUNET_free (tunnel->peers[i]);
1196 tunnel->peers[i] = tunnel->peers[tunnel->npeers - 1];
1197 GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1);
1199 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1200 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1201 msg.tunnel_id = htonl (tunnel->tid);
1202 memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
1203 send_packet (tunnel->mesh, &msg.header);
1208 * Request that the mesh should try to connect to a peer supporting the given
1211 * @param tunnel handle to existing tunnel
1212 * @param app_type application type that must be supported by the peer (MESH
1213 * should discover peer in proximity handling this type)
1216 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
1217 GNUNET_MESH_ApplicationType app_type)
1219 struct GNUNET_MESH_ConnectPeerByType msg;
1221 GNUNET_array_append (tunnel->apps, tunnel->napps, app_type);
1222 /* FIXME: add a new api call disconnect by type? */
1224 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
1225 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
1226 msg.tunnel_id = htonl (tunnel->tid);
1227 msg.type = htonl (app_type);
1228 send_packet (tunnel->mesh, &msg.header);
1233 * Ask the mesh to call "notify" once it is ready to transmit the
1234 * given number of bytes to the specified "target". If we are not yet
1235 * connected to the specified peer, a call to this function will cause
1236 * us to try to establish a connection.
1238 * @param tunnel tunnel to use for transmission
1239 * @param cork is corking allowed for this transmission?
1240 * @param priority how important is the message?
1241 * @param maxdelay how long can the message wait?
1242 * @param target destination for the message,
1243 * NULL for multicast to all tunnel targets
1244 * @param notify_size how many bytes of buffer space does notify want?
1245 * @param notify function to call when buffer space is available;
1246 * will be called with NULL on timeout or if the overall queue
1247 * for this peer is larger than queue_size and this is currently
1248 * the message with the lowest priority
1249 * @param notify_cls closure for notify
1250 * @return non-NULL if the notify callback was queued,
1251 * NULL if we can not even queue the request (insufficient
1252 * memory); if NULL is returned, "notify" will NOT be called.
1254 struct GNUNET_MESH_TransmitHandle *
1255 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1257 struct GNUNET_TIME_Relative maxdelay,
1258 const struct GNUNET_PeerIdentity *target,
1260 GNUNET_CONNECTION_TransmitReadyNotify notify,
1263 struct GNUNET_MESH_TransmitHandle *th;
1266 if (tunnel->mesh->npackets >= tunnel->mesh->max_queue_size &&
1267 tunnel->npackets > 0)
1268 return NULL; /* queue full */
1270 tunnel->mesh->npackets++;
1271 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1272 th->tunnel = tunnel;
1273 th->priority = priority;
1274 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1275 th->target = GNUNET_PEER_intern (target);
1278 target) ? sizeof (struct GNUNET_MESH_Multicast) : sizeof (struct
1279 GNUNET_MESH_Unicast);
1280 th->size = notify_size + overhead;
1281 th->notify = notify;
1282 th->notify_cls = notify_cls;
1283 add_to_queue (tunnel->mesh, th);
1289 * Cancel the specified transmission-ready notification.
1291 * @param th handle that was returned by "notify_transmit_ready".
1294 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1296 struct GNUNET_MESH_Handle *mesh;
1298 mesh = th->tunnel->mesh;
1299 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1300 GNUNET_SCHEDULER_cancel (th->timeout_task);
1301 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1303 if ((NULL == mesh->th_head) && (NULL != mesh->th))
1305 /* queue empty, no point in asking for transmission */
1306 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1312 #if 0 /* keep Emacsens' auto-indent happy */