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 new mesh service
21 * @author Bartlomiej Polot
25 #include "gnunet_util_lib.h"
26 #include "gnunet_mesh_service.h"
28 #include "mesh_protocol.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
32 /******************************************************************************/
33 /************************ DATA STRUCTURES ****************************/
34 /******************************************************************************/
37 * Transmission queue to the service
39 struct GNUNET_MESH_TransmitHandle
45 struct GNUNET_MESH_TransmitHandle *next;
50 struct GNUNET_MESH_TransmitHandle *prev;
53 * Channel this message is sent on / for (may be NULL for control messages).
55 struct GNUNET_MESH_Channel *channel;
58 * Callback to obtain the message to transmit, or NULL if we
59 * got the message in 'data'. Notice that messages built
60 * by 'notify' need to be encapsulated with information about
63 GNUNET_CONNECTION_TransmitReadyNotify notify;
66 * Closure for 'notify'
71 * How long is this message valid. Once the timeout has been
72 * reached, the message must no longer be sent. If this
73 * is a message with a 'notify' callback set, the 'notify'
74 * function should be called with 'buf' NULL and size 0.
76 struct GNUNET_TIME_Absolute timeout;
79 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
81 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
84 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
91 * Opaque handle to the service.
93 struct GNUNET_MESH_Handle
97 * Handle to the server connection, to send messages later
99 struct GNUNET_CLIENT_Connection *client;
102 * Set of handlers used for processing incoming messages in the channels
104 const struct GNUNET_MESH_MessageHandler *message_handlers;
107 * Number of handlers in the handlers array.
109 unsigned int n_handlers;
114 const uint32_t *ports;
119 unsigned int n_ports;
122 * Double linked list of the channels this client is connected to, head.
124 struct GNUNET_MESH_Channel *channels_head;
127 * Double linked list of the channels this client is connected to, tail.
129 struct GNUNET_MESH_Channel *channels_tail;
132 * Callback for inbound channel creation
134 GNUNET_MESH_InboundChannelNotificationHandler *new_channel;
137 * Callback for inbound channel disconnection
139 GNUNET_MESH_ChannelEndHandler *cleaner;
142 * Handle to cancel pending transmissions in case of disconnection
144 struct GNUNET_CLIENT_TransmitHandle *th;
147 * Closure for all the handlers given by the client
152 * Messages to send to the service, head.
154 struct GNUNET_MESH_TransmitHandle *th_head;
157 * Messages to send to the service, tail.
159 struct GNUNET_MESH_TransmitHandle *th_tail;
162 * chid of the next channel to create (to avoid reusing IDs often)
164 MESH_ChannelNumber next_chid;
167 * Have we started the task to receive messages from the service
168 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
173 * Configuration given by the client, in case of reconnection
175 const struct GNUNET_CONFIGURATION_Handle *cfg;
178 * Time to the next reconnect in case one reconnect fails
180 struct GNUNET_TIME_Relative reconnect_time;
183 * Task for trying to reconnect.
185 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
190 GNUNET_MESH_ChannelsCB channels_cb;
193 * Monitor callback closure.
200 GNUNET_MESH_ChannelCB channel_cb;
203 * Channel callback closure.
210 * Description of a peer
212 struct GNUNET_MESH_Peer
215 * ID of the peer in short form
220 * Channel this peer belongs to
222 struct GNUNET_MESH_Channel *t;
227 * Opaque handle to a channel.
229 struct GNUNET_MESH_Channel
235 struct GNUNET_MESH_Channel *next;
240 struct GNUNET_MESH_Channel *prev;
243 * Handle to the mesh this channel belongs to
245 struct GNUNET_MESH_Handle *mesh;
248 * Local ID of the channel
250 MESH_ChannelNumber chid;
258 * Other end of the channel.
263 * Any data the caller wants to put in here
268 * Size of packet queued in this channel
270 unsigned int packet_size;
273 * Channel options: reliability, etc.
275 enum GNUNET_MESH_ChannelOption options;
278 * Are we allowed to send to the service?
286 * Implementation state for mesh's message queue.
291 * The current transmit handle, or NULL
292 * if no transmit is active.
294 struct GNUNET_MESH_TransmitHandle *th;
297 * Channel to send the data over.
299 struct GNUNET_MESH_Channel *channel;
303 /******************************************************************************/
304 /*********************** DECLARATIONS *************************/
305 /******************************************************************************/
308 * Function called to send a message to the service.
309 * "buf" will be NULL and "size" zero if the socket was closed for writing in
312 * @param cls closure, the mesh handle
313 * @param size number of bytes available in buf
314 * @param buf where the callee should write the connect message
315 * @return number of bytes written to buf
318 send_callback (void *cls, size_t size, void *buf);
321 /******************************************************************************/
322 /*********************** AUXILIARY FUNCTIONS *************************/
323 /******************************************************************************/
326 * Check if transmission is a payload packet.
328 * @param th Transmission handle.
330 * @return GNUNET_YES if it is a payload packet,
331 * GNUNET_NO if it is a mesh management packet.
334 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
336 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
341 * Check whether there is any message ready in the queue and find the size.
343 * @param h Mesh handle.
345 * @return The size of the first ready message in the queue,
346 * 0 if there is none.
349 message_ready_size (struct GNUNET_MESH_Handle *h)
351 struct GNUNET_MESH_TransmitHandle *th;
352 struct GNUNET_MESH_Channel *ch;
354 for (th = h->th_head; NULL != th; th = th->next)
357 if (GNUNET_NO == th_is_payload (th))
359 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
362 if (GNUNET_YES == ch->allow_send)
364 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok\n");
373 * Get the channel handler for the channel specified by id from the given handle
374 * @param h Mesh handle
375 * @param chid ID of the wanted channel
376 * @return handle to the required channel or NULL if not found
378 static struct GNUNET_MESH_Channel *
379 retrieve_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
381 struct GNUNET_MESH_Channel *ch;
383 ch = h->channels_head;
386 if (ch->chid == chid)
395 * Create a new channel and insert it in the channel list of the mesh handle
397 * @param h Mesh handle
398 * @param chid Desired chid of the channel, 0 to assign one automatically.
400 * @return Handle to the created channel.
402 static struct GNUNET_MESH_Channel *
403 create_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
405 struct GNUNET_MESH_Channel *ch;
407 ch = GNUNET_malloc (sizeof (struct GNUNET_MESH_Channel));
408 GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
412 ch->chid = h->next_chid;
413 while (NULL != retrieve_channel (h, h->next_chid))
416 h->next_chid &= ~GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
417 h->next_chid |= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
424 ch->allow_send = GNUNET_NO;
430 * Destroy the specified channel.
431 * - Destroys all peers, calling the disconnect callback on each if needed
432 * - Cancels all outgoing traffic for that channel, calling respective notifys
433 * - Calls cleaner if channel was inbound
434 * - Frees all memory used
436 * @param ch Pointer to the channel.
437 * @param call_cleaner Whether to call the cleaner handler.
439 * @return Handle to the required channel or NULL if not found.
442 destroy_channel (struct GNUNET_MESH_Channel *ch, int call_cleaner)
444 struct GNUNET_MESH_Handle *h;
445 struct GNUNET_MESH_TransmitHandle *th;
446 struct GNUNET_MESH_TransmitHandle *next;
448 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
457 GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch);
459 /* signal channel destruction */
460 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
462 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cleaner\n");
463 h->cleaner (h->cls, ch, ch->ctx);
466 /* check that clients did not leave messages behind in the queue */
467 for (th = h->th_head; NULL != th; th = next)
470 if (th->channel != ch)
472 /* Clients should have aborted their requests already.
473 * Management traffic should be ok, as clients can't cancel that.
474 * If the service crashed and we are reconnecting, it's ok.
476 GNUNET_break (GNUNET_NO == th_is_payload (th)
477 || GNUNET_NO == h->in_receive);
478 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
480 /* clean up request */
481 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
482 GNUNET_SCHEDULER_cancel (th->timeout_task);
486 /* if there are no more pending requests with mesh service, cancel active request */
487 /* Note: this should be unnecessary... */
488 if ((0 == message_ready_size (h)) && (NULL != h->th))
490 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
495 GNUNET_PEER_change_rc (ch->peer, -1);
502 * Notify client that the transmission has timed out
505 * @param tc task context
508 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
510 struct GNUNET_MESH_TransmitHandle *th = cls;
511 struct GNUNET_MESH_Handle *mesh;
513 mesh = th->channel->mesh;
514 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
515 th->channel->packet_size = 0;
516 if (GNUNET_YES == th_is_payload (th))
517 th->notify (th->notify_cls, 0, NULL);
519 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
521 /* nothing ready to transmit, no point in asking for transmission */
522 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
529 * Add a transmit handle to the transmission queue and set the
532 * @param h mesh handle with the queue head and tail
533 * @param th handle to the packet to be transmitted
536 add_to_queue (struct GNUNET_MESH_Handle *h,
537 struct GNUNET_MESH_TransmitHandle *th)
539 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
540 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
543 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
544 (th->timeout), &timeout_transmission, th);
549 * Auxiliary function to send an already constructed packet to the service.
550 * Takes care of creating a new queue element, copying the message and
551 * calling the tmt_rdy function if necessary.
553 * @param h mesh handle
554 * @param msg message to transmit
555 * @param channel channel this send is related to (NULL if N/A)
558 send_packet (struct GNUNET_MESH_Handle *h,
559 const struct GNUNET_MessageHeader *msg,
560 struct GNUNET_MESH_Channel *channel);
564 * Send an ack on the channel to confirm the processing of a message.
566 * @param ch Channel on which to send the ACK.
569 send_ack (struct GNUNET_MESH_Channel *ch)
571 struct GNUNET_MESH_LocalAck msg;
573 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
574 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
575 msg.header.size = htons (sizeof (msg));
576 msg.channel_id = htonl (ch->chid);
578 send_packet (ch->mesh, &msg.header, ch);
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_ports * sizeof (uint32_t);
609 char buf[size] GNUNET_ALIGN;
610 struct GNUNET_MESH_ClientConnect *msg;
614 /* build connection packet */
615 msg = (struct GNUNET_MESH_ClientConnect *) buf;
616 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
617 msg->header.size = htons (size);
618 ports = (uint32_t *) &msg[1];
619 for (i = 0; i < h->n_ports; i++)
621 ports[i] = htonl (h->ports[i]);
622 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
625 LOG (GNUNET_ERROR_TYPE_DEBUG,
626 "Sending %lu bytes long message with %u ports\n",
627 ntohs (msg->header.size), h->n_ports);
628 send_packet (h, &msg->header, NULL);
634 * Reconnect to the service, retransmit all infomation to try to restore the
637 * @param h handle to the mesh
639 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
642 do_reconnect (struct GNUNET_MESH_Handle *h)
644 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
645 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
646 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
647 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
648 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
653 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
656 if (NULL != h->client)
658 GNUNET_CLIENT_disconnect (h->client);
662 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
663 if (h->client == NULL)
665 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
668 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
669 GNUNET_TIME_relative_multiply
670 (h->reconnect_time, 2));
671 LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
672 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
679 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
686 * Reconnect callback: tries to reconnect again after a failer previous
688 * @param cls closure (mesh handle)
689 * @param tc task context
692 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
694 struct GNUNET_MESH_Handle *h = cls;
696 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
697 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
704 * Reconnect to the service, retransmit all infomation to try to restore the
707 * @param h handle to the mesh
709 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
712 reconnect (struct GNUNET_MESH_Handle *h)
714 struct GNUNET_MESH_Channel *ch;
715 struct GNUNET_MESH_Channel *next;
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Requested RECONNECT, destroying all channels\n");
719 h->in_receive = GNUNET_NO;
720 for (ch = h->channels_head; NULL != ch; ch = next)
723 destroy_channel (ch, GNUNET_YES);
725 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
726 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
731 /******************************************************************************/
732 /*********************** RECEIVE HANDLERS ****************************/
733 /******************************************************************************/
736 * Process the new channel notification and add it to the channels in the handle
738 * @param h The mesh handle
739 * @param msg A message with the details of the new incoming channel
742 process_channel_created (struct GNUNET_MESH_Handle *h,
743 const struct GNUNET_MESH_ChannelMessage *msg)
745 struct GNUNET_MESH_Channel *ch;
746 MESH_ChannelNumber chid;
749 chid = ntohl (msg->channel_id);
750 port = ntohl (msg->port);
751 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, port);
752 if (chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
757 if (NULL != h->new_channel)
761 ch = create_channel (h, chid);
762 ch->allow_send = GNUNET_NO;
763 ch->peer = GNUNET_PEER_intern (&msg->peer);
767 ch->options = ntohl (msg->opt);
769 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
770 ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options);
773 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
777 struct GNUNET_MESH_ChannelMessage d_msg;
779 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
781 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
782 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
783 d_msg.channel_id = msg->channel_id;
784 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
788 send_packet (h, &d_msg.header, NULL);
795 * Process the channel destroy notification and free associated resources
797 * @param h The mesh handle
798 * @param msg A message with the details of the channel being destroyed
801 process_channel_destroy (struct GNUNET_MESH_Handle *h,
802 const struct GNUNET_MESH_ChannelMessage *msg)
804 struct GNUNET_MESH_Channel *ch;
805 MESH_ChannelNumber chid;
807 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel Destroy received from service\n");
808 chid = ntohl (msg->channel_id);
809 ch = retrieve_channel (h, chid);
813 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
816 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroying channel %X\n", ch->chid);
817 destroy_channel (ch, GNUNET_YES);
822 * Process the incoming data packets, call appropriate handlers.
824 * @param h The mesh handle
825 * @param message A message encapsulating the data
828 process_incoming_data (struct GNUNET_MESH_Handle *h,
829 const struct GNUNET_MessageHeader *message)
831 const struct GNUNET_MessageHeader *payload;
832 const struct GNUNET_MESH_MessageHandler *handler;
833 struct GNUNET_MESH_LocalData *dmsg;
834 struct GNUNET_MESH_Channel *ch;
838 LOG (GNUNET_ERROR_TYPE_DEBUG,
839 "Got a data message!\n");
840 dmsg = (struct GNUNET_MESH_LocalData *) message;
841 ch = retrieve_channel (h, ntohl (dmsg->id));
842 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
843 LOG (GNUNET_ERROR_TYPE_DEBUG,
844 " %s data on channel %s [%X]\n",
845 ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "fwd" : "bck",
846 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
849 /* Channel was ignored/destroyed, probably service didn't get it yet */
850 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
853 type = ntohs (payload->type);
854 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %u\n", type);
855 for (i = 0; i < h->n_handlers; i++)
857 handler = &h->message_handlers[i];
858 LOG (GNUNET_ERROR_TYPE_DEBUG,
859 " checking handler for type %u\n",
861 if (handler->type == type)
864 handler->callback (h->cls, ch, &ch->ctx, payload))
866 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
867 GNUNET_MESH_channel_destroy (ch);
872 LOG (GNUNET_ERROR_TYPE_DEBUG,
873 "callback completed successfully\n");
882 * Process a local ACK message, enabling the client to send
883 * more data to the service.
885 * @param h Mesh handle.
886 * @param message Message itself.
889 process_ack (struct GNUNET_MESH_Handle *h,
890 const struct GNUNET_MessageHeader *message)
892 struct GNUNET_MESH_LocalAck *msg;
893 struct GNUNET_MESH_Channel *ch;
894 MESH_ChannelNumber chid;
896 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
897 msg = (struct GNUNET_MESH_LocalAck *) message;
898 chid = ntohl (msg->channel_id);
899 ch = retrieve_channel (h, chid);
902 LOG (GNUNET_ERROR_TYPE_DEBUG, "ACK on unknown channel %X\n", chid);
905 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X!\n", ch->chid);
906 ch->allow_send = GNUNET_YES;
907 if (NULL == h->th && 0 < ch->packet_size)
909 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
910 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
911 GNUNET_TIME_UNIT_FOREVER_REL,
912 GNUNET_YES, &send_callback, h);
918 * Process a local reply about info on all channels, pass info to the user.
920 * @param h Mesh handle.
921 * @param message Message itself.
924 // process_get_channels (struct GNUNET_MESH_Handle *h,
925 // const struct GNUNET_MessageHeader *message)
927 // struct GNUNET_MESH_LocalMonitor *msg;
929 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
931 // if (NULL == h->channels_cb)
933 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
937 // msg = (struct GNUNET_MESH_LocalMonitor *) message;
938 // if (ntohs (message->size) !=
939 // (sizeof (struct GNUNET_MESH_LocalMonitor) +
940 // sizeof (struct GNUNET_PeerIdentity)))
942 // GNUNET_break_op (0);
943 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
944 // "Get channels message: size %hu - expected %u\n",
945 // ntohs (message->size),
946 // sizeof (struct GNUNET_MESH_LocalMonitor));
949 // h->channels_cb (h->channels_cls,
950 // ntohl (msg->channel_id),
952 // &msg->destination);
958 * Process a local monitor_channel reply, pass info to the user.
960 * @param h Mesh handle.
961 * @param message Message itself.
964 // process_show_channel (struct GNUNET_MESH_Handle *h,
965 // const struct GNUNET_MessageHeader *message)
967 // struct GNUNET_MESH_LocalMonitor *msg;
970 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
972 // if (NULL == h->channel_cb)
974 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
978 // /* Verify message sanity */
979 // msg = (struct GNUNET_MESH_LocalMonitor *) message;
980 // esize = sizeof (struct GNUNET_MESH_LocalMonitor);
981 // if (ntohs (message->size) != esize)
983 // GNUNET_break_op (0);
984 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
985 // "Show channel message: size %hu - expected %u\n",
986 // ntohs (message->size),
989 // h->channel_cb (h->channel_cls, NULL, NULL);
990 // h->channel_cb = NULL;
991 // h->channel_cls = NULL;
996 // h->channel_cb (h->channel_cls,
997 // &msg->destination,
1003 * Function to process all messages received from the service
1005 * @param cls closure
1006 * @param msg message received, NULL on timeout or fatal error
1009 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1011 struct GNUNET_MESH_Handle *h = cls;
1016 LOG (GNUNET_ERROR_TYPE_DEBUG,
1017 "Mesh service disconnected, reconnecting\n", h);
1021 type = ntohs (msg->type);
1022 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1023 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1027 /* Notify of a new incoming channel */
1028 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1029 process_channel_created (h, (struct GNUNET_MESH_ChannelMessage *) msg);
1031 /* Notify of a channel disconnection */
1032 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
1033 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_NACK:
1034 process_channel_destroy (h, (struct GNUNET_MESH_ChannelMessage *) msg);
1036 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
1037 process_incoming_data (h, msg);
1039 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1040 process_ack (h, msg);
1042 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS: DEPRECATED
1043 // process_get_channels (h, msg);
1045 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL: DEPRECATED
1046 // process_show_channel (h, msg);
1049 /* We shouldn't get any other packages, log and ignore */
1050 LOG (GNUNET_ERROR_TYPE_WARNING,
1051 "unsolicited message form service (type %s)\n",
1052 GM_m2s (ntohs (msg->type)));
1054 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1055 if (GNUNET_YES == h->in_receive)
1057 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1058 GNUNET_TIME_UNIT_FOREVER_REL);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG,
1063 "in receive off, not calling CLIENT_receive\n");
1068 /******************************************************************************/
1069 /************************ SEND FUNCTIONS ****************************/
1070 /******************************************************************************/
1073 * Function called to send a message to the service.
1074 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1077 * @param cls closure, the mesh handle
1078 * @param size number of bytes available in buf
1079 * @param buf where the callee should write the connect message
1080 * @return number of bytes written to buf
1083 send_callback (void *cls, size_t size, void *buf)
1085 struct GNUNET_MESH_Handle *h = cls;
1086 struct GNUNET_MESH_TransmitHandle *th;
1087 struct GNUNET_MESH_TransmitHandle *next;
1088 struct GNUNET_MESH_Channel *ch;
1094 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1095 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
1096 if ((0 == size) || (NULL == buf))
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1105 nsize = message_ready_size (h);
1106 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1109 if (GNUNET_YES == th_is_payload (th))
1111 struct GNUNET_MESH_LocalData *dmsg;
1112 struct GNUNET_MessageHeader *mh;
1114 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1115 if (GNUNET_NO == ch->allow_send)
1117 /* This channel is not ready to transmit yet, try next message */
1121 ch->packet_size = 0;
1122 GNUNET_assert (size >= th->size);
1123 dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
1124 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1125 psize = th->notify (th->notify_cls,
1126 size - sizeof (struct GNUNET_MESH_LocalData),
1130 psize += sizeof (struct GNUNET_MESH_LocalData);
1131 GNUNET_assert (size >= psize);
1132 dmsg->header.size = htons (psize);
1133 dmsg->id = htonl (ch->chid);
1134 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1135 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
1136 GM_m2s (ntohs (mh->type)));
1137 ch->allow_send = GNUNET_NO;
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 "# callback returned size 0, "
1143 "application canceled transmission\n");
1148 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1150 LOG (GNUNET_ERROR_TYPE_DEBUG, "# mesh internal traffic, type %s\n",
1151 GM_m2s (ntohs (mh->type)));
1152 memcpy (cbuf, &th[1], th->size);
1155 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1156 GNUNET_SCHEDULER_cancel (th->timeout_task);
1157 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1160 nsize = message_ready_size (h);
1165 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1167 size = message_ready_size (h);
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1172 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1173 GNUNET_TIME_UNIT_FOREVER_REL,
1174 GNUNET_YES, &send_callback, h);
1178 if (NULL != h->th_head)
1179 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1183 if (GNUNET_NO == h->in_receive)
1185 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1186 h->in_receive = GNUNET_YES;
1187 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1188 GNUNET_TIME_UNIT_FOREVER_REL);
1190 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1196 * Auxiliary function to send an already constructed packet to the service.
1197 * Takes care of creating a new queue element, copying the message and
1198 * calling the tmt_rdy function if necessary.
1200 * @param h mesh handle
1201 * @param msg message to transmit
1202 * @param channel channel this send is related to (NULL if N/A)
1205 send_packet (struct GNUNET_MESH_Handle *h,
1206 const struct GNUNET_MessageHeader *msg,
1207 struct GNUNET_MESH_Channel *channel)
1209 struct GNUNET_MESH_TransmitHandle *th;
1212 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1213 GM_m2s(ntohs(msg->type)));
1214 msize = ntohs (msg->size);
1215 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1216 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1218 th->channel = channel;
1219 memcpy (&th[1], msg, msize);
1220 add_to_queue (h, th);
1221 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1226 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1227 GNUNET_TIME_UNIT_FOREVER_REL,
1228 GNUNET_YES, &send_callback, h);
1232 /******************************************************************************/
1233 /********************** API CALL DEFINITIONS *************************/
1234 /******************************************************************************/
1236 struct GNUNET_MESH_Handle *
1237 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1238 GNUNET_MESH_InboundChannelNotificationHandler new_channel,
1239 GNUNET_MESH_ChannelEndHandler cleaner,
1240 const struct GNUNET_MESH_MessageHandler *handlers,
1241 const uint32_t *ports)
1243 struct GNUNET_MESH_Handle *h;
1245 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1246 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1247 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1249 h->new_channel = new_channel;
1250 h->cleaner = cleaner;
1251 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1252 if (h->client == NULL)
1259 h->message_handlers = handlers;
1261 h->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1262 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1263 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1265 if (NULL != ports && ports[0] != 0 && NULL == new_channel)
1268 LOG (GNUNET_ERROR_TYPE_DEBUG,
1269 "no new channel handler given, ports parameter is useless!!\n");
1271 if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
1274 LOG (GNUNET_ERROR_TYPE_DEBUG,
1275 "no ports given, new channel handler will never be called!!\n");
1277 /* count handlers */
1278 for (h->n_handlers = 0;
1279 handlers && handlers[h->n_handlers].type;
1281 for (h->n_ports = 0;
1282 ports && ports[h->n_ports];
1285 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1291 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1293 struct GNUNET_MESH_Channel *ch;
1294 struct GNUNET_MESH_Channel *aux;
1295 struct GNUNET_MESH_TransmitHandle *th;
1297 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1299 ch = handle->channels_head;
1303 if (ch->chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid);
1308 destroy_channel (ch, GNUNET_YES);
1311 while ( (th = handle->th_head) != NULL)
1313 struct GNUNET_MessageHeader *msg;
1315 /* Make sure it is an allowed packet (everything else should have been
1316 * already canceled).
1318 GNUNET_break (GNUNET_NO == th_is_payload (th));
1319 msg = (struct GNUNET_MessageHeader *) &th[1];
1320 switch (ntohs(msg->type))
1322 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1323 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1324 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1325 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
1326 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
1330 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1334 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1338 if (NULL != handle->th)
1340 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1343 if (NULL != handle->client)
1345 GNUNET_CLIENT_disconnect (handle->client);
1346 handle->client = NULL;
1348 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1350 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1351 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1353 GNUNET_free (handle);
1358 * Create a new channel towards a remote peer.
1360 * If the destination port is not open by any peer or the destination peer
1361 * does not accept the channel, #GNUNET_MESH_ChannelEndHandler will be called
1364 * @param h mesh handle
1365 * @param channel_ctx client's channel context to associate with the channel
1366 * @param peer peer identity the channel should go to
1367 * @param port Port number.
1368 * @param options MeshOption flag field, with all desired option bits set to 1.
1370 * @return handle to the channel
1372 struct GNUNET_MESH_Channel *
1373 GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
1375 const struct GNUNET_PeerIdentity *peer,
1377 enum GNUNET_MESH_ChannelOption options)
1379 struct GNUNET_MESH_Channel *ch;
1380 struct GNUNET_MESH_ChannelMessage msg;
1382 LOG (GNUNET_ERROR_TYPE_DEBUG,
1383 "Creating new channel to %s:%u\n",
1384 GNUNET_i2s (peer), port);
1385 ch = create_channel (h, 0);
1386 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1387 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid);
1388 ch->ctx = channel_ctx;
1389 ch->peer = GNUNET_PEER_intern (peer);
1390 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1391 msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
1392 msg.channel_id = htonl (ch->chid);
1393 msg.port = htonl (port);
1395 msg.opt = htonl (options);
1397 send_packet (h, &msg.header, ch);
1403 GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel)
1405 struct GNUNET_MESH_Handle *h;
1406 struct GNUNET_MESH_ChannelMessage msg;
1407 struct GNUNET_MESH_TransmitHandle *th;
1409 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1412 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1413 msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
1414 msg.channel_id = htonl (channel->chid);
1415 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1421 struct GNUNET_MESH_TransmitHandle *aux;
1422 if (th->channel == channel)
1425 /* FIXME call the handler? */
1426 if (GNUNET_YES == th_is_payload (th))
1427 th->notify (th->notify_cls, 0, NULL);
1428 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1436 destroy_channel (channel, GNUNET_YES);
1437 send_packet (h, &msg.header, NULL);
1442 * Get information about a channel.
1444 * @param channel Channel handle.
1445 * @param option Query (GNUNET_MESH_OPTION_*).
1446 * @param ... dependant on option, currently not used
1448 * @return Union with an answer to the query.
1450 const union GNUNET_MESH_ChannelInfo *
1451 GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
1452 enum GNUNET_MESH_ChannelOption option, ...)
1454 static int bool_flag;
1455 const union GNUNET_MESH_ChannelInfo *ret;
1459 case GNUNET_MESH_OPTION_NOBUFFER:
1460 case GNUNET_MESH_OPTION_RELIABLE:
1461 case GNUNET_MESH_OPTION_OOORDER:
1462 if (0 != (option & channel->options))
1463 bool_flag = GNUNET_YES;
1465 bool_flag = GNUNET_NO;
1466 ret = (const union GNUNET_MESH_ChannelInfo *) &bool_flag;
1468 case GNUNET_MESH_OPTION_PEER:
1469 ret = (const union GNUNET_MESH_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1479 struct GNUNET_MESH_TransmitHandle *
1480 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel, int cork,
1481 struct GNUNET_TIME_Relative maxdelay,
1483 GNUNET_CONNECTION_TransmitReadyNotify notify,
1486 struct GNUNET_MESH_TransmitHandle *th;
1488 GNUNET_assert (NULL != channel);
1489 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1490 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1491 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1492 if (channel->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
1493 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1495 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1497 GNUNET_assert (NULL != notify);
1498 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1499 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1500 th->channel = channel;
1501 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1502 th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
1503 channel->packet_size = th->size;
1504 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1505 th->notify = notify;
1506 th->notify_cls = notify_cls;
1507 add_to_queue (channel->mesh, th);
1508 if (NULL != channel->mesh->th)
1510 if (GNUNET_NO == channel->allow_send)
1512 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1514 GNUNET_CLIENT_notify_transmit_ready (channel->mesh->client, th->size,
1515 GNUNET_TIME_UNIT_FOREVER_REL,
1516 GNUNET_YES, &send_callback,
1518 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1524 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1526 struct GNUNET_MESH_Handle *mesh;
1528 th->channel->packet_size = 0;
1529 mesh = th->channel->mesh;
1530 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1531 GNUNET_SCHEDULER_cancel (th->timeout_task);
1532 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1534 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1536 /* queue empty, no point in asking for transmission */
1537 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1544 GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel)
1551 * Request information about the running mesh peer.
1552 * The callback will be called for every channel known to the service,
1553 * listing all active peers that blong to the channel.
1555 * If called again on the same handle, it will overwrite the previous
1556 * callback and cls. To retrieve the cls, monitor_cancel must be
1559 * WARNING: unstable API, likely to change in the future!
1561 * @param h Handle to the mesh peer.
1562 * @param callback Function to call with the requested data.
1563 * @param callback_cls Closure for @c callback.
1566 GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
1567 GNUNET_MESH_ChannelsCB callback,
1570 struct GNUNET_MessageHeader msg;
1572 msg.size = htons (sizeof (msg));
1573 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS);
1574 send_packet (h, &msg, NULL);
1575 h->channels_cb = callback;
1576 h->channels_cls = callback_cls;
1581 * Cancel a monitor request. The monitor callback will not be called.
1583 * @param h Mesh handle.
1585 * @return Closure given to GNUNET_MESH_monitor, if any.
1588 GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h)
1592 cls = h->channels_cls;
1593 h->channels_cb = NULL;
1594 h->channels_cls = NULL;
1600 * Request information about a specific channel of the running mesh peer.
1602 * WARNING: unstable API, likely to change in the future!
1603 * FIXME Add destination option.
1605 * @param h Handle to the mesh peer.
1606 * @param initiator ID of the owner of the channel.
1607 * @param channel_number Channel number.
1608 * @param callback Function to call with the requested data.
1609 * @param callback_cls Closure for @c callback.
1612 GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
1613 struct GNUNET_PeerIdentity *initiator,
1614 unsigned int channel_number,
1615 GNUNET_MESH_ChannelCB callback,
1618 struct GNUNET_MESH_LocalMonitor msg;
1620 msg.header.size = htons (sizeof (msg));
1621 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL);
1622 msg.owner = *initiator;
1623 msg.channel_id = htonl (channel_number);
1625 send_packet (h, &msg.header, NULL);
1626 h->channel_cb = callback;
1627 h->channel_cls = callback_cls;
1632 * Function called to notify a client about the connection
1633 * begin ready to queue more data. "buf" will be
1634 * NULL and "size" zero if the connection was closed for
1635 * writing in the meantime.
1637 * @param cls closure
1638 * @param size number of bytes available in buf
1639 * @param buf where the callee should write the message
1640 * @return number of bytes written to buf
1643 mesh_mq_ntr (void *cls, size_t size,
1646 struct GNUNET_MQ_Handle *mq = cls;
1647 struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
1648 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
1654 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
1657 msize = ntohs (msg->size);
1658 GNUNET_assert (msize <= size);
1659 memcpy (buf, msg, msize);
1660 GNUNET_MQ_impl_send_continue (mq);
1666 * Signature of functions implementing the
1667 * sending functionality of a message queue.
1669 * @param mq the message queue
1670 * @param msg the message to send
1671 * @param impl_state state of the implementation
1674 mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
1675 const struct GNUNET_MessageHeader *msg, void *impl_state)
1677 struct MeshMQState *state = impl_state;
1679 GNUNET_assert (NULL == state->th);
1681 GNUNET_MESH_notify_transmit_ready (state->channel,
1682 /* FIXME: add option for corking */
1684 GNUNET_TIME_UNIT_FOREVER_REL,
1692 * Signature of functions implementing the
1693 * destruction of a message queue.
1694 * Implementations must not free 'mq', but should
1695 * take care of 'impl_state'.
1697 * @param mq the message queue to destroy
1698 * @param impl_state state of the implementation
1701 mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
1703 struct MeshMQState *state = impl_state;
1705 if (NULL != state->th)
1706 GNUNET_MESH_notify_transmit_ready_cancel (state->th);
1708 GNUNET_free (state);
1713 * Create a message queue for a mesh channel.
1714 * The message queue can only be used to transmit messages,
1715 * not to receive them.
1717 * @param channel the channel to create the message qeue for
1718 * @return a message queue to messages over the channel
1720 struct GNUNET_MQ_Handle *
1721 GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel)
1723 struct GNUNET_MQ_Handle *mq;
1724 struct MeshMQState *state;
1726 state = GNUNET_new (struct MeshMQState);
1727 state->channel = channel;
1729 mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
1730 mesh_mq_destroy_impl,
1731 NULL, /* FIXME: cancel impl. */
1733 NULL, /* no msg handlers */
1734 NULL, /* no err handlers */
1735 NULL); /* no handler cls */