aboutsummaryrefslogtreecommitdiff
path: root/src/stream/stream_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stream/stream_api.c')
-rw-r--r--src/stream/stream_api.c1814
1 files changed, 1145 insertions, 669 deletions
diff --git a/src/stream/stream_api.c b/src/stream/stream_api.c
index dadba33..20df4aa 100644
--- a/src/stream/stream_api.c
+++ b/src/stream/stream_api.c
@@ -34,31 +34,41 @@
* @author Sree Harsha Totakura
*/
-
#include "platform.h"
#include "gnunet_common.h"
-#include "gnunet_crypto_lib.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_lockmanager_service.h"
+#include "gnunet_statistics_service.h"
#include "gnunet_stream_lib.h"
-#include "stream_protocol.h"
+#include "stream.h"
+/**
+ * Generic logging shorthand
+ */
#define LOG(kind,...) \
GNUNET_log_from (kind, "stream-api", __VA_ARGS__)
/**
- * The maximum packet size of a stream packet
+ * Debug logging shorthand
*/
-#define MAX_PACKET_SIZE 64000
+#define LOG_DEBUG(...) \
+ LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
/**
- * Receive buffer
+ * Time in relative seconds shorthand
*/
-#define RECEIVE_BUFFER_SIZE 4096000
+#define TIME_REL_SECS(sec) \
+ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
/**
- * The maximum payload a data message packet can carry
+ * The maximum packet size of a stream packet
*/
-static size_t max_payload_size =
- MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
+#define DEFAULT_MAX_PAYLOAD_SIZE 64000
+
+/**
+ * Receive buffer
+ */
+#define RECEIVE_BUFFER_SIZE 4096000
/**
* states in the Protocol
@@ -165,29 +175,14 @@ struct MessageQueue
struct GNUNET_STREAM_Socket
{
/**
- * Retransmission timeout
- */
- struct GNUNET_TIME_Relative retransmit_timeout;
-
- /**
- * The Acknowledgement Bitmap
- */
- GNUNET_STREAM_AckBitmap ack_bitmap;
-
- /**
- * Time when the Acknowledgement was queued
- */
- struct GNUNET_TIME_Absolute ack_time_registered;
-
- /**
- * Queued Acknowledgement deadline
+ * The mesh handle
*/
- struct GNUNET_TIME_Relative ack_time_deadline;
+ struct GNUNET_MESH_Handle *mesh;
/**
- * The mesh handle
+ * Handle to statistics
*/
- struct GNUNET_MESH_Handle *mesh;
+ struct GNUNET_STATISTICS_Handle *stat_handle;
/**
* The mesh tunnel handle
@@ -210,16 +205,6 @@ struct GNUNET_STREAM_Socket
struct GNUNET_MESH_TransmitHandle *transmit_handle;
/**
- * The current act transmit handle (if a pending ack transmit request exists)
- */
- struct GNUNET_MESH_TransmitHandle *ack_transmit_handle;
-
- /**
- * Pointer to the current ack message using in ack_task
- */
- struct GNUNET_STREAM_AckMessage *ack_msg;
-
- /**
* The current message associated with the transmit handle
*/
struct MessageQueue *queue_head;
@@ -232,12 +217,12 @@ struct GNUNET_STREAM_Socket
/**
* The write IO_handle associated with this socket
*/
- struct GNUNET_STREAM_IOWriteHandle *write_handle;
+ struct GNUNET_STREAM_WriteHandle *write_handle;
/**
* The read IO_handle associated with this socket
*/
- struct GNUNET_STREAM_IOReadHandle *read_handle;
+ struct GNUNET_STREAM_ReadHandle *read_handle;
/**
* The shutdown handle associated with this socket
@@ -261,14 +246,19 @@ struct GNUNET_STREAM_Socket
struct GNUNET_PeerIdentity other_peer;
/**
- * Task identifier for the read io timeout task
+ * The Acknowledgement Bitmap
*/
- GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
+ GNUNET_STREAM_AckBitmap ack_bitmap;
/**
* Task identifier for retransmission task after timeout
*/
- GNUNET_SCHEDULER_TaskIdentifier retransmission_timeout_task_id;
+ GNUNET_SCHEDULER_TaskIdentifier data_retransmission_task_id;
+
+ /**
+ * Task identifier for retransmission of control messages
+ */
+ GNUNET_SCHEDULER_TaskIdentifier control_retransmission_task_id;
/**
* The task for sending timely Acks
@@ -276,9 +266,29 @@ struct GNUNET_STREAM_Socket
GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
/**
- * Task scheduled to continue a read operation.
+ * Retransmission timeout
*/
- GNUNET_SCHEDULER_TaskIdentifier read_task_id;
+ struct GNUNET_TIME_Relative retransmit_timeout;
+
+ /**
+ * Time when the Acknowledgement was queued
+ */
+ struct GNUNET_TIME_Absolute ack_time_registered;
+
+ /**
+ * Queued Acknowledgement deadline
+ */
+ struct GNUNET_TIME_Relative ack_time_deadline;
+
+ /**
+ * Mesh transmit timeout
+ */
+ struct GNUNET_TIME_Relative mesh_retry_timeout;
+
+ /**
+ * Data retransmission timeout
+ */
+ struct GNUNET_TIME_Relative data_retransmit_timeout;
/**
* The state of the protocol associated with this socket
@@ -286,14 +296,19 @@ struct GNUNET_STREAM_Socket
enum State state;
/**
- * The status of the socket
+ * Whether testing mode is active or not
*/
- enum GNUNET_STREAM_Status status;
+ int testing_active;
/**
- * The number of previous timeouts; FIXME: currently not used
+ * Is receive closed
*/
- unsigned int retries;
+ int receive_closed;
+
+ /**
+ * Is transmission closed
+ */
+ int transmit_closed;
/**
* The application port number (type: uint32_t)
@@ -301,10 +316,9 @@ struct GNUNET_STREAM_Socket
GNUNET_MESH_ApplicationType app_port;
/**
- * The session id associated with this stream connection
- * FIXME: Not used currently, may be removed
+ * The write sequence number to be set incase of testing
*/
- uint32_t session_id;
+ uint32_t testing_set_write_sequence_number_value;
/**
* Write sequence number. Set to random when sending HELLO(client) and
@@ -346,6 +360,11 @@ struct GNUNET_STREAM_Socket
* The offset upto which user has read from the received buffer
*/
uint32_t copy_offset;
+
+ /**
+ * The maximum size of the data message payload this stream handle can send
+ */
+ uint16_t max_payload_size;
};
@@ -360,6 +379,31 @@ struct GNUNET_STREAM_ListenSocket
struct GNUNET_MESH_Handle *mesh;
/**
+ * Handle to statistics
+ */
+ struct GNUNET_STATISTICS_Handle *stat_handle;
+
+ /**
+ * Our configuration
+ */
+ struct GNUNET_CONFIGURATION_Handle *cfg;
+
+ /**
+ * Handle to the lock manager service
+ */
+ struct GNUNET_LOCKMANAGER_Handle *lockmanager;
+
+ /**
+ * The active LockingRequest from lockmanager
+ */
+ struct GNUNET_LOCKMANAGER_LockingRequest *locking_request;
+
+ /**
+ * Callback to call after acquring a lock and listening
+ */
+ GNUNET_STREAM_ListenSuccessCallback listen_ok_cb;
+
+ /**
* The callback function which is called after successful opening socket
*/
GNUNET_STREAM_ListenCallback listen_cb;
@@ -371,16 +415,46 @@ struct GNUNET_STREAM_ListenSocket
/**
* The service port
- * FIXME: Remove if not required!
*/
GNUNET_MESH_ApplicationType port;
+
+ /**
+ * The id of the lockmanager timeout task
+ */
+ GNUNET_SCHEDULER_TaskIdentifier lockmanager_acquire_timeout_task;
+
+ /**
+ * The retransmit timeout
+ */
+ struct GNUNET_TIME_Relative retransmit_timeout;
+
+ /**
+ * Listen enabled?
+ */
+ int listening;
+
+ /**
+ * Whether testing mode is active or not
+ */
+ int testing_active;
+
+ /**
+ * The write sequence number to be set incase of testing
+ */
+ uint32_t testing_set_write_sequence_number_value;
+
+ /**
+ * The maximum size of the data message payload this stream handle can send
+ */
+ uint16_t max_payload_size;
+
};
/**
* The IO Write Handle
*/
-struct GNUNET_STREAM_IOWriteHandle
+struct GNUNET_STREAM_WriteHandle
{
/**
* The socket to which this write handle is associated
@@ -388,11 +462,6 @@ struct GNUNET_STREAM_IOWriteHandle
struct GNUNET_STREAM_Socket *socket;
/**
- * The packet_buffers associated with this Handle
- */
- struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
-
- /**
* The write continuation callback
*/
GNUNET_STREAM_CompletionContinuation write_cont;
@@ -403,6 +472,11 @@ struct GNUNET_STREAM_IOWriteHandle
void *write_cont_cls;
/**
+ * The packet_buffers associated with this Handle
+ */
+ struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
+
+ /**
* The bitmap of this IOHandle; Corresponding bit for a message is set when
* it has been acknowledged by the receiver
*/
@@ -412,15 +486,34 @@ struct GNUNET_STREAM_IOWriteHandle
* Number of bytes in this write handle
*/
size_t size;
+
+ /**
+ * Number of packets already transmitted from this IO handle. Retransmitted
+ * packets are not taken into account here. This is used to determine which
+ * packets account for retransmission and which packets occupy buffer space at
+ * the receiver.
+ */
+ unsigned int packets_sent;
+
+ /**
+ * The maximum of the base numbers of the received acks
+ */
+ uint32_t max_ack_base_num;
+
};
/**
* The IO Read Handle
*/
-struct GNUNET_STREAM_IOReadHandle
+struct GNUNET_STREAM_ReadHandle
{
/**
+ * The socket to which this read handle is associated
+ */
+ struct GNUNET_STREAM_Socket *socket;
+
+ /**
* Callback for the read processor
*/
GNUNET_STREAM_DataProcessor proc;
@@ -429,6 +522,22 @@ struct GNUNET_STREAM_IOReadHandle
* The closure pointer for the read processor callback
*/
void *proc_cls;
+
+ /**
+ * Task identifier for the read io timeout task
+ */
+ GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
+
+ /**
+ * Task scheduled to continue a read operation.
+ */
+ GNUNET_SCHEDULER_TaskIdentifier read_task_id;
+
+ /**
+ * Task scheduled from GNUNET_STREAM_read() to lookup the ACK bitmap and call
+ * the read processor task
+ */
+ GNUNET_SCHEDULER_TaskIdentifier probe_data_availability_task_id;
};
@@ -458,6 +567,11 @@ struct GNUNET_STREAM_ShutdownHandle
GNUNET_SCHEDULER_TaskIdentifier close_msg_retransmission_task_id;
/**
+ * Task scheduled to call the shutdown continuation callback
+ */
+ GNUNET_SCHEDULER_TaskIdentifier call_cont_task_id;
+
+ /**
* Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
*/
int operation;
@@ -467,8 +581,12 @@ struct GNUNET_STREAM_ShutdownHandle
/**
* Default value in seconds for various timeouts
*/
-static unsigned int default_timeout = 10;
+static const unsigned int default_timeout = 10;
+/**
+ * The domain name for locks we use here
+ */
+static const char *locking_domain = "GNUNET_STREAM_APPLOCK";
/**
* Callback function for sending queued message
@@ -491,24 +609,23 @@ send_message_notify (void *cls, size_t size, void *buf)
return 0; /* just to be safe */
if (0 == size) /* request timed out */
{
- socket->retries++;
+ socket->mesh_retry_timeout = GNUNET_TIME_STD_BACKOFF
+ (socket->mesh_retry_timeout);
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Message sending timed out. Retry %d \n",
+ "%s: Message sending to MESH timed out. Retrying in %s \n",
GNUNET_i2s (&socket->other_peer),
- socket->retries);
+ GNUNET_STRINGS_relative_time_to_string (socket->mesh_retry_timeout,
+ GNUNET_YES));
socket->transmit_handle =
- GNUNET_MESH_notify_transmit_ready (socket->tunnel,
- 0, /* Corking */
- 1, /* Priority */
- /* FIXME: exponential backoff */
- socket->retransmit_timeout,
- &socket->other_peer,
- ntohs (head->message->header.size),
- &send_message_notify,
- socket);
+ GNUNET_MESH_notify_transmit_ready (socket->tunnel,
+ GNUNET_NO, /* Corking */
+ socket->mesh_retry_timeout,
+ &socket->other_peer,
+ ntohs (head->message->header.size),
+ &send_message_notify,
+ socket);
return 0;
}
-
ret = ntohs (head->message->header.size);
GNUNET_assert (size >= ret);
memcpy (buf, head->message, ret);
@@ -521,20 +638,20 @@ send_message_notify (void *cls, size_t size, void *buf)
head);
GNUNET_free (head->message);
GNUNET_free (head);
+ if (NULL != socket->transmit_handle)
+ return ret; /* 'finish_cb' might have triggered message already! */
head = socket->queue_head;
if (NULL != head) /* more pending messages to send */
{
- socket->retries = 0;
+ socket->mesh_retry_timeout = GNUNET_TIME_UNIT_ZERO;
socket->transmit_handle =
- GNUNET_MESH_notify_transmit_ready (socket->tunnel,
- 0, /* Corking */
- 1, /* Priority */
- /* FIXME: exponential backoff */
- socket->retransmit_timeout,
- &socket->other_peer,
- ntohs (head->message->header.size),
- &send_message_notify,
- socket);
+ GNUNET_MESH_notify_transmit_ready (socket->tunnel,
+ GNUNET_NO, /* Corking */
+ socket->mesh_retry_timeout,
+ &socket->other_peer,
+ ntohs (head->message->header.size),
+ &send_message_notify,
+ socket);
}
return ret;
}
@@ -547,19 +664,21 @@ send_message_notify (void *cls, size_t size, void *buf)
* @param message the message to be sent
* @param finish_cb the callback to be called when the message is sent
* @param finish_cb_cls the closure for the callback
+ * @param urgent set to GNUNET_YES to add the message to the beginning of the
+ * queue; GNUNET_NO to add at the tail
*/
static void
queue_message (struct GNUNET_STREAM_Socket *socket,
struct GNUNET_STREAM_MessageHeader *message,
SendFinishCallback finish_cb,
- void *finish_cb_cls)
+ void *finish_cb_cls,
+ int urgent)
{
struct MessageQueue *queue_entity;
GNUNET_assert
((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
&& (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
-
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Queueing message of type %d and size %d\n",
GNUNET_i2s (&socket->other_peer),
@@ -570,21 +689,31 @@ queue_message (struct GNUNET_STREAM_Socket *socket,
queue_entity->message = message;
queue_entity->finish_cb = finish_cb;
queue_entity->finish_cb_cls = finish_cb_cls;
- GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
- socket->queue_tail,
- queue_entity);
+ if (GNUNET_YES == urgent)
+ {
+ GNUNET_CONTAINER_DLL_insert (socket->queue_head, socket->queue_tail,
+ queue_entity);
+ if (NULL != socket->transmit_handle)
+ {
+ GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
+ socket->transmit_handle = NULL;
+ }
+ }
+ else
+ GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
+ socket->queue_tail,
+ queue_entity);
if (NULL == socket->transmit_handle)
{
- socket->retries = 0;
+ socket->mesh_retry_timeout = GNUNET_TIME_UNIT_ZERO;
socket->transmit_handle =
- GNUNET_MESH_notify_transmit_ready (socket->tunnel,
- 0, /* Corking */
- 1, /* Priority */
- socket->retransmit_timeout,
- &socket->other_peer,
- ntohs (message->header.size),
- &send_message_notify,
- socket);
+ GNUNET_MESH_notify_transmit_ready (socket->tunnel,
+ GNUNET_NO, /* Corking */
+ socket->mesh_retry_timeout,
+ &socket->other_peer,
+ ntohs (message->header.size),
+ &send_message_notify,
+ socket);
}
}
@@ -610,41 +739,11 @@ copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
size = ntohs (message->header.size);
msg_copy = GNUNET_malloc (size);
memcpy (msg_copy, message, size);
- queue_message (socket, msg_copy, finish_cb, finish_cb_cls);
+ queue_message (socket, msg_copy, finish_cb, finish_cb_cls, GNUNET_NO);
}
/**
- * Callback function for sending ack message
- *
- * @param cls closure the ACK message created in ack_task
- * @param size number of bytes available in buffer
- * @param buf where the callee should write the message
- * @return number of bytes written to buf
- */
-static size_t
-send_ack_notify (void *cls, size_t size, void *buf)
-{
- struct GNUNET_STREAM_Socket *socket = cls;
-
- if (0 == size)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s called with size 0\n", __func__);
- return 0;
- }
- GNUNET_assert (ntohs (socket->ack_msg->header.header.size) <= size);
-
- size = ntohs (socket->ack_msg->header.header.size);
- memcpy (buf, socket->ack_msg, size);
-
- GNUNET_free (socket->ack_msg);
- socket->ack_msg = NULL;
- socket->ack_transmit_handle = NULL;
- return size;
-}
-
-/**
* Writes data using the given socket. The amount of data written is limited by
* the receiver_window_size
*
@@ -653,6 +752,7 @@ send_ack_notify (void *cls, size_t size, void *buf)
static void
write_data (struct GNUNET_STREAM_Socket *socket);
+
/**
* Task for retransmitting data messages if they aren't ACK before their ack
* deadline
@@ -661,17 +761,16 @@ write_data (struct GNUNET_STREAM_Socket *socket);
* @param tc the Task context
*/
static void
-retransmission_timeout_task (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+data_retransmission_task (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct GNUNET_STREAM_Socket *socket = cls;
- if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
+ socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
+ if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
return;
-
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Retransmitting DATA...\n", GNUNET_i2s (&socket->other_peer));
- socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
write_data (socket);
}
@@ -689,11 +788,9 @@ ack_task (void *cls,
struct GNUNET_STREAM_Socket *socket = cls;
struct GNUNET_STREAM_AckMessage *ack_msg;
- if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
- {
- return;
- }
socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
+ if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
+ return;
/* Create the ACK Message */
ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
ack_msg->header.header.size = htons (sizeof (struct
@@ -703,17 +800,8 @@ ack_task (void *cls,
ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
ack_msg->receive_window_remaining =
htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
- socket->ack_msg = ack_msg;
- /* Request MESH for sending ACK */
- socket->ack_transmit_handle =
- GNUNET_MESH_notify_transmit_ready (socket->tunnel,
- 0, /* Corking */
- 1, /* Priority */
- socket->retransmit_timeout,
- &socket->other_peer,
- ntohs (ack_msg->header.header.size),
- &send_ack_notify,
- socket);
+ /* Queue up ACK for immediate sending */
+ queue_message (socket, &ack_msg->header, NULL, NULL, GNUNET_YES);
}
@@ -731,9 +819,11 @@ close_msg_retransmission_task (void *cls,
struct GNUNET_STREAM_MessageHeader *msg;
struct GNUNET_STREAM_Socket *socket;
+ shutdown_handle->close_msg_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
GNUNET_assert (NULL != shutdown_handle);
+ if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
+ return;
socket = shutdown_handle->socket;
-
msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
switch (shutdown_handle->operation)
@@ -753,7 +843,7 @@ close_msg_retransmission_task (void *cls,
GNUNET_SCHEDULER_NO_TASK;
return;
}
- queue_message (socket, msg, NULL, NULL);
+ queue_message (socket, msg, NULL, NULL, GNUNET_NO);
shutdown_handle->close_msg_retransmission_task_id =
GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
&close_msg_retransmission_task,
@@ -806,41 +896,29 @@ ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
static void
write_data (struct GNUNET_STREAM_Socket *socket)
{
- struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
- int packet; /* Although an int, should never be negative */
- int ack_packet;
-
- ack_packet = -1;
- /* Find the last acknowledged packet */
- for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
- {
- if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
- packet))
- ack_packet = packet;
- else if (NULL == io_handle->messages[packet])
- break;
- }
- /* Resend packets which weren't ack'ed */
- for (packet=0; packet < ack_packet; packet++)
+ struct GNUNET_STREAM_WriteHandle *io_handle = socket->write_handle;
+ unsigned int packet;
+
+ for (packet=0; packet < io_handle->packets_sent; packet++)
{
if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
- packet))
+ packet))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Placing DATA message with sequence %u in send queue\n",
- GNUNET_i2s (&socket->other_peer),
- ntohl (io_handle->messages[packet]->sequence_number));
+ "%s: Retransmitting DATA message with sequence %u\n",
+ GNUNET_i2s (&socket->other_peer),
+ ntohl (io_handle->messages[packet]->sequence_number));
copy_and_queue_message (socket,
- &io_handle->messages[packet]->header,
- NULL,
- NULL);
+ &io_handle->messages[packet]->header,
+ NULL,
+ NULL);
}
}
- packet = ack_packet + 1;
/* Now send new packets if there is enough buffer space */
- while ( (NULL != io_handle->messages[packet]) &&
- (socket->receiver_window_available
- >= ntohs (io_handle->messages[packet]->header.header.size)) )
+ while ((packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH) &&
+ (NULL != io_handle->messages[packet]) &&
+ (socket->receiver_window_available
+ >= ntohs (io_handle->messages[packet]->header.header.size)))
{
socket->receiver_window_available -=
ntohs (io_handle->messages[packet]->header.header.size);
@@ -854,12 +932,42 @@ write_data (struct GNUNET_STREAM_Socket *socket)
NULL);
packet++;
}
- if (GNUNET_SCHEDULER_NO_TASK == socket->retransmission_timeout_task_id)
- socket->retransmission_timeout_task_id =
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
- (GNUNET_TIME_UNIT_SECONDS, 8),
- &retransmission_timeout_task,
- socket);
+ io_handle->packets_sent = packet;
+ if (GNUNET_SCHEDULER_NO_TASK == socket->data_retransmission_task_id)
+ {
+ socket->data_retransmit_timeout = GNUNET_TIME_STD_BACKOFF
+ (socket->data_retransmit_timeout);
+ socket->data_retransmission_task_id =
+ GNUNET_SCHEDULER_add_delayed (socket->data_retransmit_timeout,
+ &data_retransmission_task,
+ socket);
+ }
+}
+
+
+/**
+ * Cleansup the sockets read handle
+ *
+ * @param socket the socket whose read handle has to be cleanedup
+ */
+static void
+cleanup_read_handle (struct GNUNET_STREAM_Socket *socket)
+{
+ struct GNUNET_STREAM_ReadHandle *read_handle;
+
+ read_handle = socket->read_handle;
+ /* Read io time task should be there; if it is already executed then this
+ read handle is not valid; However upon scheduler shutdown the read io task
+ may be executed before */
+ if (GNUNET_SCHEDULER_NO_TASK != read_handle->read_io_timeout_task_id)
+ GNUNET_SCHEDULER_cancel (read_handle->read_io_timeout_task_id);
+ /* reading task may be present; if so we have to stop it */
+ if (GNUNET_SCHEDULER_NO_TASK != read_handle->read_task_id)
+ GNUNET_SCHEDULER_cancel (read_handle->read_task_id);
+ if (GNUNET_SCHEDULER_NO_TASK != read_handle->probe_data_availability_task_id)
+ GNUNET_SCHEDULER_cancel (read_handle->probe_data_availability_task_id);
+ GNUNET_free (read_handle);
+ socket->read_handle = NULL;
}
@@ -874,22 +982,23 @@ call_read_processor (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct GNUNET_STREAM_Socket *socket = cls;
+ struct GNUNET_STREAM_ReadHandle *read_handle;
+ GNUNET_STREAM_DataProcessor proc;
+ void *proc_cls;
size_t read_size;
size_t valid_read_size;
unsigned int packet;
uint32_t sequence_increase;
uint32_t offset_increase;
- socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
+ read_handle = socket->read_handle;
+ GNUNET_assert (NULL != read_handle);
+ read_handle->read_task_id = GNUNET_SCHEDULER_NO_TASK;
if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
return;
-
if (NULL == socket->receive_buffer)
return;
-
- GNUNET_assert (NULL != socket->read_handle);
- GNUNET_assert (NULL != socket->read_handle->proc);
-
+ GNUNET_assert (NULL != read_handle->proc);
/* Check the bitmap for any holes */
for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
{
@@ -902,51 +1011,46 @@ call_read_processor (void *cls,
valid_read_size =
socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
GNUNET_assert (0 != valid_read_size);
- /* Cancel the read_io_timeout_task */
- GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task_id);
- socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
+ proc = read_handle->proc;
+ proc_cls = read_handle->proc_cls;
+ cleanup_read_handle (socket);
/* Call the data processor */
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Calling read processor\n",
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Calling read processor\n",
GNUNET_i2s (&socket->other_peer));
- read_size =
- socket->read_handle->proc (socket->read_handle->proc_cls,
- socket->status,
- socket->receive_buffer + socket->copy_offset,
- valid_read_size);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Read processor read %d bytes\n",
+ read_size = proc (proc_cls, GNUNET_STREAM_OK,
+ socket->receive_buffer + socket->copy_offset,
+ valid_read_size);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Read processor read %d bytes\n",
GNUNET_i2s (&socket->other_peer), read_size);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Read processor completed successfully\n",
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Read processor completed successfully\n",
GNUNET_i2s (&socket->other_peer));
- /* Free the read handle */
- GNUNET_free (socket->read_handle);
- socket->read_handle = NULL;
GNUNET_assert (read_size <= valid_read_size);
socket->copy_offset += read_size;
/* Determine upto which packet we can remove from the buffer */
for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
{
if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
- { packet++; break; }
+ {
+ packet++;
+ break;
+ }
if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
break;
}
-
/* If no packets can be removed we can't move the buffer */
- if (0 == packet) return;
+ if (0 == packet)
+ return;
sequence_increase = packet;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Sequence increase after read processor completion: %u\n",
GNUNET_i2s (&socket->other_peer), sequence_increase);
-
/* Shift the data in the receive buffer */
- memmove (socket->receive_buffer,
- socket->receive_buffer
- + socket->receive_buffer_boundaries[sequence_increase-1],
- socket->receive_buffer_size
- - socket->receive_buffer_boundaries[sequence_increase-1]);
+ socket->receive_buffer =
+ memmove (socket->receive_buffer,
+ socket->receive_buffer
+ + socket->receive_buffer_boundaries[sequence_increase-1],
+ socket->receive_buffer_size
+ - socket->receive_buffer_boundaries[sequence_increase-1]);
/* Shift the bitmap */
socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
/* Set read_sequence_number */
@@ -960,11 +1064,20 @@ call_read_processor (void *cls,
/* Fix relative boundaries */
for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
{
- if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
+ if (packet < (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase))
{
- socket->receive_buffer_boundaries[packet] =
- socket->receive_buffer_boundaries[packet + sequence_increase]
- - offset_increase;
+ uint32_t ahead_buffer_boundary;
+
+ ahead_buffer_boundary =
+ socket->receive_buffer_boundaries[packet + sequence_increase];
+ if (0 == ahead_buffer_boundary)
+ socket->receive_buffer_boundaries[packet] = 0;
+ else
+ {
+ GNUNET_assert (offset_increase < ahead_buffer_boundary);
+ socket->receive_buffer_boundaries[packet] =
+ ahead_buffer_boundary - offset_increase;
+ }
}
else
socket->receive_buffer_boundaries[packet] = 0;
@@ -979,27 +1092,30 @@ call_read_processor (void *cls,
* @param tc the task context
*/
static void
-read_io_timeout (void *cls,
+read_io_timeout (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct GNUNET_STREAM_Socket *socket = cls;
+ struct GNUNET_STREAM_ReadHandle *read_handle;
GNUNET_STREAM_DataProcessor proc;
void *proc_cls;
- socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
- if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
+ read_handle = socket->read_handle;
+ GNUNET_assert (NULL != read_handle);
+ read_handle->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
+ if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
+ return;
+ if (read_handle->read_task_id != GNUNET_SCHEDULER_NO_TASK)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Read task timedout - Cancelling it\n",
GNUNET_i2s (&socket->other_peer));
- GNUNET_SCHEDULER_cancel (socket->read_task_id);
- socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
+ GNUNET_SCHEDULER_cancel (read_handle->read_task_id);
+ read_handle->read_task_id = GNUNET_SCHEDULER_NO_TASK;
}
- GNUNET_assert (NULL != socket->read_handle);
- proc = socket->read_handle->proc;
- proc_cls = socket->read_handle->proc_cls;
-
- GNUNET_free (socket->read_handle);
+ proc = read_handle->proc;
+ proc_cls = read_handle->proc_cls;
+ GNUNET_free (read_handle);
socket->read_handle = NULL;
/* Call the read processor to signal timeout */
proc (proc_cls,
@@ -1028,6 +1144,7 @@ handle_data (struct GNUNET_STREAM_Socket *socket,
const struct GNUNET_ATS_Information*atsi)
{
const void *payload;
+ struct GNUNET_TIME_Relative ack_deadline_rel;
uint32_t bytes_needed;
uint32_t relative_offset;
uint32_t relative_sequence_number;
@@ -1039,28 +1156,24 @@ handle_data (struct GNUNET_STREAM_Socket *socket,
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
-
- if (0 != memcmp (sender,
- &socket->other_peer,
- sizeof (struct GNUNET_PeerIdentity)))
+ if (0 != memcmp (sender, &socket->other_peer,
+ sizeof (struct GNUNET_PeerIdentity)))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received DATA from non-confirming peer\n",
- GNUNET_i2s (&socket->other_peer));
+ "%s: Received DATA from non-confirming peer\n",
+ GNUNET_i2s (&socket->other_peer));
return GNUNET_YES;
}
-
switch (socket->state)
{
case STATE_ESTABLISHED:
case STATE_TRANSMIT_CLOSED:
- case STATE_TRANSMIT_CLOSE_WAIT:
-
+ case STATE_TRANSMIT_CLOSE_WAIT:
/* check if the message's sequence number is in the range we are
expecting */
relative_sequence_number =
ntohl (msg->sequence_number) - socket->read_sequence_number;
- if ( relative_sequence_number > GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
+ if ( relative_sequence_number >= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Ignoring received message with sequence number %u\n",
@@ -1076,8 +1189,7 @@ handle_data (struct GNUNET_STREAM_Socket *socket,
socket);
}
return GNUNET_YES;
- }
-
+ }
/* Check if we have already seen this message */
if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
relative_sequence_number))
@@ -1091,20 +1203,14 @@ handle_data (struct GNUNET_STREAM_Socket *socket,
{
socket->ack_task_id =
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
- (msg->ack_deadline),
- &ack_task,
- socket);
+ (msg->ack_deadline), &ack_task, socket);
}
return GNUNET_YES;
}
-
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Receiving DATA with sequence number: %u and size: %d from %s\n",
- GNUNET_i2s (&socket->other_peer),
- ntohl (msg->sequence_number),
- ntohs (msg->header.header.size),
- GNUNET_i2s (&socket->other_peer));
-
+ GNUNET_i2s (&socket->other_peer), ntohl (msg->sequence_number),
+ ntohs (msg->header.header.size), GNUNET_i2s (&socket->other_peer));
/* Check if we have to allocate the buffer */
size -= sizeof (struct GNUNET_STREAM_DataMessage);
relative_offset = ntohl (msg->offset) - socket->read_offset;
@@ -1121,54 +1227,67 @@ handle_data (struct GNUNET_STREAM_Socket *socket,
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Cannot accommodate packet %d as buffer is full\n",
- GNUNET_i2s (&socket->other_peer),
- ntohl (msg->sequence_number));
+ GNUNET_i2s (&socket->other_peer), ntohl (msg->sequence_number));
return GNUNET_YES;
}
}
-
/* Copy Data to buffer */
payload = &msg[1];
GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
- memcpy (socket->receive_buffer + relative_offset,
- payload,
- size);
+ memcpy (socket->receive_buffer + relative_offset, payload, size);
socket->receive_buffer_boundaries[relative_sequence_number] =
- relative_offset + size;
-
+ relative_offset + size;
/* Modify the ACK bitmap */
- ackbitmap_modify_bit (&socket->ack_bitmap,
- relative_sequence_number,
- GNUNET_YES);
-
+ ackbitmap_modify_bit (&socket->ack_bitmap, relative_sequence_number,
+ GNUNET_YES);
/* Start ACK sending task if one is not already present */
+ ack_deadline_rel = GNUNET_TIME_relative_ntoh (msg->ack_deadline);
if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
{
+ ack_deadline_rel =
+ GNUNET_TIME_relative_min (ack_deadline_rel,
+ GNUNET_TIME_relative_multiply
+ (GNUNET_TIME_UNIT_SECONDS, 300));
socket->ack_task_id =
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
- (msg->ack_deadline),
- &ack_task,
- socket);
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
+ (msg->ack_deadline), &ack_task, socket);
+ socket->ack_time_registered = GNUNET_TIME_absolute_get ();
+ socket->ack_time_deadline = ack_deadline_rel;
+ }
+ else
+ {
+ struct GNUNET_TIME_Relative ack_time_past;
+ struct GNUNET_TIME_Relative ack_time_remaining;
+ struct GNUNET_TIME_Relative ack_time_min;
+ ack_time_past =
+ GNUNET_TIME_absolute_get_duration (socket->ack_time_registered);
+ ack_time_remaining = GNUNET_TIME_relative_subtract
+ (socket->ack_time_deadline, ack_time_past);
+ ack_time_min = GNUNET_TIME_relative_min (ack_time_remaining,
+ ack_deadline_rel);
+ if (0 == memcmp(&ack_deadline_rel, &ack_time_min,
+ sizeof (struct GNUNET_TIME_Relative)))
+ {
+ ack_deadline_rel = ack_time_min;
+ GNUNET_SCHEDULER_cancel (socket->ack_task_id);
+ socket->ack_task_id = GNUNET_SCHEDULER_add_delayed (ack_deadline_rel,
+ &ack_task, socket);
+ socket->ack_time_registered = GNUNET_TIME_absolute_get ();
+ socket->ack_time_deadline = ack_deadline_rel;
+ }
}
-
if ((NULL != socket->read_handle) /* A read handle is waiting */
/* There is no current read task */
- && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
+ && (GNUNET_SCHEDULER_NO_TASK == socket->read_handle->read_task_id)
/* We have the first packet */
- && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
- 0)))
+ && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap, 0)))
{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Scheduling read processor\n",
- GNUNET_i2s (&socket->other_peer));
-
- socket->read_task_id =
- GNUNET_SCHEDULER_add_now (&call_read_processor,
- socket);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Scheduling read processor\n",
+ GNUNET_i2s (&socket->other_peer));
+ socket->read_handle->read_task_id =
+ GNUNET_SCHEDULER_add_now (&call_read_processor, socket);
}
-
break;
-
default:
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Received data message when it cannot be handled\n",
@@ -1201,18 +1320,15 @@ client_handle_data (void *cls,
{
struct GNUNET_STREAM_Socket *socket = cls;
- return handle_data (socket,
- tunnel,
- sender,
- (const struct GNUNET_STREAM_DataMessage *) message,
- atsi);
+ return handle_data (socket, tunnel, sender,
+ (const struct GNUNET_STREAM_DataMessage *) message, atsi);
}
/**
* Callback to set state to ESTABLISHED
*
- * @param cls the closure from queue_message FIXME: document
+ * @param cls the closure NULL;
* @param socket the socket to requiring state change
*/
static void
@@ -1225,7 +1341,10 @@ set_state_established (void *cls,
socket->write_offset = 0;
socket->read_offset = 0;
socket->state = STATE_ESTABLISHED;
- /* FIXME: What if listen_cb is NULL */
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
+ socket->control_retransmission_task_id);
+ GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
+ socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
if (NULL != socket->lsocket)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1237,12 +1356,12 @@ set_state_established (void *cls,
&socket->other_peer))
{
socket->state = STATE_CLOSED;
- /* FIXME: We should close in a decent way */
+ /* FIXME: We should close in a decent way (send RST) */
GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
GNUNET_free (socket);
}
}
- else if (socket->open_cb)
+ else
socket->open_cb (socket->open_cls, socket);
}
@@ -1258,7 +1377,7 @@ set_state_hello_wait (void *cls,
struct GNUNET_STREAM_Socket *socket)
{
GNUNET_assert (STATE_INIT == socket->state);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Attaining HELLO_WAIT state\n",
GNUNET_i2s (&socket->other_peer));
socket->state = STATE_HELLO_WAIT;
@@ -1335,38 +1454,115 @@ set_state_closed (void *cls,
socket->state = STATE_CLOSED;
}
+
+/**
+ * Returns GNUNET_MESSAGE_TYPE_STREAM_HELLO
+ *
+ * @return the generate hello message
+ */
+static struct GNUNET_STREAM_MessageHeader *
+generate_hello (void)
+{
+ struct GNUNET_STREAM_MessageHeader *msg;
+
+ msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
+ msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
+ msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
+ return msg;
+}
+
+
/**
* Returns a new HelloAckMessage. Also sets the write sequence number for the
* socket
*
* @param socket the socket for which this HelloAckMessage has to be generated
+ * @param generate_seq GNUNET_YES to generate the write sequence number,
+ * GNUNET_NO to use the existing sequence number
* @return the HelloAckMessage
*/
static struct GNUNET_STREAM_HelloAckMessage *
-generate_hello_ack_msg (struct GNUNET_STREAM_Socket *socket)
+generate_hello_ack (struct GNUNET_STREAM_Socket *socket,
+ int generate_seq)
{
struct GNUNET_STREAM_HelloAckMessage *msg;
- /* Get the random sequence number */
- socket->write_sequence_number =
- GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Generated write sequence number %u\n",
- GNUNET_i2s (&socket->other_peer),
- (unsigned int) socket->write_sequence_number);
-
+ if (GNUNET_YES == generate_seq)
+ {
+ if (GNUNET_YES == socket->testing_active)
+ socket->write_sequence_number =
+ socket->testing_set_write_sequence_number_value;
+ else
+ socket->write_sequence_number =
+ GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
+ LOG_DEBUG ("%s: write sequence number %u\n",
+ GNUNET_i2s (&socket->other_peer),
+ (unsigned int) socket->write_sequence_number);
+ }
msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
msg->header.header.size =
htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
msg->sequence_number = htonl (socket->write_sequence_number);
msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
-
return msg;
}
/**
+ * Task for retransmitting control messages if they aren't ACK'ed before a
+ * deadline
+ *
+ * @param cls the socket
+ * @param tc the Task context
+ */
+static void
+control_retransmission_task (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_STREAM_Socket *socket = cls;
+
+ socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
+ if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
+ return;
+ LOG_DEBUG ("%s: Retransmitting a control message\n",
+ GNUNET_i2s (&socket->other_peer));
+ switch (socket->state)
+ {
+ case STATE_INIT:
+ GNUNET_break (0);
+ break;
+ case STATE_LISTEN:
+ GNUNET_break (0);
+ break;
+ case STATE_HELLO_WAIT:
+ if (NULL == socket->lsocket) /* We are client */
+ queue_message (socket, generate_hello (), NULL, NULL, GNUNET_NO);
+ else
+ queue_message (socket,
+ (struct GNUNET_STREAM_MessageHeader *)
+ generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
+ GNUNET_NO);
+ socket->control_retransmission_task_id =
+ GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
+ &control_retransmission_task, socket);
+ break;
+ case STATE_ESTABLISHED:
+ if (NULL == socket->lsocket)
+ queue_message (socket,
+ (struct GNUNET_STREAM_MessageHeader *)
+ generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
+ GNUNET_NO);
+ else
+ GNUNET_break (0);
+ break;
+ default:
+ GNUNET_break (0);
+ }
+}
+
+
+/**
* Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
*
* @param cls the socket (set from GNUNET_MESH_connect)
@@ -1390,9 +1586,8 @@ client_handle_hello_ack (void *cls,
const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
struct GNUNET_STREAM_HelloAckMessage *reply;
- if (0 != memcmp (sender,
- &socket->other_peer,
- sizeof (struct GNUNET_PeerIdentity)))
+ if (0 != memcmp (sender, &socket->other_peer,
+ sizeof (struct GNUNET_PeerIdentity)))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Received HELLO_ACK from non-confirming peer\n",
@@ -1400,11 +1595,8 @@ client_handle_hello_ack (void *cls,
return GNUNET_YES;
}
ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received HELLO_ACK from %s\n",
- GNUNET_i2s (&socket->other_peer),
- GNUNET_i2s (&socket->other_peer));
-
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received HELLO_ACK from %s\n",
+ GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
GNUNET_assert (socket->tunnel == tunnel);
switch (socket->state)
{
@@ -1415,27 +1607,24 @@ client_handle_hello_ack (void *cls,
GNUNET_i2s (&socket->other_peer),
(unsigned int) socket->read_sequence_number);
socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
- reply = generate_hello_ack_msg (socket);
- queue_message (socket,
- &reply->header,
- &set_state_established,
- NULL);
+ reply = generate_hello_ack (socket, GNUNET_YES);
+ queue_message (socket, &reply->header, &set_state_established,
+ NULL, GNUNET_NO);
return GNUNET_OK;
case STATE_ESTABLISHED:
- case STATE_RECEIVE_CLOSE_WAIT:
// call statistics (# ACKs ignored++)
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
+ socket->control_retransmission_task_id);
+ socket->control_retransmission_task_id =
+ GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
return GNUNET_OK;
- case STATE_INIT:
default:
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Server %s sent HELLO_ACK when in state %d\n",
- GNUNET_i2s (&socket->other_peer),
- GNUNET_i2s (&socket->other_peer),
- socket->state);
+ LOG_DEBUG ("%s: Server %s sent HELLO_ACK when in state %d\n",
+ GNUNET_i2s (&socket->other_peer),
+ GNUNET_i2s (&socket->other_peer), socket->state);
socket->state = STATE_CLOSED; // introduce STATE_ERROR?
return GNUNET_SYSERR;
}
-
}
@@ -1466,6 +1655,63 @@ client_handle_reset (void *cls,
/**
+ * Frees the socket's receive buffers, marks the socket as receive closed and
+ * calls the DataProcessor with GNUNET_STREAM_SHUTDOWN status if a read handle
+ * is present
+ *
+ * @param socket the socket
+ */
+static void
+do_receive_shutdown (struct GNUNET_STREAM_Socket *socket)
+{
+ socket->receive_closed = GNUNET_YES;
+ GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
+ socket->receive_buffer = NULL;
+ socket->receive_buffer_size = 0;
+ if (NULL != socket->read_handle)
+ {
+ GNUNET_STREAM_DataProcessor proc;
+ void *proc_cls;
+
+ proc = socket->read_handle->proc;
+ proc_cls = socket->read_handle->proc_cls;
+ GNUNET_STREAM_read_cancel (socket->read_handle);
+ socket->read_handle = NULL;
+ if (NULL != proc)
+ proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
+ }
+}
+
+
+/**
+ * Marks the socket as transmit closed and calls the CompletionContinuation with
+ * GNUNET_STREAM_SHUTDOWN status if a write handle is present
+ *
+ * @param socket the socket
+ */
+static void
+do_transmit_shutdown (struct GNUNET_STREAM_Socket *socket)
+{
+ socket->transmit_closed = GNUNET_YES;
+ /* If write handle is present call it with GNUNET_STREAM_SHUTDOWN to signal
+ that that stream has been shutdown */
+ if (NULL != socket->write_handle)
+ {
+ GNUNET_STREAM_CompletionContinuation wc;
+ void *wc_cls;
+
+ wc = socket->write_handle->write_cont;
+ wc_cls = socket->write_handle->write_cont_cls;
+ GNUNET_STREAM_write_cancel (socket->write_handle);
+ socket->write_handle = NULL;
+ if (NULL != wc)
+ wc (wc_cls,
+ GNUNET_STREAM_SHUTDOWN, 0);
+ }
+}
+
+
+/**
* Common message handler for handling TRANSMIT_CLOSE messages
*
* @param socket the socket through which the ack was received
@@ -1487,22 +1733,40 @@ handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
switch (socket->state)
{
- case STATE_ESTABLISHED:
- socket->state = STATE_RECEIVE_CLOSED;
-
- /* Send TRANSMIT_CLOSE_ACK */
- reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
- reply->header.type =
- htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
- reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
- queue_message (socket, reply, NULL, NULL);
+ case STATE_INIT:
+ case STATE_LISTEN:
+ case STATE_HELLO_WAIT:
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
+ GNUNET_i2s (&socket->other_peer));
+ return GNUNET_OK;
+ default:
break;
-
+ }
+ /* Send TRANSMIT_CLOSE_ACK */
+ reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
+ reply->header.type =
+ htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
+ reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
+ queue_message (socket, reply, NULL, NULL, GNUNET_NO);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received TRANSMIT_CLOSE from %s\n",
+ GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
+ switch(socket->state)
+ {
+ case STATE_RECEIVE_CLOSED:
+ case STATE_RECEIVE_CLOSE_WAIT:
+ case STATE_CLOSE_WAIT:
+ case STATE_CLOSED:
+ return GNUNET_OK;
default:
- /* FIXME: Call statistics? */
break;
}
- return GNUNET_YES;
+ do_receive_shutdown (socket);
+ if (GNUNET_YES == socket->transmit_closed)
+ socket->state = STATE_CLOSED;
+ else
+ socket->state = STATE_RECEIVE_CLOSED;
+ return GNUNET_OK;
}
@@ -1537,6 +1801,28 @@ client_handle_transmit_close (void *cls,
/**
+ * Task for calling the shutdown continuation callback
+ *
+ * @param cls the socket
+ * @param tc the scheduler task context
+ */
+static void
+call_cont_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_STREAM_Socket *socket = cls;
+
+ GNUNET_assert (NULL != socket->shutdown_handle);
+ socket->shutdown_handle->call_cont_task_id = GNUNET_SCHEDULER_NO_TASK;
+ if (NULL != socket->shutdown_handle->completion_cb)
+ socket->shutdown_handle->completion_cb
+ (socket->shutdown_handle->completion_cls,
+ socket->shutdown_handle->operation);
+ GNUNET_free (socket->shutdown_handle);
+ socket->shutdown_handle = NULL;
+}
+
+
+/**
* Generic handler for GNUNET_MESSAGE_TYPE_STREAM_*_CLOSE_ACK messages
*
* @param socket the socket
@@ -1561,12 +1847,12 @@ handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
shutdown_handle = socket->shutdown_handle;
if (NULL == shutdown_handle)
{
+ /* This may happen when the shudown handle is cancelled */
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Received CLOSE_ACK when shutdown handle is NULL\n",
GNUNET_i2s (&socket->other_peer));
return GNUNET_OK;
}
-
switch (operation)
{
case SHUT_RDWR:
@@ -1577,25 +1863,20 @@ handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Received CLOSE_ACK when shutdown handle is not for "
- "SHUT_RDWR\n",
- GNUNET_i2s (&socket->other_peer));
+ "SHUT_RDWR\n", GNUNET_i2s (&socket->other_peer));
return GNUNET_OK;
}
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received CLOSE_ACK from %s\n",
- GNUNET_i2s (&socket->other_peer),
- GNUNET_i2s (&socket->other_peer));
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received CLOSE_ACK from %s\n",
+ GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
socket->state = STATE_CLOSED;
break;
default:
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received CLOSE_ACK when in it not expected\n",
+ "%s: Received CLOSE_ACK when it is not expected\n",
GNUNET_i2s (&socket->other_peer));
return GNUNET_OK;
}
break;
-
case SHUT_RD:
switch (socket->state)
{
@@ -1604,24 +1885,19 @@ handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Received RECEIVE_CLOSE_ACK when shutdown handle "
- "is not for SHUT_RD\n",
- GNUNET_i2s (&socket->other_peer));
+ "is not for SHUT_RD\n", GNUNET_i2s (&socket->other_peer));
return GNUNET_OK;
}
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received RECEIVE_CLOSE_ACK from %s\n",
- GNUNET_i2s (&socket->other_peer),
- GNUNET_i2s (&socket->other_peer));
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received RECEIVE_CLOSE_ACK from %s\n",
+ GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
socket->state = STATE_RECEIVE_CLOSED;
break;
default:
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received RECEIVE_CLOSE_ACK when in it not expected\n",
+ "%s: Received RECEIVE_CLOSE_ACK when it is not expected\n",
GNUNET_i2s (&socket->other_peer));
return GNUNET_OK;
}
-
break;
case SHUT_WR:
switch (socket->state)
@@ -1635,37 +1911,29 @@ handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
GNUNET_i2s (&socket->other_peer));
return GNUNET_OK;
}
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received TRANSMIT_CLOSE_ACK from %s\n",
- GNUNET_i2s (&socket->other_peer),
- GNUNET_i2s (&socket->other_peer));
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received TRANSMIT_CLOSE_ACK from %s\n",
+ GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
socket->state = STATE_TRANSMIT_CLOSED;
break;
default:
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received TRANSMIT_CLOSE_ACK when in it not expected\n",
- GNUNET_i2s (&socket->other_peer));
-
+ "%s: Received TRANSMIT_CLOSE_ACK when it is not expected\n",
+ GNUNET_i2s (&socket->other_peer));
return GNUNET_OK;
}
break;
default:
GNUNET_assert (0);
}
-
- if (NULL != shutdown_handle->completion_cb) /* Shutdown completion */
- shutdown_handle->completion_cb(shutdown_handle->completion_cls,
- operation);
- GNUNET_free (shutdown_handle); /* Free shutdown handle */
- socket->shutdown_handle = NULL;
+ shutdown_handle->call_cont_task_id = GNUNET_SCHEDULER_add_now
+ (&call_cont_task, socket);
if (GNUNET_SCHEDULER_NO_TASK
!= shutdown_handle->close_msg_retransmission_task_id)
{
GNUNET_SCHEDULER_cancel
(shutdown_handle->close_msg_retransmission_task_id);
shutdown_handle->close_msg_retransmission_task_id =
- GNUNET_SCHEDULER_NO_TASK;
+ GNUNET_SCHEDULER_NO_TASK;
}
return GNUNET_OK;
}
@@ -1734,26 +2002,31 @@ handle_receive_close (struct GNUNET_STREAM_Socket *socket,
return GNUNET_OK;
default:
break;
- }
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received RECEIVE_CLOSE from %s\n",
- GNUNET_i2s (&socket->other_peer),
- GNUNET_i2s (&socket->other_peer));
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received RECEIVE_CLOSE from %s\n",
+ GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
receive_close_ack =
GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
receive_close_ack->header.size =
htons (sizeof (struct GNUNET_STREAM_MessageHeader));
receive_close_ack->header.type =
htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK);
- queue_message (socket,
- receive_close_ack,
- &set_state_closed,
- NULL);
-
- /* FIXME: Handle the case where write handle is present; the write operation
- should be deemed as finised and the write continuation callback
- has to be called with the stream status GNUNET_STREAM_SHUTDOWN */
+ queue_message (socket, receive_close_ack, NULL, NULL, GNUNET_NO);
+ switch (socket->state)
+ {
+ case STATE_TRANSMIT_CLOSED:
+ case STATE_TRANSMIT_CLOSE_WAIT:
+ case STATE_CLOSED:
+ case STATE_CLOSE_WAIT:
+ return GNUNET_OK;
+ default:
+ break;
+ }
+ do_transmit_shutdown (socket);
+ if (GNUNET_YES == socket->receive_closed)
+ socket->state = STATE_CLOSED;
+ else
+ socket->state = STATE_TRANSMIT_CLOSED;
return GNUNET_OK;
}
@@ -1853,24 +2126,18 @@ handle_close (struct GNUNET_STREAM_Socket *socket,
default:
break;
}
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received CLOSE from %s\n",
- GNUNET_i2s (&socket->other_peer),
- GNUNET_i2s (&socket->other_peer));
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received CLOSE from %s\n",
+ GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK);
- queue_message (socket,
- close_ack,
- &set_state_closed,
- NULL);
- if (socket->state == STATE_CLOSED)
+ queue_message (socket, close_ack, &set_state_closed, NULL, GNUNET_NO);
+ if ((STATE_CLOSED == socket->state) || (STATE_CLOSE_WAIT == socket->state))
return GNUNET_OK;
-
- GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
- socket->receive_buffer = NULL;
- socket->receive_buffer_size = 0;
+ if (GNUNET_NO == socket->transmit_closed)
+ do_transmit_shutdown (socket);
+ if (GNUNET_NO == socket->receive_closed)
+ do_receive_shutdown (socket);
return GNUNET_OK;
}
@@ -1997,34 +2264,38 @@ server_handle_hello (void *cls,
&socket->other_peer,
sizeof (struct GNUNET_PeerIdentity)))
{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received HELLO from non-confirming peer\n",
- GNUNET_i2s (&socket->other_peer));
+ LOG_DEBUG ("%s: Received HELLO from non-confirming peer\n",
+ GNUNET_i2s (&socket->other_peer));
return GNUNET_YES;
}
-
- GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO ==
- ntohs (message->type));
+ GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO == ntohs (message->type));
GNUNET_assert (socket->tunnel == tunnel);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received HELLO from %s\n",
- GNUNET_i2s (&socket->other_peer),
- GNUNET_i2s (&socket->other_peer));
-
- if (STATE_INIT == socket->state)
- {
- reply = generate_hello_ack_msg (socket);
- queue_message (socket,
- &reply->header,
- &set_state_hello_wait,
- NULL);
- }
- else
+ LOG_DEBUG ("%s: Received HELLO from %s\n", GNUNET_i2s (&socket->other_peer),
+ GNUNET_i2s (&socket->other_peer));
+ switch (socket->state)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Client sent HELLO when in state %d\n", socket->state);
+ case STATE_INIT:
+ reply = generate_hello_ack (socket, GNUNET_YES);
+ queue_message (socket, &reply->header, &set_state_hello_wait, NULL,
+ GNUNET_NO);
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
+ socket->control_retransmission_task_id);
+ socket->control_retransmission_task_id =
+ GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
+ &control_retransmission_task, socket);
+ break;
+ case STATE_HELLO_WAIT:
+ /* Perhaps our HELLO_ACK was lost */
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
+ socket->control_retransmission_task_id);
+ GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
+ socket->control_retransmission_task_id =
+ GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
+ break;
+ default:
+ LOG_DEBUG( "%s: Client sent HELLO when in state %d\n",
+ GNUNET_i2s (&socket->other_peer), socket->state);
/* FIXME: Send RESET? */
-
}
return GNUNET_OK;
}
@@ -2057,8 +2328,9 @@ server_handle_hello_ack (void *cls,
ntohs (message->type));
GNUNET_assert (socket->tunnel == tunnel);
ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
- if (STATE_HELLO_WAIT == socket->state)
+ switch (socket->state)
{
+ case STATE_HELLO_WAIT:
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Received HELLO_ACK from %s\n",
GNUNET_i2s (&socket->other_peer),
@@ -2070,15 +2342,11 @@ server_handle_hello_ack (void *cls,
(unsigned int) socket->read_sequence_number);
socket->receiver_window_available =
ntohl (ack_message->receiver_window_size);
- /* Attain ESTABLISHED state */
set_state_established (NULL, socket);
- }
- else
- {
+ break;
+ default:
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Client sent HELLO_ACK when in state %d\n", socket->state);
- /* FIXME: Send RESET? */
-
+ "Client sent HELLO_ACK when in state %d\n", socket->state);
}
return GNUNET_OK;
}
@@ -2316,9 +2584,11 @@ handle_ack (struct GNUNET_STREAM_Socket *socket,
const struct GNUNET_STREAM_AckMessage *ack,
const struct GNUNET_ATS_Information*atsi)
{
+ struct GNUNET_STREAM_WriteHandle *write_handle;
+ uint64_t ack_bitmap;
unsigned int packet;
int need_retransmission;
-
+ uint32_t sequence_difference;
if (0 != memcmp (sender,
&socket->other_peer,
@@ -2329,7 +2599,6 @@ handle_ack (struct GNUNET_STREAM_Socket *socket,
GNUNET_i2s (&socket->other_peer));
return GNUNET_YES;
}
-
switch (socket->state)
{
case (STATE_ESTABLISHED):
@@ -2342,10 +2611,9 @@ handle_ack (struct GNUNET_STREAM_Socket *socket,
GNUNET_i2s (&socket->other_peer));
return GNUNET_OK;
}
- /* FIXME: increment in the base sequence number is breaking current flow
- */
- if (!((socket->write_sequence_number
- - ntohl (ack->base_sequence_number)) < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
+ sequence_difference =
+ socket->write_sequence_number - ntohl (ack->base_sequence_number);
+ if (!(sequence_difference <= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Received DATA_ACK with unexpected base sequence number\n",
@@ -2357,46 +2625,59 @@ handle_ack (struct GNUNET_STREAM_Socket *socket,
ntohl (ack->base_sequence_number));
return GNUNET_OK;
}
- /* FIXME: include the case when write_handle is cancelled - ignore the
- acks */
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Received DATA_ACK from %s\n",
- GNUNET_i2s (&socket->other_peer),
- GNUNET_i2s (&socket->other_peer));
-
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received DATA_ACK from %s\n",
+ GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
/* Cancel the retransmission task */
- if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
+ if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
{
- GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
- socket->retransmission_timeout_task_id =
- GNUNET_SCHEDULER_NO_TASK;
+ GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
+ socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
+ socket->data_retransmit_timeout = GNUNET_TIME_UNIT_SECONDS;
}
-
for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
{
+ if (NULL == socket->write_handle->messages[packet])
+ break;
+ /* BS: Base sequence from ack; PS: sequence num of current packet */
+ sequence_difference = ntohl (ack->base_sequence_number)
+ - ntohl (socket->write_handle->messages[packet]->sequence_number);
+ if (0 == sequence_difference)
+ break; /* The message in our handle is not yet received */
+ /* case where BS = PS + GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
+ /* sequence_difference <= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
+ ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
+ packet, GNUNET_YES);
+ }
+ if (((ntohl (ack->base_sequence_number)
+ - (socket->write_handle->max_ack_base_num))
+ <= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
+ {
+ socket->write_handle->max_ack_base_num = ntohl (ack->base_sequence_number);
+ socket->receiver_window_available =
+ ntohl (ack->receive_window_remaining);
+ }
+ else
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Ignoring to modify receive window available as base: %u, max_ack_base: %u\n",
+ ntohl (ack->base_sequence_number),
+ socket->write_handle->max_ack_base_num);
+ if ((packet == GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
+ || ((packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
+ && (NULL == socket->write_handle->messages[packet])))
+ goto call_write_cont_cb;
+ GNUNET_assert (ntohl
+ (socket->write_handle->messages[packet]->sequence_number)
+ == ntohl (ack->base_sequence_number));
+ /* Update our bitmap */
+ ack_bitmap = GNUNET_ntohll (ack->bitmap);
+ for (; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
+ {
if (NULL == socket->write_handle->messages[packet]) break;
- if (ntohl (ack->base_sequence_number)
- >= ntohl (socket->write_handle->messages[packet]->sequence_number))
- ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
- packet,
- GNUNET_YES);
- else
- if (GNUNET_YES ==
- ackbitmap_is_bit_set (&socket->write_handle->ack_bitmap,
- ntohl (socket->write_handle->messages[packet]->sequence_number)
- - ntohl (ack->base_sequence_number)))
- ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
- packet,
- GNUNET_YES);
+ if (ackbitmap_is_bit_set (&ack_bitmap, ntohl
+ (socket->write_handle->messages[packet]->sequence_number)
+ - ntohl (ack->base_sequence_number)))
+ ackbitmap_modify_bit (&socket->write_handle->ack_bitmap, packet, GNUNET_YES);
}
-
- /* Update the receive window remaining
- FIXME : Should update with the value from a data ack with greater
- sequence number */
- socket->receiver_window_available =
- ntohl (ack->receive_window_remaining);
-
/* Check if we have received all acknowledgements */
need_retransmission = GNUNET_NO;
for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
@@ -2412,26 +2693,26 @@ handle_ack (struct GNUNET_STREAM_Socket *socket,
if (GNUNET_YES == need_retransmission)
{
write_data (socket);
+ return GNUNET_OK;
}
- else /* We have to call the write continuation callback now */
+
+ call_write_cont_cb:
+ /* Free the packets */
+ for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
{
- /* Free the packets */
- for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
- {
- GNUNET_free_non_null (socket->write_handle->messages[packet]);
- }
- if (NULL != socket->write_handle->write_cont)
- socket->write_handle->write_cont
- (socket->write_handle->write_cont_cls,
- socket->status,
- socket->write_handle->size);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: Write completion callback completed\n",
- GNUNET_i2s (&socket->other_peer));
- /* We are done with the write handle - Freeing it */
- GNUNET_free (socket->write_handle);
- socket->write_handle = NULL;
+ GNUNET_free_non_null (socket->write_handle->messages[packet]);
}
+ write_handle = socket->write_handle;
+ socket->write_handle = NULL;
+ if (NULL != write_handle->write_cont)
+ write_handle->write_cont (write_handle->write_cont_cls,
+ GNUNET_STREAM_OK,
+ write_handle->size);
+ /* We are done with the write handle - Freeing it */
+ GNUNET_free (write_handle);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "%s: Write completion callback completed\n",
+ GNUNET_i2s (&socket->other_peer));
break;
default:
break;
@@ -2576,30 +2857,20 @@ mesh_peer_connect_callback (void *cls,
GNUNET_i2s(peer));
return;
}
-
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Target peer %s connected\n",
GNUNET_i2s (&socket->other_peer),
GNUNET_i2s (&socket->other_peer));
-
/* Set state to INIT */
socket->state = STATE_INIT;
-
/* Send HELLO message */
- message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
- message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
- message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
- queue_message (socket,
- message,
- &set_state_hello_wait,
- NULL);
-
- /* Call open callback */
- if (NULL == socket->open_cb)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "STREAM_open callback is NULL\n");
- }
+ message = generate_hello ();
+ queue_message (socket, message, &set_state_hello_wait, NULL, GNUNET_NO);
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
+ socket->control_retransmission_task_id);
+ socket->control_retransmission_task_id =
+ GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
+ &control_retransmission_task, socket);
}
@@ -2645,19 +2916,34 @@ new_tunnel_notify (void *cls,
/* FIXME: If a tunnel is already created, we should not accept new tunnels
from the same peer again until the socket is closed */
+ if (GNUNET_NO == lsocket->listening)
+ {
+ GNUNET_MESH_tunnel_destroy (tunnel);
+ return NULL;
+ }
socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
socket->other_peer = *initiator;
socket->tunnel = tunnel;
- socket->session_id = 0; /* FIXME */
socket->state = STATE_INIT;
socket->lsocket = lsocket;
-
+ socket->stat_handle = lsocket->stat_handle;
+ socket->retransmit_timeout = lsocket->retransmit_timeout;
+ socket->testing_active = lsocket->testing_active;
+ socket->testing_set_write_sequence_number_value =
+ lsocket->testing_set_write_sequence_number_value;
+ socket->max_payload_size = lsocket->max_payload_size;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Peer %s initiated tunnel to us\n",
GNUNET_i2s (&socket->other_peer),
GNUNET_i2s (&socket->other_peer));
-
- /* FIXME: Copy MESH handle from lsocket to socket */
+ if (NULL != socket->stat_handle)
+ {
+ GNUNET_STATISTICS_update (socket->stat_handle,
+ "total inbound connections received",
+ 1, GNUNET_NO);
+ GNUNET_STATISTICS_update (socket->stat_handle,
+ "inbound connections", 1, GNUNET_NO);
+ }
return socket;
}
@@ -2681,47 +2967,123 @@ tunnel_cleaner (void *cls,
void *tunnel_ctx)
{
struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
+ struct MessageQueue *head;
- if (tunnel != socket->tunnel)
- return;
-
+ GNUNET_assert (tunnel == socket->tunnel);
GNUNET_break_op(0);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: Peer %s has terminated connection abruptly\n",
GNUNET_i2s (&socket->other_peer),
GNUNET_i2s (&socket->other_peer));
-
- socket->status = GNUNET_STREAM_SHUTDOWN;
-
+ if (NULL != socket->stat_handle)
+ {
+ GNUNET_STATISTICS_update (socket->stat_handle,
+ "connections terminated abruptly", 1, GNUNET_NO);
+ GNUNET_STATISTICS_update (socket->stat_handle,
+ "inbound connections", -1, GNUNET_NO);
+ }
/* Clear Transmit handles */
if (NULL != socket->transmit_handle)
{
GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
socket->transmit_handle = NULL;
}
- if (NULL != socket->ack_transmit_handle)
- {
- GNUNET_MESH_notify_transmit_ready_cancel (socket->ack_transmit_handle);
- GNUNET_free (socket->ack_msg);
- socket->ack_msg = NULL;
- socket->ack_transmit_handle = NULL;
- }
/* Stop Tasks using socket->tunnel */
if (GNUNET_SCHEDULER_NO_TASK != socket->ack_task_id)
{
GNUNET_SCHEDULER_cancel (socket->ack_task_id);
socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
}
- if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
+ if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
+ {
+ GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
+ socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
+ }
+ /* Terminate the control retransmission tasks */
+ if (GNUNET_SCHEDULER_NO_TASK != socket->control_retransmission_task_id)
{
- GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
- socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
+ GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
+ socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
+ }
+ /* Clear existing message queue */
+ while (NULL != (head = socket->queue_head)) {
+ GNUNET_CONTAINER_DLL_remove (socket->queue_head,
+ socket->queue_tail,
+ head);
+ GNUNET_free (head->message);
+ GNUNET_free (head);
}
- /* FIXME: Cancel all other tasks using socket->tunnel */
socket->tunnel = NULL;
}
+/**
+ * Callback to signal timeout on lockmanager lock acquire
+ *
+ * @param cls the ListenSocket
+ * @param tc the scheduler task context
+ */
+static void
+lockmanager_acquire_timeout (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_STREAM_ListenSocket *lsocket = cls;
+ GNUNET_STREAM_ListenCallback listen_cb;
+ void *listen_cb_cls;
+
+ lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
+ listen_cb = lsocket->listen_cb;
+ listen_cb_cls = lsocket->listen_cb_cls;
+ if (NULL != listen_cb)
+ listen_cb (listen_cb_cls, NULL, NULL);
+}
+
+
+/**
+ * Callback to notify us on the status changes on app_port lock
+ *
+ * @param cls the ListenSocket
+ * @param domain the domain name of the lock
+ * @param lock the app_port
+ * @param status the current status of the lock
+ */
+static void
+lock_status_change_cb (void *cls, const char *domain, uint32_t lock,
+ enum GNUNET_LOCKMANAGER_Status status)
+{
+ struct GNUNET_STREAM_ListenSocket *lsocket = cls;
+
+ GNUNET_assert (lock == (uint32_t) lsocket->port);
+ if (GNUNET_LOCKMANAGER_SUCCESS == status)
+ {
+ lsocket->listening = GNUNET_YES;
+ if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
+ {
+ GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
+ lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ if (NULL == lsocket->mesh)
+ {
+ GNUNET_MESH_ApplicationType ports[] = {lsocket->port, 0};
+
+ lsocket->mesh = GNUNET_MESH_connect (lsocket->cfg,
+ lsocket, /* Closure */
+ &new_tunnel_notify,
+ &tunnel_cleaner,
+ server_message_handlers,
+ ports);
+ GNUNET_assert (NULL != lsocket->mesh);
+ if (NULL != lsocket->listen_ok_cb)
+ {
+ (void) lsocket->listen_ok_cb ();
+ }
+ }
+ }
+ if (GNUNET_LOCKMANAGER_RELEASE == status)
+ lsocket->listening = GNUNET_NO;
+}
+
+
/*****************/
/* API functions */
/*****************/
@@ -2734,7 +3096,8 @@ tunnel_cleaner (void *cls,
* @param target the target peer to which the stream has to be opened
* @param app_port the application port number which uniquely identifies this
* stream
- * @param open_cb this function will be called after stream has be established
+ * @param open_cb this function will be called after stream has be established;
+ * cannot be NULL
* @param open_cb_cls the closure for open_cb
* @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
* @return if successful it returns the stream socket; NULL if stream cannot be
@@ -2751,17 +3114,20 @@ GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
struct GNUNET_STREAM_Socket *socket;
enum GNUNET_STREAM_Option option;
GNUNET_MESH_ApplicationType ports[] = {app_port, 0};
- va_list vargs; /* Variable arguments */
+ va_list vargs;
+ uint16_t payload_size;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s\n", __func__);
+ GNUNET_assert (NULL != open_cb);
socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
socket->other_peer = *target;
socket->open_cb = open_cb;
socket->open_cls = open_cb_cls;
/* Set defaults */
- socket->retransmit_timeout =
- GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
+ socket->retransmit_timeout = TIME_REL_SECS (default_timeout);
+ socket->testing_active = GNUNET_NO;
+ socket->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
va_start (vargs, open_cb_cls); /* Parse variable args */
do {
option = va_arg (vargs, enum GNUNET_STREAM_Option);
@@ -2772,13 +3138,29 @@ GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
socket->retransmit_timeout = va_arg (vargs,
struct GNUNET_TIME_Relative);
break;
+ case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
+ socket->testing_active = GNUNET_YES;
+ socket->testing_set_write_sequence_number_value = va_arg (vargs,
+ uint32_t);
+ break;
+ case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
+ GNUNET_break (0); /* Option irrelevant in STREAM_open */
+ break;
+ case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
+ GNUNET_break (0); /* Option irrelevant in STREAM_open */
+ break;
+ case GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE:
+ payload_size = (uint16_t) va_arg (vargs, unsigned int);
+ GNUNET_assert (0 != payload_size);
+ if (payload_size < socket->max_payload_size)
+ socket->max_payload_size = payload_size;
+ break;
case GNUNET_STREAM_OPTION_END:
break;
}
} while (GNUNET_STREAM_OPTION_END != option);
va_end (vargs); /* End of variable args parsing */
socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
- 10, /* QUEUE size as parameter? */
socket, /* cls */
NULL, /* No inbound tunnel handler */
NULL, /* No in-tunnel cleaner */
@@ -2789,10 +3171,8 @@ GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
GNUNET_free (socket);
return NULL;
}
-
/* Now create the mesh tunnel to target */
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Creating MESH Tunnel\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating MESH Tunnel\n");
socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
NULL, /* Tunnel context */
&mesh_peer_connect_callback,
@@ -2801,9 +3181,8 @@ GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
GNUNET_assert (NULL != socket->tunnel);
GNUNET_MESH_peer_request_connect_add (socket->tunnel,
&socket->other_peer);
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s() END\n", __func__);
+ socket->stat_handle = GNUNET_STATISTICS_create ("stream", cfg);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
return socket;
}
@@ -2828,13 +3207,22 @@ GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
struct GNUNET_STREAM_MessageHeader *msg;
GNUNET_assert (NULL == socket->shutdown_handle);
-
handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle));
handle->socket = socket;
handle->completion_cb = completion_cb;
handle->completion_cls = completion_cls;
socket->shutdown_handle = handle;
-
+ if ( ((GNUNET_YES == socket->receive_closed) && (SHUT_RD == operation))
+ || ((GNUNET_YES == socket->transmit_closed) && (SHUT_WR == operation))
+ || ((GNUNET_YES == socket->transmit_closed)
+ && (GNUNET_YES == socket->receive_closed)
+ && (SHUT_RDWR == operation)) )
+ {
+ handle->operation = operation;
+ handle->call_cont_task_id = GNUNET_SCHEDULER_add_now (&call_cont_task,
+ socket);
+ return handle;
+ }
msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
switch (operation)
@@ -2846,10 +3234,9 @@ GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
"Existing read handle should be cancelled before shutting"
" down reading\n");
msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
- queue_message (socket,
- msg,
- &set_state_receive_close_wait,
- NULL);
+ queue_message (socket, msg, &set_state_receive_close_wait, NULL,
+ GNUNET_NO);
+ socket->receive_closed = GNUNET_YES;
break;
case SHUT_WR:
handle->operation = SHUT_WR;
@@ -2858,10 +3245,9 @@ GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
"Existing write handle should be cancelled before shutting"
" down writing\n");
msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
- queue_message (socket,
- msg,
- &set_state_transmit_close_wait,
- NULL);
+ queue_message (socket, msg, &set_state_transmit_close_wait, NULL,
+ GNUNET_NO);
+ socket->transmit_closed = GNUNET_YES;
break;
case SHUT_RDWR:
handle->operation = SHUT_RDWR;
@@ -2874,10 +3260,9 @@ GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
"Existing read handle should be cancelled before shutting"
" down reading\n");
msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
- queue_message (socket,
- msg,
- &set_state_close_wait,
- NULL);
+ queue_message (socket, msg, &set_state_close_wait, NULL, GNUNET_NO);
+ socket->transmit_closed = GNUNET_YES;
+ socket->receive_closed = GNUNET_YES;
break;
default:
LOG (GNUNET_ERROR_TYPE_WARNING,
@@ -2896,7 +3281,10 @@ GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
/**
- * Cancels a pending shutdown
+ * Cancels a pending shutdown. Note that the shutdown messages may already be
+ * sent and the stream is shutdown already for the operation given to
+ * GNUNET_STREAM_shutdown(). This function only clears up any retranmissions of
+ * shutdown messages and frees the shutdown handle.
*
* @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
*/
@@ -2905,8 +3293,10 @@ GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle)
{
if (GNUNET_SCHEDULER_NO_TASK != handle->close_msg_retransmission_task_id)
GNUNET_SCHEDULER_cancel (handle->close_msg_retransmission_task_id);
+ if (GNUNET_SCHEDULER_NO_TASK != handle->call_cont_task_id)
+ GNUNET_SCHEDULER_cancel (handle->call_cont_task_id);
+ handle->socket->shutdown_handle = NULL;
GNUNET_free (handle);
- return;
}
@@ -2924,42 +3314,32 @@ GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
{
LOG (GNUNET_ERROR_TYPE_WARNING,
"Closing STREAM socket when a read handle is pending\n");
+ GNUNET_STREAM_read_cancel (socket->read_handle);
}
if (NULL != socket->write_handle)
{
LOG (GNUNET_ERROR_TYPE_WARNING,
"Closing STREAM socket when a write handle is pending\n");
+ GNUNET_STREAM_write_cancel (socket->write_handle);
+ //socket->write_handle = NULL;
}
-
- if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
- {
- /* socket closed with read task pending!? */
- GNUNET_break (0);
- GNUNET_SCHEDULER_cancel (socket->read_task_id);
- socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
- }
-
- /* Terminate the ack'ing tasks if they are still present */
+ /* Terminate the ack'ing task if they are still present */
if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
{
GNUNET_SCHEDULER_cancel (socket->ack_task_id);
socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
}
-
+ /* Terminate the control retransmission tasks */
+ if (GNUNET_SCHEDULER_NO_TASK != socket->control_retransmission_task_id)
+ {
+ GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
+ }
/* Clear Transmit handles */
if (NULL != socket->transmit_handle)
{
GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
socket->transmit_handle = NULL;
}
- if (NULL != socket->ack_transmit_handle)
- {
- GNUNET_MESH_notify_transmit_ready_cancel (socket->ack_transmit_handle);
- GNUNET_free (socket->ack_msg);
- socket->ack_msg = NULL;
- socket->ack_transmit_handle = NULL;
- }
-
/* Clear existing message queue */
while (NULL != (head = socket->queue_head)) {
GNUNET_CONTAINER_DLL_remove (socket->queue_head,
@@ -2968,27 +3348,26 @@ GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
GNUNET_free (head->message);
GNUNET_free (head);
}
-
/* Close associated tunnel */
if (NULL != socket->tunnel)
{
GNUNET_MESH_tunnel_destroy (socket->tunnel);
socket->tunnel = NULL;
}
-
/* Close mesh connection */
- if (NULL != socket->mesh && NULL == socket->lsocket)
+ if ((NULL != socket->mesh) && (NULL == socket->lsocket))
{
GNUNET_MESH_disconnect (socket->mesh);
socket->mesh = NULL;
}
-
+ /* Close statistics connection */
+ if ( (NULL != socket->stat_handle) && (NULL == socket->lsocket) )
+ GNUNET_STATISTICS_destroy (socket->stat_handle, GNUNET_YES);
/* Release receive buffer */
if (NULL != socket->receive_buffer)
{
GNUNET_free (socket->receive_buffer);
}
-
GNUNET_free (socket);
}
@@ -3001,30 +3380,84 @@ GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
* @param listen_cb this function will be called when a peer tries to establish
* a stream with us
* @param listen_cb_cls closure for listen_cb
+ * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
* @return listen socket, NULL for any error
*/
struct GNUNET_STREAM_ListenSocket *
GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
GNUNET_MESH_ApplicationType app_port,
GNUNET_STREAM_ListenCallback listen_cb,
- void *listen_cb_cls)
+ void *listen_cb_cls,
+ ...)
{
- /* FIXME: Add variable args for passing configration options? */
struct GNUNET_STREAM_ListenSocket *lsocket;
- GNUNET_MESH_ApplicationType ports[] = {app_port, 0};
+ struct GNUNET_TIME_Relative listen_timeout;
+ enum GNUNET_STREAM_Option option;
+ va_list vargs;
+ uint16_t payload_size;
+ GNUNET_assert (NULL != listen_cb);
lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
+ lsocket->cfg = GNUNET_CONFIGURATION_dup (cfg);
+ lsocket->lockmanager = GNUNET_LOCKMANAGER_connect (lsocket->cfg);
+ if (NULL == lsocket->lockmanager)
+ {
+ GNUNET_CONFIGURATION_destroy (lsocket->cfg);
+ GNUNET_free (lsocket);
+ return NULL;
+ }
+ lsocket->listening = GNUNET_NO;/* We listen when we get a lock on app_port */
+ /* Set defaults */
+ lsocket->retransmit_timeout = TIME_REL_SECS (default_timeout);
+ lsocket->testing_active = GNUNET_NO;
+ lsocket->listen_ok_cb = NULL;
+ lsocket->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
+ listen_timeout = TIME_REL_SECS (60); /* A minute for listen timeout */
+ va_start (vargs, listen_cb_cls);
+ do {
+ option = va_arg (vargs, enum GNUNET_STREAM_Option);
+ switch (option)
+ {
+ case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
+ lsocket->retransmit_timeout = va_arg (vargs,
+ struct GNUNET_TIME_Relative);
+ break;
+ case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
+ lsocket->testing_active = GNUNET_YES;
+ lsocket->testing_set_write_sequence_number_value = va_arg (vargs,
+ uint32_t);
+ break;
+ case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
+ listen_timeout = GNUNET_TIME_relative_multiply
+ (GNUNET_TIME_UNIT_MILLISECONDS, va_arg (vargs, uint32_t));
+ break;
+ case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
+ lsocket->listen_ok_cb = va_arg (vargs,
+ GNUNET_STREAM_ListenSuccessCallback);
+ break;
+ case GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE:
+ payload_size = (uint16_t) va_arg (vargs, unsigned int);
+ GNUNET_assert (0 != payload_size);
+ if (payload_size < lsocket->max_payload_size)
+ lsocket->max_payload_size = payload_size;
+ break;
+ case GNUNET_STREAM_OPTION_END:
+ break;
+ }
+ } while (GNUNET_STREAM_OPTION_END != option);
+ va_end (vargs);
lsocket->port = app_port;
lsocket->listen_cb = listen_cb;
lsocket->listen_cb_cls = listen_cb_cls;
- lsocket->mesh = GNUNET_MESH_connect (cfg,
- 10, /* FIXME: QUEUE size as parameter? */
- lsocket, /* Closure */
- &new_tunnel_notify,
- &tunnel_cleaner,
- server_message_handlers,
- ports);
- GNUNET_assert (NULL != lsocket->mesh);
+ lsocket->locking_request =
+ GNUNET_LOCKMANAGER_acquire_lock (lsocket->lockmanager, locking_domain,
+ (uint32_t) lsocket->port,
+ &lock_status_change_cb, lsocket);
+ lsocket->lockmanager_acquire_timeout_task =
+ GNUNET_SCHEDULER_add_delayed (listen_timeout,
+ &lockmanager_acquire_timeout, lsocket);
+ lsocket->stat_handle = GNUNET_STATISTICS_create ("stream",
+ lsocket->cfg);
return lsocket;
}
@@ -3038,16 +3471,24 @@ void
GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
{
/* Close MESH connection */
- GNUNET_assert (NULL != lsocket->mesh);
- GNUNET_MESH_disconnect (lsocket->mesh);
-
+ if (NULL != lsocket->mesh)
+ GNUNET_MESH_disconnect (lsocket->mesh);
+ if (NULL != lsocket->stat_handle)
+ GNUNET_STATISTICS_destroy (lsocket->stat_handle, GNUNET_YES);
+ GNUNET_CONFIGURATION_destroy (lsocket->cfg);
+ if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
+ GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
+ if (NULL != lsocket->locking_request)
+ GNUNET_LOCKMANAGER_cancel_request (lsocket->locking_request);
+ if (NULL != lsocket->lockmanager)
+ GNUNET_LOCKMANAGER_disconnect (lsocket->lockmanager);
GNUNET_free (lsocket);
}
/**
* Tries to write the given data to the stream. The maximum size of data that
- * can be written as part of a write operation is (64 * (64000 - sizeof (struct
+ * can be written per a write operation is ~ 4MB (64 * (64000 - sizeof (struct
* GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
* violation, however only the said number of maximum bytes will be written.
*
@@ -3059,11 +3500,12 @@ GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
* stream
* @param write_cont_cls the closure
*
- * @return handle to cancel the operation; if a previous write is pending or
- * the stream has been shutdown for this operation then write_cont is
- * immediately called and NULL is returned.
+ * @return handle to cancel the operation; if a previous write is pending NULL
+ * is returned. If the stream has been shutdown for this operation or
+ * is broken then write_cont is immediately called and NULL is
+ * returned.
*/
-struct GNUNET_STREAM_IOWriteHandle *
+struct GNUNET_STREAM_WriteHandle *
GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
const void *data,
size_t size,
@@ -3071,25 +3513,29 @@ GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
GNUNET_STREAM_CompletionContinuation write_cont,
void *write_cont_cls)
{
+ struct GNUNET_STREAM_WriteHandle *io_handle;
+ struct GNUNET_STREAM_DataMessage *data_msg;
+ const void *sweep;
+ struct GNUNET_TIME_Relative ack_deadline;
unsigned int num_needed_packets;
unsigned int packet;
- struct GNUNET_STREAM_IOWriteHandle *io_handle;
uint32_t packet_size;
uint32_t payload_size;
- struct GNUNET_STREAM_DataMessage *data_msg;
- const void *sweep;
- struct GNUNET_TIME_Relative ack_deadline;
+ uint16_t max_data_packet_size;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s\n", __func__);
-
- /* Return NULL if there is already a write request pending */
if (NULL != socket->write_handle)
{
GNUNET_break (0);
return NULL;
}
-
+ if (NULL == socket->tunnel)
+ {
+ if (NULL != write_cont)
+ write_cont (write_cont_cls, GNUNET_STREAM_SYSERR, 0);
+ return NULL;
+ }
switch (socket->state)
{
case STATE_TRANSMIT_CLOSED:
@@ -3105,7 +3551,6 @@ GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
case STATE_LISTEN:
case STATE_HELLO_WAIT:
if (NULL != write_cont)
- /* FIXME: GNUNET_STREAM_SYSERR?? */
write_cont (write_cont_cls, GNUNET_STREAM_SYSERR, 0);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s() END\n", __func__);
@@ -3115,32 +3560,36 @@ GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
case STATE_RECEIVE_CLOSE_WAIT:
break;
}
-
- if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
- size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
- num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
- io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
+ if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * socket->max_payload_size < size)
+ size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * socket->max_payload_size;
+ num_needed_packets =
+ (size + (socket->max_payload_size - 1)) / socket->max_payload_size;
+ io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_WriteHandle));
io_handle->socket = socket;
io_handle->write_cont = write_cont;
io_handle->write_cont_cls = write_cont_cls;
io_handle->size = size;
+ io_handle->packets_sent = 0;
sweep = data;
/* FIXME: Remove the fixed delay for ack deadline; Set it to the value
determined from RTT */
ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
/* Divide the given buffer into packets for sending */
+ max_data_packet_size =
+ socket->max_payload_size + sizeof (struct GNUNET_STREAM_DataMessage);
+ io_handle->max_ack_base_num = socket->write_sequence_number;
for (packet=0; packet < num_needed_packets; packet++)
{
- if ((packet + 1) * max_payload_size < size)
+ if ((packet + 1) * socket->max_payload_size < size)
{
- payload_size = max_payload_size;
- packet_size = MAX_PACKET_SIZE;
+ payload_size = socket->max_payload_size;
+ packet_size = max_data_packet_size;
}
else
{
- payload_size = size - packet * max_payload_size;
- packet_size = payload_size + sizeof (struct
- GNUNET_STREAM_DataMessage);
+ payload_size = size - packet * socket->max_payload_size;
+ packet_size =
+ payload_size + sizeof (struct GNUNET_STREAM_DataMessage);
}
io_handle->messages[packet] = GNUNET_malloc (packet_size);
io_handle->messages[packet]->header.header.size = htons (packet_size);
@@ -3149,143 +3598,170 @@ GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
io_handle->messages[packet]->sequence_number =
htonl (socket->write_sequence_number++);
io_handle->messages[packet]->offset = htonl (socket->write_offset);
-
/* FIXME: Remove the fixed delay for ack deadline; Set it to the value
determined from RTT */
io_handle->messages[packet]->ack_deadline =
GNUNET_TIME_relative_hton (ack_deadline);
data_msg = io_handle->messages[packet];
/* Copy data from given buffer to the packet */
- memcpy (&data_msg[1],
- sweep,
- payload_size);
+ memcpy (&data_msg[1], sweep, payload_size);
sweep += payload_size;
socket->write_offset += payload_size;
}
+ /* ack the last data message. FIXME: remove when we figure out how to do this
+ using RTT */
+ io_handle->messages[num_needed_packets - 1]->ack_deadline =
+ GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO);
+ socket->data_retransmit_timeout = GNUNET_TIME_UNIT_SECONDS;
socket->write_handle = io_handle;
write_data (socket);
-
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s() END\n", __func__);
-
return io_handle;
}
+/**
+ * Function to check the ACK bitmap for any received messages and call the data processor
+ *
+ * @param cls the socket
+ * @param tc the scheduler task context
+ */
+static void
+probe_data_availability (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_STREAM_Socket *socket = cls;
+
+ GNUNET_assert (NULL != socket->read_handle);
+ socket->read_handle->probe_data_availability_task_id =
+ GNUNET_SCHEDULER_NO_TASK;
+ if (GNUNET_SCHEDULER_NO_TASK != socket->read_handle->read_task_id)
+ return; /* A task to call read processor is present */
+ if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
+ 0))
+ socket->read_handle->read_task_id
+ = GNUNET_SCHEDULER_add_now (&call_read_processor, socket);
+}
+
+
/**
- * Tries to read data from the stream.
+ * Tries to read data from the stream. Should not be called when another read
+ * handle is present; the existing read handle should be canceled with
+ * GNUNET_STREAM_read_cancel(). Only one read handle per socket is present at
+ * any time
*
* @param socket the socket representing a stream
* @param timeout the timeout period
* @param proc function to call with data (once only)
* @param proc_cls the closure for proc
- *
- * @return handle to cancel the operation; if the stream has been shutdown for
- * this type of opeartion then the DataProcessor is immediately
- * called with GNUNET_STREAM_SHUTDOWN as status and NULL if returned
+ * @return handle to cancel the operation; NULL is returned if the stream has
+ * been shutdown for this type of opeartion (the DataProcessor is
+ * immediately called with GNUNET_STREAM_SHUTDOWN as status)
*/
-struct GNUNET_STREAM_IOReadHandle *
+struct GNUNET_STREAM_ReadHandle *
GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
struct GNUNET_TIME_Relative timeout,
GNUNET_STREAM_DataProcessor proc,
void *proc_cls)
{
- struct GNUNET_STREAM_IOReadHandle *read_handle;
+ struct GNUNET_STREAM_ReadHandle *read_handle;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: %s()\n",
GNUNET_i2s (&socket->other_peer),
__func__);
-
- /* Return NULL if there is already a read handle; the user has to cancel that
- first before continuing or has to wait until it is completed */
- if (NULL != socket->read_handle) return NULL;
-
+ /* Only one read handle is permitted at any time; cancel the existing or wait
+ for it to complete */
+ GNUNET_assert (NULL == socket->read_handle);
GNUNET_assert (NULL != proc);
-
+ if (GNUNET_YES == socket->receive_closed)
+ return NULL;
switch (socket->state)
{
case STATE_RECEIVE_CLOSED:
case STATE_RECEIVE_CLOSE_WAIT:
case STATE_CLOSED:
case STATE_CLOSE_WAIT:
- proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s: %s() END\n",
GNUNET_i2s (&socket->other_peer),
__func__);
+ proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
return NULL;
default:
break;
}
-
- read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
+ read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ReadHandle));
read_handle->proc = proc;
read_handle->proc_cls = proc_cls;
+ read_handle->socket = socket;
socket->read_handle = read_handle;
-
- /* Check if we have a packet at bitmap 0 */
- if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
- 0))
- {
- socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
- socket);
-
- }
-
- /* Setup the read timeout task */
- socket->read_io_timeout_task_id =
- GNUNET_SCHEDULER_add_delayed (timeout,
- &read_io_timeout,
- socket);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s: %s() END\n",
- GNUNET_i2s (&socket->other_peer),
- __func__);
+ read_handle->probe_data_availability_task_id =
+ GNUNET_SCHEDULER_add_now (&probe_data_availability, socket);
+ read_handle->read_io_timeout_task_id =
+ GNUNET_SCHEDULER_add_delayed (timeout, &read_io_timeout, socket);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: %s() END\n",
+ GNUNET_i2s (&socket->other_peer), __func__);
return read_handle;
}
/**
- * Cancel pending write operation.
+ * Cancels pending write operation. Also cancels packet retransmissions which
+ * may have resulted otherwise.
+ *
+ * CAUTION: Normally a write operation is considered successful if the data
+ * given to it is sent and acknowledged by the receiver. As data is divided
+ * into packets, it is possible that not all packets are received by the
+ * receiver. Any missing packets are then retransmitted till the receiver
+ * acknowledges all packets or until a timeout . During this scenario if the
+ * write operation is cancelled all such retransmissions are also
+ * cancelled. This may leave the receiver's receive buffer incompletely filled
+ * as some missing packets are never retransmitted. So this operation should be
+ * used before shutting down transmission from our side or before closing the
+ * socket.
*
- * @param ioh handle to operation to cancel
+ * @param wh write operation handle to cancel
*/
void
-GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
+GNUNET_STREAM_write_cancel (struct GNUNET_STREAM_WriteHandle *wh)
{
- struct GNUNET_STREAM_Socket *socket = ioh->socket;
+ struct GNUNET_STREAM_Socket *socket = wh->socket;
unsigned int packet;
GNUNET_assert (NULL != socket->write_handle);
- GNUNET_assert (socket->write_handle == ioh);
-
- if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
+ GNUNET_assert (socket->write_handle == wh);
+ if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
{
- GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
- socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
+ GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
+ socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
}
-
for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
{
- if (NULL == ioh->messages[packet]) break;
- GNUNET_free (ioh->messages[packet]);
- }
-
+ if (NULL == wh->messages[packet]) break;
+ GNUNET_free (wh->messages[packet]);
+ }
GNUNET_free (socket->write_handle);
socket->write_handle = NULL;
- return;
}
/**
* Cancel pending read operation.
*
- * @param ioh handle to operation to cancel
+ * @param rh read operation handle to cancel
*/
void
-GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)
+GNUNET_STREAM_read_cancel (struct GNUNET_STREAM_ReadHandle *rh)
{
- return;
+ struct GNUNET_STREAM_Socket *socket;
+
+ socket = rh->socket;
+ GNUNET_assert (NULL != socket->read_handle);
+ GNUNET_assert (rh == socket->read_handle);
+ cleanup_read_handle (socket);
}
+
+/* end of stream_api.c */