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.
94 GNUNET_MESH_ChannelCB channel_cb;
99 GNUNET_MESH_PeersCB peers_cb;
104 GNUNET_MESH_TunnelsCB tunnels_cb;
109 GNUNET_MESH_TunnelCB tunnel_cb;
114 * Opaque handle to the service.
116 struct GNUNET_MESH_Handle
120 * Handle to the server connection, to send messages later
122 struct GNUNET_CLIENT_Connection *client;
125 * Set of handlers used for processing incoming messages in the channels
127 const struct GNUNET_MESH_MessageHandler *message_handlers;
130 * Number of handlers in the handlers array.
132 unsigned int n_handlers;
137 const uint32_t *ports;
142 unsigned int n_ports;
145 * Double linked list of the channels this client is connected to, head.
147 struct GNUNET_MESH_Channel *channels_head;
150 * Double linked list of the channels this client is connected to, tail.
152 struct GNUNET_MESH_Channel *channels_tail;
155 * Callback for inbound channel creation
157 GNUNET_MESH_InboundChannelNotificationHandler *new_channel;
160 * Callback for inbound channel disconnection
162 GNUNET_MESH_ChannelEndHandler *cleaner;
165 * Handle to cancel pending transmissions in case of disconnection
167 struct GNUNET_CLIENT_TransmitHandle *th;
170 * Closure for all the handlers given by the client
175 * Messages to send to the service, head.
177 struct GNUNET_MESH_TransmitHandle *th_head;
180 * Messages to send to the service, tail.
182 struct GNUNET_MESH_TransmitHandle *th_tail;
185 * chid of the next channel to create (to avoid reusing IDs often)
187 MESH_ChannelNumber next_chid;
190 * Have we started the task to receive messages from the service
191 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
196 * Configuration given by the client, in case of reconnection
198 const struct GNUNET_CONFIGURATION_Handle *cfg;
201 * Time to the next reconnect in case one reconnect fails
203 struct GNUNET_TIME_Relative reconnect_time;
206 * Task for trying to reconnect.
208 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
211 * Callback for an info task (only one active at a time).
213 union MeshInfoCB info_cb;
216 * Info callback closure for @c info_cb.
223 * Description of a peer
225 struct GNUNET_MESH_Peer
228 * ID of the peer in short form
233 * Channel this peer belongs to
235 struct GNUNET_MESH_Channel *t;
240 * Opaque handle to a channel.
242 struct GNUNET_MESH_Channel
248 struct GNUNET_MESH_Channel *next;
253 struct GNUNET_MESH_Channel *prev;
256 * Handle to the mesh this channel belongs to
258 struct GNUNET_MESH_Handle *mesh;
261 * Local ID of the channel
263 MESH_ChannelNumber chid;
271 * Other end of the channel.
276 * Any data the caller wants to put in here
281 * Size of packet queued in this channel
283 unsigned int packet_size;
286 * Channel options: reliability, etc.
288 enum GNUNET_MESH_ChannelOption options;
291 * Are we allowed to send to the service?
299 * Implementation state for mesh's message queue.
304 * The current transmit handle, or NULL
305 * if no transmit is active.
307 struct GNUNET_MESH_TransmitHandle *th;
310 * Channel to send the data over.
312 struct GNUNET_MESH_Channel *channel;
316 /******************************************************************************/
317 /*********************** DECLARATIONS *************************/
318 /******************************************************************************/
321 * Function called to send a message to the service.
322 * "buf" will be NULL and "size" zero if the socket was closed for writing in
325 * @param cls closure, the mesh handle
326 * @param size number of bytes available in buf
327 * @param buf where the callee should write the connect message
328 * @return number of bytes written to buf
331 send_callback (void *cls, size_t size, void *buf);
334 /******************************************************************************/
335 /*********************** AUXILIARY FUNCTIONS *************************/
336 /******************************************************************************/
339 * Check if transmission is a payload packet.
341 * @param th Transmission handle.
343 * @return GNUNET_YES if it is a payload packet,
344 * GNUNET_NO if it is a mesh management packet.
347 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
349 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
354 * Check whether there is any message ready in the queue and find the size.
356 * @param h Mesh handle.
358 * @return The size of the first ready message in the queue,
359 * 0 if there is none.
362 message_ready_size (struct GNUNET_MESH_Handle *h)
364 struct GNUNET_MESH_TransmitHandle *th;
365 struct GNUNET_MESH_Channel *ch;
367 for (th = h->th_head; NULL != th; th = th->next)
370 if (GNUNET_NO == th_is_payload (th))
372 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
375 if (GNUNET_YES == ch->allow_send)
377 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok\n");
386 * Get the channel handler for the channel specified by id from the given handle
387 * @param h Mesh handle
388 * @param chid ID of the wanted channel
389 * @return handle to the required channel or NULL if not found
391 static struct GNUNET_MESH_Channel *
392 retrieve_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
394 struct GNUNET_MESH_Channel *ch;
396 ch = h->channels_head;
399 if (ch->chid == chid)
408 * Create a new channel and insert it in the channel list of the mesh handle
410 * @param h Mesh handle
411 * @param chid Desired chid of the channel, 0 to assign one automatically.
413 * @return Handle to the created channel.
415 static struct GNUNET_MESH_Channel *
416 create_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
418 struct GNUNET_MESH_Channel *ch;
420 ch = GNUNET_new (struct GNUNET_MESH_Channel);
421 GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
425 ch->chid = h->next_chid;
426 while (NULL != retrieve_channel (h, h->next_chid))
429 h->next_chid &= ~GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
430 h->next_chid |= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
437 ch->allow_send = GNUNET_NO;
443 * Destroy the specified channel.
444 * - Destroys all peers, calling the disconnect callback on each if needed
445 * - Cancels all outgoing traffic for that channel, calling respective notifys
446 * - Calls cleaner if channel was inbound
447 * - Frees all memory used
449 * @param ch Pointer to the channel.
450 * @param call_cleaner Whether to call the cleaner handler.
452 * @return Handle to the required channel or NULL if not found.
455 destroy_channel (struct GNUNET_MESH_Channel *ch, int call_cleaner)
457 struct GNUNET_MESH_Handle *h;
458 struct GNUNET_MESH_TransmitHandle *th;
459 struct GNUNET_MESH_TransmitHandle *next;
461 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
470 GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch);
472 /* signal channel destruction */
473 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
475 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cleaner\n");
476 h->cleaner (h->cls, ch, ch->ctx);
479 /* check that clients did not leave messages behind in the queue */
480 for (th = h->th_head; NULL != th; th = next)
483 if (th->channel != ch)
485 /* Clients should have aborted their requests already.
486 * Management traffic should be ok, as clients can't cancel that.
487 * If the service crashed and we are reconnecting, it's ok.
489 GNUNET_break (GNUNET_NO == th_is_payload (th)
490 || GNUNET_NO == h->in_receive);
491 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
493 /* clean up request */
494 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
495 GNUNET_SCHEDULER_cancel (th->timeout_task);
499 /* if there are no more pending requests with mesh service, cancel active request */
500 /* Note: this should be unnecessary... */
501 if ((0 == message_ready_size (h)) && (NULL != h->th))
503 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
508 GNUNET_PEER_change_rc (ch->peer, -1);
515 * Notify client that the transmission has timed out
518 * @param tc task context
521 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
523 struct GNUNET_MESH_TransmitHandle *th = cls;
524 struct GNUNET_MESH_Handle *mesh;
526 mesh = th->channel->mesh;
527 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
528 th->channel->packet_size = 0;
529 if (GNUNET_YES == th_is_payload (th))
530 th->notify (th->notify_cls, 0, NULL);
532 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
534 /* nothing ready to transmit, no point in asking for transmission */
535 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
542 * Add a transmit handle to the transmission queue and set the
545 * @param h mesh handle with the queue head and tail
546 * @param th handle to the packet to be transmitted
549 add_to_queue (struct GNUNET_MESH_Handle *h,
550 struct GNUNET_MESH_TransmitHandle *th)
552 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
553 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
556 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
557 (th->timeout), &timeout_transmission, th);
562 * Auxiliary function to send an already constructed packet to the service.
563 * Takes care of creating a new queue element, copying the message and
564 * calling the tmt_rdy function if necessary.
566 * @param h mesh handle
567 * @param msg message to transmit
568 * @param channel channel this send is related to (NULL if N/A)
571 send_packet (struct GNUNET_MESH_Handle *h,
572 const struct GNUNET_MessageHeader *msg,
573 struct GNUNET_MESH_Channel *channel);
577 * Send an ack on the channel to confirm the processing of a message.
579 * @param ch Channel on which to send the ACK.
582 send_ack (struct GNUNET_MESH_Channel *ch)
584 struct GNUNET_MESH_LocalAck msg;
586 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
587 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
588 msg.header.size = htons (sizeof (msg));
589 msg.channel_id = htonl (ch->chid);
591 send_packet (ch->mesh, &msg.header, ch);
598 * Reconnect callback: tries to reconnect again after a failer previous
600 * @param cls closure (mesh handle)
601 * @param tc task context
604 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
608 * Send a connect packet to the service with the applications and types
609 * requested by the user.
611 * @param h The mesh handle.
615 send_connect (struct GNUNET_MESH_Handle *h)
619 size = sizeof (struct GNUNET_MESH_ClientConnect);
620 size += h->n_ports * sizeof (uint32_t);
622 char buf[size] GNUNET_ALIGN;
623 struct GNUNET_MESH_ClientConnect *msg;
627 /* build connection packet */
628 msg = (struct GNUNET_MESH_ClientConnect *) buf;
629 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
630 msg->header.size = htons (size);
631 ports = (uint32_t *) &msg[1];
632 for (i = 0; i < h->n_ports; i++)
634 ports[i] = htonl (h->ports[i]);
635 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
638 LOG (GNUNET_ERROR_TYPE_DEBUG,
639 "Sending %lu bytes long message with %u ports\n",
640 ntohs (msg->header.size), h->n_ports);
641 send_packet (h, &msg->header, NULL);
647 * Reconnect to the service, retransmit all infomation to try to restore the
650 * @param h handle to the mesh
652 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
655 do_reconnect (struct GNUNET_MESH_Handle *h)
657 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
658 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
659 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
660 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
661 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
666 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
669 if (NULL != h->client)
671 GNUNET_CLIENT_disconnect (h->client);
675 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
676 if (h->client == NULL)
678 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
681 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
682 GNUNET_TIME_relative_multiply
683 (h->reconnect_time, 2));
684 LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
685 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
692 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
699 * Reconnect callback: tries to reconnect again after a failer previous
701 * @param cls closure (mesh handle)
702 * @param tc task context
705 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
707 struct GNUNET_MESH_Handle *h = cls;
709 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
710 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
717 * Reconnect to the service, retransmit all infomation to try to restore the
720 * @param h handle to the mesh
722 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
725 reconnect (struct GNUNET_MESH_Handle *h)
727 struct GNUNET_MESH_Channel *ch;
728 struct GNUNET_MESH_Channel *next;
730 LOG (GNUNET_ERROR_TYPE_DEBUG,
731 "Requested RECONNECT, destroying all channels\n");
732 h->in_receive = GNUNET_NO;
733 for (ch = h->channels_head; NULL != ch; ch = next)
736 destroy_channel (ch, GNUNET_YES);
738 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
739 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
744 /******************************************************************************/
745 /*********************** RECEIVE HANDLERS ****************************/
746 /******************************************************************************/
749 * Process the new channel notification and add it to the channels in the handle
751 * @param h The mesh handle
752 * @param msg A message with the details of the new incoming channel
755 process_channel_created (struct GNUNET_MESH_Handle *h,
756 const struct GNUNET_MESH_ChannelMessage *msg)
758 struct GNUNET_MESH_Channel *ch;
759 MESH_ChannelNumber chid;
762 chid = ntohl (msg->channel_id);
763 port = ntohl (msg->port);
764 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, port);
765 if (chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
770 if (NULL != h->new_channel)
774 ch = create_channel (h, chid);
775 ch->allow_send = GNUNET_NO;
776 ch->peer = GNUNET_PEER_intern (&msg->peer);
780 ch->options = ntohl (msg->opt);
782 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
783 ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options);
786 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
790 struct GNUNET_MESH_ChannelMessage d_msg;
792 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
794 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
795 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
796 d_msg.channel_id = msg->channel_id;
797 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
801 send_packet (h, &d_msg.header, NULL);
808 * Process the channel destroy notification and free associated resources
810 * @param h The mesh handle
811 * @param msg A message with the details of the channel being destroyed
814 process_channel_destroy (struct GNUNET_MESH_Handle *h,
815 const struct GNUNET_MESH_ChannelMessage *msg)
817 struct GNUNET_MESH_Channel *ch;
818 MESH_ChannelNumber chid;
820 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel Destroy received from service\n");
821 chid = ntohl (msg->channel_id);
822 ch = retrieve_channel (h, chid);
826 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
829 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroying channel %X\n", ch->chid);
830 destroy_channel (ch, GNUNET_YES);
835 * Process the incoming data packets, call appropriate handlers.
837 * @param h The mesh handle
838 * @param message A message encapsulating the data
841 process_incoming_data (struct GNUNET_MESH_Handle *h,
842 const struct GNUNET_MessageHeader *message)
844 const struct GNUNET_MessageHeader *payload;
845 const struct GNUNET_MESH_MessageHandler *handler;
846 struct GNUNET_MESH_LocalData *dmsg;
847 struct GNUNET_MESH_Channel *ch;
852 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
853 dmsg = (struct GNUNET_MESH_LocalData *) message;
854 ch = retrieve_channel (h, ntohl (dmsg->id));
855 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
856 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
857 GM_f2s (ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV),
858 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
860 size = ntohs (message->size);
861 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes\n", size);
865 /* Channel was ignored/destroyed, probably service didn't get it yet */
866 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
869 type = ntohs (payload->type);
870 size = ntohs (payload->size);
871 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GM_m2s (type));
872 for (i = 0; i < h->n_handlers; i++)
874 handler = &h->message_handlers[i];
875 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
877 if (handler->type == type)
880 handler->callback (h->cls, ch, &ch->ctx, payload))
882 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
883 GNUNET_MESH_channel_destroy (ch);
888 LOG (GNUNET_ERROR_TYPE_DEBUG,
889 "callback completed successfully\n");
898 * Process a local ACK message, enabling the client to send
899 * more data to the service.
901 * @param h Mesh handle.
902 * @param message Message itself.
905 process_ack (struct GNUNET_MESH_Handle *h,
906 const struct GNUNET_MessageHeader *message)
908 struct GNUNET_MESH_LocalAck *msg;
909 struct GNUNET_MESH_Channel *ch;
910 MESH_ChannelNumber chid;
912 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
913 msg = (struct GNUNET_MESH_LocalAck *) message;
914 chid = ntohl (msg->channel_id);
915 ch = retrieve_channel (h, chid);
918 LOG (GNUNET_ERROR_TYPE_DEBUG, "ACK on unknown channel %X\n", chid);
921 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X!\n", ch->chid);
922 ch->allow_send = GNUNET_YES;
923 if (NULL == h->th && 0 < ch->packet_size)
925 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
926 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
927 GNUNET_TIME_UNIT_FOREVER_REL,
928 GNUNET_YES, &send_callback, h);
934 * Process a local reply about info on all channels, pass info to the user.
936 * @param h Mesh handle.
937 * @param message Message itself.
940 // process_get_channels (struct GNUNET_MESH_Handle *h,
941 // const struct GNUNET_MessageHeader *message)
943 // struct GNUNET_MESH_LocalInfo *msg;
945 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
947 // if (NULL == h->channels_cb)
949 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
953 // msg = (struct GNUNET_MESH_LocalInfo *) message;
954 // if (ntohs (message->size) !=
955 // (sizeof (struct GNUNET_MESH_LocalInfo) +
956 // sizeof (struct GNUNET_PeerIdentity)))
958 // GNUNET_break_op (0);
959 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
960 // "Get channels message: size %hu - expected %u\n",
961 // ntohs (message->size),
962 // sizeof (struct GNUNET_MESH_LocalInfo));
965 // h->channels_cb (h->channels_cls,
966 // ntohl (msg->channel_id),
968 // &msg->destination);
974 * Process a local monitor_channel reply, pass info to the user.
976 * @param h Mesh handle.
977 * @param message Message itself.
980 // process_show_channel (struct GNUNET_MESH_Handle *h,
981 // const struct GNUNET_MessageHeader *message)
983 // struct GNUNET_MESH_LocalInfo *msg;
986 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
988 // if (NULL == h->channel_cb)
990 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
994 // /* Verify message sanity */
995 // msg = (struct GNUNET_MESH_LocalInfo *) message;
996 // esize = sizeof (struct GNUNET_MESH_LocalInfo);
997 // if (ntohs (message->size) != esize)
999 // GNUNET_break_op (0);
1000 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1001 // "Show channel message: size %hu - expected %u\n",
1002 // ntohs (message->size),
1005 // h->channel_cb (h->channel_cls, NULL, NULL);
1006 // h->channel_cb = NULL;
1007 // h->channel_cls = NULL;
1012 // h->channel_cb (h->channel_cls,
1013 // &msg->destination,
1020 * Process a local reply about info on all tunnels, pass info to the user.
1022 * @param h Mesh handle.
1023 * @param message Message itself.
1026 process_get_peers (struct GNUNET_MESH_Handle *h,
1027 const struct GNUNET_MessageHeader *message)
1029 struct GNUNET_MESH_LocalInfoPeer *msg;
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Peer messasge received\n");
1034 if (NULL == h->info_cb.peers_cb)
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1040 size = ntohs (message->size);
1041 if (sizeof (struct GNUNET_MESH_LocalInfoPeer) > size)
1043 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
1044 h->info_cb.peers_cb = NULL;
1049 msg = (struct GNUNET_MESH_LocalInfoPeer *) message;
1050 h->info_cb.peers_cb (h->info_cls, &msg->destination,
1051 (int) ntohs (msg->tunnel),
1052 (unsigned int ) ntohs (msg->paths),
1058 * Process a local reply about info on all tunnels, pass info to the user.
1060 * @param h Mesh handle.
1061 * @param message Message itself.
1064 process_get_tunnels (struct GNUNET_MESH_Handle *h,
1065 const struct GNUNET_MessageHeader *message)
1067 struct GNUNET_MESH_LocalInfoTunnel *msg;
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnels messasge received\n");
1072 if (NULL == h->info_cb.tunnels_cb)
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1078 size = ntohs (message->size);
1079 if (sizeof (struct GNUNET_MESH_LocalInfoTunnel) > size)
1081 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1082 h->info_cb.tunnels_cb = NULL;
1087 msg = (struct GNUNET_MESH_LocalInfoTunnel *) message;
1088 h->info_cb.tunnels_cb (h->info_cls, &msg->destination,
1089 ntohl (msg->channels), ntohl (msg->connections),
1090 ntohs (msg->estate), ntohs (msg->cstate));
1097 * Process a local tunnel info reply, pass info to the user.
1099 * @param h Mesh handle.
1100 * @param message Message itself.
1103 process_get_tunnel (struct GNUNET_MESH_Handle *h,
1104 const struct GNUNET_MessageHeader *message)
1106 struct GNUNET_MESH_LocalInfoTunnel *msg;
1111 struct GNUNET_HashCode *conns;
1112 MESH_ChannelNumber *chns;
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
1115 if (NULL == h->info_cb.tunnel_cb)
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1121 /* Verify message sanity */
1122 msg = (struct GNUNET_MESH_LocalInfoTunnel *) message;
1123 msize = ntohs (message->size);
1124 esize = sizeof (struct GNUNET_MESH_LocalInfoTunnel);
1127 GNUNET_break_op (0);
1128 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1131 ch_n = ntohl (msg->channels);
1132 c_n = ntohl (msg->connections);
1133 esize += ch_n * sizeof (MESH_ChannelNumber);
1134 esize += c_n * sizeof (struct GNUNET_HashCode);
1137 GNUNET_break_op (0);
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
1139 msize, esize, ch_n, c_n);
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
1141 sizeof (struct GNUNET_MESH_LocalInfoTunnel),
1142 sizeof (MESH_ChannelNumber), sizeof (struct GNUNET_HashCode));
1143 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1147 /* Call Callback with tunnel info. */
1148 conns = (struct GNUNET_HashCode *) &msg[1];
1149 chns = (MESH_ChannelNumber *) &conns[c_n];
1150 h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
1151 ch_n, c_n, chns, conns,
1152 ntohs (msg->estate), ntohs (msg->cstate));
1155 h->info_cb.tunnel_cb = NULL;
1160 * Function to process all messages received from the service
1162 * @param cls closure
1163 * @param msg message received, NULL on timeout or fatal error
1166 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1168 struct GNUNET_MESH_Handle *h = cls;
1173 LOG (GNUNET_ERROR_TYPE_DEBUG,
1174 "Mesh service disconnected, reconnecting\n", h);
1178 type = ntohs (msg->type);
1179 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1180 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1184 /* Notify of a new incoming channel */
1185 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1186 process_channel_created (h, (struct GNUNET_MESH_ChannelMessage *) msg);
1188 /* Notify of a channel disconnection */
1189 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
1190 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1191 process_channel_destroy (h, (struct GNUNET_MESH_ChannelMessage *) msg);
1193 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
1194 process_incoming_data (h, msg);
1196 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1197 process_ack (h, msg);
1199 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
1200 // process_get_channels (h, msg);
1202 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
1203 // process_show_channel (h, msg);
1205 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS:
1206 process_get_peers (h, msg);
1208 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1209 process_get_tunnels (h, msg);
1211 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1212 process_get_tunnel (h, msg);
1214 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
1215 // process_show_channel (h, msg);
1218 /* We shouldn't get any other packages, log and ignore */
1219 LOG (GNUNET_ERROR_TYPE_WARNING,
1220 "unsolicited message form service (type %s)\n",
1221 GM_m2s (ntohs (msg->type)));
1223 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1224 if (GNUNET_YES == h->in_receive)
1226 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1227 GNUNET_TIME_UNIT_FOREVER_REL);
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "in receive off, not calling CLIENT_receive\n");
1237 /******************************************************************************/
1238 /************************ SEND FUNCTIONS ****************************/
1239 /******************************************************************************/
1242 * Function called to send a message to the service.
1243 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1246 * @param cls closure, the mesh handle
1247 * @param size number of bytes available in buf
1248 * @param buf where the callee should write the connect message
1249 * @return number of bytes written to buf
1252 send_callback (void *cls, size_t size, void *buf)
1254 struct GNUNET_MESH_Handle *h = cls;
1255 struct GNUNET_MESH_TransmitHandle *th;
1256 struct GNUNET_MESH_TransmitHandle *next;
1257 struct GNUNET_MESH_Channel *ch;
1263 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1264 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
1265 if ((0 == size) || (NULL == buf))
1267 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1274 nsize = message_ready_size (h);
1275 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1278 if (GNUNET_YES == th_is_payload (th))
1280 struct GNUNET_MESH_LocalData *dmsg;
1281 struct GNUNET_MessageHeader *mh;
1283 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1284 if (GNUNET_NO == ch->allow_send)
1286 /* This channel is not ready to transmit yet, try next message */
1290 ch->packet_size = 0;
1291 GNUNET_assert (size >= th->size);
1292 dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
1293 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1294 psize = th->notify (th->notify_cls,
1295 size - sizeof (struct GNUNET_MESH_LocalData),
1299 psize += sizeof (struct GNUNET_MESH_LocalData);
1300 GNUNET_assert (size >= psize);
1301 dmsg->header.size = htons (psize);
1302 dmsg->id = htonl (ch->chid);
1303 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
1305 GM_m2s (ntohs (mh->type)));
1306 ch->allow_send = GNUNET_NO;
1310 LOG (GNUNET_ERROR_TYPE_DEBUG,
1311 "# callback returned size 0, "
1312 "application canceled transmission\n");
1317 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1319 LOG (GNUNET_ERROR_TYPE_DEBUG, "# mesh internal traffic, type %s\n",
1320 GM_m2s (ntohs (mh->type)));
1321 memcpy (cbuf, &th[1], th->size);
1324 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1325 GNUNET_SCHEDULER_cancel (th->timeout_task);
1326 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1329 nsize = message_ready_size (h);
1334 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1336 size = message_ready_size (h);
1339 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1341 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1342 GNUNET_TIME_UNIT_FOREVER_REL,
1343 GNUNET_YES, &send_callback, h);
1347 if (NULL != h->th_head)
1348 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1350 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1352 if (GNUNET_NO == h->in_receive)
1354 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1355 h->in_receive = GNUNET_YES;
1356 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1357 GNUNET_TIME_UNIT_FOREVER_REL);
1359 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1365 * Auxiliary function to send an already constructed packet to the service.
1366 * Takes care of creating a new queue element, copying the message and
1367 * calling the tmt_rdy function if necessary.
1369 * @param h mesh handle
1370 * @param msg message to transmit
1371 * @param channel channel this send is related to (NULL if N/A)
1374 send_packet (struct GNUNET_MESH_Handle *h,
1375 const struct GNUNET_MessageHeader *msg,
1376 struct GNUNET_MESH_Channel *channel)
1378 struct GNUNET_MESH_TransmitHandle *th;
1381 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1382 GM_m2s(ntohs(msg->type)));
1383 msize = ntohs (msg->size);
1384 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1385 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1387 th->channel = channel;
1388 memcpy (&th[1], msg, msize);
1389 add_to_queue (h, th);
1390 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1393 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1395 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1396 GNUNET_TIME_UNIT_FOREVER_REL,
1397 GNUNET_YES, &send_callback, h);
1401 /******************************************************************************/
1402 /********************** API CALL DEFINITIONS *************************/
1403 /******************************************************************************/
1405 struct GNUNET_MESH_Handle *
1406 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1407 GNUNET_MESH_InboundChannelNotificationHandler new_channel,
1408 GNUNET_MESH_ChannelEndHandler cleaner,
1409 const struct GNUNET_MESH_MessageHandler *handlers,
1410 const uint32_t *ports)
1412 struct GNUNET_MESH_Handle *h;
1414 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1415 h = GNUNET_new (struct GNUNET_MESH_Handle);
1416 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1418 h->new_channel = new_channel;
1419 h->cleaner = cleaner;
1420 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1421 if (h->client == NULL)
1428 h->message_handlers = handlers;
1430 h->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1431 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1432 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1434 if (NULL != ports && ports[0] != 0 && NULL == new_channel)
1437 LOG (GNUNET_ERROR_TYPE_DEBUG,
1438 "no new channel handler given, ports parameter is useless!!\n");
1440 if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
1443 LOG (GNUNET_ERROR_TYPE_DEBUG,
1444 "no ports given, new channel handler will never be called!!\n");
1446 /* count handlers */
1447 for (h->n_handlers = 0;
1448 handlers && handlers[h->n_handlers].type;
1450 for (h->n_ports = 0;
1451 ports && ports[h->n_ports];
1454 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1460 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1462 struct GNUNET_MESH_Channel *ch;
1463 struct GNUNET_MESH_Channel *aux;
1464 struct GNUNET_MESH_TransmitHandle *th;
1466 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1468 ch = handle->channels_head;
1472 if (ch->chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
1475 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid);
1477 destroy_channel (ch, GNUNET_YES);
1480 while ( (th = handle->th_head) != NULL)
1482 struct GNUNET_MessageHeader *msg;
1484 /* Make sure it is an allowed packet (everything else should have been
1485 * already canceled).
1487 GNUNET_break (GNUNET_NO == th_is_payload (th));
1488 msg = (struct GNUNET_MessageHeader *) &th[1];
1489 switch (ntohs(msg->type))
1491 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1492 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1493 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1494 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
1495 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
1496 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEER:
1497 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS:
1498 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1499 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1503 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1507 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1511 if (NULL != handle->th)
1513 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1516 if (NULL != handle->client)
1518 GNUNET_CLIENT_disconnect (handle->client);
1519 handle->client = NULL;
1521 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1523 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1524 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1526 GNUNET_free (handle);
1531 * Create a new channel towards a remote peer.
1533 * If the destination port is not open by any peer or the destination peer
1534 * does not accept the channel, #GNUNET_MESH_ChannelEndHandler will be called
1537 * @param h mesh handle
1538 * @param channel_ctx client's channel context to associate with the channel
1539 * @param peer peer identity the channel should go to
1540 * @param port Port number.
1541 * @param options MeshOption flag field, with all desired option bits set to 1.
1543 * @return handle to the channel
1545 struct GNUNET_MESH_Channel *
1546 GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
1548 const struct GNUNET_PeerIdentity *peer,
1550 enum GNUNET_MESH_ChannelOption options)
1552 struct GNUNET_MESH_Channel *ch;
1553 struct GNUNET_MESH_ChannelMessage msg;
1555 LOG (GNUNET_ERROR_TYPE_DEBUG,
1556 "Creating new channel to %s:%u\n",
1557 GNUNET_i2s (peer), port);
1558 ch = create_channel (h, 0);
1559 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1560 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid);
1561 ch->ctx = channel_ctx;
1562 ch->peer = GNUNET_PEER_intern (peer);
1563 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1564 msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
1565 msg.channel_id = htonl (ch->chid);
1566 msg.port = htonl (port);
1568 msg.opt = htonl (options);
1570 send_packet (h, &msg.header, ch);
1576 GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel)
1578 struct GNUNET_MESH_Handle *h;
1579 struct GNUNET_MESH_ChannelMessage msg;
1580 struct GNUNET_MESH_TransmitHandle *th;
1582 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1585 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1586 msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
1587 msg.channel_id = htonl (channel->chid);
1588 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1594 struct GNUNET_MESH_TransmitHandle *aux;
1595 if (th->channel == channel)
1598 /* FIXME call the handler? */
1599 if (GNUNET_YES == th_is_payload (th))
1600 th->notify (th->notify_cls, 0, NULL);
1601 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1609 destroy_channel (channel, GNUNET_YES);
1610 send_packet (h, &msg.header, NULL);
1615 * Get information about a channel.
1617 * @param channel Channel handle.
1618 * @param option Query (GNUNET_MESH_OPTION_*).
1619 * @param ... dependant on option, currently not used
1621 * @return Union with an answer to the query.
1623 const union GNUNET_MESH_ChannelInfo *
1624 GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
1625 enum GNUNET_MESH_ChannelOption option, ...)
1627 static int bool_flag;
1628 const union GNUNET_MESH_ChannelInfo *ret;
1632 case GNUNET_MESH_OPTION_NOBUFFER:
1633 case GNUNET_MESH_OPTION_RELIABLE:
1634 case GNUNET_MESH_OPTION_OOORDER:
1635 if (0 != (option & channel->options))
1636 bool_flag = GNUNET_YES;
1638 bool_flag = GNUNET_NO;
1639 ret = (const union GNUNET_MESH_ChannelInfo *) &bool_flag;
1641 case GNUNET_MESH_OPTION_PEER:
1642 ret = (const union GNUNET_MESH_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1652 struct GNUNET_MESH_TransmitHandle *
1653 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel, int cork,
1654 struct GNUNET_TIME_Relative maxdelay,
1656 GNUNET_CONNECTION_TransmitReadyNotify notify,
1659 struct GNUNET_MESH_TransmitHandle *th;
1661 GNUNET_assert (NULL != channel);
1662 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1663 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1664 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1665 if (channel->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
1666 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1669 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1670 GNUNET_assert (NULL != notify);
1671 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1672 th = GNUNET_new (struct GNUNET_MESH_TransmitHandle);
1673 th->channel = channel;
1674 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1675 th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
1676 channel->packet_size = th->size;
1677 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1678 th->notify = notify;
1679 th->notify_cls = notify_cls;
1680 add_to_queue (channel->mesh, th);
1681 if (NULL != channel->mesh->th)
1683 if (GNUNET_NO == channel->allow_send)
1685 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1687 GNUNET_CLIENT_notify_transmit_ready (channel->mesh->client, th->size,
1688 GNUNET_TIME_UNIT_FOREVER_REL,
1689 GNUNET_YES, &send_callback,
1691 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1697 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1699 struct GNUNET_MESH_Handle *mesh;
1701 th->channel->packet_size = 0;
1702 mesh = th->channel->mesh;
1703 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1704 GNUNET_SCHEDULER_cancel (th->timeout_task);
1705 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1707 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1709 /* queue empty, no point in asking for transmission */
1710 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1717 GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel)
1724 send_info_request (struct GNUNET_MESH_Handle *h, uint16_t type)
1726 struct GNUNET_MessageHeader msg;
1728 msg.size = htons (sizeof (msg));
1729 msg.type = htons (type);
1730 send_packet (h, &msg, NULL);
1735 * Request information about peers known to the running mesh service.
1736 * The callback will be called for every peer known to the service.
1737 * Only one info request (of any kind) can be active at once.
1740 * WARNING: unstable API, likely to change in the future!
1742 * @param h Handle to the mesh peer.
1743 * @param callback Function to call with the requested data.
1744 * @param callback_cls Closure for @c callback.
1746 * @return #GNUNET_OK / #GNUNET_SYSERR
1749 GNUNET_MESH_get_peers (struct GNUNET_MESH_Handle *h,
1750 GNUNET_MESH_PeersCB callback,
1753 if (NULL != h->info_cb.peers_cb)
1756 return GNUNET_SYSERR;
1758 send_info_request (h, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS);
1759 h->info_cb.peers_cb = callback;
1760 h->info_cls = callback_cls;
1766 * Cancel a peer info request. The callback will not be called (anymore).
1768 * WARNING: unstable API, likely to change in the future!
1770 * @param h Mesh handle.
1772 * @return Closure given to GNUNET_MESH_get_peers.
1775 GNUNET_MESH_get_peers_cancel (struct GNUNET_MESH_Handle *h)
1780 h->info_cb.peers_cb = NULL;
1787 * Request information about tunnels of the running mesh peer.
1788 * The callback will be called for every tunnel of the service.
1789 * Only one info request (of any kind) can be active at once.
1791 * WARNING: unstable API, likely to change in the future!
1793 * @param h Handle to the mesh peer.
1794 * @param callback Function to call with the requested data.
1795 * @param callback_cls Closure for @c callback.
1797 * @return #GNUNET_OK / #GNUNET_SYSERR
1800 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
1801 GNUNET_MESH_TunnelsCB callback,
1804 if (NULL != h->info_cb.tunnels_cb)
1807 return GNUNET_SYSERR;
1809 send_info_request (h, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
1810 h->info_cb.tunnels_cb = callback;
1811 h->info_cls = callback_cls;
1817 * Cancel a monitor request. The monitor callback will not be called.
1819 * @param h Mesh handle.
1821 * @return Closure given to GNUNET_MESH_get_tunnels.
1824 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
1828 h->info_cb.tunnels_cb = NULL;
1838 * Request information about a tunnel of the running mesh peer.
1839 * The callback will be called for the tunnel once.
1840 * Only one info request (of any kind) can be active at once.
1842 * WARNING: unstable API, likely to change in the future!
1844 * @param h Handle to the mesh peer.
1845 * @param id Peer whose tunnel to examine.
1846 * @param callback Function to call with the requested data.
1847 * @param callback_cls Closure for @c callback.
1849 * @return #GNUNET_OK / #GNUNET_SYSERR
1852 GNUNET_MESH_get_tunnel (struct GNUNET_MESH_Handle *h,
1853 const struct GNUNET_PeerIdentity *id,
1854 GNUNET_MESH_TunnelCB callback,
1857 struct GNUNET_MESH_LocalInfo msg;
1859 if (NULL != h->info_cb.tunnel_cb)
1862 return GNUNET_SYSERR;
1865 memset (&msg, 0, sizeof (msg));
1866 msg.header.size = htons (sizeof (msg));
1867 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
1869 send_packet (h, &msg.header, NULL);
1870 h->info_cb.tunnel_cb = callback;
1871 h->info_cls = callback_cls;
1877 * Request information about a specific channel of the running mesh peer.
1879 * WARNING: unstable API, likely to change in the future!
1880 * FIXME Add destination option.
1882 * @param h Handle to the mesh peer.
1883 * @param initiator ID of the owner of the channel.
1884 * @param channel_number Channel number.
1885 * @param callback Function to call with the requested data.
1886 * @param callback_cls Closure for @c callback.
1888 * @return #GNUNET_OK / #GNUNET_SYSERR
1891 GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
1892 struct GNUNET_PeerIdentity *initiator,
1893 unsigned int channel_number,
1894 GNUNET_MESH_ChannelCB callback,
1897 struct GNUNET_MESH_LocalInfo msg;
1899 if (NULL != h->info_cb.channel_cb)
1902 return GNUNET_SYSERR;
1905 msg.header.size = htons (sizeof (msg));
1906 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL);
1907 msg.peer = *initiator;
1908 msg.channel_id = htonl (channel_number);
1909 // msg.reserved = 0;
1910 send_packet (h, &msg.header, NULL);
1911 h->info_cb.channel_cb = callback;
1912 h->info_cls = callback_cls;
1918 * Function called to notify a client about the connection
1919 * begin ready to queue more data. "buf" will be
1920 * NULL and "size" zero if the connection was closed for
1921 * writing in the meantime.
1923 * @param cls closure
1924 * @param size number of bytes available in buf
1925 * @param buf where the callee should write the message
1926 * @return number of bytes written to buf
1929 mesh_mq_ntr (void *cls, size_t size,
1932 struct GNUNET_MQ_Handle *mq = cls;
1933 struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
1934 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
1940 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
1943 msize = ntohs (msg->size);
1944 GNUNET_assert (msize <= size);
1945 memcpy (buf, msg, msize);
1946 GNUNET_MQ_impl_send_continue (mq);
1952 * Signature of functions implementing the
1953 * sending functionality of a message queue.
1955 * @param mq the message queue
1956 * @param msg the message to send
1957 * @param impl_state state of the implementation
1960 mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
1961 const struct GNUNET_MessageHeader *msg, void *impl_state)
1963 struct MeshMQState *state = impl_state;
1965 GNUNET_assert (NULL == state->th);
1967 GNUNET_MESH_notify_transmit_ready (state->channel,
1968 /* FIXME: add option for corking */
1970 GNUNET_TIME_UNIT_FOREVER_REL,
1978 * Signature of functions implementing the
1979 * destruction of a message queue.
1980 * Implementations must not free 'mq', but should
1981 * take care of 'impl_state'.
1983 * @param mq the message queue to destroy
1984 * @param impl_state state of the implementation
1987 mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
1989 struct MeshMQState *state = impl_state;
1991 if (NULL != state->th)
1992 GNUNET_MESH_notify_transmit_ready_cancel (state->th);
1994 GNUNET_free (state);
1999 * Create a message queue for a mesh channel.
2000 * The message queue can only be used to transmit messages,
2001 * not to receive them.
2003 * @param channel the channel to create the message qeue for
2004 * @return a message queue to messages over the channel
2006 struct GNUNET_MQ_Handle *
2007 GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel)
2009 struct GNUNET_MQ_Handle *mq;
2010 struct MeshMQState *state;
2012 state = GNUNET_new (struct MeshMQState);
2013 state->channel = channel;
2015 mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
2016 mesh_mq_destroy_impl,
2017 NULL, /* FIXME: cancel impl. */
2019 NULL, /* no msg handlers */
2020 NULL, /* no err handlers */
2021 NULL); /* no handler cls */