*/
extern struct GNUNET_STATISTICS_Handle *stats;
+/**
+ * Local peer own ID (memory efficient handle).
+ */
+extern GNUNET_PEER_Id myid;
+
/******************************************************************************/
/******************************** STATIC ***********************************/
LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
}
+/**
+ * Add a destination client to a channel, initializing all data structures
+ * in the channel and the client.
+ *
+ * @param ch Channel to which add the destination.
+ * @param c Client which to add to the channel.
+ */
+static void
+add_destination (struct MeshChannel *ch, struct MeshClient *c)
+{
+ if (NULL != ch->dest)
+ {
+ GNUNET_break (0);
+ return;
+ }
+
+ /* Assign local id as destination */
+ ch->lid_dest = GML_get_next_chid (c);
+
+ /* Store in client's hashmap */
+ GML_channel_add (c, ch->lid_dest, ch);
+
+ GNUNET_break (NULL == ch->dest_rel);
+ ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
+ ch->dest_rel->ch = ch;
+ ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
+
+ ch->dest = c;
+}
+
/**
* Send data to a client.
/**
- * Add a client to a channel, initializing all needed data structures.
+ * Send a buffered message to the client, for in order delivery or
+ * as result of client ACK.
*
- * @param ch Channel to which add the client.
- * @param c Client which to add to the channel.
+ * @param ch Channel on which to empty the message buffer.
+ * @param c Client to send to.
+ * @param fwd Is this to send FWD data?.
*/
-void
-GMCH_add_client (struct MeshChannel *ch, struct MeshClient *c)
+static void
+send_client_buffered_data (struct MeshChannel *ch,
+ struct MeshClient *c,
+ int fwd)
{
- if (NULL != ch->dest)
+ struct MeshReliableMessage *copy;
+ struct MeshChannelReliability *rel;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
+ rel = fwd ? ch->dest_rel : ch->root_rel;
+ if (GNUNET_NO == rel->client_ready)
{
- GNUNET_break (0);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
return;
}
- /* Assign local id as destination */
- ch->lid_dest = GML_get_next_chid (c);
+ copy = rel->head_recv;
+ /* We never buffer channel management messages */
+ if (NULL != copy)
+ {
+ if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
+ {
+ struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
- /* Store in client's hashmap */
- GML_channel_add (c, ch->lid_dest, ch);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ " have %u! now expecting %u\n",
+ copy->mid, rel->mid_recv + 1);
+ send_client_data (ch, msg, fwd);
+ rel->n_recv--;
+ rel->mid_recv++;
+ GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
+ GNUNET_free (copy);
+ }
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ " reliable && don't have %u, next is %u\n",
+ rel->mid_recv,
+ copy->mid);
+ return;
+ }
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
+}
- GNUNET_break (NULL == ch->dest_rel);
- ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
- ch->dest_rel->ch = ch;
- ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
- ch->dest = c;
+/**
+ * Allow a client to send more data.
+ *
+ * In case the client was already allowed to send data, do nothing.
+ *
+ * @param ch Channel.
+ * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
+ */
+static void
+send_client_ack (struct MeshChannel *ch, int fwd)
+{
+ struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ " sending %s ack to client on channel %s\n",
+ fwd ? "FWD" : "BCK", GMCH_2s (ch));
+
+ if (NULL == rel)
+ {
+ GNUNET_break (0);
+ return;
+ }
+
+ GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
}
}
+/**
+ * Confirm we got a channel create.
+ *
+ * @param ch The channel to confirm.
+ * @param fwd Should we send a FWD ACK? (going dest->root)
+ */
+static void
+channel_send_ack (struct MeshChannel *ch, int fwd)
+{
+ struct GNUNET_MESH_ChannelManage msg;
+
+ msg.header.size = htons (sizeof (msg));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ " sending channel %s ack for channel %s\n",
+ fwd ? "FWD" : "BCK", GMCH_2s (ch));
+
+ msg.chid = htonl (ch->gid);
+ GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
+}
+
/**
* Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
*
* @param ch Channel to mark as ready.
- * @param fwd Was the CREATE message sent fwd?
+ * @param fwd Was the ACK message sent fwd? (dest->root, SYNACK)
*/
static void
channel_confirm (struct MeshChannel *ch, int fwd)
ch->state = MESH_CHANNEL_READY;
rel = fwd ? ch->root_rel : ch->dest_rel;
+ rel->client_ready = GNUNET_YES;
for (copy = rel->head_sent; NULL != copy; copy = next)
{
struct GNUNET_MessageHeader *msg;
/* TODO return? */
}
}
- send_ack (NULL, ch, fwd);
+ send_client_ack (ch, fwd);
+
+ /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
+ if (fwd)
+ channel_send_ack (ch, !fwd);
}
}
-
-/**
- * Send a buffered message to the client, for in order delivery or
- * as result of client ACK.
- *
- * @param ch Channel on which to empty the message buffer.
- * @param c Client to send to.
- * @param fwd Is this to send FWD data?.
- */
-static void
-send_client_buffered_data (struct MeshChannel *ch,
- struct MeshClient *c,
- int fwd)
-{
- struct MeshReliableMessage *copy;
- struct MeshChannelReliability *rel;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
- rel = fwd ? ch->dest_rel : ch->root_rel;
- if (GNUNET_NO == rel->client_ready)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
- return;
- }
-
- copy = rel->head_recv;
- /* We never buffer channel management messages */
- if (NULL != copy)
- {
- if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
- {
- struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- " have %u! now expecting %u\n",
- copy->mid, rel->mid_recv + 1);
- send_client_data (ch, msg, fwd);
- rel->n_recv--;
- rel->mid_recv++;
- GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
- GNUNET_free (copy);
- }
- else
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- " reliable && don't have %u, next is %u\n",
- rel->mid_recv,
- copy->mid);
- return;
- }
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
-}
-
-
-
-
/**
* Destroy a channel and free all resources.
*
*/
static struct MeshChannel *
channel_new (struct MeshTunnel3 *t,
- struct MeshClient *owner, MESH_ChannelNumber lid_root)
+ struct MeshClient *owner,
+ MESH_ChannelNumber lid_root)
{
struct MeshChannel *ch;
}
-
-/**
- * Confirm we got a channel create.
- *
- * @param ch The channel to confirm.
- * @param fwd Should we send the ACK fwd?
- */
-static void
-channel_send_ack (struct MeshChannel *ch, int fwd)
-{
- struct GNUNET_MESH_ChannelManage msg;
-
- msg.header.size = htons (sizeof (msg));
- msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- " sending channel %s ack for channel %s:%X\n",
- fwd ? "FWD" : "BCK", GMT_2s (ch->t),
- ch->gid);
-
- msg.chid = htonl (ch->gid);
- GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
-}
-
-
-/**
- * Iterator for deleting each channel whose client endpoint disconnected.
- *
- * @param cls Closure (client that has disconnected).
- * @param key The local channel id (used to access the hashmap).
- * @param value The value stored at the key (channel to destroy).
- *
- * @return GNUNET_OK, keep iterating.
- */
-static int
-channel_destroy_iterator (void *cls,
- uint32_t key,
- void *value)
-{
- struct MeshChannel *ch = value;
- struct MeshClient *c = cls;
- struct MeshTunnel3 *t;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- " Channel %X (%X / %X) destroy, due to client %s shutdown.\n",
- ch->gid, ch->lid_root, ch->lid_dest, GML_2s (c));
- GMCH_debug (ch);
-
- if (c == ch->dest)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
- }
- if (c == ch->root)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
- }
-
- t = ch->t;
- GMCH_send_destroy (ch);
- channel_destroy (ch);
- GMT_destroy_if_empty (t);
-
- return GNUNET_OK;
-}
-
-
/**
* Handle a loopback message: call the appropriate handler for the message type.
*
type = ntohs (msgh->type);
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Loopback %s message!\n",
- GNUNET_MESH_DEBUG_M2S (type));
+ "Loopback %s %s message!\n",
+ fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type));
switch (type)
{
break;
case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
- // FIXME store channel in loopback tunnel?
- GMCH_handle_create ((struct GNUNET_MESH_ChannelCreate *) msgh,
+ GMCH_handle_create (ch->t,
+ (struct GNUNET_MESH_ChannelCreate *) msgh,
fwd);
break;
/******************************** API ***********************************/
/******************************************************************************/
+
/**
* Get channel ID.
*
const struct GNUNET_MESH_Data *msg,
int fwd)
{
+ if (GMCH_is_terminal (ch, fwd))
+ {
+ GML_send_data (fwd ? ch->dest : ch->root,
+ msg,
+ fwd ? ch->lid_dest : ch->lid_root);
+ }
+ else
+ {
+ GMT_send_prebuilt_message (&msg->header, ch->t, ch, fwd);
+ }
}
* @param fwd Is for FWD traffic? (ACK dest->owner)
*/
void
-GMCH_send_ack (struct MeshChannel *ch, int fwd)
+GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
{
struct GNUNET_MESH_DataACK msg;
struct MeshChannelReliability *rel;
rel->client_ready = GNUNET_YES;
send_client_buffered_data (ch, c, fwd);
- send_ack (NULL, ch, fwd);
+ GMC_send_ack (NULL, ch, fwd);
}
payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
payload->chid = htonl (ch->gid);
LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
- GMCH_send_prebuilt_message (&payload->header, ch, fwd);
-
if (GNUNET_YES == ch->reliable)
channel_save_copy (ch, &payload->header, fwd);
+ GMCH_send_prebuilt_message (&payload->header, ch, fwd);
+
if (GMT_get_buffer (ch->t, fwd) > 0)
- GML_send_ack (c, fwd ? ch->lid_root : ch->lid_dest);
+ {
+ send_client_ack (ch, fwd);
+ }
return GNUNET_OK;
}
*
* @param ch Channel.
* @param c Client that requested the destruction (to avoid notifying him).
- * @param chid Channel ID used.
*/
void
GMCH_handle_local_destroy (struct MeshChannel *ch,
- struct MeshClient *c,
- MESH_ChannelNumber chid)
+ struct MeshClient *c)
{
struct MeshTunnel3 *t;
/* Cleanup after the tunnel */
- GML_client_delete_channel (c, ch, chid);
- if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
+ if (c == ch->dest)
{
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
+ GML_client_delete_channel (c, ch, ch->lid_dest);
ch->dest = NULL;
}
- else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
+ if (c == ch->root)
{
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
+ GML_client_delete_channel (c, ch, ch->lid_root);
ch->root = NULL;
}
- else
- {
- LOG (GNUNET_ERROR_TYPE_ERROR,
- " channel %X client %p (%p, %p)\n",
- chid, c, ch->root, ch->dest);
- GNUNET_break (0);
- }
t = ch->t;
+ GMCH_send_destroy (ch);
channel_destroy (ch);
GMT_destroy_if_empty (t);
}
*
* @param c Client that requested the creation (will be the root).
* @param msg Create Channel message.
+ *
+ * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
*/
-void
+int
GMCH_handle_local_create (struct MeshClient *c,
struct GNUNET_MESH_ChannelMessage *msg)
{
if (NULL != GML_channel_get (c, chid))
{
GNUNET_break (0);
- GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
- return;
+ return GNUNET_SYSERR;
}
peer = GMP_get (&msg->peer);
GMP_add_tunnel (peer);
- t = GMP_get_tunnel(peer);
+ t = GMP_get_tunnel (peer);
- if (GMP_get_short_id(peer) == myid)
+ if (GMP_get_short_id (peer) == myid)
{
GMT_change_state (t, MESH_TUNNEL3_READY);
}
if (NULL == ch)
{
GNUNET_break (0);
- return;
+ return GNUNET_SYSERR;
}
ch->port = ntohl (msg->port);
channel_set_options (ch, ntohl (msg->opt));
ch->root_rel->ch = ch;
ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
- LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
- peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
/* Send create channel */
{
GMT_queue_data (t, ch, &msgcc.header, GNUNET_YES);
}
+ return GNUNET_OK;
}
/**
mid, rel->mid_recv, rel->mid_recv + 64);
}
- GMCH_send_ack (ch, fwd);
+ GMCH_send_data_ack (ch, fwd);
}
/**
* Handler for channel create messages.
*
+ * @param t Tunnel this channel will be in.
* @param msg Message.
* @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
*/
struct MeshChannel *
-GMCH_handle_create (const struct GNUNET_MESH_ChannelCreate *msg,
+GMCH_handle_create (struct MeshTunnel3 *t,
+ const struct GNUNET_MESH_ChannelCreate *msg,
int fwd)
{
MESH_ChannelNumber chid;
chid = ntohl (msg->chid);
- /* Create channel */
- ch = channel_new (NULL, NULL, 0); /* FIXME pass t */
- ch->gid = chid;
+ ch = GMT_get_channel (t, chid);
+ if (NULL == ch)
+ {
+ /* Create channel */
+ ch = channel_new (t, NULL, 0);
+ ch->gid = chid;
+ }
channel_set_options (ch, ntohl (msg->opt));
/* Find a destination client */
channel_destroy (ch);
return NULL;
}
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " client %p has port registered\n", c);
+ }
- GMCH_add_client (ch, c);
+ add_destination (ch, c);
if (GNUNET_YES == ch->reliable)
LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
+ else
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
GMCH_send_create (ch);
- GMCH_send_ack (ch, fwd);
-
- if (GNUNET_NO == ch->dest_rel->client_ready)
- {
- GML_send_ack (ch->dest, ch->lid_dest);
- ch->dest_rel->client_ready = GNUNET_YES;
- }
+ channel_send_ack (ch, fwd);
return ch;
}
const struct GNUNET_MESH_ChannelManage *msg,
int fwd)
{
+ struct MeshTunnel3 *t;
+
+ GMCH_debug (ch);
if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
{
/* Not for us (don't destroy twice a half-open loopback channel) */
return;
}
+ t = ch->t;
GMCH_send_destroy (ch);
channel_destroy (ch);
+ GMT_destroy_if_empty (t);
}
GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
struct MeshChannel *ch, int fwd)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
- GMT_2s (ch->t), ch->gid, fwd ? "FWD" : "BCK");
- LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
- GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
+ fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (ntohs (message->type)),
+ GMCH_2s (ch));
if (GMT_is_loopback (ch->t))
{
GMT_send_prebuilt_message (message, ch->t, ch, fwd);
}
+
+
+/**
+ * Get the static string for identification of the channel.
+ *
+ * @param ch Channel.
+ *
+ * @return Static string with the channel IDs.
+ */
+const char *
+GMCH_2s (const struct MeshChannel *ch)
+{
+ static char buf[64];
+
+ if (NULL == ch)
+ return "(NULL Channel)";
+
+ sprintf (buf, "%s:%X (%X / %X)",
+ GMT_2s (ch->t), ch->gid, ch->lid_root, ch->lid_dest);
+
+ return buf;
+}