diff options
Diffstat (limited to 'src/stream/stream_api.c')
-rw-r--r-- | src/stream/stream_api.c | 1814 |
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,< |