/*
This file is part of GNUnet.
- (C) 2011 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2011 GNUnet e.V.
+
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
+
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
+
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
-
/**
* @file cadet/cadet_api.c
* @brief cadet api: client implementation of new cadet service
#include "platform.h"
#include "gnunet_util_lib.h"
+#include "gnunet_constants.h"
#include "gnunet_cadet_service.h"
#include "cadet.h"
#include "cadet_protocol.h"
*/
struct GNUNET_CADET_TransmitHandle
{
-
/**
* Double Linked list
*/
*/
struct GNUNET_CADET_Channel *channel;
+ /**
+ * Request data task.
+ */
+ struct GNUNET_SCHEDULER_Task *request_data_task;
+
/**
* Callback to obtain the message to transmit, or NULL if we
* got the message in 'data'. Notice that messages built
void *notify_cls;
/**
- * How long is this message valid. Once the timeout has been
- * reached, the message must no longer be sent. If this
- * is a message with a 'notify' callback set, the 'notify'
- * function should be called with 'buf' NULL and size 0.
- */
- struct GNUNET_TIME_Absolute timeout;
-
- /**
- * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
- */
- struct GNUNET_SCHEDULER_Task * timeout_task;
-
- /**
- * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
+ * Size of the payload.
*/
size_t size;
};
*/
struct GNUNET_CADET_Handle
{
-
/**
- * Handle to the server connection, to send messages later
+ * Message queue (if available).
*/
- struct GNUNET_CLIENT_Connection *client;
+ struct GNUNET_MQ_Handle *mq;
/**
* Set of handlers used for processing incoming messages in the channels
/**
* Ports open.
*/
- const uint32_t *ports;
-
- /**
- * Number of ports.
- */
- unsigned int n_ports;
+ struct GNUNET_CONTAINER_MultiHashMap *ports;
/**
* Double linked list of the channels this client is connected to, head.
*/
struct GNUNET_CADET_Channel *channels_tail;
- /**
- * Callback for inbound channel creation
- */
- GNUNET_CADET_InboundChannelNotificationHandler *new_channel;
-
/**
* Callback for inbound channel disconnection
*/
GNUNET_CADET_ChannelEndHandler *cleaner;
- /**
- * Handle to cancel pending transmissions in case of disconnection
- */
- struct GNUNET_CLIENT_TransmitHandle *th;
-
/**
* Closure for all the handlers given by the client
*/
*/
CADET_ChannelNumber next_chid;
- /**
- * Have we started the task to receive messages from the service
- * yet? We do this after we send the 'CADET_LOCAL_CONNECT' message.
- */
- int in_receive;
-
/**
* Configuration given by the client, in case of reconnection
*/
*/
struct GNUNET_CADET_Channel
{
-
/**
* DLL next
*/
CADET_ChannelNumber chid;
/**
- * Port number.
+ * Channel's port, if any.
*/
- uint32_t port;
+ struct GNUNET_CADET_Port *port;
/**
* Other end of the channel.
};
+/**
+ * Opaque handle to a port.
+ */
+struct GNUNET_CADET_Port
+{
+ /**
+ * Handle to the CADET session this port belongs to.
+ */
+ struct GNUNET_CADET_Handle *cadet;
+
+ /**
+ * Port ID.
+ */
+ struct GNUNET_HashCode *hash;
+
+ /**
+ * Callback handler for incoming channels on this port.
+ */
+ GNUNET_CADET_InboundChannelNotificationHandler *handler;
+
+ /**
+ * Closure for @a handler.
+ */
+ void *cls;
+};
+
/**
* Implementation state for cadet's message queue.
};
-/******************************************************************************/
-/*********************** DECLARATIONS *************************/
-/******************************************************************************/
-
-/**
- * Function called to send a message to the service.
- * "buf" will be NULL and "size" zero if the socket was closed for writing in
- * the meantime.
- *
- * @param cls closure, the cadet handle
- * @param size number of bytes available in buf
- * @param buf where the callee should write the connect message
- * @return number of bytes written to buf
- */
-static size_t
-send_callback (void *cls, size_t size, void *buf);
-
-
/******************************************************************************/
/*********************** AUXILIARY FUNCTIONS *************************/
/******************************************************************************/
/**
- * Check whether there is any message ready in the queue and find the size.
+ * Find the Port struct for a hash.
*
- * @param h Cadet handle.
+ * @param h CADET handle.
+ * @param hash HashCode for the port number.
*
- * @return The size of the first ready message in the queue,
- * 0 if there is none.
+ * @return The port handle if known, NULL otherwise.
*/
-static size_t
-message_ready_size (struct GNUNET_CADET_Handle *h)
+static struct GNUNET_CADET_Port *
+find_port (const struct GNUNET_CADET_Handle *h,
+ const struct GNUNET_HashCode *hash)
{
- struct GNUNET_CADET_TransmitHandle *th;
- struct GNUNET_CADET_Channel *ch;
+ struct GNUNET_CADET_Port *p;
- for (th = h->th_head; NULL != th; th = th->next)
- {
- ch = th->channel;
- if (GNUNET_NO == th_is_payload (th))
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
- return th->size;
- }
- if (GNUNET_YES == ch->allow_send)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok\n");
- return th->size;
- }
- }
- return 0;
+ p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
+
+ return p;
}
*
* @return Handle to the required channel or NULL if not found.
*/
+// FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
static void
destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
{
* Management traffic should be ok, as clients can't cancel that.
* If the service crashed and we are reconnecting, it's ok.
*/
- GNUNET_break (GNUNET_NO == th_is_payload (th)
- || GNUNET_NO == h->in_receive);
- GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
-
- /* clean up request */
- if (NULL != th->timeout_task)
- GNUNET_SCHEDULER_cancel (th->timeout_task);
- GNUNET_free (th);
- }
-
- /* if there are no more pending requests with cadet service, cancel active request */
- /* Note: this should be unnecessary... */
- if ((0 == message_ready_size (h)) && (NULL != h->th))
- {
- GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
- h->th = NULL;
+ GNUNET_break (GNUNET_NO == th_is_payload (th));
+ GNUNET_CADET_notify_transmit_ready_cancel (th);
}
if (0 != ch->peer)
}
-/**
- * Notify client that the transmission has timed out
- *
- * @param cls closure
- * @param tc task context
- */
-static void
-timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
- struct GNUNET_CADET_TransmitHandle *th = cls;
- struct GNUNET_CADET_Handle *cadet = th->channel->cadet;
-
- th->timeout_task = NULL;
- th->channel->packet_size = 0;
- GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
- if (GNUNET_YES == th_is_payload (th))
- GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
- GNUNET_free (th);
- if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
- {
- /* nothing ready to transmit, no point in asking for transmission */
- GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
- cadet->th = NULL;
- }
-}
-
-
/**
* Add a transmit handle to the transmission queue and set the
* timeout if needed.
struct GNUNET_CADET_TransmitHandle *th)
{
GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
- if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
- return;
- th->timeout_task =
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
- (th->timeout), &timeout_transmission, th);
}
/**
- * Auxiliary function to send an already constructed packet to the service.
- * Takes care of creating a new queue element, copying the message and
- * calling the tmt_rdy function if necessary.
+ * Remove a transmit handle from the transmission queue, if present.
*
- * @param h cadet handle
- * @param msg message to transmit
- * @param channel channel this send is related to (NULL if N/A)
+ * Safe to call even if not queued.
+ *
+ * @param th handle to the packet to be unqueued.
*/
static void
-send_packet (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_MessageHeader *msg,
- struct GNUNET_CADET_Channel *channel);
+remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
+{
+ struct GNUNET_CADET_Handle *h = th->channel->cadet;
+
+ /* It might or might not have been queued (rarely not), but check anyway. */
+ if (NULL != th->next || h->th_tail == th)
+ {
+ GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
+ }
+}
/**
static void
send_ack (struct GNUNET_CADET_Channel *ch)
{
- struct GNUNET_CADET_LocalAck msg;
+ struct GNUNET_CADET_LocalAck *msg;
+ struct GNUNET_MQ_Envelope *env;
+
+ env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
- msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
- msg.header.size = htons (sizeof (msg));
- msg.channel_id = htonl (ch->chid);
+ msg->channel_id = htonl (ch->chid);
+ GNUNET_MQ_send (ch->cadet->mq, env);
- send_packet (ch->cadet, &msg.header, ch);
return;
}
-/**
- * Reconnect callback: tries to reconnect again after a failer previous
- * reconnecttion
- * @param cls closure (cadet handle)
- * @param tc task context
- */
-static void
-reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+/******************************************************************************/
+/*********************** RECEIVE HANDLERS ****************************/
+/******************************************************************************/
/**
- * Send a connect packet to the service with the applications and types
- * requested by the user.
- *
- * @param h The cadet handle.
+ * Call the @a notify callback given to #GNUNET_CADET_notify_transmit_ready to
+ * request the data to send over MQ. Since MQ manages the queue, this function
+ * is scheduled immediatly after a transmit ready notification.
*
+ * @param cls Closure (transmit handle).
*/
static void
-send_connect (struct GNUNET_CADET_Handle *h)
+request_data (void *cls)
{
- size_t size;
-
- size = sizeof (struct GNUNET_CADET_ClientConnect);
- size += h->n_ports * sizeof (uint32_t);
- {
- char buf[size] GNUNET_ALIGN;
- struct GNUNET_CADET_ClientConnect *msg;
- uint32_t *ports;
- uint16_t i;
-
- /* build connection packet */
- msg = (struct GNUNET_CADET_ClientConnect *) buf;
- msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT);
- msg->header.size = htons (size);
- ports = (uint32_t *) &msg[1];
- for (i = 0; i < h->n_ports; i++)
- {
- ports[i] = htonl (h->ports[i]);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
- h->ports[i]);
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Sending %lu bytes long message with %u ports\n",
- ntohs (msg->header.size), h->n_ports);
- send_packet (h, &msg->header, NULL);
- }
-}
-
-
-/**
- * Reconnect to the service, retransmit all infomation to try to restore the
- * original state.
- *
- * @param h handle to the cadet
- *
- * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
- */
-static int
-do_reconnect (struct GNUNET_CADET_Handle *h)
-{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
- LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
- LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
- LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
- LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
-
- /* disconnect */
- if (NULL != h->th)
- {
- GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
- h->th = NULL;
- }
- if (NULL != h->client)
- {
- GNUNET_CLIENT_disconnect (h->client);
- }
-
- /* connect again */
- h->client = GNUNET_CLIENT_connect ("cadet", h->cfg);
- if (h->client == NULL)
- {
- h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
- &reconnect_cbk, h);
- h->reconnect_time =
- GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
- GNUNET_TIME_relative_multiply
- (h->reconnect_time, 2));
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
- GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
- GNUNET_NO));
- GNUNET_break (0);
- return GNUNET_NO;
- }
- else
- {
- h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
- }
- send_connect (h);
- return GNUNET_YES;
-}
-
-/**
- * Reconnect callback: tries to reconnect again after a failer previous
- * reconnecttion
- * @param cls closure (cadet handle)
- * @param tc task context
- */
-static void
-reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
- struct GNUNET_CADET_Handle *h = cls;
+ struct GNUNET_CADET_TransmitHandle *th = cls;
+ struct GNUNET_CADET_LocalData *msg;
+ struct GNUNET_MQ_Envelope *env;
+ size_t osize;
- h->reconnect_task = NULL;
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
- do_reconnect (h);
-}
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting Data: %u bytes\n", th->size);
+ GNUNET_assert (GNUNET_YES == th->channel->allow_send);
+ th->channel->allow_send = GNUNET_NO;
+ th->request_data_task = NULL;
+ th->channel->packet_size = 0;
+ remove_from_queue (th);
-/**
- * Reconnect to the service, retransmit all infomation to try to restore the
- * original state.
- *
- * @param h handle to the cadet
- *
- * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
- */
-static void
-reconnect (struct GNUNET_CADET_Handle *h)
-{
- struct GNUNET_CADET_Channel *ch;
- struct GNUNET_CADET_Channel *next;
+ env = GNUNET_MQ_msg_extra (msg, th->size,
+ GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
+ msg->id = htonl (th->channel->chid);
+ osize = th->notify (th->notify_cls, th->size, &msg[1]);
+ GNUNET_assert (osize == th->size);
+ GNUNET_MQ_send (th->channel->cadet->mq, env);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Requested RECONNECT, destroying all channels\n");
- h->in_receive = GNUNET_NO;
- for (ch = h->channels_head; NULL != ch; ch = next)
- {
- next = ch->next;
- destroy_channel (ch, GNUNET_YES);
- }
- if (NULL == h->reconnect_task)
- h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
- &reconnect_cbk, h);
+ GNUNET_free (th);
}
-/******************************************************************************/
-/*********************** RECEIVE HANDLERS ****************************/
-/******************************************************************************/
-
/**
* Process the new channel notification and add it to the channels in the handle
*
* @param msg A message with the details of the new incoming channel
*/
static void
-process_channel_created (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_CADET_ChannelMessage *msg)
+handle_channel_created (void *cls,
+ const struct GNUNET_CADET_ChannelCreateMessage *msg)
{
+ struct GNUNET_CADET_Handle *h = cls;
struct GNUNET_CADET_Channel *ch;
+ struct GNUNET_CADET_Port *port;
+ const struct GNUNET_HashCode *port_number;
CADET_ChannelNumber chid;
- uint32_t port;
chid = ntohl (msg->channel_id);
- port = ntohl (msg->port);
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, port);
+ port_number = &msg->port;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X [%s]\n",
+ chid, GNUNET_h2s (port_number));
if (chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
{
GNUNET_break (0);
return;
}
- if (NULL != h->new_channel)
+ port = find_port (h, port_number);
+ if (NULL != port)
{
void *ctx;
ch->options = ntohl (msg->opt);
LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
- ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options);
+ ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
if (NULL != ctx)
ch->ctx = ctx;
LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
}
else
{
- struct GNUNET_CADET_ChannelMessage d_msg;
+ struct GNUNET_CADET_ChannelDestroyMessage *d_msg;
+ struct GNUNET_MQ_Envelope *env;
LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
-
- d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
- d_msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
- d_msg.channel_id = msg->channel_id;
- memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
- d_msg.port = 0;
- d_msg.opt = 0;
-
- send_packet (h, &d_msg.header, NULL);
+ env = GNUNET_MQ_msg (d_msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
+ d_msg->channel_id = msg->channel_id;
+ GNUNET_MQ_send (h->mq, env);
}
return;
}
* @param msg A message with the details of the channel being destroyed
*/
static void
-process_channel_destroy (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_CADET_ChannelMessage *msg)
+handle_channel_destroy (void *cls,
+ const struct GNUNET_CADET_ChannelDestroyMessage *msg)
{
+ struct GNUNET_CADET_Handle *h = cls;
struct GNUNET_CADET_Channel *ch;
CADET_ChannelNumber chid;
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel Destroy received from service\n");
chid = ntohl (msg->channel_id);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X Destroy from service\n", chid);
ch = retrieve_channel (h, chid);
if (NULL == ch)
LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
return;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, " destroying channel %X\n", ch->chid);
destroy_channel (ch, GNUNET_YES);
}
+/**
+ * Check that message received from CADET service is well-formed.
+ *
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param message the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ * #GNUNET_SYSERR otherwise
+ */
+static int
+check_local_data (void *cls,
+ const struct GNUNET_CADET_LocalData *message)
+{
+ struct GNUNET_CADET_Handle *h = cls;
+ struct GNUNET_CADET_Channel *ch;
+ uint16_t size;
+
+ size = ntohs (message->header.size);
+ if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+
+ ch = retrieve_channel (h, ntohl (message->id));
+ if (NULL == ch)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+
+ return GNUNET_OK;
+}
+
+
/**
* Process the incoming data packets, call appropriate handlers.
*
- * @param h The cadet handle
- * @param message A message encapsulating the data
+ * @param h The cadet handle
+ * @param message A message encapsulating the data
*/
static void
-process_incoming_data (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_MessageHeader *message)
+handle_local_data (void *cls,
+ const struct GNUNET_CADET_LocalData *message)
{
+ struct GNUNET_CADET_Handle *h = cls;
const struct GNUNET_MessageHeader *payload;
const struct GNUNET_CADET_MessageHandler *handler;
- struct GNUNET_CADET_LocalData *dmsg;
struct GNUNET_CADET_Channel *ch;
- size_t size;
unsigned int i;
uint16_t type;
LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
- dmsg = (struct GNUNET_CADET_LocalData *) message;
- ch = retrieve_channel (h, ntohl (dmsg->id));
- if (NULL == ch)
- {
- GNUNET_break (0);
- return;
- }
+ ch = retrieve_channel (h, ntohl (message->id));
+ GNUNET_assert (NULL != ch);
- payload = (struct GNUNET_MessageHeader *) &dmsg[1];
+ payload = (struct GNUNET_MessageHeader *) &message[1];
LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
GC_f2s (ch->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV),
- GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
-
- size = ntohs (message->size);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes\n", size);
+ GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (message->id));
type = ntohs (payload->type);
- size = ntohs (payload->size);
LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
for (i = 0; i < h->n_handlers; i++)
{
{
LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
GNUNET_CADET_channel_destroy (ch);
- return;
+ break;
}
else
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"callback completed successfully\n");
- return;
+ break;
}
}
}
* @param message Message itself.
*/
static void
-process_ack (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_MessageHeader *message)
+handle_local_ack (void *cls,
+ const struct GNUNET_CADET_LocalAck *message)
{
- struct GNUNET_CADET_LocalAck *msg;
+ struct GNUNET_CADET_Handle *h = cls;
struct GNUNET_CADET_Channel *ch;
CADET_ChannelNumber chid;
LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
- msg = (struct GNUNET_CADET_LocalAck *) message;
- chid = ntohl (msg->channel_id);
+ chid = ntohl (message->channel_id);
ch = retrieve_channel (h, chid);
if (NULL == ch)
{
}
LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X!\n", ch->chid);
ch->allow_send = GNUNET_YES;
- if (NULL == h->th && 0 < ch->packet_size)
+ if (0 < ch->packet_size)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
- h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_YES, &send_callback, h);
+ struct GNUNET_CADET_TransmitHandle *th;
+ struct GNUNET_CADET_TransmitHandle *next;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ " pending data, sending %u bytes!\n",
+ ch->packet_size);
+ for (th = h->th_head; NULL != th; th = next)
+ {
+ next = th->next;
+ if (th->channel == ch)
+ {
+ GNUNET_assert (NULL == th->request_data_task);
+ th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
+ break;
+ }
+ }
+ /* Complain if we got thru all th without sending anything, ch was wrong */
+ GNUNET_break (NULL != th);
}
}
+/**
+ * Reconnect to the service, retransmit all infomation to try to restore the
+ * original state.
+ *
+ * @param h handle to the cadet
+ *
+ * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
+ */
+static void
+reconnect (struct GNUNET_CADET_Handle *h);
+
+
+/**
+ * Reconnect callback: tries to reconnect again after a failer previous
+ * reconnection.
+ *
+ * @param cls closure (cadet handle)
+ */
+static void
+reconnect_cbk (void *cls);
+
+
+/**
+ * Generic error handler, called with the appropriate error code and
+ * the same closure specified at the creation of the message queue.
+ * Not every message queue implementation supports an error handler.
+ *
+ * @param cls closure, a `struct GNUNET_CORE_Handle *`
+ * @param error error code
+ */
+static void
+handle_mq_error (void *cls,
+ enum GNUNET_MQ_Error error)
+{
+ struct GNUNET_CADET_Handle *h = cls;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
+ GNUNET_MQ_destroy (h->mq);
+ h->mq = NULL;
+ reconnect (h);
+}
+
/*
* Process a local reply about info on all channels, pass info to the user.
/**
- * Process a local reply about info on all tunnels, pass info to the user.
+ * Check that message received from CADET service is well-formed.
*
- * @param h Cadet handle.
- * @param message Message itself.
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param message the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ * #GNUNET_SYSERR otherwise
*/
-static void
-process_get_peers (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_MessageHeader *message)
+static int
+check_get_peers (void *cls,
+ const struct GNUNET_CADET_LocalInfoPeer *message)
{
- struct GNUNET_CADET_LocalInfoPeer *msg;
+ struct GNUNET_CADET_Handle *h = cls;
uint16_t size;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Peer messasge received\n");
-
if (NULL == h->info_cb.peers_cb)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
- return;
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ " no handler for peesr monitor message!\n");
+ return GNUNET_SYSERR;
}
- size = ntohs (message->size);
+ size = ntohs (message->header.size);
if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
{
h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
h->info_cb.peers_cb = NULL;
h->info_cls = NULL;
- return;
+ return GNUNET_SYSERR;
}
- msg = (struct GNUNET_CADET_LocalInfoPeer *) message;
+ return GNUNET_OK;
+}
+
+
+/**
+ * Process a local reply about info on all tunnels, pass info to the user.
+ *
+ * @param cls Closure (Cadet handle).
+ * @param msg Message itself.
+ */
+static void
+handle_get_peers (void *cls,
+ const struct GNUNET_CADET_LocalInfoPeer *msg)
+{
+ struct GNUNET_CADET_Handle *h = cls;
h->info_cb.peers_cb (h->info_cls, &msg->destination,
(int) ntohs (msg->tunnel),
(unsigned int ) ntohs (msg->paths),
/**
- * Process a local peer info reply, pass info to the user.
+ * Check that message received from CADET service is well-formed.
*
- * @param h Cadet handle.
- * @param message Message itself.
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param message the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ * #GNUNET_SYSERR otherwise
*/
-static void
-process_get_peer (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_MessageHeader *message)
+static int
+check_get_peer (void *cls,
+ const struct GNUNET_CADET_LocalInfoPeer *message)
{
- struct GNUNET_CADET_LocalInfoTunnel *msg;
+ struct GNUNET_CADET_Handle *h = cls;
+ const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
+ struct GNUNET_PeerIdentity *paths_array;
size_t esize;
- size_t msize;
+ unsigned int epaths;
+ unsigned int paths;
+ unsigned int peers;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
if (NULL == h->info_cb.peer_cb)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
- return;
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ " no handler for peer monitor message!\n");
+ goto clean_cls;
}
/* Verify message sanity */
- msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
- msize = ntohs (message->size);
- esize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
- if (esize > msize)
+ esize = ntohs (message->header.size);
+ if (esize < msize)
{
GNUNET_break_op (0);
h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
goto clean_cls;
}
-// esize += ch_n * sizeof (CADET_ChannelNumber);
-// esize += c_n * sizeof (struct GNUNET_CADET_Hash);
- if (msize != esize)
+ if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
+ {
+ GNUNET_break_op (0);
+ h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
+ goto clean_cls;
+
+ }
+ peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
+ epaths = (unsigned int) ntohs (message->paths);
+ paths_array = (struct GNUNET_PeerIdentity *) &message[1];
+ paths = 0;
+ for (int i = 0; i < peers; i++)
+ {
+ if (0 == memcmp (&paths_array[i], &message->destination,
+ sizeof (struct GNUNET_PeerIdentity)))
+ {
+ paths++;
+ }
+ }
+ if (paths != epaths)
{
GNUNET_break_op (0);
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "m:%u, e: %u\n", msize, esize);
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
goto clean_cls;
}
- /* Call Callback with tunnel info. */
- h->info_cb.peer_cb (h->info_cls, &msg->destination, 0, 0, 0, NULL);
+ return GNUNET_OK;
- clean_cls:
+clean_cls:
h->info_cb.peer_cb = NULL;
h->info_cls = NULL;
+ return GNUNET_SYSERR;
}
/**
- * Process a local reply about info on all tunnels, pass info to the user.
+ * Process a local peer info reply, pass info to the user.
*
- * @param h Cadet handle.
+ * @param cls Closure (Cadet handle).
* @param message Message itself.
*/
static void
-process_get_tunnels (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_MessageHeader *message)
+handle_get_peer (void *cls,
+ const struct GNUNET_CADET_LocalInfoPeer *message)
{
- struct GNUNET_CADET_LocalInfoTunnel *msg;
- uint16_t size;
+ struct GNUNET_CADET_Handle *h = cls;
+ struct GNUNET_PeerIdentity *paths_array;
+ unsigned int paths;
+ unsigned int path_length;
+ int neighbor;
+ unsigned int peers;
+
+ paths = (unsigned int) ntohs (message->paths);
+ paths_array = (struct GNUNET_PeerIdentity *) &message[1];
+ peers = (ntohs (message->header.size) - sizeof (*message))
+ / sizeof (struct GNUNET_PeerIdentity);
+ path_length = 0;
+ neighbor = GNUNET_NO;
+
+ for (int i = 0; i < peers; i++)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
+ path_length++;
+ if (0 == memcmp (&paths_array[i], &message->destination,
+ sizeof (struct GNUNET_PeerIdentity)))
+ {
+ if (1 == path_length)
+ neighbor = GNUNET_YES;
+ path_length = 0;
+ }
+ }
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnels messasge received\n");
+ /* Call Callback with tunnel info. */
+ paths_array = (struct GNUNET_PeerIdentity *) &message[1];
+ h->info_cb.peer_cb (h->info_cls,
+ &message->destination,
+ (int) ntohs (message->tunnel),
+ neighbor,
+ paths,
+ paths_array);
+}
+
+
+/**
+ * Check that message received from CADET service is well-formed.
+ *
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param msg the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ * #GNUNET_SYSERR otherwise
+ */
+static int
+check_get_tunnels (void *cls,
+ const struct GNUNET_CADET_LocalInfoTunnel *msg)
+{
+ struct GNUNET_CADET_Handle *h = cls;
+ uint16_t size;
if (NULL == h->info_cb.tunnels_cb)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
- return;
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ " no handler for tunnels monitor message!\n");
+ return GNUNET_SYSERR;
}
- size = ntohs (message->size);
+ size = ntohs (msg->header.size);
if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
{
h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
h->info_cb.tunnels_cb = NULL;
h->info_cls = NULL;
- return;
+ return GNUNET_SYSERR;
}
+ return GNUNET_OK;
+}
+
+
+/**
+ * Process a local reply about info on all tunnels, pass info to the user.
+ *
+ * @param cls Closure (Cadet handle).
+ * @param message Message itself.
+ */
+static void
+handle_get_tunnels (void *cls,
+ const struct GNUNET_CADET_LocalInfoTunnel *msg)
+{
+ struct GNUNET_CADET_Handle *h = cls;
- msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
h->info_cb.tunnels_cb (h->info_cls, &msg->destination,
ntohl (msg->channels), ntohl (msg->connections),
ntohs (msg->estate), ntohs (msg->cstate));
/**
- * Process a local tunnel info reply, pass info to the user.
+ * Check that message received from CADET service is well-formed.
*
- * @param h Cadet handle.
- * @param message Message itself.
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param msg the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ * #GNUNET_SYSERR otherwise
*/
-static void
-process_get_tunnel (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_MessageHeader *message)
+static int
+check_get_tunnel (void *cls,
+ const struct GNUNET_CADET_LocalInfoTunnel *msg)
{
- struct GNUNET_CADET_LocalInfoTunnel *msg;
- size_t esize;
- size_t msize;
+ struct GNUNET_CADET_Handle *h = cls;
unsigned int ch_n;
unsigned int c_n;
- struct GNUNET_CADET_Hash *conns;
- CADET_ChannelNumber *chns;
+ size_t esize;
+ size_t msize;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
if (NULL == h->info_cb.tunnel_cb)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
- return;
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ " no handler for tunnel monitor message!\n");
+ goto clean_cls;
}
/* Verify message sanity */
- msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
- msize = ntohs (message->size);
+ msize = ntohs (msg->header.size);
esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
if (esize > msize)
{
if (msize != esize)
{
GNUNET_break_op (0);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
- msize, esize, ch_n, c_n);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
- sizeof (struct GNUNET_CADET_LocalInfoTunnel),
- sizeof (CADET_ChannelNumber), sizeof (struct GNUNET_HashCode));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "m:%u, e: %u (%u ch, %u conn)\n",
+ (unsigned int) msize,
+ (unsigned int) esize,
+ ch_n,
+ c_n);
h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
goto clean_cls;
}
- /* Call Callback with tunnel info. */
- conns = (struct GNUNET_CADET_Hash *) &msg[1];
- chns = (CADET_ChannelNumber *) &conns[c_n];
- h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
- ch_n, c_n, chns, conns,
- ntohs (msg->estate), ntohs (msg->cstate));
+ return GNUNET_OK;
clean_cls:
h->info_cb.tunnel_cb = NULL;
h->info_cls = NULL;
+ return GNUNET_SYSERR;
}
/**
- * Function to process all messages received from the service
+ * Process a local tunnel info reply, pass info to the user.
*
- * @param cls closure
- * @param msg message received, NULL on timeout or fatal error
+ * @param cls Closure (Cadet handle).
+ * @param msg Message itself.
*/
static void
-msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
+handle_get_tunnel (void *cls,
+ const struct GNUNET_CADET_LocalInfoTunnel *msg)
{
struct GNUNET_CADET_Handle *h = cls;
- uint16_t type;
+ unsigned int ch_n;
+ unsigned int c_n;
+ struct GNUNET_CADET_Hash *conns;
+ CADET_ChannelNumber *chns;
- if (msg == NULL)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Cadet service disconnected, reconnecting\n", h);
- reconnect (h);
- return;
- }
- type = ntohs (msg->type);
- LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
- GC_m2s (type));
- switch (type)
- {
- /* Notify of a new incoming channel */
- case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
- process_channel_created (h, (struct GNUNET_CADET_ChannelMessage *) msg);
- break;
- /* Notify of a channel disconnection */
- case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
- case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
- process_channel_destroy (h, (struct GNUNET_CADET_ChannelMessage *) msg);
- break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA:
- process_incoming_data (h, msg);
- break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK:
- process_ack (h, msg);
- break;
-// case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
-// process_get_channels (h, msg);
-// break;
-// case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
-// process_show_channel (h, msg);
-// break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
- process_get_peers (h, msg);
- break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
- process_get_peer (h, msg);
- break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
- process_get_tunnels (h, msg);
- break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
- process_get_tunnel (h, msg);
- break;
-// case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
-// process_show_channel (h, msg);
-// break;
- default:
- /* We shouldn't get any other packages, log and ignore */
- LOG (GNUNET_ERROR_TYPE_WARNING,
- "unsolicited message form service (type %s)\n",
- GC_m2s (ntohs (msg->type)));
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
- if (GNUNET_YES == h->in_receive)
- {
- GNUNET_CLIENT_receive (h->client, &msg_received, h,
- GNUNET_TIME_UNIT_FOREVER_REL);
- }
- else
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "in receive off, not calling CLIENT_receive\n");
- }
+ ch_n = ntohl (msg->channels);
+ c_n = ntohl (msg->connections);
+
+ /* Call Callback with tunnel info. */
+ conns = (struct GNUNET_CADET_Hash *) &msg[1];
+ chns = (CADET_ChannelNumber *) &conns[c_n];
+ h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
+ ch_n, c_n, chns, conns,
+ ntohs (msg->estate), ntohs (msg->cstate));
}
-/******************************************************************************/
-/************************ SEND FUNCTIONS ****************************/
-/******************************************************************************/
/**
- * Function called to send a message to the service.
- * "buf" will be NULL and "size" zero if the socket was closed for writing in
- * the meantime.
+ * Reconnect to the service, retransmit all infomation to try to restore the
+ * original state.
*
- * @param cls closure, the cadet handle
- * @param size number of bytes available in buf
- * @param buf where the callee should write the connect message
- * @return number of bytes written to buf
+ * @param h handle to the cadet
+ *
+ * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
*/
-static size_t
-send_callback (void *cls, size_t size, void *buf)
+static int
+do_reconnect (struct GNUNET_CADET_Handle *h)
{
- struct GNUNET_CADET_Handle *h = cls;
- struct GNUNET_CADET_TransmitHandle *th;
- struct GNUNET_CADET_TransmitHandle *next;
- struct GNUNET_CADET_Channel *ch;
- char *cbuf = buf;
- size_t tsize;
- size_t psize;
- size_t nsize;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
- if ((0 == size) || (NULL == buf))
+ struct GNUNET_MQ_MessageHandler handlers[] = {
+ GNUNET_MQ_hd_fixed_size (channel_created,
+ GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
+ struct GNUNET_CADET_ChannelCreateMessage,
+ h),
+ GNUNET_MQ_hd_fixed_size (channel_destroy,
+ GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
+ struct GNUNET_CADET_ChannelDestroyMessage,
+ h),
+ GNUNET_MQ_hd_var_size (local_data,
+ GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
+ struct GNUNET_CADET_LocalData,
+ h),
+ GNUNET_MQ_hd_fixed_size (local_ack,
+ GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
+ struct GNUNET_CADET_LocalAck,
+ h),
+ GNUNET_MQ_hd_var_size (get_peers,
+ GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
+ struct GNUNET_CADET_LocalInfoPeer,
+ h),
+ GNUNET_MQ_hd_var_size (get_peer,
+ GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
+ struct GNUNET_CADET_LocalInfoPeer,
+ h),
+ GNUNET_MQ_hd_var_size (get_tunnels,
+ GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
+ struct GNUNET_CADET_LocalInfoTunnel,
+ h),
+ GNUNET_MQ_hd_var_size (get_tunnel,
+ GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
+ struct GNUNET_CADET_LocalInfoTunnel,
+ h),
+ // FIXME
+// GNUNET_MQ_hd_fixed_Y size (channel_destroyed,
+// GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK,
+// struct GNUNET_CADET_ChannelDestroyMessage);
+ GNUNET_MQ_handler_end ()
+ };
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
+
+ GNUNET_assert (NULL == h->mq);
+ h->mq = GNUNET_CLIENT_connecT (h->cfg,
+ "cadet",
+ handlers,
+ &handle_mq_error,
+ h);
+ if (NULL == h->mq)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
reconnect (h);
- h->th = NULL;
- return 0;
- }
- tsize = 0;
- next = h->th_head;
- nsize = message_ready_size (h);
- while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
- {
- ch = th->channel;
- if (GNUNET_YES == th_is_payload (th))
- {
- struct GNUNET_CADET_LocalData *dmsg;
- struct GNUNET_MessageHeader *mh;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
- if (GNUNET_NO == ch->allow_send)
- {
- /* This channel is not ready to transmit yet, try next message */
- next = th->next;
- continue;
- }
- ch->packet_size = 0;
- GNUNET_assert (size >= th->size);
- dmsg = (struct GNUNET_CADET_LocalData *) cbuf;
- mh = (struct GNUNET_MessageHeader *) &dmsg[1];
- psize = th->notify (th->notify_cls,
- size - sizeof (struct GNUNET_CADET_LocalData),
- mh);
-
- if (psize > 0)
- {
- psize += sizeof (struct GNUNET_CADET_LocalData);
- GNUNET_assert (size >= psize);
- dmsg->header.size = htons (psize);
- dmsg->id = htonl (ch->chid);
- dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
- GC_m2s (ntohs (mh->type)));
- ch->allow_send = GNUNET_NO;
- }
- else
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "# callback returned size 0, "
- "application canceled transmission\n");
- }
- }
- else
- {
- const struct GNUNET_MessageHeader *mh;
-
- mh = (const struct GNUNET_MessageHeader *) &th[1];
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# cadet internal traffic, type %s\n",
- GC_m2s (ntohs (mh->type)));
- memcpy (cbuf, &th[1], th->size);
- psize = th->size;
- }
- GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= psize);
- if (th->timeout_task != NULL)
- GNUNET_SCHEDULER_cancel (th->timeout_task);
- GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
- GNUNET_free (th);
- next = h->th_head;
- nsize = message_ready_size (h);
- cbuf += psize;
- size -= psize;
- tsize += psize;
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
- h->th = NULL;
- size = message_ready_size (h);
- if (0 != size)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
- h->th =
- GNUNET_CLIENT_notify_transmit_ready (h->client, size,
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_YES, &send_callback, h);
+ return GNUNET_NO;
}
else
{
- if (NULL != h->th_head)
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
- else
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
- }
- if (GNUNET_NO == h->in_receive)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
- h->in_receive = GNUNET_YES;
- GNUNET_CLIENT_receive (h->client, &msg_received, h,
- GNUNET_TIME_UNIT_FOREVER_REL);
+ h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
- return tsize;
+ return GNUNET_YES;
+}
+
+/**
+ * Reconnect callback: tries to reconnect again after a failer previous
+ * reconnecttion
+ *
+ * @param cls closure (cadet handle)
+ */
+static void
+reconnect_cbk (void *cls)
+{
+ struct GNUNET_CADET_Handle *h = cls;
+
+ h->reconnect_task = NULL;
+ do_reconnect (h);
}
/**
- * Auxiliary function to send an already constructed packet to the service.
- * Takes care of creating a new queue element, copying the message and
- * calling the tmt_rdy function if necessary.
+ * Reconnect to the service, retransmit all infomation to try to restore the
+ * original state.
*
- * @param h cadet handle
- * @param msg message to transmit
- * @param channel channel this send is related to (NULL if N/A)
+ * @param h handle to the cadet
+ *
+ * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
*/
static void
-send_packet (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_MessageHeader *msg,
- struct GNUNET_CADET_Channel *channel)
+reconnect (struct GNUNET_CADET_Handle *h)
{
- struct GNUNET_CADET_TransmitHandle *th;
- size_t msize;
+ struct GNUNET_CADET_Channel *ch;
- LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
- GC_m2s(ntohs(msg->type)));
- msize = ntohs (msg->size);
- th = GNUNET_malloc (sizeof (struct GNUNET_CADET_TransmitHandle) + msize);
- th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
- th->size = msize;
- th->channel = channel;
- memcpy (&th[1], msg, msize);
- add_to_queue (h, th);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
- h->th =
- GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_YES, &send_callback, h);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Requested RECONNECT, destroying all channels\n");
+ for (ch = h->channels_head; NULL != ch; ch = h->channels_head)
+ destroy_channel (ch, GNUNET_YES);
+ if (NULL == h->reconnect_task)
+ h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
+ &reconnect_cbk, h);
}
struct GNUNET_CADET_Handle *
GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
- GNUNET_CADET_InboundChannelNotificationHandler new_channel,
GNUNET_CADET_ChannelEndHandler cleaner,
- const struct GNUNET_CADET_MessageHandler *handlers,
- const uint32_t *ports)
+ const struct GNUNET_CADET_MessageHandler *handlers)
{
struct GNUNET_CADET_Handle *h;
h = GNUNET_new (struct GNUNET_CADET_Handle);
LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
h->cfg = cfg;
- h->new_channel = new_channel;
h->cleaner = cleaner;
- h->client = GNUNET_CLIENT_connect ("cadet", cfg);
- if (h->client == NULL)
+ h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
+ do_reconnect (h);
+ if (h->mq == NULL)
{
GNUNET_break (0);
- GNUNET_free (h);
+ GNUNET_CADET_disconnect (h);
return NULL;
}
h->cls = cls;
h->message_handlers = handlers;
- h->ports = ports;
h->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
h->reconnect_task = NULL;
- if (NULL != ports && ports[0] != 0 && NULL == new_channel)
- {
- GNUNET_break (0);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "no new channel handler given, ports parameter is useless!!\n");
- }
- if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
- {
- GNUNET_break (0);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "no ports given, new channel handler will never be called!!\n");
- }
/* count handlers */
for (h->n_handlers = 0;
handlers && handlers[h->n_handlers].type;
h->n_handlers++) ;
- for (h->n_ports = 0;
- ports && ports[h->n_ports];
- h->n_ports++) ;
- send_connect (h);
LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
return h;
}
msg = (struct GNUNET_MessageHeader *) &th[1];
switch (ntohs(msg->type))
{
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT:
case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
+ case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
+ case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
break;
default:
GNUNET_break (0);
- LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
- ntohs(msg->type));
+ LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
+ GC_m2s (ntohs(msg->type)));
}
- GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
- GNUNET_free (th);
+ GNUNET_CADET_notify_transmit_ready_cancel (th);
}
- if (NULL != handle->th)
- {
- GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
- handle->th = NULL;
- }
- if (NULL != handle->client)
+ if (NULL != handle->mq)
{
- GNUNET_CLIENT_disconnect (handle->client);
- handle->client = NULL;
+ GNUNET_MQ_destroy (handle->mq);
+ handle->mq = NULL;
}
if (NULL != handle->reconnect_task)
{
GNUNET_SCHEDULER_cancel(handle->reconnect_task);
handle->reconnect_task = NULL;
}
+
+ GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
+ handle->ports = NULL;
GNUNET_free (handle);
}
+/**
+ * Open a port to receive incomming channels.
+ *
+ * @param h CADET handle.
+ * @param port Hash representing the port number.
+ * @param new_channel Function called when an channel is received.
+ * @param new_channel_cls Closure for @a new_channel.
+ *
+ * @return Port handle.
+ */
+struct GNUNET_CADET_Port *
+GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
+ const struct GNUNET_HashCode *port,
+ GNUNET_CADET_InboundChannelNotificationHandler
+ new_channel,
+ void *new_channel_cls)
+{
+ struct GNUNET_CADET_PortMessage *msg;
+ struct GNUNET_MQ_Envelope *env;
+ struct GNUNET_CADET_Port *p;
+
+ GNUNET_assert (NULL != new_channel);
+ p = GNUNET_new (struct GNUNET_CADET_Port);
+ p->cadet = h;
+ p->hash = GNUNET_new (struct GNUNET_HashCode);
+ *p->hash = *port;
+ p->handler = new_channel;
+ p->cls = new_channel_cls;
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_multihashmap_put (h->ports,
+ p->hash,
+ p,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+
+ env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
+ msg->port = *p->hash;
+ GNUNET_MQ_send (h->mq, env);
+
+ return p;
+}
+
+/**
+ * Close a port opened with @a GNUNET_CADET_open_port.
+ * The @a new_channel callback will no longer be called.
+ *
+ * @param p Port handle.
+ */
+void
+GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
+{
+ struct GNUNET_CADET_PortMessage *msg;
+ struct GNUNET_MQ_Envelope *env;
+
+ env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
+
+ msg->port = *p->hash;
+ GNUNET_MQ_send (p->cadet->mq, env);
+ GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
+ GNUNET_free (p->hash);
+ GNUNET_free (p);
+}
+
+
/**
* Create a new channel towards a remote peer.
*
* @param h cadet handle
* @param channel_ctx client's channel context to associate with the channel
* @param peer peer identity the channel should go to
- * @param port Port number.
+ * @param port Port hash (port number).
* @param options CadetOption flag field, with all desired option bits set to 1.
*
* @return handle to the channel
GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
void *channel_ctx,
const struct GNUNET_PeerIdentity *peer,
- uint32_t port,
+ const struct GNUNET_HashCode *port,
enum GNUNET_CADET_ChannelOption options)
{
+ struct GNUNET_CADET_ChannelCreateMessage *msg;
+ struct GNUNET_MQ_Envelope *env;
struct GNUNET_CADET_Channel *ch;
- struct GNUNET_CADET_ChannelMessage msg;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Creating new channel to %s:%u\n",
LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid);
ch->ctx = channel_ctx;
ch->peer = GNUNET_PEER_intern (peer);
- msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
- msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
- msg.channel_id = htonl (ch->chid);
- msg.port = htonl (port);
- msg.peer = *peer;
- msg.opt = htonl (options);
+
+ env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
+ msg->channel_id = htonl (ch->chid);
+ msg->port = *port;
+ msg->peer = *peer;
+ msg->opt = htonl (options);
ch->allow_send = GNUNET_NO;
- send_packet (h, &msg.header, ch);
+ GNUNET_MQ_send (h->mq, env);
+
return ch;
}
GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
{
struct GNUNET_CADET_Handle *h;
- struct GNUNET_CADET_ChannelMessage msg;
+ struct GNUNET_CADET_ChannelDestroyMessage *msg;
+ struct GNUNET_MQ_Envelope *env;
struct GNUNET_CADET_TransmitHandle *th;
+ struct GNUNET_CADET_TransmitHandle *next;
LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
h = channel->cadet;
-
- msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
- msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
- msg.channel_id = htonl (channel->chid);
- memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
- msg.port = 0;
- msg.opt = 0;
- th = h->th_head;
- while (th != NULL)
+ for (th = h->th_head; th != NULL; th = next)
{
- struct GNUNET_CADET_TransmitHandle *aux;
+ next = th->next;
if (th->channel == channel)
{
- aux = th->next;
+ GNUNET_break (0);
if (GNUNET_YES == th_is_payload (th))
{
/* applications should cancel before destroying channel */
- GNUNET_break (0);
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "Channel destroyed without cancelling transmission requests\n");
th->notify (th->notify_cls, 0, NULL);
}
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n");
+ }
+ GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
GNUNET_CADET_notify_transmit_ready_cancel (th);
- th = aux;
}
- else
- th = th->next;
}
+ env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
+ msg->channel_id = htonl (channel->chid);
+ GNUNET_MQ_send (h->mq, env);
+
destroy_channel (channel, GNUNET_YES);
- send_packet (h, &msg.header, NULL);
}
return ret;
}
+
struct GNUNET_CADET_TransmitHandle *
-GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel, int cork,
+GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
+ int cork,
struct GNUNET_TIME_Relative maxdelay,
size_t notify_size,
GNUNET_CONNECTION_TransmitReadyNotify notify,
LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
GNUNET_assert (NULL != notify);
GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
+
+ if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
+ {
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "CADET transmit ready timeout is deprected (has no effect)\n");
+ }
+
th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
th->channel = channel;
- th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
- th->size = notify_size + sizeof (struct GNUNET_CADET_LocalData);
+ th->size = notify_size;
channel->packet_size = th->size;
LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
th->notify = notify;
th->notify_cls = notify_cls;
- add_to_queue (channel->cadet, th);
- if (NULL != channel->cadet->th)
- return th;
- if (GNUNET_NO == channel->allow_send)
- return th;
- LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
- channel->cadet->th =
- GNUNET_CLIENT_notify_transmit_ready (channel->cadet->client, th->size,
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_YES, &send_callback,
- channel->cadet);
+ if (GNUNET_YES == channel->allow_send)
+ th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
+ else
+ add_to_queue (channel->cadet, th);
+
LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
return th;
}
void
GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
{
- struct GNUNET_CADET_Handle *cadet;
-
- th->channel->packet_size = 0;
- cadet = th->channel->cadet;
- if (th->timeout_task != NULL)
- GNUNET_SCHEDULER_cancel (th->timeout_task);
- GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
- GNUNET_free (th);
- if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
+ if (NULL != th->request_data_task)
{
- /* queue empty, no point in asking for transmission */
- GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
- cadet->th = NULL;
+ GNUNET_SCHEDULER_cancel (th->request_data_task);
}
+ th->request_data_task = NULL;
+
+ remove_from_queue (th);
+ GNUNET_free (th);
}
static void
send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
{
- struct GNUNET_MessageHeader msg;
+ struct GNUNET_MessageHeader *msg;
+ struct GNUNET_MQ_Envelope *env;
- msg.size = htons (sizeof (msg));
- msg.type = htons (type);
- send_packet (h, &msg, NULL);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ " Sending %s monitor message to service\n",
+ GC_m2s(type));
+
+ env = GNUNET_MQ_msg (msg, type);
+ GNUNET_MQ_send (h->mq, env);
}
*/
int
GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
- const struct GNUNET_PeerIdentity *id,
- GNUNET_CADET_PeerCB callback,
- void *callback_cls)
+ const struct GNUNET_PeerIdentity *id,
+ GNUNET_CADET_PeerCB callback,
+ void *callback_cls)
{
- struct GNUNET_CADET_LocalInfo msg;
+ struct GNUNET_CADET_LocalInfo *msg;
+ struct GNUNET_MQ_Envelope *env;
if (NULL != h->info_cb.peer_cb)
{
return GNUNET_SYSERR;
}
- memset (&msg, 0, sizeof (msg));
- msg.header.size = htons (sizeof (msg));
- msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
- msg.peer = *id;
- send_packet (h, &msg.header, NULL);
+ env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
+ msg->peer = *id;
+ GNUNET_MQ_send (h->mq, env);
+
h->info_cb.peer_cb = callback;
h->info_cls = callback_cls;
return GNUNET_OK;
GNUNET_CADET_TunnelCB callback,
void *callback_cls)
{
- struct GNUNET_CADET_LocalInfo msg;
+ struct GNUNET_CADET_LocalInfo *msg;
+ struct GNUNET_MQ_Envelope *env;
if (NULL != h->info_cb.tunnel_cb)
{
return GNUNET_SYSERR;
}
- memset (&msg, 0, sizeof (msg));
- msg.header.size = htons (sizeof (msg));
- msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
- msg.peer = *id;
- send_packet (h, &msg.header, NULL);
+ env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
+ msg->peer = *id;
+ GNUNET_MQ_send (h->mq, env);
+
h->info_cb.tunnel_cb = callback;
h->info_cls = callback_cls;
return GNUNET_OK;
*/
int
GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
- struct GNUNET_PeerIdentity *initiator,
- unsigned int channel_number,
- GNUNET_CADET_ChannelCB callback,
- void *callback_cls)
+ struct GNUNET_PeerIdentity *initiator,
+ unsigned int channel_number,
+ GNUNET_CADET_ChannelCB callback,
+ void *callback_cls)
{
- struct GNUNET_CADET_LocalInfo msg;
+ struct GNUNET_CADET_LocalInfo *msg;
+ struct GNUNET_MQ_Envelope *env;
if (NULL != h->info_cb.channel_cb)
{
return GNUNET_SYSERR;
}
- msg.header.size = htons (sizeof (msg));
- msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
- msg.peer = *initiator;
- msg.channel_id = htonl (channel_number);
-// msg.reserved = 0;
- send_packet (h, &msg.header, NULL);
+ env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
+ msg->peer = *initiator;
+ msg->channel_id = htonl (channel_number);
+ GNUNET_MQ_send (h->mq, env);
+
h->info_cb.channel_cb = callback;
h->info_cls = callback_cls;
return GNUNET_OK;
}
msize = ntohs (msg->size);
GNUNET_assert (msize <= size);
- memcpy (buf, msg, msize);
+ GNUNET_memcpy (buf, msg, msize);
GNUNET_MQ_impl_send_continue (mq);
return msize;
}
return mq;
}
+
+/**
+ * Transitional function to convert an unsigned int port to a hash value.
+ * WARNING: local static value returned, NOT reentrant!
+ * WARNING: do not use this function for new code!
+ *
+ * @param port Numerical port (unsigned int format).
+ *
+ * @return A GNUNET_HashCode usable for the new CADET API.
+ */
+const struct GNUNET_HashCode *
+GC_u2h (uint32_t port)
+{
+ static struct GNUNET_HashCode hash;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "This is a transitional function, "
+ "use proper crypto hashes as CADET ports\n");
+ GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);
+
+ return &hash;
+}