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 * - handle reconnect (service crash/disconnect) properly
26 * - callbacks to client missing on certain events
27 * - processing messages from service is incomplete
32 * - AUXILIARY FUNCTIONS
35 * - API CALL DEFINITIONS
40 #if 0 /* keep Emacsens' auto-indent happy */
46 #include "gnunet_common.h"
47 #include "gnunet_client_lib.h"
48 #include "gnunet_util_lib.h"
49 #include "gnunet_peer_lib.h"
50 #include "gnunet_mesh_service_new.h"
52 #include "mesh_protocol.h"
55 /******************************************************************************/
56 /************************ DATA STRUCTURES ****************************/
57 /******************************************************************************/
60 * Transmission queue to the service
62 struct GNUNET_MESH_TransmitHandle
68 struct GNUNET_MESH_TransmitHandle *next;
73 struct GNUNET_MESH_TransmitHandle *prev;
76 * Tunnel this message is sent over (may be NULL for control messages).
78 struct GNUNET_MESH_Tunnel *tunnel;
81 * Data itself, currently points to the end of this struct if
82 * we have a message already, NULL if the message is to be
83 * obtained from the callback.
85 const struct GNUNET_MessageHeader *data;
88 * Callback to obtain the message to transmit, or NULL if we
89 * got the message in 'data'. Notice that messages built
90 * by 'notify' need to be encapsulated with information about
93 GNUNET_CONNECTION_TransmitReadyNotify notify;
96 * Closure for 'notify'
101 * How long is this message valid. Once the timeout has been
102 * reached, the message must no longer be sent. If this
103 * is a message with a 'notify' callback set, the 'notify'
104 * function should be called with 'buf' NULL and size 0.
106 struct GNUNET_TIME_Absolute timeout;
109 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
111 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
114 * Priority of the message. The queue is sorted by priority,
115 * control messages have the maximum priority (UINT32_MAX).
120 * Target of the message, 0 for broadcast. This field
121 * is only valid if 'notify' is non-NULL.
123 GNUNET_PEER_Id target;
126 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
133 * Opaque handle to the service.
135 struct GNUNET_MESH_Handle
139 * Handle to the server connection, to send messages later
141 struct GNUNET_CLIENT_Connection *client;
144 * Set of handlers used for processing incoming messages in the tunnels
146 const struct GNUNET_MESH_MessageHandler *message_handlers;
149 * Set of applications that should be claimed to be offered at this node.
150 * Note that this is just informative, the appropiate handlers must be
151 * registered independently and the mapping is up to the developer of the
152 * client application.
154 const GNUNET_MESH_ApplicationType *applications;
157 * Double linked list of the tunnels this client is connected to.
159 struct GNUNET_MESH_Tunnel *tunnels_head;
160 struct GNUNET_MESH_Tunnel *tunnels_tail;
163 * Callback for tunnel disconnection
165 GNUNET_MESH_TunnelEndHandler *cleaner;
168 * Handle to cancel pending transmissions in case of disconnection
170 struct GNUNET_CLIENT_TransmitHandle *th;
173 * Closure for all the handlers given by the client
178 * Messages to send to the service
180 struct GNUNET_MESH_TransmitHandle *th_head;
181 struct GNUNET_MESH_TransmitHandle *th_tail;
184 * tid of the next tunnel to create (to avoid reusing IDs often)
186 MESH_TunnelNumber next_tid;
187 unsigned int n_handlers;
188 unsigned int n_applications;
189 unsigned int max_queue_size;
192 * Have we started the task to receive messages from the service
193 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
198 * Number of packets queued
200 unsigned int npackets;
205 * Description of a peer
207 struct GNUNET_MESH_Peer
210 * ID of the peer in short form
215 * Tunnel this peer belongs to
217 struct GNUNET_MESH_Tunnel *t;
220 * Flag indicating whether service has informed about its connection
225 * Task to cancel the connection request for this peer
227 GNUNET_SCHEDULER_TaskIdentifier cancel;
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 * Number of peers added to the tunnel
284 * Number of packets queued in this tunnel
286 unsigned int npackets;
290 /******************************************************************************/
291 /*********************** AUXILIARY FUNCTIONS *************************/
292 /******************************************************************************/
295 * Get the tunnel handler for the tunnel specified by id from the given handle
296 * @param h Mesh handle
297 * @param tid ID of the wanted tunnel
298 * @return handle to the required tunnel or NULL if not found
300 static struct GNUNET_MESH_Tunnel *
301 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
303 struct GNUNET_MESH_Tunnel *t;
317 * Notify client that the transmission has timed out
319 * @param tc task context
322 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
324 struct GNUNET_MESH_TransmitHandle *th = cls;
325 struct GNUNET_MESH_Handle *mesh;
327 mesh = th->tunnel->mesh;
328 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
329 if (th->notify != NULL)
330 th->notify (th->notify_cls, 0, NULL);
332 if ((NULL == mesh->th_head) && (NULL != mesh->th))
334 /* queue empty, no point in asking for transmission */
335 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
342 * Notify client that the transmission has timed out
344 * @param tc task context
347 timeout_peer_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
349 struct GNUNET_MESH_Peer *p = cls;
350 struct GNUNET_PeerIdentity id;
353 GNUNET_assert (0 == p->connected);
354 for (i = 0; i < p->t->npeers; i++)
356 if (p->t->peers[i] == p)
359 if (i == p->t->npeers)
364 p->t->peers[i] = p->t->peers[p->t->npeers - 1];
365 GNUNET_array_grow (p->t->peers, p->t->npeers, p->t->npeers - 1);
367 if (NULL != p->t->connect_handler)
368 p->t->connect_handler (p->t->cls, 0, NULL);
370 GNUNET_PEER_resolve (p->id, &id);
371 GNUNET_MESH_peer_request_connect_del (p->t, &id);
375 * Add a transmit handle to the transmission queue by priority and set the
378 * @param h mesh handle with the queue head and tail
379 * @param q handle to the packet to be transmitted
382 add_to_queue (struct GNUNET_MESH_Handle *h,
383 struct GNUNET_MESH_TransmitHandle *th)
385 struct GNUNET_MESH_TransmitHandle *p;
388 while ((NULL != p) && (th->priority < p->priority))
394 GNUNET_CONTAINER_DLL_insert_after (h->th_head, h->th_tail, p, th);
395 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
398 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
399 (th->timeout), &timeout_transmission, th);
403 /******************************************************************************/
404 /*********************** RECEIVE HANDLERS ****************************/
405 /******************************************************************************/
408 * Process the new tunnel notification and add it to the tunnels in the handle
410 * @param h The mesh handle
411 * @param msg A message with the details of the new incoming tunnel
414 process_tunnel_create (struct GNUNET_MESH_Handle *h,
415 const struct GNUNET_MESH_TunnelMessage *msg)
417 struct GNUNET_MESH_Tunnel *t;
418 MESH_TunnelNumber tid;
420 tid = ntohl (msg->tunnel_id);
421 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "MESH: received an incoming tunnel with tid in local range (%X)\n",
427 return; //FIXME abort? reconnect?
429 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
438 * Process the new peer event and notify the upper level of it
440 * @param h The mesh handle
441 * @param msg A message with the details of the peer event
444 process_peer_event (struct GNUNET_MESH_Handle *h,
445 const struct GNUNET_MESH_PeerControl *msg)
447 struct GNUNET_MESH_Tunnel *t;
450 size = ntohs (msg->header.size);
451 if (size != sizeof (struct GNUNET_MESH_PeerControl))
456 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
462 if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED == msg->header.type)
464 if (NULL != t->connect_handler)
466 t->connect_handler (t->cls, &msg->peer, NULL); /* FIXME atsi */
471 if (NULL != t->disconnect_handler)
473 t->disconnect_handler (t->cls, &msg->peer);
480 * Process the incoming data packets
482 * @param h The mesh handle
483 * @param msh A message encapsulating the data
486 process_incoming_data (struct GNUNET_MESH_Handle *h,
487 const struct GNUNET_MessageHeader *message)
489 const struct GNUNET_MessageHeader *payload;
490 const struct GNUNET_MESH_MessageHandler *handler;
491 const struct GNUNET_PeerIdentity *peer;
492 struct GNUNET_MESH_Unicast *ucast;
493 struct GNUNET_MESH_Multicast *mcast;
494 struct GNUNET_MESH_ToOrigin *to_orig;
495 struct GNUNET_MESH_Tunnel *t;
499 type = ntohs (message->type);
502 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
503 ucast = (struct GNUNET_MESH_Unicast *) message;
504 t = retrieve_tunnel (h, ntohl (ucast->tid));
505 payload = (struct GNUNET_MessageHeader *) &ucast[1];
508 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
509 mcast = (struct GNUNET_MESH_Multicast *) message;
510 t = retrieve_tunnel (h, ntohl (mcast->tid));
511 payload = (struct GNUNET_MessageHeader *) &mcast[1];
514 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
515 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
516 t = retrieve_tunnel (h, ntohl (to_orig->tid));
517 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
518 peer = &to_orig->sender;
529 for (i = 0; i < h->n_handlers; i++)
531 handler = &h->message_handlers[i];
532 if (handler->type == type)
534 if (GNUNET_OK == handler->callback (h->cls, t, NULL, /* FIXME ctx */
535 peer, payload, NULL))
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "MESH: callback completed successfully\n");
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "MESH: callback caused disconnection\n");
544 GNUNET_MESH_disconnect (h);
552 * Function to process all messages received from the service
555 * @param msg message received, NULL on timeout or fatal error
558 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
560 struct GNUNET_MESH_Handle *h = cls;
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: received a message from MESH\n");
566 h->in_receive = GNUNET_NO;
567 // rather: do_reconnect () -- and set 'in_receive' to NO there...
568 // FIXME: service disconnect, handle!
571 switch (ntohs (msg->type))
573 /* Notify of a new incoming tunnel */
574 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
575 process_tunnel_create (h, (struct GNUNET_MESH_TunnelMessage *) msg);
577 /* Notify of a new peer or a peer disconnect in the tunnel */
578 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED:
579 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED:
580 process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
582 /* Notify of a new data packet in the tunnel */
583 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
584 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
585 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
586 process_incoming_data (h, msg);
588 /* We shouldn't get any other packages, log and ignore */
590 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
591 "MESH: unsolicited message form service (type %d)\n",
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: message processed\n");
595 GNUNET_CLIENT_receive (h->client, &msg_received, h,
596 GNUNET_TIME_UNIT_FOREVER_REL);
600 /******************************************************************************/
601 /************************ SEND FUNCTIONS ****************************/
602 /******************************************************************************/
605 * Function called to send a message to the service.
606 * "buf" will be NULL and "size" zero if the socket was closed for writing in
609 * @param cls closure, the mesh handle
610 * @param size number of bytes available in buf
611 * @param buf where the callee should write the connect message
612 * @return number of bytes written to buf
615 send_raw (void *cls, size_t size, void *buf)
617 struct GNUNET_MESH_Handle *h = cls;
618 struct GNUNET_MESH_TransmitHandle *th;
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send packet() Buffer %u\n", size);
625 if ((0 == size) || (NULL == buf))
627 // FIXME: disconnect, reconnect, retry?
632 while ((NULL != (th = h->th_head)) && (size >= th->size))
634 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "mesh-api", "type: %u\n",
635 ntohs (th->data->type));
636 if (NULL == th->data)
638 GNUNET_assert (NULL != th->notify);
642 struct GNUNET_MESH_Multicast mc;
644 GNUNET_assert (size >= sizeof (mc) + th->size);
646 th->notify (th->notify_cls, size - sizeof (mc), &cbuf[sizeof (mc)]);
649 mc.header.size = htons (sizeof (mc) + th->size);
650 mc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
651 mc.tid = htonl (th->tunnel->tid);
652 memset (&mc.oid, 0, sizeof (struct GNUNET_PeerIdentity)); /* myself */
653 memcpy (cbuf, &mc, sizeof (mc));
654 psize = th->size + sizeof (mc);
660 struct GNUNET_MESH_Unicast uc;
662 GNUNET_assert (size >= sizeof (uc) + th->size);
664 th->notify (th->notify_cls, size - sizeof (uc), &cbuf[sizeof (uc)]);
667 uc.header.size = htons (sizeof (uc) + th->size);
668 uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
669 uc.tid = htonl (th->tunnel->tid);
670 memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity)); /* myself */
671 GNUNET_PEER_resolve (th->target, &uc.destination);
672 memcpy (cbuf, &uc, sizeof (uc));
673 psize = th->size + sizeof (uc);
679 memcpy (cbuf, th->data, th->size);
682 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
683 GNUNET_SCHEDULER_cancel (th->timeout_task);
684 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: size: %u\n", ret);
691 if (NULL != (th = h->th_head))
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: next size: %u\n", th->size);
695 GNUNET_CLIENT_notify_transmit_ready (h->client, th->size,
696 GNUNET_TIME_UNIT_FOREVER_REL,
697 GNUNET_YES, &send_raw, h);
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send packet() END\n");
700 if (GNUNET_NO == h->in_receive)
702 h->in_receive = GNUNET_YES;
703 GNUNET_CLIENT_receive (h->client, &msg_received, h,
704 GNUNET_TIME_UNIT_FOREVER_REL);
711 * Auxiliary function to send an already constructed packet to the service.
712 * Takes care of creating a new queue element, copying the message and
713 * calling the tmt_rdy function if necessary.
714 * @param h mesh handle
715 * @param msg message to transmit
718 send_packet (struct GNUNET_MESH_Handle *h,
719 const struct GNUNET_MessageHeader *msg)
721 struct GNUNET_MESH_TransmitHandle *th;
724 msize = ntohs (msg->size);
725 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
726 th->priority = UINT32_MAX;
727 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
729 th->data = (void *) &th[1];
730 memcpy (&th[1], msg, msize);
731 add_to_queue (h, th);
735 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
736 GNUNET_TIME_UNIT_FOREVER_REL,
737 GNUNET_YES, &send_raw, h);
741 /******************************************************************************/
742 /********************** API CALL DEFINITIONS *************************/
743 /******************************************************************************/
746 * Connect to the mesh service.
748 * @param cfg configuration to use
749 * @param cls closure for the various callbacks that follow
750 * (including handlers in the handlers array)
751 * @param queue_size size of the data message queue, shared among all tunnels
752 * (each tunnel is guaranteed to accept at least one message,
753 * no matter what is the status of other tunnels)
754 * @param cleaner function called when an *inbound* tunnel is destroyed
755 * @param handlers callbacks for messages we care about, NULL-terminated
756 * note that the mesh is allowed to drop notifications about
757 * inbound messages if the client does not process them fast
758 * enough (for this notification type, a bounded queue is used)
759 * @param stypes Application Types the client claims to offer
760 * @return handle to the mesh service
761 * NULL on error (in this case, init is never called)
763 struct GNUNET_MESH_Handle *
764 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
765 unsigned int queue_size, void *cls,
766 GNUNET_MESH_TunnelEndHandler cleaner,
767 const struct GNUNET_MESH_MessageHandler *handlers,
768 const GNUNET_MESH_ApplicationType *stypes)
770 struct GNUNET_MESH_Handle *h;
771 struct GNUNET_MESH_ClientConnect *msg;
772 GNUNET_MESH_ApplicationType *apps;
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect()\n");
779 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
780 h->max_queue_size = queue_size;
781 h->cleaner = cleaner;
782 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
783 if (h->client == NULL)
790 h->message_handlers = handlers;
791 h->applications = stypes;
792 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_MARK;
794 /* count handlers and apps, calculate size */
795 for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
796 for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
797 size = sizeof (struct GNUNET_MESH_ClientConnect);
798 size += h->n_handlers * sizeof (uint16_t);
799 size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
804 /* build connection packet */
805 msg = (struct GNUNET_MESH_ClientConnect *) buf;
806 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
807 msg->header.size = htons (size);
808 types = (uint16_t *) & msg[1];
809 for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
810 types[ntypes] = h->message_handlers[ntypes].type;
811 apps = (GNUNET_MESH_ApplicationType *) &types[ntypes];
812 for (napps = 0; napps < h->n_applications; napps++)
813 apps[napps] = h->applications[napps];
814 msg->applications = htons (napps);
815 msg->types = htons (ntypes);
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817 "mesh: Sending %lu bytes long message %d types and %d apps\n",
818 ntohs (msg->header.size), ntypes, napps);
819 send_packet (h, &msg->header);
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect() END\n");
827 * Disconnect from the mesh service.
829 * @param handle connection to mesh to disconnect
832 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
834 if (NULL != handle->th)
836 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
838 if (NULL != handle->client)
840 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
842 GNUNET_free (handle);
847 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
850 * @param h mesh handle
851 * @param connect_handler function to call when peers are actually connected
852 * @param disconnect_handler function to call when peers are disconnected
853 * @param handler_cls closure for connect/disconnect handlers
855 struct GNUNET_MESH_Tunnel *
856 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
857 GNUNET_MESH_TunnelConnectHandler connect_handler,
858 GNUNET_MESH_TunnelDisconnectHandler
859 disconnect_handler, void *handler_cls)
861 struct GNUNET_MESH_Tunnel *t;
862 struct GNUNET_MESH_TunnelMessage msg;
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Creating new tunnel\n");
865 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
866 t->connect_handler = connect_handler;
867 t->disconnect_handler = disconnect_handler;
868 t->cls = handler_cls;
870 t->tid = h->next_tid++;
871 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK; // keep in range
872 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
873 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
874 msg.tunnel_id = htonl (t->tid);
875 send_packet (h, &msg.header);
881 * Destroy an existing tunnel.
883 * @param tun tunnel handle
886 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tun)
888 struct GNUNET_MESH_Handle *h;
889 struct GNUNET_MESH_TunnelMessage msg;
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Destroying tunnel\n");
894 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
895 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
896 msg.tunnel_id = htonl (tun->tid);
898 send_packet (h, &msg.header);
903 * Request that a peer should be added to the tunnel. The existing
904 * connect handler will be called ONCE with either success or failure.
905 * This function should NOT be called again with the same peer before the
906 * connect handler is called
908 * @param tunnel handle to existing tunnel
909 * @param timeout how long to try to establish a connection
910 * @param peer peer to add
913 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
914 struct GNUNET_TIME_Relative timeout,
915 const struct GNUNET_PeerIdentity *peer)
917 struct GNUNET_MESH_PeerControl msg;
918 struct GNUNET_MESH_Peer *p;
919 GNUNET_PEER_Id peer_id;
922 peer_id = GNUNET_PEER_intern (peer);
923 for (i = 0; i < tunnel->npeers; i++)
925 if (tunnel->peers[i]->id == peer_id)
927 GNUNET_PEER_change_rc (peer_id, -1);
928 /* FIXME: peer was already in the tunnel */
932 p = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
935 p->cancel = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_peer_request, p);
936 GNUNET_array_append (tunnel->peers, tunnel->npeers, p);
938 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
939 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD);
940 msg.tunnel_id = htonl (tunnel->tid);
942 send_packet (tunnel->mesh, &msg.header);
943 // tunnel->connect_handler (tunnel->cls, peer, NULL); FIXME call this later
944 // TODO: remember timeout
950 * Request that a peer should be removed from the tunnel. The existing
951 * disconnect handler will be called ONCE if we were connected.
953 * @param tunnel handle to existing tunnel
954 * @param peer peer to remove
957 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
958 const struct GNUNET_PeerIdentity *peer)
960 struct GNUNET_MESH_PeerControl msg;
961 GNUNET_PEER_Id peer_id;
964 peer_id = GNUNET_PEER_search (peer);
970 for (i = 0; i < tunnel->npeers; i++)
971 if (tunnel->peers[i]->id == peer_id)
973 if (i == tunnel->npeers)
978 if (NULL != tunnel->disconnect_handler && tunnel->peers[i]->connected == 1)
979 tunnel->disconnect_handler (tunnel->cls, peer);
980 GNUNET_PEER_change_rc (peer_id, -1);
981 GNUNET_free (tunnel->peers[i]);
982 tunnel->peers[i] = tunnel->peers[tunnel->npeers - 1];
983 GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1);
985 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
986 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL);
987 msg.tunnel_id = htonl (tunnel->tid);
988 memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
989 send_packet (tunnel->mesh, &msg.header);
994 * Request that the mesh should try to connect to a peer supporting the given
997 * @param tunnel handle to existing tunnel
998 * @param timeout how long to try to establish a connection
999 * @param app_type application type that must be supported by the peer (MESH
1000 * should discover peer in proximity handling this type)
1003 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
1004 struct GNUNET_TIME_Relative timeout,
1005 GNUNET_MESH_ApplicationType app_type)
1007 struct GNUNET_MESH_ConnectPeerByType msg;
1009 /* FIXME: remember request connect by type for reconnect! */
1010 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
1011 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE);
1012 msg.tunnel_id = htonl (tunnel->tid);
1013 msg.type = htonl (app_type);
1014 send_packet (tunnel->mesh, &msg.header);
1019 * Ask the mesh to call "notify" once it is ready to transmit the
1020 * given number of bytes to the specified "target". If we are not yet
1021 * connected to the specified peer, a call to this function will cause
1022 * us to try to establish a connection.
1024 * @param tunnel tunnel to use for transmission
1025 * @param cork is corking allowed for this transmission?
1026 * @param priority how important is the message?
1027 * @param maxdelay how long can the message wait?
1028 * @param target destination for the message,
1029 * NULL for multicast to all tunnel targets
1030 * @param notify_size how many bytes of buffer space does notify want?
1031 * @param notify function to call when buffer space is available;
1032 * will be called with NULL on timeout or if the overall queue
1033 * for this peer is larger than queue_size and this is currently
1034 * the message with the lowest priority
1035 * @param notify_cls closure for notify
1036 * @return non-NULL if the notify callback was queued,
1037 * NULL if we can not even queue the request (insufficient
1038 * memory); if NULL is returned, "notify" will NOT be called.
1040 struct GNUNET_MESH_TransmitHandle *
1041 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1043 struct GNUNET_TIME_Relative maxdelay,
1044 const struct GNUNET_PeerIdentity *target,
1046 GNUNET_CONNECTION_TransmitReadyNotify notify,
1049 struct GNUNET_MESH_TransmitHandle *th;
1052 if (tunnel->mesh->npackets >= tunnel->mesh->max_queue_size &&
1053 tunnel->npackets > 0)
1054 return NULL; /* queue full */
1056 tunnel->mesh->npackets++;
1057 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1058 th->tunnel = tunnel;
1059 th->priority = priority;
1060 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1061 th->target = GNUNET_PEER_intern (target);
1064 target) ? sizeof (struct GNUNET_MESH_Multicast) : sizeof (struct
1065 GNUNET_MESH_Unicast);
1066 th->size = notify_size + overhead;
1067 th->notify = notify;
1068 th->notify_cls = notify_cls;
1069 add_to_queue (tunnel->mesh, th);
1075 * Cancel the specified transmission-ready notification.
1077 * @param th handle that was returned by "notify_transmit_ready".
1080 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1082 struct GNUNET_MESH_Handle *mesh;
1084 mesh = th->tunnel->mesh;
1085 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1086 GNUNET_SCHEDULER_cancel (th->timeout_task);
1087 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1089 if ((NULL == mesh->th_head) && (NULL != mesh->th))
1091 /* queue empty, no point in asking for transmission */
1092 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1098 #if 0 /* keep Emacsens' auto-indent happy */