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 GNUNET_MESH_PeersCB peers_cb;
213 * Monitor callback closure.
220 GNUNET_MESH_TunnelsCB tunnels_cb;
223 * Monitor callback closure.
230 GNUNET_MESH_TunnelCB tunnel_cb;
233 * Tunnel callback closure.
240 * Description of a peer
242 struct GNUNET_MESH_Peer
245 * ID of the peer in short form
250 * Channel this peer belongs to
252 struct GNUNET_MESH_Channel *t;
257 * Opaque handle to a channel.
259 struct GNUNET_MESH_Channel
265 struct GNUNET_MESH_Channel *next;
270 struct GNUNET_MESH_Channel *prev;
273 * Handle to the mesh this channel belongs to
275 struct GNUNET_MESH_Handle *mesh;
278 * Local ID of the channel
280 MESH_ChannelNumber chid;
288 * Other end of the channel.
293 * Any data the caller wants to put in here
298 * Size of packet queued in this channel
300 unsigned int packet_size;
303 * Channel options: reliability, etc.
305 enum GNUNET_MESH_ChannelOption options;
308 * Are we allowed to send to the service?
316 * Implementation state for mesh's message queue.
321 * The current transmit handle, or NULL
322 * if no transmit is active.
324 struct GNUNET_MESH_TransmitHandle *th;
327 * Channel to send the data over.
329 struct GNUNET_MESH_Channel *channel;
333 /******************************************************************************/
334 /*********************** DECLARATIONS *************************/
335 /******************************************************************************/
338 * Function called to send a message to the service.
339 * "buf" will be NULL and "size" zero if the socket was closed for writing in
342 * @param cls closure, the mesh handle
343 * @param size number of bytes available in buf
344 * @param buf where the callee should write the connect message
345 * @return number of bytes written to buf
348 send_callback (void *cls, size_t size, void *buf);
351 /******************************************************************************/
352 /*********************** AUXILIARY FUNCTIONS *************************/
353 /******************************************************************************/
356 * Check if transmission is a payload packet.
358 * @param th Transmission handle.
360 * @return GNUNET_YES if it is a payload packet,
361 * GNUNET_NO if it is a mesh management packet.
364 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
366 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
371 * Check whether there is any message ready in the queue and find the size.
373 * @param h Mesh handle.
375 * @return The size of the first ready message in the queue,
376 * 0 if there is none.
379 message_ready_size (struct GNUNET_MESH_Handle *h)
381 struct GNUNET_MESH_TransmitHandle *th;
382 struct GNUNET_MESH_Channel *ch;
384 for (th = h->th_head; NULL != th; th = th->next)
387 if (GNUNET_NO == th_is_payload (th))
389 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
392 if (GNUNET_YES == ch->allow_send)
394 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok\n");
403 * Get the channel handler for the channel specified by id from the given handle
404 * @param h Mesh handle
405 * @param chid ID of the wanted channel
406 * @return handle to the required channel or NULL if not found
408 static struct GNUNET_MESH_Channel *
409 retrieve_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
411 struct GNUNET_MESH_Channel *ch;
413 ch = h->channels_head;
416 if (ch->chid == chid)
425 * Create a new channel and insert it in the channel list of the mesh handle
427 * @param h Mesh handle
428 * @param chid Desired chid of the channel, 0 to assign one automatically.
430 * @return Handle to the created channel.
432 static struct GNUNET_MESH_Channel *
433 create_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
435 struct GNUNET_MESH_Channel *ch;
437 ch = GNUNET_new (struct GNUNET_MESH_Channel);
438 GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
442 ch->chid = h->next_chid;
443 while (NULL != retrieve_channel (h, h->next_chid))
446 h->next_chid &= ~GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
447 h->next_chid |= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
454 ch->allow_send = GNUNET_NO;
460 * Destroy the specified channel.
461 * - Destroys all peers, calling the disconnect callback on each if needed
462 * - Cancels all outgoing traffic for that channel, calling respective notifys
463 * - Calls cleaner if channel was inbound
464 * - Frees all memory used
466 * @param ch Pointer to the channel.
467 * @param call_cleaner Whether to call the cleaner handler.
469 * @return Handle to the required channel or NULL if not found.
472 destroy_channel (struct GNUNET_MESH_Channel *ch, int call_cleaner)
474 struct GNUNET_MESH_Handle *h;
475 struct GNUNET_MESH_TransmitHandle *th;
476 struct GNUNET_MESH_TransmitHandle *next;
478 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
487 GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch);
489 /* signal channel destruction */
490 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
492 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cleaner\n");
493 h->cleaner (h->cls, ch, ch->ctx);
496 /* check that clients did not leave messages behind in the queue */
497 for (th = h->th_head; NULL != th; th = next)
500 if (th->channel != ch)
502 /* Clients should have aborted their requests already.
503 * Management traffic should be ok, as clients can't cancel that.
504 * If the service crashed and we are reconnecting, it's ok.
506 GNUNET_break (GNUNET_NO == th_is_payload (th)
507 || GNUNET_NO == h->in_receive);
508 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
510 /* clean up request */
511 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
512 GNUNET_SCHEDULER_cancel (th->timeout_task);
516 /* if there are no more pending requests with mesh service, cancel active request */
517 /* Note: this should be unnecessary... */
518 if ((0 == message_ready_size (h)) && (NULL != h->th))
520 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
525 GNUNET_PEER_change_rc (ch->peer, -1);
532 * Notify client that the transmission has timed out
535 * @param tc task context
538 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
540 struct GNUNET_MESH_TransmitHandle *th = cls;
541 struct GNUNET_MESH_Handle *mesh;
543 mesh = th->channel->mesh;
544 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
545 th->channel->packet_size = 0;
546 if (GNUNET_YES == th_is_payload (th))
547 th->notify (th->notify_cls, 0, NULL);
549 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
551 /* nothing ready to transmit, no point in asking for transmission */
552 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
559 * Add a transmit handle to the transmission queue and set the
562 * @param h mesh handle with the queue head and tail
563 * @param th handle to the packet to be transmitted
566 add_to_queue (struct GNUNET_MESH_Handle *h,
567 struct GNUNET_MESH_TransmitHandle *th)
569 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
570 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
573 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
574 (th->timeout), &timeout_transmission, th);
579 * Auxiliary function to send an already constructed packet to the service.
580 * Takes care of creating a new queue element, copying the message and
581 * calling the tmt_rdy function if necessary.
583 * @param h mesh handle
584 * @param msg message to transmit
585 * @param channel channel this send is related to (NULL if N/A)
588 send_packet (struct GNUNET_MESH_Handle *h,
589 const struct GNUNET_MessageHeader *msg,
590 struct GNUNET_MESH_Channel *channel);
594 * Send an ack on the channel to confirm the processing of a message.
596 * @param ch Channel on which to send the ACK.
599 send_ack (struct GNUNET_MESH_Channel *ch)
601 struct GNUNET_MESH_LocalAck msg;
603 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
604 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
605 msg.header.size = htons (sizeof (msg));
606 msg.channel_id = htonl (ch->chid);
608 send_packet (ch->mesh, &msg.header, ch);
615 * Reconnect callback: tries to reconnect again after a failer previous
617 * @param cls closure (mesh handle)
618 * @param tc task context
621 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
625 * Send a connect packet to the service with the applications and types
626 * requested by the user.
628 * @param h The mesh handle.
632 send_connect (struct GNUNET_MESH_Handle *h)
636 size = sizeof (struct GNUNET_MESH_ClientConnect);
637 size += h->n_ports * sizeof (uint32_t);
639 char buf[size] GNUNET_ALIGN;
640 struct GNUNET_MESH_ClientConnect *msg;
644 /* build connection packet */
645 msg = (struct GNUNET_MESH_ClientConnect *) buf;
646 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
647 msg->header.size = htons (size);
648 ports = (uint32_t *) &msg[1];
649 for (i = 0; i < h->n_ports; i++)
651 ports[i] = htonl (h->ports[i]);
652 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
655 LOG (GNUNET_ERROR_TYPE_DEBUG,
656 "Sending %lu bytes long message with %u ports\n",
657 ntohs (msg->header.size), h->n_ports);
658 send_packet (h, &msg->header, NULL);
664 * Reconnect to the service, retransmit all infomation to try to restore the
667 * @param h handle to the mesh
669 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
672 do_reconnect (struct GNUNET_MESH_Handle *h)
674 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
675 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
676 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
677 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
678 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
683 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
686 if (NULL != h->client)
688 GNUNET_CLIENT_disconnect (h->client);
692 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
693 if (h->client == NULL)
695 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
698 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
699 GNUNET_TIME_relative_multiply
700 (h->reconnect_time, 2));
701 LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
702 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
709 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
716 * Reconnect callback: tries to reconnect again after a failer previous
718 * @param cls closure (mesh handle)
719 * @param tc task context
722 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
724 struct GNUNET_MESH_Handle *h = cls;
726 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
727 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
734 * Reconnect to the service, retransmit all infomation to try to restore the
737 * @param h handle to the mesh
739 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
742 reconnect (struct GNUNET_MESH_Handle *h)
744 struct GNUNET_MESH_Channel *ch;
745 struct GNUNET_MESH_Channel *next;
747 LOG (GNUNET_ERROR_TYPE_DEBUG,
748 "Requested RECONNECT, destroying all channels\n");
749 h->in_receive = GNUNET_NO;
750 for (ch = h->channels_head; NULL != ch; ch = next)
753 destroy_channel (ch, GNUNET_YES);
755 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
756 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
761 /******************************************************************************/
762 /*********************** RECEIVE HANDLERS ****************************/
763 /******************************************************************************/
766 * Process the new channel notification and add it to the channels in the handle
768 * @param h The mesh handle
769 * @param msg A message with the details of the new incoming channel
772 process_channel_created (struct GNUNET_MESH_Handle *h,
773 const struct GNUNET_MESH_ChannelMessage *msg)
775 struct GNUNET_MESH_Channel *ch;
776 MESH_ChannelNumber chid;
779 chid = ntohl (msg->channel_id);
780 port = ntohl (msg->port);
781 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, port);
782 if (chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
787 if (NULL != h->new_channel)
791 ch = create_channel (h, chid);
792 ch->allow_send = GNUNET_NO;
793 ch->peer = GNUNET_PEER_intern (&msg->peer);
797 ch->options = ntohl (msg->opt);
799 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
800 ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options);
803 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
807 struct GNUNET_MESH_ChannelMessage d_msg;
809 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
811 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
812 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
813 d_msg.channel_id = msg->channel_id;
814 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
818 send_packet (h, &d_msg.header, NULL);
825 * Process the channel destroy notification and free associated resources
827 * @param h The mesh handle
828 * @param msg A message with the details of the channel being destroyed
831 process_channel_destroy (struct GNUNET_MESH_Handle *h,
832 const struct GNUNET_MESH_ChannelMessage *msg)
834 struct GNUNET_MESH_Channel *ch;
835 MESH_ChannelNumber chid;
837 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel Destroy received from service\n");
838 chid = ntohl (msg->channel_id);
839 ch = retrieve_channel (h, chid);
843 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
846 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroying channel %X\n", ch->chid);
847 destroy_channel (ch, GNUNET_YES);
852 * Process the incoming data packets, call appropriate handlers.
854 * @param h The mesh handle
855 * @param message A message encapsulating the data
858 process_incoming_data (struct GNUNET_MESH_Handle *h,
859 const struct GNUNET_MessageHeader *message)
861 const struct GNUNET_MessageHeader *payload;
862 const struct GNUNET_MESH_MessageHandler *handler;
863 struct GNUNET_MESH_LocalData *dmsg;
864 struct GNUNET_MESH_Channel *ch;
869 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
870 dmsg = (struct GNUNET_MESH_LocalData *) message;
871 ch = retrieve_channel (h, ntohl (dmsg->id));
872 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
873 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
874 GM_f2s (ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV),
875 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
877 size = ntohs (message->size);
878 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes\n", size);
882 /* Channel was ignored/destroyed, probably service didn't get it yet */
883 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
886 type = ntohs (payload->type);
887 size = ntohs (payload->size);
888 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GM_m2s (type));
889 for (i = 0; i < h->n_handlers; i++)
891 handler = &h->message_handlers[i];
892 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
894 if (handler->type == type)
897 handler->callback (h->cls, ch, &ch->ctx, payload))
899 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
900 GNUNET_MESH_channel_destroy (ch);
905 LOG (GNUNET_ERROR_TYPE_DEBUG,
906 "callback completed successfully\n");
915 * Process a local ACK message, enabling the client to send
916 * more data to the service.
918 * @param h Mesh handle.
919 * @param message Message itself.
922 process_ack (struct GNUNET_MESH_Handle *h,
923 const struct GNUNET_MessageHeader *message)
925 struct GNUNET_MESH_LocalAck *msg;
926 struct GNUNET_MESH_Channel *ch;
927 MESH_ChannelNumber chid;
929 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
930 msg = (struct GNUNET_MESH_LocalAck *) message;
931 chid = ntohl (msg->channel_id);
932 ch = retrieve_channel (h, chid);
935 LOG (GNUNET_ERROR_TYPE_DEBUG, "ACK on unknown channel %X\n", chid);
938 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X!\n", ch->chid);
939 ch->allow_send = GNUNET_YES;
940 if (NULL == h->th && 0 < ch->packet_size)
942 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
943 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
944 GNUNET_TIME_UNIT_FOREVER_REL,
945 GNUNET_YES, &send_callback, h);
951 * Process a local reply about info on all channels, pass info to the user.
953 * @param h Mesh handle.
954 * @param message Message itself.
957 // process_get_channels (struct GNUNET_MESH_Handle *h,
958 // const struct GNUNET_MessageHeader *message)
960 // struct GNUNET_MESH_LocalInfo *msg;
962 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
964 // if (NULL == h->channels_cb)
966 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
970 // msg = (struct GNUNET_MESH_LocalInfo *) message;
971 // if (ntohs (message->size) !=
972 // (sizeof (struct GNUNET_MESH_LocalInfo) +
973 // sizeof (struct GNUNET_PeerIdentity)))
975 // GNUNET_break_op (0);
976 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
977 // "Get channels message: size %hu - expected %u\n",
978 // ntohs (message->size),
979 // sizeof (struct GNUNET_MESH_LocalInfo));
982 // h->channels_cb (h->channels_cls,
983 // ntohl (msg->channel_id),
985 // &msg->destination);
991 * Process a local monitor_channel reply, pass info to the user.
993 * @param h Mesh handle.
994 * @param message Message itself.
997 // process_show_channel (struct GNUNET_MESH_Handle *h,
998 // const struct GNUNET_MessageHeader *message)
1000 // struct GNUNET_MESH_LocalInfo *msg;
1003 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
1005 // if (NULL == h->channel_cb)
1007 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1011 // /* Verify message sanity */
1012 // msg = (struct GNUNET_MESH_LocalInfo *) message;
1013 // esize = sizeof (struct GNUNET_MESH_LocalInfo);
1014 // if (ntohs (message->size) != esize)
1016 // GNUNET_break_op (0);
1017 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1018 // "Show channel message: size %hu - expected %u\n",
1019 // ntohs (message->size),
1022 // h->channel_cb (h->channel_cls, NULL, NULL);
1023 // h->channel_cb = NULL;
1024 // h->channel_cls = NULL;
1029 // h->channel_cb (h->channel_cls,
1030 // &msg->destination,
1037 * Process a local reply about info on all tunnels, pass info to the user.
1039 * @param h Mesh handle.
1040 * @param message Message itself.
1043 process_get_peers (struct GNUNET_MESH_Handle *h,
1044 const struct GNUNET_MessageHeader *message)
1046 struct GNUNET_MESH_LocalInfoPeer *msg;
1049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Peer messasge received\n");
1051 if (NULL == h->peers_cb)
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1057 size = ntohs (message->size);
1058 if (sizeof (struct GNUNET_MESH_LocalInfoPeer) > size)
1060 h->peers_cb (h->peers_cls, NULL, -1, 0, 0);
1062 h->peers_cls = NULL;
1066 msg = (struct GNUNET_MESH_LocalInfoPeer *) message;
1067 h->peers_cb (h->peers_cls, &msg->destination,
1068 (int) ntohs (msg->tunnel), (unsigned int ) ntohs (msg->paths),
1074 * Process a local reply about info on all tunnels, pass info to the user.
1076 * @param h Mesh handle.
1077 * @param message Message itself.
1080 process_get_tunnels (struct GNUNET_MESH_Handle *h,
1081 const struct GNUNET_MessageHeader *message)
1083 struct GNUNET_MESH_LocalInfoTunnel *msg;
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnels messasge received\n");
1088 if (NULL == h->tunnels_cb)
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1094 size = ntohs (message->size);
1095 if (sizeof (struct GNUNET_MESH_LocalInfoTunnel) > size)
1097 h->tunnels_cb (h->tunnel_cls, NULL, 0, 0, 0, 0);
1098 h->tunnels_cb = NULL;
1099 h->tunnels_cls = NULL;
1103 msg = (struct GNUNET_MESH_LocalInfoTunnel *) message;
1104 h->tunnels_cb (h->tunnel_cls,
1106 ntohl (msg->channels),
1107 ntohl (msg->connections),
1108 ntohs (msg->estate),
1109 ntohs (msg->cstate));
1116 * Process a local tunnel info reply, pass info to the user.
1118 * @param h Mesh handle.
1119 * @param message Message itself.
1122 process_get_tunnel (struct GNUNET_MESH_Handle *h,
1123 const struct GNUNET_MessageHeader *message)
1125 struct GNUNET_MESH_LocalInfoTunnel *msg;
1130 struct GNUNET_HashCode *conns;
1131 MESH_ChannelNumber *chns;
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
1134 if (NULL == h->tunnel_cb)
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1140 /* Verify message sanity */
1141 msg = (struct GNUNET_MESH_LocalInfoTunnel *) message;
1142 msize = ntohs (message->size);
1143 esize = sizeof (struct GNUNET_MESH_LocalInfoTunnel);
1146 GNUNET_break_op (0);
1147 h->tunnel_cb (h->tunnel_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1150 ch_n = ntohl (msg->channels);
1151 c_n = ntohl (msg->connections);
1152 esize += ch_n * sizeof (MESH_ChannelNumber);
1153 esize += c_n * sizeof (struct GNUNET_HashCode);
1156 GNUNET_break_op (0);
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
1158 msize, esize, ch_n, c_n);
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
1160 sizeof (struct GNUNET_MESH_LocalInfoTunnel),
1161 sizeof (MESH_ChannelNumber), sizeof (struct GNUNET_HashCode));
1162 h->tunnel_cb (h->tunnel_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1166 /* Call Callback with tunnel info. */
1167 conns = (struct GNUNET_HashCode *) &msg[1];
1168 chns = (MESH_ChannelNumber *) &conns[c_n];
1169 h->tunnel_cb (h->tunnel_cls, &msg->destination,
1170 ch_n, c_n, chns, conns,
1171 ntohs (msg->estate), ntohs (msg->cstate));
1174 h->tunnel_cb = NULL;
1175 h->tunnel_cls = NULL;
1179 * Function to process all messages received from the service
1181 * @param cls closure
1182 * @param msg message received, NULL on timeout or fatal error
1185 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1187 struct GNUNET_MESH_Handle *h = cls;
1192 LOG (GNUNET_ERROR_TYPE_DEBUG,
1193 "Mesh service disconnected, reconnecting\n", h);
1197 type = ntohs (msg->type);
1198 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1199 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1203 /* Notify of a new incoming channel */
1204 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1205 process_channel_created (h, (struct GNUNET_MESH_ChannelMessage *) msg);
1207 /* Notify of a channel disconnection */
1208 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
1209 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
1210 process_channel_destroy (h, (struct GNUNET_MESH_ChannelMessage *) msg);
1212 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
1213 process_incoming_data (h, msg);
1215 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1216 process_ack (h, msg);
1218 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
1219 // process_get_channels (h, msg);
1221 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
1222 // process_show_channel (h, msg);
1224 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS:
1225 process_get_peers (h, msg);
1227 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1228 process_get_tunnels (h, msg);
1230 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1231 process_get_tunnel (h, msg);
1233 // case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
1234 // process_show_channel (h, msg);
1237 /* We shouldn't get any other packages, log and ignore */
1238 LOG (GNUNET_ERROR_TYPE_WARNING,
1239 "unsolicited message form service (type %s)\n",
1240 GM_m2s (ntohs (msg->type)));
1242 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1243 if (GNUNET_YES == h->in_receive)
1245 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1246 GNUNET_TIME_UNIT_FOREVER_REL);
1250 LOG (GNUNET_ERROR_TYPE_DEBUG,
1251 "in receive off, not calling CLIENT_receive\n");
1256 /******************************************************************************/
1257 /************************ SEND FUNCTIONS ****************************/
1258 /******************************************************************************/
1261 * Function called to send a message to the service.
1262 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1265 * @param cls closure, the mesh handle
1266 * @param size number of bytes available in buf
1267 * @param buf where the callee should write the connect message
1268 * @return number of bytes written to buf
1271 send_callback (void *cls, size_t size, void *buf)
1273 struct GNUNET_MESH_Handle *h = cls;
1274 struct GNUNET_MESH_TransmitHandle *th;
1275 struct GNUNET_MESH_TransmitHandle *next;
1276 struct GNUNET_MESH_Channel *ch;
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1283 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
1284 if ((0 == size) || (NULL == buf))
1286 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1293 nsize = message_ready_size (h);
1294 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1297 if (GNUNET_YES == th_is_payload (th))
1299 struct GNUNET_MESH_LocalData *dmsg;
1300 struct GNUNET_MessageHeader *mh;
1302 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1303 if (GNUNET_NO == ch->allow_send)
1305 /* This channel is not ready to transmit yet, try next message */
1309 ch->packet_size = 0;
1310 GNUNET_assert (size >= th->size);
1311 dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
1312 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1313 psize = th->notify (th->notify_cls,
1314 size - sizeof (struct GNUNET_MESH_LocalData),
1318 psize += sizeof (struct GNUNET_MESH_LocalData);
1319 GNUNET_assert (size >= psize);
1320 dmsg->header.size = htons (psize);
1321 dmsg->id = htonl (ch->chid);
1322 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
1324 GM_m2s (ntohs (mh->type)));
1325 ch->allow_send = GNUNET_NO;
1329 LOG (GNUNET_ERROR_TYPE_DEBUG,
1330 "# callback returned size 0, "
1331 "application canceled transmission\n");
1336 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "# mesh internal traffic, type %s\n",
1339 GM_m2s (ntohs (mh->type)));
1340 memcpy (cbuf, &th[1], th->size);
1343 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1344 GNUNET_SCHEDULER_cancel (th->timeout_task);
1345 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1348 nsize = message_ready_size (h);
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1355 size = message_ready_size (h);
1358 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1360 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1361 GNUNET_TIME_UNIT_FOREVER_REL,
1362 GNUNET_YES, &send_callback, h);
1366 if (NULL != h->th_head)
1367 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1369 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1371 if (GNUNET_NO == h->in_receive)
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1374 h->in_receive = GNUNET_YES;
1375 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1376 GNUNET_TIME_UNIT_FOREVER_REL);
1378 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1384 * Auxiliary function to send an already constructed packet to the service.
1385 * Takes care of creating a new queue element, copying the message and
1386 * calling the tmt_rdy function if necessary.
1388 * @param h mesh handle
1389 * @param msg message to transmit
1390 * @param channel channel this send is related to (NULL if N/A)
1393 send_packet (struct GNUNET_MESH_Handle *h,
1394 const struct GNUNET_MessageHeader *msg,
1395 struct GNUNET_MESH_Channel *channel)
1397 struct GNUNET_MESH_TransmitHandle *th;
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1401 GM_m2s(ntohs(msg->type)));
1402 msize = ntohs (msg->size);
1403 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1404 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1406 th->channel = channel;
1407 memcpy (&th[1], msg, msize);
1408 add_to_queue (h, th);
1409 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1412 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1414 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1415 GNUNET_TIME_UNIT_FOREVER_REL,
1416 GNUNET_YES, &send_callback, h);
1420 /******************************************************************************/
1421 /********************** API CALL DEFINITIONS *************************/
1422 /******************************************************************************/
1424 struct GNUNET_MESH_Handle *
1425 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1426 GNUNET_MESH_InboundChannelNotificationHandler new_channel,
1427 GNUNET_MESH_ChannelEndHandler cleaner,
1428 const struct GNUNET_MESH_MessageHandler *handlers,
1429 const uint32_t *ports)
1431 struct GNUNET_MESH_Handle *h;
1433 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1434 h = GNUNET_new (struct GNUNET_MESH_Handle);
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1437 h->new_channel = new_channel;
1438 h->cleaner = cleaner;
1439 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1440 if (h->client == NULL)
1447 h->message_handlers = handlers;
1449 h->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1450 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1451 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1453 if (NULL != ports && ports[0] != 0 && NULL == new_channel)
1456 LOG (GNUNET_ERROR_TYPE_DEBUG,
1457 "no new channel handler given, ports parameter is useless!!\n");
1459 if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
1462 LOG (GNUNET_ERROR_TYPE_DEBUG,
1463 "no ports given, new channel handler will never be called!!\n");
1465 /* count handlers */
1466 for (h->n_handlers = 0;
1467 handlers && handlers[h->n_handlers].type;
1469 for (h->n_ports = 0;
1470 ports && ports[h->n_ports];
1473 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1479 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1481 struct GNUNET_MESH_Channel *ch;
1482 struct GNUNET_MESH_Channel *aux;
1483 struct GNUNET_MESH_TransmitHandle *th;
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1487 ch = handle->channels_head;
1491 if (ch->chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid);
1496 destroy_channel (ch, GNUNET_YES);
1499 while ( (th = handle->th_head) != NULL)
1501 struct GNUNET_MessageHeader *msg;
1503 /* Make sure it is an allowed packet (everything else should have been
1504 * already canceled).
1506 GNUNET_break (GNUNET_NO == th_is_payload (th));
1507 msg = (struct GNUNET_MessageHeader *) &th[1];
1508 switch (ntohs(msg->type))
1510 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1511 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1512 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1513 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
1514 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
1515 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEER:
1516 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS:
1517 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1518 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1522 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1526 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1530 if (NULL != handle->th)
1532 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1535 if (NULL != handle->client)
1537 GNUNET_CLIENT_disconnect (handle->client);
1538 handle->client = NULL;
1540 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1542 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1543 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1545 GNUNET_free (handle);
1550 * Create a new channel towards a remote peer.
1552 * If the destination port is not open by any peer or the destination peer
1553 * does not accept the channel, #GNUNET_MESH_ChannelEndHandler will be called
1556 * @param h mesh handle
1557 * @param channel_ctx client's channel context to associate with the channel
1558 * @param peer peer identity the channel should go to
1559 * @param port Port number.
1560 * @param options MeshOption flag field, with all desired option bits set to 1.
1562 * @return handle to the channel
1564 struct GNUNET_MESH_Channel *
1565 GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
1567 const struct GNUNET_PeerIdentity *peer,
1569 enum GNUNET_MESH_ChannelOption options)
1571 struct GNUNET_MESH_Channel *ch;
1572 struct GNUNET_MESH_ChannelMessage msg;
1574 LOG (GNUNET_ERROR_TYPE_DEBUG,
1575 "Creating new channel to %s:%u\n",
1576 GNUNET_i2s (peer), port);
1577 ch = create_channel (h, 0);
1578 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1579 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid);
1580 ch->ctx = channel_ctx;
1581 ch->peer = GNUNET_PEER_intern (peer);
1582 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1583 msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
1584 msg.channel_id = htonl (ch->chid);
1585 msg.port = htonl (port);
1587 msg.opt = htonl (options);
1589 send_packet (h, &msg.header, ch);
1595 GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel)
1597 struct GNUNET_MESH_Handle *h;
1598 struct GNUNET_MESH_ChannelMessage msg;
1599 struct GNUNET_MESH_TransmitHandle *th;
1601 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1604 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1605 msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
1606 msg.channel_id = htonl (channel->chid);
1607 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1613 struct GNUNET_MESH_TransmitHandle *aux;
1614 if (th->channel == channel)
1617 /* FIXME call the handler? */
1618 if (GNUNET_YES == th_is_payload (th))
1619 th->notify (th->notify_cls, 0, NULL);
1620 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1628 destroy_channel (channel, GNUNET_YES);
1629 send_packet (h, &msg.header, NULL);
1634 * Get information about a channel.
1636 * @param channel Channel handle.
1637 * @param option Query (GNUNET_MESH_OPTION_*).
1638 * @param ... dependant on option, currently not used
1640 * @return Union with an answer to the query.
1642 const union GNUNET_MESH_ChannelInfo *
1643 GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
1644 enum GNUNET_MESH_ChannelOption option, ...)
1646 static int bool_flag;
1647 const union GNUNET_MESH_ChannelInfo *ret;
1651 case GNUNET_MESH_OPTION_NOBUFFER:
1652 case GNUNET_MESH_OPTION_RELIABLE:
1653 case GNUNET_MESH_OPTION_OOORDER:
1654 if (0 != (option & channel->options))
1655 bool_flag = GNUNET_YES;
1657 bool_flag = GNUNET_NO;
1658 ret = (const union GNUNET_MESH_ChannelInfo *) &bool_flag;
1660 case GNUNET_MESH_OPTION_PEER:
1661 ret = (const union GNUNET_MESH_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1671 struct GNUNET_MESH_TransmitHandle *
1672 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel, int cork,
1673 struct GNUNET_TIME_Relative maxdelay,
1675 GNUNET_CONNECTION_TransmitReadyNotify notify,
1678 struct GNUNET_MESH_TransmitHandle *th;
1680 GNUNET_assert (NULL != channel);
1681 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
1682 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1683 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1684 if (channel->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
1685 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1687 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1688 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1689 GNUNET_assert (NULL != notify);
1690 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1691 th = GNUNET_new (struct GNUNET_MESH_TransmitHandle);
1692 th->channel = channel;
1693 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1694 th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
1695 channel->packet_size = th->size;
1696 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1697 th->notify = notify;
1698 th->notify_cls = notify_cls;
1699 add_to_queue (channel->mesh, th);
1700 if (NULL != channel->mesh->th)
1702 if (GNUNET_NO == channel->allow_send)
1704 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1706 GNUNET_CLIENT_notify_transmit_ready (channel->mesh->client, th->size,
1707 GNUNET_TIME_UNIT_FOREVER_REL,
1708 GNUNET_YES, &send_callback,
1710 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
1716 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
1718 struct GNUNET_MESH_Handle *mesh;
1720 th->channel->packet_size = 0;
1721 mesh = th->channel->mesh;
1722 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1723 GNUNET_SCHEDULER_cancel (th->timeout_task);
1724 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
1726 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
1728 /* queue empty, no point in asking for transmission */
1729 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
1736 GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel)
1743 send_info_request (struct GNUNET_MESH_Handle *h, uint16_t type)
1745 struct GNUNET_MessageHeader msg;
1747 msg.size = htons (sizeof (msg));
1748 msg.type = htons (type);
1749 send_packet (h, &msg, NULL);
1753 * Request information about the running mesh peer.
1754 * The callback will be called for every channel known to the service,
1755 * listing all active peers that blong to the channel.
1757 * If called again on the same handle, it will overwrite the previous
1758 * callback and cls. To retrieve the cls, monitor_cancel must be
1761 * WARNING: unstable API, likely to change in the future!
1763 * @param h Handle to the mesh peer.
1764 * @param callback Function to call with the requested data.
1765 * @param callback_cls Closure for @c callback.
1768 GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
1769 GNUNET_MESH_ChannelsCB callback,
1772 send_info_request (h, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS);
1773 h->channels_cb = callback;
1774 h->channels_cls = callback_cls;
1779 * Cancel a monitor request. The monitor callback will not be called.
1781 * WARNING: unstable API, likely to change in the future!
1783 * @param h Mesh handle.
1785 * @return Closure given to GNUNET_MESH_monitor, if any.
1788 GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h)
1792 cls = h->channels_cls;
1793 h->channels_cb = NULL;
1794 h->channels_cls = NULL;
1800 * Request information about the running mesh peer.
1801 * The callback will be called for every peer known to the service.
1803 * If called again on the same handle, it will overwrite the previous
1804 * callback and cls. To retrieve the cls, monitor_cancel must be
1807 * WARNING: unstable API, likely to change in the future!
1809 * @param h Handle to the mesh peer.
1810 * @param callback Function to call with the requested data.
1811 * @param callback_cls Closure for @c callback.
1814 GNUNET_MESH_get_peers (struct GNUNET_MESH_Handle *h,
1815 GNUNET_MESH_PeersCB callback,
1818 send_info_request (h, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS);
1819 h->peers_cb = callback;
1820 h->peers_cls = callback_cls;
1826 * Request information about the running mesh peer.
1827 * The callback will be called for every tunnel known to the service.
1829 * If called again on the same handle, it will overwrite the previous
1830 * callback and cls. To retrieve the cls, monitor_cancel must be
1833 * WARNING: unstable API, likely to change in the future!
1835 * @param h Handle to the mesh peer.
1836 * @param callback Function to call with the requested data.
1837 * @param callback_cls Closure for @c callback.
1840 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
1841 GNUNET_MESH_TunnelsCB callback,
1844 send_info_request (h, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
1845 h->tunnels_cb = callback;
1846 h->tunnels_cls = callback_cls;
1851 * Cancel a monitor request. The monitor callback will not be called.
1853 * @param h Mesh handle.
1855 * @return Closure given to GNUNET_MESH_monitor, if any.
1858 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
1862 h->tunnels_cb = NULL;
1863 cls = h->tunnels_cls;
1864 h->tunnels_cls = NULL;
1872 * Request information about the running mesh peer.
1873 * The callback will be called for every channel known to the service,
1874 * listing all active peers that blong to the channel.
1876 * If called again on the same handle, it will overwrite the previous
1877 * callback and cls. To retrieve the cls, monitor_cancel must be
1880 * WARNING: unstable API, likely to change in the future!
1882 * @param h Handle to the mesh peer.
1883 * @param callback Function to call with the requested data.
1884 * @param callback_cls Closure for @c callback.
1887 GNUNET_MESH_get_tunnel (struct GNUNET_MESH_Handle *h,
1888 const struct GNUNET_PeerIdentity *id,
1889 GNUNET_MESH_TunnelCB callback,
1892 struct GNUNET_MESH_LocalInfo msg;
1894 memset (&msg, 0, sizeof (msg));
1895 msg.header.size = htons (sizeof (msg));
1896 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
1898 send_packet (h, &msg.header, NULL);
1899 h->tunnel_cb = callback;
1900 h->tunnel_cls = callback_cls;
1905 * Request information about a specific channel of the running mesh peer.
1907 * WARNING: unstable API, likely to change in the future!
1908 * FIXME Add destination option.
1910 * @param h Handle to the mesh peer.
1911 * @param initiator ID of the owner of the channel.
1912 * @param channel_number Channel number.
1913 * @param callback Function to call with the requested data.
1914 * @param callback_cls Closure for @c callback.
1917 GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
1918 struct GNUNET_PeerIdentity *initiator,
1919 unsigned int channel_number,
1920 GNUNET_MESH_ChannelCB callback,
1923 struct GNUNET_MESH_LocalInfo msg;
1925 msg.header.size = htons (sizeof (msg));
1926 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL);
1927 msg.peer = *initiator;
1928 msg.channel_id = htonl (channel_number);
1929 // msg.reserved = 0;
1930 send_packet (h, &msg.header, NULL);
1931 h->channel_cb = callback;
1932 h->channel_cls = callback_cls;
1937 * Function called to notify a client about the connection
1938 * begin ready to queue more data. "buf" will be
1939 * NULL and "size" zero if the connection was closed for
1940 * writing in the meantime.
1942 * @param cls closure
1943 * @param size number of bytes available in buf
1944 * @param buf where the callee should write the message
1945 * @return number of bytes written to buf
1948 mesh_mq_ntr (void *cls, size_t size,
1951 struct GNUNET_MQ_Handle *mq = cls;
1952 struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
1953 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
1959 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
1962 msize = ntohs (msg->size);
1963 GNUNET_assert (msize <= size);
1964 memcpy (buf, msg, msize);
1965 GNUNET_MQ_impl_send_continue (mq);
1971 * Signature of functions implementing the
1972 * sending functionality of a message queue.
1974 * @param mq the message queue
1975 * @param msg the message to send
1976 * @param impl_state state of the implementation
1979 mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
1980 const struct GNUNET_MessageHeader *msg, void *impl_state)
1982 struct MeshMQState *state = impl_state;
1984 GNUNET_assert (NULL == state->th);
1986 GNUNET_MESH_notify_transmit_ready (state->channel,
1987 /* FIXME: add option for corking */
1989 GNUNET_TIME_UNIT_FOREVER_REL,
1997 * Signature of functions implementing the
1998 * destruction of a message queue.
1999 * Implementations must not free 'mq', but should
2000 * take care of 'impl_state'.
2002 * @param mq the message queue to destroy
2003 * @param impl_state state of the implementation
2006 mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
2008 struct MeshMQState *state = impl_state;
2010 if (NULL != state->th)
2011 GNUNET_MESH_notify_transmit_ready_cancel (state->th);
2013 GNUNET_free (state);
2018 * Create a message queue for a mesh channel.
2019 * The message queue can only be used to transmit messages,
2020 * not to receive them.
2022 * @param channel the channel to create the message qeue for
2023 * @return a message queue to messages over the channel
2025 struct GNUNET_MQ_Handle *
2026 GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel)
2028 struct GNUNET_MQ_Handle *mq;
2029 struct MeshMQState *state;
2031 state = GNUNET_new (struct MeshMQState);
2032 state->channel = channel;
2034 mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
2035 mesh_mq_destroy_impl,
2036 NULL, /* FIXME: cancel impl. */
2038 NULL, /* no msg handlers */
2039 NULL, /* no err handlers */
2040 NULL); /* no handler cls */