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_enc.c
20 * @brief mesh api: client implementation of new mesh service
21 * @author Bartlomiej Polot
25 #include "gnunet_common.h"
26 #include "gnunet_client_lib.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_peer_lib.h"
29 #include "gnunet_mesh_service_enc.h"
31 #include "mesh_protocol_enc.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
35 /******************************************************************************/
36 /************************ DATA STRUCTURES ****************************/
37 /******************************************************************************/
40 * Transmission queue to the service
42 struct GNUNET_MESH_TransmitHandle
48 struct GNUNET_MESH_TransmitHandle *next;
53 struct GNUNET_MESH_TransmitHandle *prev;
56 * Channel this message is sent on / for (may be NULL for control messages).
58 struct GNUNET_MESH_Channel *channel;
61 * Callback to obtain the message to transmit, or NULL if we
62 * got the message in 'data'. Notice that messages built
63 * by 'notify' need to be encapsulated with information about
66 GNUNET_CONNECTION_TransmitReadyNotify notify;
69 * Closure for 'notify'
74 * How long is this message valid. Once the timeout has been
75 * reached, the message must no longer be sent. If this
76 * is a message with a 'notify' callback set, the 'notify'
77 * function should be called with 'buf' NULL and size 0.
79 struct GNUNET_TIME_Absolute timeout;
82 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
84 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
87 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
94 * Opaque handle to the service.
96 struct GNUNET_MESH_Handle
100 * Handle to the server connection, to send messages later
102 struct GNUNET_CLIENT_Connection *client;
105 * Set of handlers used for processing incoming messages in the channels
107 const struct GNUNET_MESH_MessageHandler *message_handlers;
110 * Number of handlers in the handlers array.
112 unsigned int n_handlers;
117 const uint32_t *ports;
122 unsigned int n_ports;
125 * Double linked list of the channels this client is connected to, head.
127 struct GNUNET_MESH_Channel *channels_head;
130 * Double linked list of the channels this client is connected to, tail.
132 struct GNUNET_MESH_Channel *channels_tail;
135 * Callback for inbound channel creation
137 GNUNET_MESH_InboundChannelNotificationHandler *new_channel;
140 * Callback for inbound channel disconnection
142 GNUNET_MESH_ChannelEndHandler *cleaner;
145 * Handle to cancel pending transmissions in case of disconnection
147 struct GNUNET_CLIENT_TransmitHandle *th;
150 * Closure for all the handlers given by the client
155 * Messages to send to the service, head.
157 struct GNUNET_MESH_TransmitHandle *th_head;
160 * Messages to send to the service, tail.
162 struct GNUNET_MESH_TransmitHandle *th_tail;
165 * chid of the next channel to create (to avoid reusing IDs often)
167 MESH_ChannelNumber next_chid;
170 * Have we started the task to receive messages from the service
171 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
176 * Configuration given by the client, in case of reconnection
178 const struct GNUNET_CONFIGURATION_Handle *cfg;
181 * Time to the next reconnect in case one reconnect fails
183 struct GNUNET_TIME_Relative reconnect_time;
186 * Task for trying to reconnect.
188 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
193 GNUNET_MESH_ChannelsCB channels_cb;
196 * Monitor callback closure.
203 GNUNET_MESH_ChannelCB channel_cb;
206 * Channel callback closure.
213 * Description of a peer
215 struct GNUNET_MESH_Peer
218 * ID of the peer in short form
223 * Channel this peer belongs to
225 struct GNUNET_MESH_Channel *t;
228 * Flag indicating whether service has informed about its connection
229 * FIXME-BART: is this flag used? Seems dead right now...
237 * Opaque handle to a channel.
239 struct GNUNET_MESH_Channel
245 struct GNUNET_MESH_Channel *next;
250 struct GNUNET_MESH_Channel *prev;
253 * Handle to the mesh this channel belongs to
255 struct GNUNET_MESH_Handle *mesh;
258 * Local ID of the channel
260 MESH_ChannelNumber chid;
268 * Other end of the channel.
273 * Any data the caller wants to put in here
278 * Size of packet queued in this channel
280 unsigned int packet_size;
283 * Is the channel allowed to buffer?
288 * Is the channel realiable?
293 * If reliable, is the channel out of order?
298 * Are we allowed to send to the service?
306 * Implementation state for mesh's message queue.
311 * The current transmit handle, or NULL
312 * if no transmit is active.
314 struct GNUNET_MESH_TransmitHandle *th;
317 * Channel to send the data over.
319 struct GNUNET_MESH_Channel *channel;
323 /******************************************************************************/
324 /*********************** DECLARATIONS *************************/
325 /******************************************************************************/
328 * Function called to send a message to the service.
329 * "buf" will be NULL and "size" zero if the socket was closed for writing in
332 * @param cls closure, the mesh handle
333 * @param size number of bytes available in buf
334 * @param buf where the callee should write the connect message
335 * @return number of bytes written to buf
338 send_callback (void *cls, size_t size, void *buf);
341 /******************************************************************************/
342 /*********************** AUXILIARY FUNCTIONS *************************/
343 /******************************************************************************/
346 * Check if transmission is a payload packet.
348 * @param th Transmission handle.
350 * @return GNUNET_YES if it is a payload packet,
351 * GNUNET_NO if it is a mesh management packet.
354 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
356 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
361 * Check whether there is any message ready in the queue and find the size.
363 * @param h Mesh handle.
365 * @return The size of the first ready message in the queue,
366 * 0 if there is none.
369 message_ready_size (struct GNUNET_MESH_Handle *h)
371 struct GNUNET_MESH_TransmitHandle *th;
372 struct GNUNET_MESH_Channel *ch;
374 for (th = h->th_head; NULL != th; th = th->next)
377 if (GNUNET_NO == th_is_payload (th))
379 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
382 if (GNUNET_YES == ch->allow_send)
384 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok\n");
393 * Get the channel handler for the channel specified by id from the given handle
394 * @param h Mesh handle
395 * @param chid ID of the wanted channel
396 * @return handle to the required channel or NULL if not found
398 static struct GNUNET_MESH_Channel *
399 retrieve_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
401 struct GNUNET_MESH_Channel *ch;
403 ch = h->channels_head;
406 if (ch->chid == chid)
415 * Create a new channel and insert it in the channel list of the mesh handle
417 * @param h Mesh handle
418 * @param chid Desired chid of the channel, 0 to assign one automatically.
420 * @return Handle to the created channel.
422 static struct GNUNET_MESH_Channel *
423 create_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
425 struct GNUNET_MESH_Channel *ch;
427 ch = GNUNET_malloc (sizeof (struct GNUNET_MESH_Channel));
428 GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
432 ch->chid = h->next_chid;
433 while (NULL != retrieve_channel (h, h->next_chid))
436 h->next_chid &= ~GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
437 h->next_chid |= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
444 ch->allow_send = GNUNET_NO;
445 ch->nobuffer = GNUNET_NO;
451 * Destroy the specified channel.
452 * - Destroys all peers, calling the disconnect callback on each if needed
453 * - Cancels all outgoing traffic for that channel, calling respective notifys
454 * - Calls cleaner if channel was inbound
455 * - Frees all memory used
457 * @param t Pointer to the channel.
458 * @param call_cleaner Whether to call the cleaner handler.
460 * @return Handle to the required channel or NULL if not found.
463 destroy_channel (struct GNUNET_MESH_Channel *ch, int call_cleaner)
465 struct GNUNET_MESH_Handle *h;
466 struct GNUNET_MESH_TransmitHandle *th;
467 struct GNUNET_MESH_TransmitHandle *next;
469 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_channel %X\n", ch->chid);
478 GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch);
480 /* signal channel destruction */
481 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
482 h->cleaner (h->cls, ch, ch->ctx);
484 /* check that clients did not leave messages behind in the queue */
485 for (th = h->th_head; NULL != th; th = next)
488 if (th->channel != ch)
490 /* Clients should have aborted their requests already.
491 * Management traffic should be ok, as clients can't cancel that */
492 GNUNET_break (GNUNET_NO == th_is_payload(th));
493 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
495 /* clean up request */
496 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
497 GNUNET_SCHEDULER_cancel (th->timeout_task);
501 /* if there are no more pending requests with mesh service, cancel active request */
502 /* Note: this should be unnecessary... */
503 if ((0 == message_ready_size (h)) && (NULL != h->th))
505 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
510 GNUNET_PEER_change_rc (ch->peer, -1);
517 * Notify client that the transmission has timed out
520 * @param tc task context
523 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
525 struct GNUNET_MESH_TransmitHandle *th = cls;
526 struct GNUNET_MESH_Handle *mesh;
528 mesh = th->channel->mesh;
529 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
530 th->channel->packet_size = 0;
531 if (GNUNET_YES == th_is_payload (th))
532 th->notify (th->notify_cls, 0, NULL);
534 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
536 /* nothing ready to transmit, no point in asking for transmission */
537 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
544 * Add a transmit handle to the transmission queue and set the
547 * @param h mesh handle with the queue head and tail
548 * @param th handle to the packet to be transmitted
551 add_to_queue (struct GNUNET_MESH_Handle *h,
552 struct GNUNET_MESH_TransmitHandle *th)
554 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
555 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
558 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
559 (th->timeout), &timeout_transmission, th);
564 * Auxiliary function to send an already constructed packet to the service.
565 * Takes care of creating a new queue element, copying the message and
566 * calling the tmt_rdy function if necessary.
568 * @param h mesh handle
569 * @param msg message to transmit
570 * @param channel channel this send is related to (NULL if N/A)
573 send_packet (struct GNUNET_MESH_Handle *h,
574 const struct GNUNET_MessageHeader *msg,
575 struct GNUNET_MESH_Channel *channel);
579 * Send an ack on the channel to confirm the processing of a message.
581 * @param ch Channel on which to send the ACK.
584 send_ack (struct GNUNET_MESH_Channel *ch)
586 struct GNUNET_MESH_LocalAck msg;
588 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
589 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
590 msg.header.size = htons (sizeof (msg));
591 msg.channel_id = htonl (ch->chid);
593 send_packet (ch->mesh, &msg.header, ch);
600 * Reconnect callback: tries to reconnect again after a failer previous
602 * @param cls closure (mesh handle)
603 * @param tc task context
606 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
610 * Send a connect packet to the service with the applications and types
611 * requested by the user.
613 * @param h The mesh handle.
617 send_connect (struct GNUNET_MESH_Handle *h)
621 size = sizeof (struct GNUNET_MESH_ClientConnect);
622 size += h->n_ports * sizeof (uint32_t);
624 char buf[size] GNUNET_ALIGN;
625 struct GNUNET_MESH_ClientConnect *msg;
629 /* build connection packet */
630 msg = (struct GNUNET_MESH_ClientConnect *) buf;
631 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
632 msg->header.size = htons (size);
633 ports = (uint32_t *) &msg[1];
634 for (i = 0; i < h->n_ports; i++)
636 ports[i] = htonl (h->ports[i]);
637 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
640 LOG (GNUNET_ERROR_TYPE_DEBUG,
641 "Sending %lu bytes long message with %u ports\n",
642 ntohs (msg->header.size), h->n_ports);
643 send_packet (h, &msg->header, NULL);
649 * Reconnect to the service, retransmit all infomation to try to restore the
652 * @param h handle to the mesh
654 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
657 do_reconnect (struct GNUNET_MESH_Handle *h)
659 struct GNUNET_MESH_Channel *ch;
661 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
662 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
663 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
664 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
665 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
670 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
673 if (NULL != h->client)
675 GNUNET_CLIENT_disconnect (h->client);
679 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
680 if (h->client == NULL)
682 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
685 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
686 GNUNET_TIME_relative_multiply
687 (h->reconnect_time, 2));
688 LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
689 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
696 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
699 /* Rebuild all channels */
700 for (ch = h->channels_head; NULL != ch; ch = ch->next)
702 struct GNUNET_MESH_ChannelMessage tmsg;
705 if (ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
707 /* Channel was created by service (incoming channel) */
708 /* TODO: Notify service of missing channel, to request
709 * creator to recreate path (find a path to him via DHT?)
713 ch->allow_send = GNUNET_NO;
714 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
715 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
716 tmsg.channel_id = htonl (ch->chid);
717 tmsg.port = htonl (ch->port);
718 GNUNET_PEER_resolve (ch->peer, &tmsg.peer);
721 if (GNUNET_YES == ch->nobuffer)
722 options |= GNUNET_MESH_OPTION_NOBUFFER;
724 if (GNUNET_YES == ch->reliable)
725 options |= GNUNET_MESH_OPTION_RELIABLE;
727 tmsg.opt = htonl (options);
728 send_packet (h, &tmsg.header, ch);
734 * Reconnect callback: tries to reconnect again after a failer previous
736 * @param cls closure (mesh handle)
737 * @param tc task context
740 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
742 struct GNUNET_MESH_Handle *h = cls;
744 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
745 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
752 * Reconnect to the service, retransmit all infomation to try to restore the
755 * @param h handle to the mesh
757 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
760 reconnect (struct GNUNET_MESH_Handle *h)
762 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
763 h->in_receive = GNUNET_NO;
764 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
765 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
770 /******************************************************************************/
771 /*********************** RECEIVE HANDLERS ****************************/
772 /******************************************************************************/
775 * Process the new channel notification and add it to the channels in the handle
777 * @param h The mesh handle
778 * @param msg A message with the details of the new incoming channel
781 process_channel_created (struct GNUNET_MESH_Handle *h,
782 const struct GNUNET_MESH_ChannelMessage *msg)
784 struct GNUNET_MESH_Channel *ch;
785 MESH_ChannelNumber chid;
788 chid = ntohl (msg->channel_id);
789 port = ntohl (msg->port);
790 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, port);
791 if (chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
796 if (NULL != h->new_channel)
798 ch = create_channel (h, chid);
799 ch->allow_send = GNUNET_NO;
800 ch->peer = GNUNET_PEER_intern (&msg->peer);
804 if (0 != (msg->opt & GNUNET_MESH_OPTION_NOBUFFER))
805 ch->nobuffer = GNUNET_YES;
807 ch->nobuffer = GNUNET_NO;
809 if (0 != (msg->opt & GNUNET_MESH_OPTION_RELIABLE))
810 ch->reliable = GNUNET_YES;
812 ch->reliable = GNUNET_NO;
814 if (GNUNET_YES == ch->reliable &&
815 0 != (msg->opt & GNUNET_MESH_OPTION_OOORDER))
816 ch->ooorder = GNUNET_YES;
818 ch->ooorder = GNUNET_NO;
820 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
821 ch->ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port);
822 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
826 struct GNUNET_MESH_ChannelMessage d_msg;
828 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
830 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
831 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
832 d_msg.channel_id = msg->channel_id;
833 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
837 send_packet (h, &d_msg.header, NULL);
844 * Process the channel destroy notification and free associated resources
846 * @param h The mesh handle
847 * @param msg A message with the details of the channel being destroyed
850 process_channel_destroy (struct GNUNET_MESH_Handle *h,
851 const struct GNUNET_MESH_ChannelMessage *msg)
853 struct GNUNET_MESH_Channel *ch;
854 MESH_ChannelNumber chid;
856 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel from service\n");
857 chid = ntohl (msg->channel_id);
858 ch = retrieve_channel (h, chid);
862 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
865 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X destroyed\n", ch->chid);
866 destroy_channel (ch, GNUNET_YES);
871 * Process the incoming data packets, call appropriate handlers.
873 * @param h The mesh handle
874 * @param message A message encapsulating the data
877 process_incoming_data (struct GNUNET_MESH_Handle *h,
878 const struct GNUNET_MessageHeader *message)
880 const struct GNUNET_MessageHeader *payload;
881 const struct GNUNET_MESH_MessageHandler *handler;
882 struct GNUNET_MESH_LocalData *dmsg;
883 struct GNUNET_MESH_Channel *ch;
887 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
889 dmsg = (struct GNUNET_MESH_LocalData *) message;
891 ch = retrieve_channel (h, ntohl (dmsg->id));
892 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
893 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
894 ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "fwd" : "bck",
895 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
898 /* Channel was ignored/destroyed, probably service didn't get it yet */
899 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
902 type = ntohs (payload->type);
903 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %u\n", type);
904 for (i = 0; i < h->n_handlers; i++)
906 handler = &h->message_handlers[i];
907 LOG (GNUNET_ERROR_TYPE_DEBUG,
908 " checking handler for type %u\n",
910 if (handler->type == type)
913 handler->callback (h->cls, ch, &ch->ctx, payload))
915 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
916 GNUNET_MESH_channel_destroy (ch);
921 LOG (GNUNET_ERROR_TYPE_DEBUG,
922 "callback completed successfully\n");
930 * Process a local ACK message, enabling the client to send
931 * more data to the service.
933 * @param h Mesh handle.
934 * @param message Message itself.
937 process_ack (struct GNUNET_MESH_Handle *h,
938 const struct GNUNET_MessageHeader *message)
940 struct GNUNET_MESH_LocalAck *msg;
941 struct GNUNET_MESH_Channel *ch;
942 MESH_ChannelNumber chid;
944 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
945 msg = (struct GNUNET_MESH_LocalAck *) message;
946 chid = ntohl (msg->channel_id);
947 ch = retrieve_channel (h, chid);
950 LOG (GNUNET_ERROR_TYPE_WARNING, "ACK on unknown channel %X\n", chid);
953 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X!\n", ch->chid);
954 ch->allow_send = GNUNET_YES;
955 if (NULL == h->th && 0 < ch->packet_size)
957 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
959 GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
960 GNUNET_TIME_UNIT_FOREVER_REL,
961 GNUNET_YES, &send_callback, h);
967 * Process a local reply about info on all channels, pass info to the user.
969 * @param h Mesh handle.
970 * @param message Message itself.
973 process_get_channels (struct GNUNET_MESH_Handle *h,
974 const struct GNUNET_MessageHeader *message)
976 struct GNUNET_MESH_LocalMonitor *msg;
978 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
980 if (NULL == h->channels_cb)
982 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
986 msg = (struct GNUNET_MESH_LocalMonitor *) message;
987 if (ntohs (message->size) !=
988 (sizeof (struct GNUNET_MESH_LocalMonitor) +
989 sizeof (struct GNUNET_PeerIdentity)))
992 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
993 "Get channels message: size %hu - expected %u\n",
994 ntohs (message->size),
995 sizeof (struct GNUNET_MESH_LocalMonitor));
998 h->channels_cb (h->channels_cls,
999 ntohl (msg->channel_id),
1007 * Process a local monitor_channel reply, pass info to the user.
1009 * @param h Mesh handle.
1010 * @param message Message itself.
1013 process_show_channel (struct GNUNET_MESH_Handle *h,
1014 const struct GNUNET_MessageHeader *message)
1016 struct GNUNET_MESH_LocalMonitor *msg;
1019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
1021 if (NULL == h->channel_cb)
1023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1027 /* Verify message sanity */
1028 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1029 esize = sizeof (struct GNUNET_MESH_LocalMonitor);
1030 if (ntohs (message->size) != esize)
1032 GNUNET_break_op (0);
1033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1034 "Show channel message: size %hu - expected %u\n",
1035 ntohs (message->size),
1038 h->channel_cb (h->channel_cls, NULL, NULL);
1039 h->channel_cb = NULL;
1040 h->channel_cls = NULL;
1045 h->channel_cb (h->channel_cls,
1052 * Function to process all messages received from the service
1054 * @param cls closure
1055 * @param msg message received, NULL on timeout or fatal error
1058 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1060 struct GNUNET_MESH_Handle *h = cls;
1065 LOG (GNUNET_ERROR_TYPE_DEBUG,
1066 "Mesh service disconnected, reconnecting\n", h);
1070 type = ntohs (msg->type);
1071 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1073 GNUNET_MESH_DEBUG_M2S (type));
1076 /* Notify of a new incoming channel */
1077 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1078 process_channel_created (h, (struct GNUNET_MESH_ChannelMessage *) msg);
1080 /* Notify of a channel disconnection */
1081 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1082 process_channel_destroy (h, (struct GNUNET_MESH_ChannelMessage *) msg);
1084 /* Notify of a new data packet in the channel */
1085 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
1086 process_incoming_data (h, msg);
1088 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1089 process_ack (h, msg);
1091 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
1092 process_get_channels (h, msg);
1094 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
1095 process_show_channel (h, msg);
1098 /* We shouldn't get any other packages, log and ignore */
1099 LOG (GNUNET_ERROR_TYPE_WARNING,
1100 "unsolicited message form service (type %s)\n",
1101 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1103 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1104 if (GNUNET_YES == h->in_receive)
1106 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1107 GNUNET_TIME_UNIT_FOREVER_REL);
1111 LOG (GNUNET_ERROR_TYPE_DEBUG,
1112 "in receive off, not calling CLIENT_receive\n");
1117 /******************************************************************************/
1118 /************************ SEND FUNCTIONS ****************************/
1119 /******************************************************************************/
1122 * Function called to send a message to the service.
1123 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1126 * @param cls closure, the mesh handle
1127 * @param size number of bytes available in buf
1128 * @param buf where the callee should write the connect message
1129 * @return number of bytes written to buf
1132 send_callback (void *cls, size_t size, void *buf)
1134 struct GNUNET_MESH_Handle *h = cls;
1135 struct GNUNET_MESH_TransmitHandle *th;
1136 struct GNUNET_MESH_TransmitHandle *next;
1137 struct GNUNET_MESH_Channel *ch;
1143 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
1145 if ((0 == size) || (NULL == buf))
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1154 nsize = message_ready_size (h);
1155 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1158 if (GNUNET_YES == th_is_payload (th))
1160 struct GNUNET_MESH_LocalData *dmsg;
1161 struct GNUNET_MessageHeader *mh;
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1164 if (GNUNET_NO == ch->allow_send)
1166 /* This channel is not ready to transmit yet, try next message */
1170 ch->packet_size = 0;
1171 GNUNET_assert (size >= th->size);
1172 dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
1173 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1174 psize = th->notify (th->notify_cls,
1175 size - sizeof (struct GNUNET_MESH_LocalData),
1179 psize += sizeof (struct GNUNET_MESH_LocalData);
1180 GNUNET_assert (size >= psize);
1181 dmsg->header.size = htons (psize);
1182 dmsg->id = htonl (ch->chid);
1183 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1184 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
1185 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1186 ch->allow_send = GNUNET_NO;
1190 LOG (GNUNET_ERROR_TYPE_DEBUG,
1191 "# callback returned size 0, "
1192 "application canceled transmission\n");
1197 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1199 LOG (GNUNET_ERROR_TYPE_DEBUG, "# mesh internal traffic, type %s\n",
1200 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1201 memcpy (cbuf, &th[1], th->size);
1204 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1205 GNUNET_SCHEDULER_cancel (th->timeout_task);
1206 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1209 nsize = message_ready_size (h);
1214 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1216 size = message_ready_size (h);
1219 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1221 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1222 GNUNET_TIME_UNIT_FOREVER_REL,
1223 GNUNET_YES, &send_callback, h);
1227 if (NULL != h->th_head)
1228 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1230 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1232 if (GNUNET_NO == h->in_receive)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1235 h->in_receive = GNUNET_YES;
1236 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1237 GNUNET_TIME_UNIT_FOREVER_REL);
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1245 * Auxiliary function to send an already constructed packet to the service.
1246 * Takes care of creating a new queue element, copying the message and
1247 * calling the tmt_rdy function if necessary.
1249 * @param h mesh handle
1250 * @param msg message to transmit
1251 * @param channel channel this send is related to (NULL if N/A)
1254 send_packet (struct GNUNET_MESH_Handle *h,
1255 const struct GNUNET_MessageHeader *msg,
1256 struct GNUNET_MESH_Channel *channel)
1258 struct GNUNET_MESH_TransmitHandle *th;
1261 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1262 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1263 msize = ntohs (msg->size);
1264 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1265 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1267 th->channel = channel;
1268 memcpy (&th[1], msg, msize);
1269 add_to_queue (h, th);
1270 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1273 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1275 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1276 GNUNET_TIME_UNIT_FOREVER_REL,
1277 GNUNET_YES, &send_callback, h);
1281 /******************************************************************************/
1282 /********************** API CALL DEFINITIONS *************************/
1283 /******************************************************************************/
1285 struct GNUNET_MESH_Handle *
1286 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1287 GNUNET_MESH_InboundChannelNotificationHandler new_channel,
1288 GNUNET_MESH_ChannelEndHandler cleaner,
1289 const struct GNUNET_MESH_MessageHandler *handlers,
1290 const uint32_t *ports)
1292 struct GNUNET_MESH_Handle *h;
1294 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1295 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1296 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1298 h->new_channel = new_channel;
1299 h->cleaner = cleaner;
1300 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1301 if (h->client == NULL)
1308 h->message_handlers = handlers;
1310 h->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1311 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1312 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1314 if (NULL != ports && ports[0] != 0 && NULL == new_channel)
1317 LOG (GNUNET_ERROR_TYPE_DEBUG,
1318 "no new channel handler given, ports parameter is useless!!\n");
1320 if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
1323 LOG (GNUNET_ERROR_TYPE_DEBUG,
1324 "no ports given, new channel handler will never be called!!\n");
1326 /* count handlers */
1327 for (h->n_handlers = 0;
1328 handlers && handlers[h->n_handlers].type;
1330 for (h->n_ports = 0;
1331 ports && ports[h->n_ports];
1334 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1340 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1342 struct GNUNET_MESH_Channel *ch;
1343 struct GNUNET_MESH_Channel *aux;
1344 struct GNUNET_MESH_TransmitHandle *th;
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1348 ch = handle->channels_head;
1352 if (ch->chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
1355 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid);
1357 destroy_channel (ch, GNUNET_YES);
1360 while ( (th = handle->th_head) != NULL)
1362 struct GNUNET_MessageHeader *msg;
1364 /* Make sure it is an allowed packet (everything else should have been
1365 * already canceled).
1367 GNUNET_break (GNUNET_NO == th_is_payload (th));
1368 msg = (struct GNUNET_MessageHeader *) &th[1];
1369 switch (ntohs(msg->type))
1371 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1372 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1373 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1374 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
1375 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
1379 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1383 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1387 if (NULL != handle->th)
1389 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1392 if (NULL != handle->client)
1394 GNUNET_CLIENT_disconnect (handle->client);
1395 handle->client = NULL;
1397 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1399 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1400 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1402 GNUNET_free (handle);
1407 * Create a new channel (we're initiator and will be allowed to add/remove peers
1408 * and to broadcast).
1410 * @param h mesh handle
1411 * @param channel_ctx client's channel context to associate with the channel
1412 * @param peer peer identity the channel should go to
1413 * @param port Port number.
1414 * @param nobuffer Flag for disabling buffering on relay nodes.
1415 * @param reliable Flag for end-to-end reliability.
1417 * @return handle to the channel
1419 struct GNUNET_MESH_Channel *
1420 GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
1422 const struct GNUNET_PeerIdentity *peer,
1427 struct GNUNET_MESH_Channel *ch;
1428 struct GNUNET_MESH_ChannelMessage msg;
1430 LOG (GNUNET_ERROR_TYPE_DEBUG,
1431 "Creating new channel to %s:%u\n",
1432 GNUNET_i2s (peer), port);
1433 ch = create_channel (h, 0);
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid);
1436 ch->ctx = channel_ctx;
1437 ch->peer = GNUNET_PEER_intern (peer);
1438 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1439 msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
1440 msg.channel_id = htonl (ch->chid);
1441 msg.port = htonl (port);
1444 if (GNUNET_YES == reliable)
1445 msg.opt |= GNUNET_MESH_OPTION_RELIABLE;
1446 if (GNUNET_YES == nobuffer)
1447 msg.opt |= GNUNET_MESH_OPTION_NOBUFFER;
1448 msg.opt = htonl (msg.opt);
1450 send_packet (h, &msg.header, ch);
1456 GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel)
1458 struct GNUNET_MESH_Handle *h;
1459 struct GNUNET_MESH_ChannelMessage msg;
1460 struct GNUNET_MESH_TransmitHandle *th;
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1465 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1466 msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
1467 msg.channel_id = htonl (channel->chid);
1468 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1474 struct GNUNET_MESH_TransmitHandle *aux;
1475 if (th->channel == channel)
1478 /* FIXME call the handler? */
1479 if (GNUNET_YES == th_is_payload (th))
1480 th->notify (th->notify_cls, 0, NULL);
1481 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1489 destroy_channel (channel, GNUNET_YES);
1490 send_packet (h, &msg.header, NULL);
1495 * Get information about a channel.
1497 * @param channel Channel handle.
1498 * @param option Query (GNUNET_MESH_OPTION_*).
1499 * @param ... dependant on option, currently not used
1501 * @return Union with an answer to the query.
1503 const union GNUNET_MESH_ChannelInfo *
1504 GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
1505 enum MeshOption option, ...)
1507 const union GNUNET_MESH_ChannelInfo *ret;
1511 case GNUNET_MESH_OPTION_NOBUFFER:
1512 ret = (const union GNUNET_MESH_ChannelInfo *) &channel->nobuffer;
1514 case GNUNET_MESH_OPTION_RELIABLE:
1515 ret = (const union GNUNET_MESH_ChannelInfo *) &channel->reliable;
1517 case GNUNET_MESH_OPTION_OOORDER:
1518 ret = (const union GNUNET_MESH_ChannelInfo *) &channel->ooorder;
1520 case GNUNET_MESH_OPTION_PEER:
1521 ret = (const union GNUNET_MESH_ChannelInfo *) &channel->peer;
1531 struct GNUNET_MESH_TransmitHandle *
1532 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel, int cork,
1533 struct GNUNET_TIME_Relative maxdelay,
1535 GNUNET_CONNECTION_TransmitReadyNotify notify,
1538 struct GNUNET_MESH_TransmitHandle *th;
1540 GNUNET_assert (NULL != channel);
1541 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1542 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1543 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1544 if (channel->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1547 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1548 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1549 GNUNET_assert (NULL != notify);
1550 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1551 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
1552 th->channel = channel;
1553 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1554 th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
1555 channel->packet_size = th->size;
1556 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1557 th->notify = notify;
1558 th->notify_cls = notify_cls;
1559 add_to_queue (channel->mesh, th);
1560 if (NULL != channel->mesh->th)
1562 if (GNUNET_NO == channel->allow_send)
1564 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1566 GNUNET_CLIENT_notify_transmit_ready (channel->mesh->client, th->size,
1567 GNUNET_TIME_UNIT_FOREVER_REL,
1568 GNUNET_YES, &send_callback,
1570 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1576 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1578 struct GNUNET_MESH_Handle *mesh;
1580 th->channel->packet_size = 0;
1581 mesh = th->channel->mesh;
1582 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1583 GNUNET_SCHEDULER_cancel (th->timeout_task);
1584 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1586 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1588 /* queue empty, no point in asking for transmission */
1589 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1595 GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel)
1602 * Request information about the running mesh peer.
1603 * The callback will be called for every channel known to the service,
1604 * listing all active peers that blong to the channel.
1606 * If called again on the same handle, it will overwrite the previous
1607 * callback and cls. To retrieve the cls, monitor_cancel must be
1610 * WARNING: unstable API, likely to change in the future!
1612 * @param h Handle to the mesh peer.
1613 * @param callback Function to call with the requested data.
1614 * @param callback_cls Closure for @c callback.
1617 GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
1618 GNUNET_MESH_ChannelsCB callback,
1621 struct GNUNET_MessageHeader msg;
1623 msg.size = htons (sizeof (msg));
1624 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS);
1625 send_packet (h, &msg, NULL);
1626 h->channels_cb = callback;
1627 h->channels_cls = callback_cls;
1634 * Cancel a monitor request. The monitor callback will not be called.
1636 * @param h Mesh handle.
1638 * @return Closure given to GNUNET_MESH_monitor, if any.
1641 GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h)
1645 cls = h->channels_cls;
1646 h->channels_cb = NULL;
1647 h->channels_cls = NULL;
1653 * Request information about a specific channel of the running mesh peer.
1655 * WARNING: unstable API, likely to change in the future!
1656 * FIXME Add destination option.
1658 * @param h Handle to the mesh peer.
1659 * @param initiator ID of the owner of the channel.
1660 * @param channel_number Channel number.
1661 * @param callback Function to call with the requested data.
1662 * @param callback_cls Closure for @c callback.
1665 GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
1666 struct GNUNET_PeerIdentity *initiator,
1667 unsigned int channel_number,
1668 GNUNET_MESH_ChannelCB callback,
1671 struct GNUNET_MESH_LocalMonitor msg;
1673 msg.header.size = htons (sizeof (msg));
1674 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL);
1675 msg.owner = *initiator;
1676 msg.channel_id = htonl (channel_number);
1678 send_packet (h, &msg.header, NULL);
1679 h->channel_cb = callback;
1680 h->channel_cls = callback_cls;
1687 * Function called to notify a client about the connection
1688 * begin ready to queue more data. "buf" will be
1689 * NULL and "size" zero if the connection was closed for
1690 * writing in the meantime.
1692 * @param cls closure
1693 * @param size number of bytes available in buf
1694 * @param buf where the callee should write the message
1695 * @return number of bytes written to buf
1698 mesh_mq_ntr (void *cls, size_t size,
1701 struct GNUNET_MQ_Handle *mq = cls;
1702 struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
1703 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
1709 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
1712 msize = ntohs (msg->size);
1713 GNUNET_assert (msize <= size);
1714 memcpy (buf, msg, msize);
1715 GNUNET_MQ_impl_send_continue (mq);
1721 * Signature of functions implementing the
1722 * sending functionality of a message queue.
1724 * @param mq the message queue
1725 * @param msg the message to send
1726 * @param impl_state state of the implementation
1729 mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
1730 const struct GNUNET_MessageHeader *msg, void *impl_state)
1732 struct MeshMQState *state = impl_state;
1734 GNUNET_assert (NULL == state->th);
1735 GNUNET_MQ_impl_send_commit (mq);
1737 GNUNET_MESH_notify_transmit_ready (state->channel,
1738 /* FIXME: add option for corking */
1740 GNUNET_TIME_UNIT_FOREVER_REL,
1748 * Signature of functions implementing the
1749 * destruction of a message queue.
1750 * Implementations must not free 'mq', but should
1751 * take care of 'impl_state'.
1753 * @param mq the message queue to destroy
1754 * @param impl_state state of the implementation
1757 mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
1759 struct MeshMQState *state = impl_state;
1761 if (NULL != state->th)
1762 GNUNET_MESH_notify_transmit_ready_cancel (state->th);
1764 GNUNET_free (state);
1769 * Create a message queue for a mesh channel.
1770 * The message queue can only be used to transmit messages,
1771 * not to receive them.
1773 * @param channel the channel to create the message qeue for
1774 * @return a message queue to messages over the channel
1776 struct GNUNET_MQ_Handle *
1777 GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel)
1779 struct GNUNET_MQ_Handle *mq;
1780 struct MeshMQState *state;
1782 state = GNUNET_new (struct MeshMQState);
1783 state->channel = channel;
1785 mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
1786 mesh_mq_destroy_impl,
1787 NULL, /* FIXME: cancel impl. */
1789 NULL, /* no msg handlers */
1790 NULL, /* no err handlers */
1791 NULL); /* no handler cls */