2 This file is part of GNUnet.
3 (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.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/mesh_api_new.c
23 * @brief mesh api: client implementation of mesh service
24 * @author Bartlomiej Polot
29 * - AUXILIARY FUNCTIONS
32 * - API CALL DEFINITIONS
39 #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"
54 #define MESH_API_MAX_QUEUE 10
56 /******************************************************************************/
57 /************************ DATA STRUCTURES ****************************/
58 /******************************************************************************/
61 * Transmission queue to the service
63 struct GNUNET_MESH_TransmitHandle
68 struct GNUNET_MESH_TransmitHandle *next;
73 struct GNUNET_MESH_TransmitHandle *prev;
76 * Data itself, currently points to the end of this struct if
77 * we have a message already, NULL if the message is to be
78 * obtained from the callback.
80 const struct GNUNET_MessageHeader *data;
83 * Tunnel this message is sent over (may be NULL for control messages).
85 struct GNUNET_MESH_Tunnel *tunnel;
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
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 *queue_head;
180 struct GNUNET_MESH_TransmitHandle *queue_tail;
183 * tid of the next tunnel to create (to avoid reusing IDs often)
185 MESH_TunnelNumber next_tid;
187 unsigned int n_handlers;
189 unsigned int n_applications;
191 unsigned int max_queue_size;
194 * Have we started the task to receive messages from the service
195 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
201 * Opaque handle to a tunnel.
203 struct GNUNET_MESH_Tunnel
209 struct GNUNET_MESH_Tunnel *next;
210 struct GNUNET_MESH_Tunnel *prev;
213 * Callback to execute when peers connect to the tunnel
215 GNUNET_MESH_TunnelConnectHandler connect_handler;
218 * Callback to execute when peers disconnect to the tunnel
220 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
223 * All peers added to the tunnel
225 GNUNET_PEER_Id *peers;
228 * Closure for the connect/disconnect handlers
233 * Handle to the mesh this tunnel belongs to
235 struct GNUNET_MESH_Handle *mesh;
238 * Local ID of the tunnel
240 MESH_TunnelNumber tid;
243 * Owner of the tunnel
245 GNUNET_PEER_Id owner;
248 * Number of peer added to the tunnel
254 /******************************************************************************/
255 /*********************** AUXILIARY FUNCTIONS *************************/
256 /******************************************************************************/
259 * Get the tunnel handler for the tunnel specified by id from the given handle
260 * @param h Mesh handle
261 * @param tid ID of the wanted tunnel
262 * @return handle to the required tunnel or NULL if not found
264 static struct GNUNET_MESH_Tunnel *
265 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
267 struct GNUNET_MESH_Tunnel *t;
280 * Get the length of the transmission queue
281 * @param h mesh handle whose queue is to be measured
284 get_queue_length (struct GNUNET_MESH_Handle *h)
286 struct GNUNET_MESH_TransmitHandle *q;
290 for (q = h->queue_head, i = 0; NULL != q; q = q->next, i++) ;
296 /******************************************************************************/
297 /*********************** RECEIVE HANDLERS ****************************/
298 /******************************************************************************/
301 * Process the new tunnel notification and add it to the tunnels in the handle
303 * @param h The mesh handle
304 * @param msg A message with the details of the new incoming tunnel
307 process_tunnel_create (struct GNUNET_MESH_Handle *h,
308 const struct GNUNET_MESH_TunnelMessage *msg)
310 struct GNUNET_MESH_Tunnel *t;
311 MESH_TunnelNumber tid;
313 tid = ntohl (msg->tunnel_id);
314 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "MESH: received an incoming tunnel with tid in local range (%X)\n",
320 return; //FIXME abort? reconnect?
322 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
332 * Process the new peer event and notify the upper level of it
334 * @param h The mesh handle
335 * @param msg A message with the details of the peer event
338 process_peer_event (struct GNUNET_MESH_Handle *h,
339 const struct GNUNET_MESH_PeerControl *msg)
341 struct GNUNET_MESH_Tunnel *t;
344 size = ntohs (msg->header.size);
345 if (size != sizeof (struct GNUNET_MESH_PeerControl))
350 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
356 if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED == msg->header.type)
358 if (NULL != t->connect_handler)
360 t->connect_handler (t->cls, &msg->peer, NULL); /* FIXME atsi */
365 if (NULL != t->disconnect_handler)
367 t->disconnect_handler (t->cls, &msg->peer);
374 * Process the incoming data packets
376 * @param h The mesh handle
377 * @param msh A message encapsulating the data
380 process_incoming_data (struct GNUNET_MESH_Handle *h,
381 const struct GNUNET_MessageHeader *message)
383 const struct GNUNET_MessageHeader *payload;
384 const struct GNUNET_MESH_MessageHandler *handler;
385 const struct GNUNET_PeerIdentity *peer;
386 struct GNUNET_MESH_Unicast *ucast;
387 struct GNUNET_MESH_Multicast *mcast;
388 struct GNUNET_MESH_ToOrigin *to_orig;
389 struct GNUNET_MESH_Tunnel *t;
393 type = ntohs (message->type);
396 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
397 ucast = (struct GNUNET_MESH_Unicast *) message;
398 t = retrieve_tunnel (h, ntohl (ucast->tid));
399 payload = (struct GNUNET_MessageHeader *) &ucast[1];
402 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
403 mcast = (struct GNUNET_MESH_Multicast *) message;
404 t = retrieve_tunnel (h, ntohl (mcast->tid));
405 payload = (struct GNUNET_MessageHeader *) &mcast[1];
408 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
409 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
410 t = retrieve_tunnel (h, ntohl (to_orig->tid));
411 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
412 peer = &to_orig->sender;
423 for (i = 0; i < h->n_handlers; i++)
425 handler = &h->message_handlers[i];
426 if (handler->type == type)
428 if (GNUNET_OK == handler->callback (h->cls, t, NULL, /* FIXME ctx */
429 peer, payload, NULL)) /* FIXME atsi */
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "MESH: callback completed successfully\n");
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "MESH: callback caused disconnection\n");
438 GNUNET_MESH_disconnect (h);
446 * Function to process all messages received from the service
449 * @param msg message received, NULL on timeout or fatal error
452 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
454 struct GNUNET_MESH_Handle *h = cls;
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: received a message from MESH\n");
460 h->in_receive = GNUNET_NO;
461 // rather: do_reconnect () -- and set 'in_receive' to NO there...
462 // FIXME: service disconnect, handle!
466 switch (ntohs (msg->type))
468 /* Notify of a new incoming tunnel */
469 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
470 process_tunnel_create (h, (struct GNUNET_MESH_TunnelMessage *) msg);
472 /* Notify of a new peer or a peer disconnect in the tunnel */
473 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED:
474 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED:
475 process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
477 /* Notify of a new data packet in the tunnel */
478 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
479 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
480 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
481 process_incoming_data (h, msg);
483 /* We shouldn't get any other packages, log and ignore */
485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
486 "MESH: unsolicited message form service (type %d)\n",
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: message processed\n");
491 GNUNET_CLIENT_receive (h->client, &msg_received, h,
492 GNUNET_TIME_UNIT_FOREVER_REL);
496 /******************************************************************************/
497 /************************ SEND FUNCTIONS ****************************/
498 /******************************************************************************/
501 * Function called to send a message to the service.
502 * "buf" will be NULL and "size" zero if the socket was closed for writing in
505 * @param cls closure, the mesh handle
506 * @param size number of bytes available in buf
507 * @param buf where the callee should write the connect message
508 * @return number of bytes written to buf
511 send_raw (void *cls, size_t size, void *buf)
513 struct GNUNET_MESH_Handle *h = cls;
514 struct GNUNET_MESH_TransmitHandle *q;
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send packet() Buffer %u\n", size);
518 if (0 == size || NULL == buf)
520 // FIXME: disconnect, reconnect, retry?
525 GNUNET_assert (NULL != q);
526 if (sizeof (struct GNUNET_MessageHeader) > size)
529 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) > ntohs (q->data->size));
531 GNUNET_CLIENT_notify_transmit_ready (h->client, q->size,
532 GNUNET_TIME_UNIT_FOREVER_REL,
533 GNUNET_YES, &send_raw, h);
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: type: %i\n",
537 ntohs (q->data->type));
540 GNUNET_assert (NULL != q->notify);
544 struct GNUNET_MESH_Multicast mc;
547 GNUNET_assert (size >= sizeof (mc) + q->size);
549 q->size = q->notify (q->notify_cls,
558 mc.header.size = htons (sizeof (mc) + q->size);
559 mc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
560 mc.tid = htonl (q->tunnel->tid);
561 memset (&mc.oid, 0, sizeof (struct GNUNET_PeerIdentity)); /* myself */
562 memcpy (buf, &mc, sizeof (mc));
563 size = q->size + sizeof (mc);
569 struct GNUNET_MESH_Unicast uc;
572 GNUNET_assert (size >= sizeof (uc) + q->size);
574 q->size = q->notify (q->notify_cls,
583 uc.header.size = htons (sizeof (uc) + q->size);
584 uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
585 uc.tid = htonl (q->tunnel->tid);
586 memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity)); /* myself */
587 GNUNET_PEER_resolve (q->target, &uc.destination);
588 memcpy (buf, &uc, sizeof (uc));
589 size = q->size + sizeof (uc);
595 memcpy (buf, q->data, q->size);
598 if (q->timeout_task != GNUNET_SCHEDULER_NO_TASK)
599 GNUNET_SCHEDULER_cancel (q->timeout_task);
600 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, q);
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: size: %u\n", size);
604 if (NULL != h->queue_head)
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: next size: %u\n",
607 h->queue_head->size);
609 GNUNET_CLIENT_notify_transmit_ready (h->client, h->queue_head->size,
610 GNUNET_TIME_UNIT_FOREVER_REL,
611 GNUNET_YES, &send_raw, h);
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send packet() END\n");
614 if (GNUNET_NO == h->in_receive)
616 h->in_receive = GNUNET_YES;
617 GNUNET_CLIENT_receive (h->client, &msg_received, h,
618 GNUNET_TIME_UNIT_FOREVER_REL);
625 timeout_transmission (void *cls,
626 const struct GNUNET_SCHEDULER_TaskContext *tc)
628 struct GNUNET_MESH_TransmitHandle *q = cls;
629 struct GNUNET_MESH_Handle *mesh;
631 mesh = q->tunnel->mesh;
632 GNUNET_CONTAINER_DLL_remove (mesh->queue_head,
635 if (q->notify != NULL)
636 q->notify (q->notify_cls, 0, NULL); /* signal timeout */
638 if ( (NULL == mesh->queue_head) &&
641 /* queue empty, no point in asking for transmission */
642 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
649 * Add a transmit handle to the transmission queue (by priority).
650 * Also manage timeout.
652 * @param h mesh handle with the queue head and tail
653 * @param q handle to add
656 queue_transmit_handle (struct GNUNET_MESH_Handle *h,
657 struct GNUNET_MESH_TransmitHandle *q)
659 struct GNUNET_MESH_TransmitHandle *p;
662 while ( (NULL != p) && (q->priority < p->priority) )
664 GNUNET_CONTAINER_DLL_insert_after (h->queue_head, h->queue_tail, p->prev, q);
665 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value != q->timeout.abs_value)
666 q->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (q->timeout),
667 &timeout_transmission,
673 * Auxiliary function to send a packet to the service
674 * Takes care of creating a new queue element and calling the tmt_rdy function
676 * @param h mesh handle
677 * @param msg message to transmit
680 send_packet (struct GNUNET_MESH_Handle *h,
681 const struct GNUNET_MessageHeader *msg)
683 struct GNUNET_MESH_TransmitHandle *q;
686 msize = ntohs (msg->size);
687 q = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
688 q->priority = UINT32_MAX;
689 q->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
691 q->data = (void*) &q[1];
692 memcpy (&q[1], msg, msize);
693 queue_transmit_handle (h, q);
697 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
698 GNUNET_TIME_UNIT_FOREVER_REL,
699 GNUNET_YES, &send_raw, h);
702 /******************************************************************************/
703 /********************** API CALL DEFINITIONS *************************/
704 /******************************************************************************/
707 * Connect to the mesh service.
709 * @param cfg configuration to use
710 * @param cls closure for the various callbacks that follow
711 * (including handlers in the handlers array)
712 * @param cleaner function called when an *inbound* tunnel is destroyed
713 * @param handlers callbacks for messages we care about, NULL-terminated
714 * note that the mesh is allowed to drop notifications about
715 * inbound messages if the client does not process them fast
716 * enough (for this notification type, a bounded queue is used)
717 * @param stypes Application Types the client claims to offer
718 * @return handle to the mesh service
719 * NULL on error (in this case, init is never called)
721 struct GNUNET_MESH_Handle *
722 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
723 GNUNET_MESH_TunnelEndHandler cleaner,
724 const struct GNUNET_MESH_MessageHandler *handlers,
725 const GNUNET_MESH_ApplicationType *stypes)
727 struct GNUNET_MESH_Handle *h;
728 struct GNUNET_MESH_ClientConnect *msg;
729 GNUNET_MESH_ApplicationType *apps;
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect()\n");
736 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
737 h->max_queue_size = MESH_API_MAX_QUEUE; /* FIXME: add to arguments to 'GNUNET_MESH_connect' */
738 h->cleaner = cleaner;
739 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
740 if (h->client == NULL)
748 h->message_handlers = handlers;
749 h->applications = stypes;
750 h->next_tid = 0x80000000;
752 /* count handlers and apps, calculate size */
753 for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
754 for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
756 size = sizeof (struct GNUNET_MESH_ClientConnect);
757 size += h->n_handlers * sizeof (uint16_t);
758 size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
763 /* build connection packet */
764 msg = (struct GNUNET_MESH_ClientConnect *) buf;
765 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
766 msg->header.size = htons (size);
767 types = (uint16_t *) & msg[1];
768 for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
769 types[ntypes] = h->message_handlers[ntypes].type;
770 apps = (GNUNET_MESH_ApplicationType *) &types[ntypes];
771 for (napps = 0; napps < h->n_applications; napps++)
772 apps[napps] = h->applications[napps];
773 msg->applications = htons (napps);
774 msg->types = htons (ntypes);
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "mesh: Sending %lu bytes long message %d types and %d apps\n",
778 ntohs (msg->header.size), ntypes, napps);
780 send_packet (h, &msg->header);
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect() END\n");
790 * Disconnect from the mesh service.
792 * @param handle connection to mesh to disconnect
795 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
797 if (NULL != handle->th)
799 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
801 if (NULL != handle->client)
803 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
805 GNUNET_free (handle);
810 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
813 * @param h mesh handle
814 * @param connect_handler function to call when peers are actually connected
815 * @param disconnect_handler function to call when peers are disconnected
816 * @param handler_cls closure for connect/disconnect handlers
818 struct GNUNET_MESH_Tunnel *
819 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
820 GNUNET_MESH_TunnelConnectHandler connect_handler,
821 GNUNET_MESH_TunnelDisconnectHandler
822 disconnect_handler, void *handler_cls)
824 struct GNUNET_MESH_Tunnel *t;
825 struct GNUNET_MESH_TunnelMessage msg;
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Creating new tunnel\n");
828 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
830 t->connect_handler = connect_handler;
831 t->disconnect_handler = disconnect_handler;
832 t->cls = handler_cls;
834 t->tid = h->next_tid++;
835 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK; // keep in range
837 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
838 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
839 msg.tunnel_id = htonl (t->tid);
840 send_packet (h, &msg.header);
846 * Destroy an existing tunnel.
848 * @param tun tunnel handle
851 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tun)
853 struct GNUNET_MESH_Handle *h;
854 struct GNUNET_MESH_TunnelMessage *msg;
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Destroying tunnel\n");
859 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_TunnelMessage));
860 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
861 msg->header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
862 msg->tunnel_id = htonl (tun->tid);
866 send_packet (h, &msg->header);
871 * Request that a peer should be added to the tunnel. The existing
872 * connect handler will be called ONCE with either success or failure.
874 * @param tunnel handle to existing tunnel
875 * @param timeout how long to try to establish a connection
876 * @param peer peer to add
879 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
880 struct GNUNET_TIME_Relative timeout,
881 const struct GNUNET_PeerIdentity *peer)
883 struct GNUNET_MESH_PeerControl *msg;
884 GNUNET_PEER_Id peer_id;
887 peer_id = GNUNET_PEER_intern (peer);
888 for (i = 0; i < tunnel->npeers; i++)
890 if (tunnel->peers[i] == peer_id)
892 GNUNET_PEER_change_rc (peer_id, -1);
898 GNUNET_realloc (tunnel->peers, tunnel->npeers * sizeof (GNUNET_PEER_Id));
899 tunnel->peers[tunnel->npeers - 1] = peer_id;
901 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PeerControl));
902 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
903 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD);
904 msg->tunnel_id = htonl (tunnel->tid);
905 memcpy (&msg->peer, peer, sizeof (struct GNUNET_PeerIdentity));
907 send_packet (tunnel->mesh, &msg->header);
909 // tunnel->connect_handler (tunnel->cls, peer, NULL); FIXME call this later
910 // TODO: remember timeout
916 * Request that a peer should be removed from the tunnel. The existing
917 * disconnect handler will be called ONCE if we were connected.
919 * @param tunnel handle to existing tunnel
920 * @param peer peer to remove
923 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
924 const struct GNUNET_PeerIdentity *peer)
926 struct GNUNET_MESH_PeerControl msg;
927 GNUNET_PEER_Id peer_id;
930 peer_id = GNUNET_PEER_search (peer);
936 for (i = 0; i < tunnel->npeers; i++)
937 if (tunnel->peers[i] == peer_id)
939 if (i == tunnel->npeers)
944 GNUNET_PEER_change_rc (peer_id, -1);
945 tunnel->peers[i] = tunnel->peers[tunnel->npeers-1];
946 GNUNET_array_grow (tunnel->peers,
949 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
950 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL);
951 msg.tunnel_id = htonl (tunnel->tid);
952 memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
953 send_packet (tunnel->mesh, &msg.header);
958 * Request that the mesh should try to connect to a peer supporting the given
961 * @param tunnel handle to existing tunnel
962 * @param timeout how long to try to establish a connection
963 * @param app_type application type that must be supported by the peer (MESH
964 * should discover peer in proximity handling this type)
967 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
968 struct GNUNET_TIME_Relative timeout,
969 GNUNET_MESH_ApplicationType app_type)
971 struct GNUNET_MESH_ConnectPeerByType msg;
973 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
974 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE);
975 msg.tunnel_id = htonl (tunnel->tid);
976 msg.type = htonl (app_type);
977 send_packet (tunnel->mesh, &msg.header);
978 // TODO: remember timeout
983 * Ask the mesh to call "notify" once it is ready to transmit the
984 * given number of bytes to the specified "target". If we are not yet
985 * connected to the specified peer, a call to this function will cause
986 * us to try to establish a connection.
988 * @param tunnel tunnel to use for transmission
989 * @param cork is corking allowed for this transmission?
990 * @param priority how important is the message?
991 * @param maxdelay how long can the message wait?
992 * @param target destination for the message,
993 * NULL for multicast to all tunnel targets
994 * @param notify_size how many bytes of buffer space does notify want?
995 * @param notify function to call when buffer space is available;
996 * will be called with NULL on timeout or if the overall queue
997 * for this peer is larger than queue_size and this is currently
998 * the message with the lowest priority
999 * @param notify_cls closure for notify
1000 * @return non-NULL if the notify callback was queued,
1001 * NULL if we can not even queue the request (insufficient
1002 * memory); if NULL is returned, "notify" will NOT be called.
1004 struct GNUNET_MESH_TransmitHandle *
1005 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
1007 struct GNUNET_TIME_Relative maxdelay,
1008 const struct GNUNET_PeerIdentity *target,
1010 GNUNET_CONNECTION_TransmitReadyNotify notify,
1013 struct GNUNET_MESH_TransmitHandle *q;
1015 if (get_queue_length (tunnel->mesh) >= tunnel->mesh->max_queue_size)
1016 return NULL; /* queue full */
1018 q = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1020 q->priority = priority;
1021 q->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1022 q->target = GNUNET_PEER_intern (target);
1023 q->size = notify_size;
1025 q->notify_cls = notify_cls;
1026 queue_transmit_handle (tunnel->mesh, q);
1032 * Cancel the specified transmission-ready notification.
1034 * @param th handle that was returned by "notify_transmit_ready".
1037 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1039 struct GNUNET_MESH_Handle *mesh;
1041 mesh = th->tunnel->mesh;
1042 if (q->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1043 GNUNET_SCHEDULER_cancel (q->timeout_task);
1044 GNUNET_CONTAINER_DLL_remove (mesh->queue_head,
1048 if ( (NULL == mesh->queue_head) &&
1049 (NULL != mesh->th) )
1051 /* queue empty, no point in asking for transmission */
1052 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1058 #if 0 /* keep Emacsens' auto-indent happy */