2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * Checks for matching the sender and socket->other_peer in server
26 * Add code for write io timeout
28 * Include retransmission for control messages
32 * @file stream/stream_api.c
33 * @brief Implementation of the stream library
34 * @author Sree Harsha Totakura
39 #include "gnunet_common.h"
40 #include "gnunet_crypto_lib.h"
41 #include "gnunet_stream_lib.h"
42 #include "stream_protocol.h"
44 #define LOG(kind,...) \
45 GNUNET_log_from (kind, "stream-api", __VA_ARGS__)
48 * The maximum packet size of a stream packet
50 #define MAX_PACKET_SIZE 512//64000
55 #define RECEIVE_BUFFER_SIZE 4096000
58 * The maximum payload a data message packet can carry
60 static const size_t max_payload_size =
61 MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
64 * states in the Protocol
69 * Client initialization state
74 * Listener initialization state
79 * Pre-connection establishment state
84 * State where a connection has been established
89 * State where the socket is closed on our side and waiting to be ACK'ed
91 STATE_RECEIVE_CLOSE_WAIT,
94 * State where the socket is closed for reading
99 * State where the socket is closed on our side and waiting to be ACK'ed
101 STATE_TRANSMIT_CLOSE_WAIT,
104 * State where the socket is closed for writing
106 STATE_TRANSMIT_CLOSED,
109 * State where the socket is closed on our side and waiting to be ACK'ed
114 * State where the socket is closed
121 * Functions of this type are called when a message is written
123 * @param cls the closure from queue_message
124 * @param socket the socket the written message was bound to
126 typedef void (*SendFinishCallback) (void *cls,
127 struct GNUNET_STREAM_Socket *socket);
131 * The send message queue
138 struct GNUNET_STREAM_MessageHeader *message;
141 * Callback to be called when the message is sent
143 SendFinishCallback finish_cb;
146 * The closure for finish_cb
151 * The next message in queue. Should be NULL in the last message
153 struct MessageQueue *next;
156 * The next message in queue. Should be NULL in the first message
158 struct MessageQueue *prev;
163 * The STREAM Socket Handler
165 struct GNUNET_STREAM_Socket
168 * Retransmission timeout
170 struct GNUNET_TIME_Relative retransmit_timeout;
173 * The Acknowledgement Bitmap
175 GNUNET_STREAM_AckBitmap ack_bitmap;
178 * Time when the Acknowledgement was queued
180 struct GNUNET_TIME_Absolute ack_time_registered;
183 * Queued Acknowledgement deadline
185 struct GNUNET_TIME_Relative ack_time_deadline;
190 struct GNUNET_MESH_Handle *mesh;
193 * The mesh tunnel handle
195 struct GNUNET_MESH_Tunnel *tunnel;
198 * Stream open closure
203 * Stream open callback
205 GNUNET_STREAM_OpenCallback open_cb;
208 * The current transmit handle (if a pending transmit request exists)
210 struct GNUNET_MESH_TransmitHandle *transmit_handle;
213 * The current act transmit handle (if a pending ack transmit request exists)
215 struct GNUNET_MESH_TransmitHandle *ack_transmit_handle;
218 * Pointer to the current ack message using in ack_task
220 struct GNUNET_STREAM_AckMessage *ack_msg;
223 * The current message associated with the transmit handle
225 struct MessageQueue *queue_head;
228 * The queue tail, should always point to the last message in queue
230 struct MessageQueue *queue_tail;
233 * The write IO_handle associated with this socket
235 struct GNUNET_STREAM_IOWriteHandle *write_handle;
238 * The read IO_handle associated with this socket
240 struct GNUNET_STREAM_IOReadHandle *read_handle;
243 * The shutdown handle associated with this socket
245 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
248 * Buffer for storing received messages
250 void *receive_buffer;
253 * The listen socket from which this socket is derived. Should be NULL if it
254 * is not a derived socket
256 struct GNUNET_STREAM_ListenSocket *lsocket;
259 * The peer identity of the peer at the other end of the stream
261 struct GNUNET_PeerIdentity other_peer;
264 * Task identifier for the read io timeout task
266 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
269 * Task identifier for retransmission task after timeout
271 GNUNET_SCHEDULER_TaskIdentifier retransmission_timeout_task_id;
274 * The task for sending timely Acks
276 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
279 * Task scheduled to continue a read operation.
281 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
284 * The state of the protocol associated with this socket
289 * The status of the socket
291 enum GNUNET_STREAM_Status status;
294 * The number of previous timeouts; FIXME: currently not used
296 unsigned int retries;
299 * The application port number (type: uint32_t)
301 GNUNET_MESH_ApplicationType app_port;
304 * The session id associated with this stream connection
305 * FIXME: Not used currently, may be removed
310 * Write sequence number. Set to random when sending HELLO(client) and
313 uint32_t write_sequence_number;
316 * Read sequence number. This number's value is determined during handshake
318 uint32_t read_sequence_number;
321 * The receiver buffer size
323 uint32_t receive_buffer_size;
326 * The receiver buffer boundaries
328 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
331 * receiver's available buffer after the last acknowledged packet
333 uint32_t receiver_window_available;
336 * The offset pointer used during write operation
338 uint32_t write_offset;
341 * The offset after which we are expecting data
343 uint32_t read_offset;
346 * The offset upto which user has read from the received buffer
348 uint32_t copy_offset;
353 * A socket for listening
355 struct GNUNET_STREAM_ListenSocket
360 struct GNUNET_MESH_Handle *mesh;
363 * The callback function which is called after successful opening socket
365 GNUNET_STREAM_ListenCallback listen_cb;
368 * The call back closure
374 * FIXME: Remove if not required!
376 GNUNET_MESH_ApplicationType port;
381 * The IO Write Handle
383 struct GNUNET_STREAM_IOWriteHandle
386 * The socket to which this write handle is associated
388 struct GNUNET_STREAM_Socket *socket;
391 * The packet_buffers associated with this Handle
393 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
396 * The write continuation callback
398 GNUNET_STREAM_CompletionContinuation write_cont;
401 * Write continuation closure
403 void *write_cont_cls;
406 * The bitmap of this IOHandle; Corresponding bit for a message is set when
407 * it has been acknowledged by the receiver
409 GNUNET_STREAM_AckBitmap ack_bitmap;
412 * Number of bytes in this write handle
421 struct GNUNET_STREAM_IOReadHandle
424 * Callback for the read processor
426 GNUNET_STREAM_DataProcessor proc;
429 * The closure pointer for the read processor callback
436 * Handle for Shutdown
438 struct GNUNET_STREAM_ShutdownHandle
441 * The socket associated with this shutdown handle
443 struct GNUNET_STREAM_Socket *socket;
446 * Shutdown completion callback
448 GNUNET_STREAM_ShutdownCompletion completion_cb;
451 * Closure for completion callback
453 void *completion_cls;
456 * Close message retransmission task id
458 GNUNET_SCHEDULER_TaskIdentifier close_msg_retransmission_task_id;
461 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
468 * Default value in seconds for various timeouts
470 static unsigned int default_timeout = 10;
474 * Callback function for sending queued message
476 * @param cls closure the socket
477 * @param size number of bytes available in buf
478 * @param buf where the callee should write the message
479 * @return number of bytes written to buf
482 send_message_notify (void *cls, size_t size, void *buf)
484 struct GNUNET_STREAM_Socket *socket = cls;
485 struct MessageQueue *head;
488 socket->transmit_handle = NULL; /* Remove the transmit handle */
489 head = socket->queue_head;
491 return 0; /* just to be safe */
492 if (0 == size) /* request timed out */
495 LOG (GNUNET_ERROR_TYPE_DEBUG,
496 "%s: Message sending timed out. Retry %d \n",
497 GNUNET_i2s (&socket->other_peer),
499 socket->transmit_handle =
500 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
503 /* FIXME: exponential backoff */
504 socket->retransmit_timeout,
506 ntohs (head->message->header.size),
507 &send_message_notify,
512 ret = ntohs (head->message->header.size);
513 GNUNET_assert (size >= ret);
514 memcpy (buf, head->message, ret);
515 if (NULL != head->finish_cb)
517 head->finish_cb (head->finish_cb_cls, socket);
519 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
522 GNUNET_free (head->message);
524 head = socket->queue_head;
525 if (NULL != head) /* more pending messages to send */
528 socket->transmit_handle =
529 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
532 /* FIXME: exponential backoff */
533 socket->retransmit_timeout,
535 ntohs (head->message->header.size),
536 &send_message_notify,
544 * Queues a message for sending using the mesh connection of a socket
546 * @param socket the socket whose mesh connection is used
547 * @param message the message to be sent
548 * @param finish_cb the callback to be called when the message is sent
549 * @param finish_cb_cls the closure for the callback
552 queue_message (struct GNUNET_STREAM_Socket *socket,
553 struct GNUNET_STREAM_MessageHeader *message,
554 SendFinishCallback finish_cb,
557 struct MessageQueue *queue_entity;
560 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
561 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
563 LOG (GNUNET_ERROR_TYPE_DEBUG,
564 "%s: Queueing message of type %d and size %d\n",
565 GNUNET_i2s (&socket->other_peer),
566 ntohs (message->header.type),
567 ntohs (message->header.size));
568 GNUNET_assert (NULL != message);
569 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
570 queue_entity->message = message;
571 queue_entity->finish_cb = finish_cb;
572 queue_entity->finish_cb_cls = finish_cb_cls;
573 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
576 if (NULL == socket->transmit_handle)
579 socket->transmit_handle =
580 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
583 socket->retransmit_timeout,
585 ntohs (message->header.size),
586 &send_message_notify,
593 * Copies a message and queues it for sending using the mesh connection of
596 * @param socket the socket whose mesh connection is used
597 * @param message the message to be sent
598 * @param finish_cb the callback to be called when the message is sent
599 * @param finish_cb_cls the closure for the callback
602 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
603 const struct GNUNET_STREAM_MessageHeader *message,
604 SendFinishCallback finish_cb,
607 struct GNUNET_STREAM_MessageHeader *msg_copy;
610 size = ntohs (message->header.size);
611 msg_copy = GNUNET_malloc (size);
612 memcpy (msg_copy, message, size);
613 queue_message (socket, msg_copy, finish_cb, finish_cb_cls);
618 * Callback function for sending ack message
620 * @param cls closure the ACK message created in ack_task
621 * @param size number of bytes available in buffer
622 * @param buf where the callee should write the message
623 * @return number of bytes written to buf
626 send_ack_notify (void *cls, size_t size, void *buf)
628 struct GNUNET_STREAM_Socket *socket = cls;
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "%s called with size 0\n", __func__);
636 GNUNET_assert (ntohs (socket->ack_msg->header.header.size) <= size);
638 size = ntohs (socket->ack_msg->header.header.size);
639 memcpy (buf, socket->ack_msg, size);
641 GNUNET_free (socket->ack_msg);
642 socket->ack_msg = NULL;
643 socket->ack_transmit_handle = NULL;
648 * Writes data using the given socket. The amount of data written is limited by
649 * the receiver_window_size
651 * @param socket the socket to use
654 write_data (struct GNUNET_STREAM_Socket *socket);
657 * Task for retransmitting data messages if they aren't ACK before their ack
660 * @param cls the socket
661 * @param tc the Task context
664 retransmission_timeout_task (void *cls,
665 const struct GNUNET_SCHEDULER_TaskContext *tc)
667 struct GNUNET_STREAM_Socket *socket = cls;
669 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
672 LOG (GNUNET_ERROR_TYPE_DEBUG,
673 "%s: Retransmitting DATA...\n", GNUNET_i2s (&socket->other_peer));
674 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
680 * Task for sending ACK message
682 * @param cls the socket
683 * @param tc the Task context
687 const struct GNUNET_SCHEDULER_TaskContext *tc)
689 struct GNUNET_STREAM_Socket *socket = cls;
690 struct GNUNET_STREAM_AckMessage *ack_msg;
692 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
696 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
697 /* Create the ACK Message */
698 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
699 ack_msg->header.header.size = htons (sizeof (struct
700 GNUNET_STREAM_AckMessage));
701 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
702 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
703 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
704 ack_msg->receive_window_remaining =
705 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
706 socket->ack_msg = ack_msg;
707 /* Request MESH for sending ACK */
708 socket->ack_transmit_handle =
709 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
712 socket->retransmit_timeout,
714 ntohs (ack_msg->header.header.size),
721 * Retransmission task for shutdown messages
723 * @param cls the shutdown handle
724 * @param tc the Task Context
727 close_msg_retransmission_task (void *cls,
728 const struct GNUNET_SCHEDULER_TaskContext *tc)
730 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle = cls;
731 struct GNUNET_STREAM_MessageHeader *msg;
732 struct GNUNET_STREAM_Socket *socket;
734 GNUNET_assert (NULL != shutdown_handle);
735 socket = shutdown_handle->socket;
737 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
738 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
739 switch (shutdown_handle->operation)
742 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
745 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
748 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
752 shutdown_handle->close_msg_retransmission_task_id =
753 GNUNET_SCHEDULER_NO_TASK;
756 queue_message (socket, msg, NULL, NULL);
757 shutdown_handle->close_msg_retransmission_task_id =
758 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
759 &close_msg_retransmission_task,
765 * Function to modify a bit in GNUNET_STREAM_AckBitmap
767 * @param bitmap the bitmap to modify
768 * @param bit the bit number to modify
769 * @param value GNUNET_YES to on, GNUNET_NO to off
772 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
776 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
777 if (GNUNET_YES == value)
778 *bitmap |= (1LL << bit);
780 *bitmap &= ~(1LL << bit);
785 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
787 * @param bitmap address of the bitmap that has to be checked
788 * @param bit the bit number to check
789 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
792 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
795 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
796 return 0 != (*bitmap & (1LL << bit));
801 * Writes data using the given socket. The amount of data written is limited by
802 * the receiver_window_size
804 * @param socket the socket to use
807 write_data (struct GNUNET_STREAM_Socket *socket)
809 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
810 int packet; /* Although an int, should never be negative */
814 /* Find the last acknowledged packet */
815 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
817 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
820 else if (NULL == io_handle->messages[packet])
823 /* Resend packets which weren't ack'ed */
824 for (packet=0; packet < ack_packet; packet++)
826 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
829 LOG (GNUNET_ERROR_TYPE_DEBUG,
830 "%s: Placing DATA message with sequence %u in send queue\n",
831 GNUNET_i2s (&socket->other_peer),
832 ntohl (io_handle->messages[packet]->sequence_number));
833 copy_and_queue_message (socket,
834 &io_handle->messages[packet]->header,
839 packet = ack_packet + 1;
840 /* Now send new packets if there is enough buffer space */
841 while ( (NULL != io_handle->messages[packet]) &&
842 (socket->receiver_window_available
843 >= ntohs (io_handle->messages[packet]->header.header.size)) &&
844 (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
846 socket->receiver_window_available -=
847 ntohs (io_handle->messages[packet]->header.header.size);
848 LOG (GNUNET_ERROR_TYPE_DEBUG,
849 "%s: Placing DATA message with sequence %u in send queue\n",
850 GNUNET_i2s (&socket->other_peer),
851 ntohl (io_handle->messages[packet]->sequence_number));
852 copy_and_queue_message (socket,
853 &io_handle->messages[packet]->header,
858 if (GNUNET_SCHEDULER_NO_TASK == socket->retransmission_timeout_task_id)
859 socket->retransmission_timeout_task_id =
860 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
861 (GNUNET_TIME_UNIT_SECONDS, 8),
862 &retransmission_timeout_task,
868 * Task for calling the read processor
870 * @param cls the socket
871 * @param tc the task context
874 call_read_processor (void *cls,
875 const struct GNUNET_SCHEDULER_TaskContext *tc)
877 struct GNUNET_STREAM_Socket *socket = cls;
879 size_t valid_read_size;
881 uint32_t sequence_increase;
882 uint32_t offset_increase;
884 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
885 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
888 if (NULL == socket->receive_buffer)
891 GNUNET_assert (NULL != socket->read_handle);
892 GNUNET_assert (NULL != socket->read_handle->proc);
894 /* Check the bitmap for any holes */
895 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
897 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
901 /* We only call read processor if we have the first packet */
902 GNUNET_assert (0 < packet);
904 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
905 GNUNET_assert (0 != valid_read_size);
906 /* Cancel the read_io_timeout_task */
907 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task_id);
908 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
909 /* Call the data processor */
910 LOG (GNUNET_ERROR_TYPE_DEBUG,
911 "%s: Calling read processor\n",
912 GNUNET_i2s (&socket->other_peer));
914 socket->read_handle->proc (socket->read_handle->proc_cls,
916 socket->receive_buffer + socket->copy_offset,
918 LOG (GNUNET_ERROR_TYPE_DEBUG,
919 "%s: Read processor read %d bytes\n",
920 GNUNET_i2s (&socket->other_peer), read_size);
921 LOG (GNUNET_ERROR_TYPE_DEBUG,
922 "%s: Read processor completed successfully\n",
923 GNUNET_i2s (&socket->other_peer));
924 /* Free the read handle */
925 GNUNET_free (socket->read_handle);
926 socket->read_handle = NULL;
927 GNUNET_assert (read_size <= valid_read_size);
928 socket->copy_offset += read_size;
929 /* Determine upto which packet we can remove from the buffer */
930 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
932 if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
934 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
938 /* If no packets can be removed we can't move the buffer */
939 if (0 == packet) return;
940 sequence_increase = packet;
941 LOG (GNUNET_ERROR_TYPE_DEBUG,
942 "%s: Sequence increase after read processor completion: %u\n",
943 GNUNET_i2s (&socket->other_peer), sequence_increase);
945 /* Shift the data in the receive buffer */
946 socket->receive_buffer =
947 memmove (socket->receive_buffer,
948 socket->receive_buffer
949 + socket->receive_buffer_boundaries[sequence_increase-1],
950 socket->receive_buffer_size
951 - socket->receive_buffer_boundaries[sequence_increase-1]);
952 /* Shift the bitmap */
953 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
954 /* Set read_sequence_number */
955 socket->read_sequence_number += sequence_increase;
956 /* Set read_offset */
957 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
958 socket->read_offset += offset_increase;
959 /* Fix copy_offset */
960 GNUNET_assert (offset_increase <= socket->copy_offset);
961 socket->copy_offset -= offset_increase;
962 /* Fix relative boundaries */
963 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
965 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
967 uint32_t ahead_buffer_boundary;
969 ahead_buffer_boundary =
970 socket->receive_buffer_boundaries[packet + sequence_increase];
971 if (0 == ahead_buffer_boundary)
972 socket->receive_buffer_boundaries[packet] = 0;
975 GNUNET_assert (offset_increase < ahead_buffer_boundary);
976 socket->receive_buffer_boundaries[packet] =
977 ahead_buffer_boundary - offset_increase;
981 socket->receive_buffer_boundaries[packet] = 0;
987 * Cancels the existing read io handle
989 * @param cls the closure from the SCHEDULER call
990 * @param tc the task context
993 read_io_timeout (void *cls,
994 const struct GNUNET_SCHEDULER_TaskContext *tc)
996 struct GNUNET_STREAM_Socket *socket = cls;
997 GNUNET_STREAM_DataProcessor proc;
1000 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
1001 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
1003 LOG (GNUNET_ERROR_TYPE_DEBUG,
1004 "%s: Read task timedout - Cancelling it\n",
1005 GNUNET_i2s (&socket->other_peer));
1006 GNUNET_SCHEDULER_cancel (socket->read_task_id);
1007 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
1009 GNUNET_assert (NULL != socket->read_handle);
1010 proc = socket->read_handle->proc;
1011 proc_cls = socket->read_handle->proc_cls;
1013 GNUNET_free (socket->read_handle);
1014 socket->read_handle = NULL;
1015 /* Call the read processor to signal timeout */
1017 GNUNET_STREAM_TIMEOUT,
1024 * Handler for DATA messages; Same for both client and server
1026 * @param socket the socket through which the ack was received
1027 * @param tunnel connection to the other end
1028 * @param sender who sent the message
1029 * @param msg the data message
1030 * @param atsi performance data for the connection
1031 * @return GNUNET_OK to keep the connection open,
1032 * GNUNET_SYSERR to close it (signal serious error)
1035 handle_data (struct GNUNET_STREAM_Socket *socket,
1036 struct GNUNET_MESH_Tunnel *tunnel,
1037 const struct GNUNET_PeerIdentity *sender,
1038 const struct GNUNET_STREAM_DataMessage *msg,
1039 const struct GNUNET_ATS_Information*atsi)
1041 const void *payload;
1042 uint32_t bytes_needed;
1043 uint32_t relative_offset;
1044 uint32_t relative_sequence_number;
1047 size = htons (msg->header.header.size);
1048 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
1050 GNUNET_break_op (0);
1051 return GNUNET_SYSERR;
1054 if (0 != memcmp (sender,
1055 &socket->other_peer,
1056 sizeof (struct GNUNET_PeerIdentity)))
1058 LOG (GNUNET_ERROR_TYPE_DEBUG,
1059 "%s: Received DATA from non-confirming peer\n",
1060 GNUNET_i2s (&socket->other_peer));
1064 switch (socket->state)
1066 case STATE_ESTABLISHED:
1067 case STATE_TRANSMIT_CLOSED:
1068 case STATE_TRANSMIT_CLOSE_WAIT:
1070 /* check if the message's sequence number is in the range we are
1072 relative_sequence_number =
1073 ntohl (msg->sequence_number) - socket->read_sequence_number;
1074 if ( relative_sequence_number > GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1076 LOG (GNUNET_ERROR_TYPE_DEBUG,
1077 "%s: Ignoring received message with sequence number %u\n",
1078 GNUNET_i2s (&socket->other_peer),
1079 ntohl (msg->sequence_number));
1080 /* Start ACK sending task if one is not already present */
1081 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1083 socket->ack_task_id =
1084 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1085 (msg->ack_deadline),
1092 /* Check if we have already seen this message */
1093 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1094 relative_sequence_number))
1096 LOG (GNUNET_ERROR_TYPE_DEBUG,
1097 "%s: Ignoring already received message with sequence number %u\n",
1098 GNUNET_i2s (&socket->other_peer),
1099 ntohl (msg->sequence_number));
1100 /* Start ACK sending task if one is not already present */
1101 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1103 socket->ack_task_id =
1104 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1105 (msg->ack_deadline),
1112 LOG (GNUNET_ERROR_TYPE_DEBUG,
1113 "%s: Receiving DATA with sequence number: %u and size: %d from %s\n",
1114 GNUNET_i2s (&socket->other_peer),
1115 ntohl (msg->sequence_number),
1116 ntohs (msg->header.header.size),
1117 GNUNET_i2s (&socket->other_peer));
1119 /* Check if we have to allocate the buffer */
1120 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1121 relative_offset = ntohl (msg->offset) - socket->read_offset;
1122 bytes_needed = relative_offset + size;
1123 if (bytes_needed > socket->receive_buffer_size)
1125 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1127 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1129 socket->receive_buffer_size = bytes_needed;
1133 LOG (GNUNET_ERROR_TYPE_DEBUG,
1134 "%s: Cannot accommodate packet %d as buffer is full\n",
1135 GNUNET_i2s (&socket->other_peer),
1136 ntohl (msg->sequence_number));
1141 /* Copy Data to buffer */
1143 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1144 memcpy (socket->receive_buffer + relative_offset,
1147 socket->receive_buffer_boundaries[relative_sequence_number] =
1148 relative_offset + size;
1150 /* Modify the ACK bitmap */
1151 ackbitmap_modify_bit (&socket->ack_bitmap,
1152 relative_sequence_number,
1155 /* Start ACK sending task if one is not already present */
1156 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1158 socket->ack_task_id =
1159 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1160 (msg->ack_deadline),
1165 if ((NULL != socket->read_handle) /* A read handle is waiting */
1166 /* There is no current read task */
1167 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
1168 /* We have the first packet */
1169 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
1172 LOG (GNUNET_ERROR_TYPE_DEBUG,
1173 "%s: Scheduling read processor\n",
1174 GNUNET_i2s (&socket->other_peer));
1176 socket->read_task_id =
1177 GNUNET_SCHEDULER_add_now (&call_read_processor,
1184 LOG (GNUNET_ERROR_TYPE_DEBUG,
1185 "%s: Received data message when it cannot be handled\n",
1186 GNUNET_i2s (&socket->other_peer));
1194 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1196 * @param cls the socket (set from GNUNET_MESH_connect)
1197 * @param tunnel connection to the other end
1198 * @param tunnel_ctx place to store local state associated with the tunnel
1199 * @param sender who sent the message
1200 * @param message the actual message
1201 * @param atsi performance data for the connection
1202 * @return GNUNET_OK to keep the connection open,
1203 * GNUNET_SYSERR to close it (signal serious error)
1206 client_handle_data (void *cls,
1207 struct GNUNET_MESH_Tunnel *tunnel,
1209 const struct GNUNET_PeerIdentity *sender,
1210 const struct GNUNET_MessageHeader *message,
1211 const struct GNUNET_ATS_Information*atsi)
1213 struct GNUNET_STREAM_Socket *socket = cls;
1215 return handle_data (socket,
1218 (const struct GNUNET_STREAM_DataMessage *) message,
1224 * Callback to set state to ESTABLISHED
1226 * @param cls the closure from queue_message FIXME: document
1227 * @param socket the socket to requiring state change
1230 set_state_established (void *cls,
1231 struct GNUNET_STREAM_Socket *socket)
1233 LOG (GNUNET_ERROR_TYPE_DEBUG,
1234 "%s: Attaining ESTABLISHED state\n",
1235 GNUNET_i2s (&socket->other_peer));
1236 socket->write_offset = 0;
1237 socket->read_offset = 0;
1238 socket->state = STATE_ESTABLISHED;
1239 /* FIXME: What if listen_cb is NULL */
1240 if (NULL != socket->lsocket)
1242 LOG (GNUNET_ERROR_TYPE_DEBUG,
1243 "%s: Calling listen callback\n",
1244 GNUNET_i2s (&socket->other_peer));
1245 if (GNUNET_SYSERR ==
1246 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1248 &socket->other_peer))
1250 socket->state = STATE_CLOSED;
1251 /* FIXME: We should close in a decent way */
1252 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1253 GNUNET_free (socket);
1256 else if (socket->open_cb)
1257 socket->open_cb (socket->open_cls, socket);
1262 * Callback to set state to HELLO_WAIT
1264 * @param cls the closure from queue_message
1265 * @param socket the socket to requiring state change
1268 set_state_hello_wait (void *cls,
1269 struct GNUNET_STREAM_Socket *socket)
1271 GNUNET_assert (STATE_INIT == socket->state);
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 "%s: Attaining HELLO_WAIT state\n",
1274 GNUNET_i2s (&socket->other_peer));
1275 socket->state = STATE_HELLO_WAIT;
1280 * Callback to set state to CLOSE_WAIT
1282 * @param cls the closure from queue_message
1283 * @param socket the socket requiring state change
1286 set_state_close_wait (void *cls,
1287 struct GNUNET_STREAM_Socket *socket)
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 "%s: Attaing CLOSE_WAIT state\n",
1291 GNUNET_i2s (&socket->other_peer));
1292 socket->state = STATE_CLOSE_WAIT;
1293 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1294 socket->receive_buffer = NULL;
1295 socket->receive_buffer_size = 0;
1300 * Callback to set state to RECEIVE_CLOSE_WAIT
1302 * @param cls the closure from queue_message
1303 * @param socket the socket requiring state change
1306 set_state_receive_close_wait (void *cls,
1307 struct GNUNET_STREAM_Socket *socket)
1309 LOG (GNUNET_ERROR_TYPE_DEBUG,
1310 "%s: Attaing RECEIVE_CLOSE_WAIT state\n",
1311 GNUNET_i2s (&socket->other_peer));
1312 socket->state = STATE_RECEIVE_CLOSE_WAIT;
1313 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1314 socket->receive_buffer = NULL;
1315 socket->receive_buffer_size = 0;
1320 * Callback to set state to TRANSMIT_CLOSE_WAIT
1322 * @param cls the closure from queue_message
1323 * @param socket the socket requiring state change
1326 set_state_transmit_close_wait (void *cls,
1327 struct GNUNET_STREAM_Socket *socket)
1329 LOG (GNUNET_ERROR_TYPE_DEBUG,
1330 "%s: Attaing TRANSMIT_CLOSE_WAIT state\n",
1331 GNUNET_i2s (&socket->other_peer));
1332 socket->state = STATE_TRANSMIT_CLOSE_WAIT;
1337 * Callback to set state to CLOSED
1339 * @param cls the closure from queue_message
1340 * @param socket the socket requiring state change
1343 set_state_closed (void *cls,
1344 struct GNUNET_STREAM_Socket *socket)
1346 socket->state = STATE_CLOSED;
1350 * Returns a new HelloAckMessage. Also sets the write sequence number for the
1353 * @param socket the socket for which this HelloAckMessage has to be generated
1354 * @return the HelloAckMessage
1356 static struct GNUNET_STREAM_HelloAckMessage *
1357 generate_hello_ack_msg (struct GNUNET_STREAM_Socket *socket)
1359 struct GNUNET_STREAM_HelloAckMessage *msg;
1361 /* Get the random sequence number */
1362 socket->write_sequence_number =
1363 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1364 LOG (GNUNET_ERROR_TYPE_DEBUG,
1365 "%s: Generated write sequence number %u\n",
1366 GNUNET_i2s (&socket->other_peer),
1367 (unsigned int) socket->write_sequence_number);
1369 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1370 msg->header.header.size =
1371 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1372 msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1373 msg->sequence_number = htonl (socket->write_sequence_number);
1374 msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1381 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1383 * @param cls the socket (set from GNUNET_MESH_connect)
1384 * @param tunnel connection to the other end
1385 * @param tunnel_ctx this is NULL
1386 * @param sender who sent the message
1387 * @param message the actual message
1388 * @param atsi performance data for the connection
1389 * @return GNUNET_OK to keep the connection open,
1390 * GNUNET_SYSERR to close it (signal serious error)
1393 client_handle_hello_ack (void *cls,
1394 struct GNUNET_MESH_Tunnel *tunnel,
1396 const struct GNUNET_PeerIdentity *sender,
1397 const struct GNUNET_MessageHeader *message,
1398 const struct GNUNET_ATS_Information*atsi)
1400 struct GNUNET_STREAM_Socket *socket = cls;
1401 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1402 struct GNUNET_STREAM_HelloAckMessage *reply;
1404 if (0 != memcmp (sender,
1405 &socket->other_peer,
1406 sizeof (struct GNUNET_PeerIdentity)))
1408 LOG (GNUNET_ERROR_TYPE_DEBUG,
1409 "%s: Received HELLO_ACK from non-confirming peer\n",
1410 GNUNET_i2s (&socket->other_peer));
1413 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1414 LOG (GNUNET_ERROR_TYPE_DEBUG,
1415 "%s: Received HELLO_ACK from %s\n",
1416 GNUNET_i2s (&socket->other_peer),
1417 GNUNET_i2s (&socket->other_peer));
1419 GNUNET_assert (socket->tunnel == tunnel);
1420 switch (socket->state)
1422 case STATE_HELLO_WAIT:
1423 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1424 LOG (GNUNET_ERROR_TYPE_DEBUG,
1425 "%s: Read sequence number %u\n",
1426 GNUNET_i2s (&socket->other_peer),
1427 (unsigned int) socket->read_sequence_number);
1428 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1429 reply = generate_hello_ack_msg (socket);
1430 queue_message (socket,
1432 &set_state_established,
1435 case STATE_ESTABLISHED:
1436 case STATE_RECEIVE_CLOSE_WAIT:
1437 // call statistics (# ACKs ignored++)
1441 LOG (GNUNET_ERROR_TYPE_DEBUG,
1442 "%s: Server %s sent HELLO_ACK when in state %d\n",
1443 GNUNET_i2s (&socket->other_peer),
1444 GNUNET_i2s (&socket->other_peer),
1446 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1447 return GNUNET_SYSERR;
1454 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1456 * @param cls the socket (set from GNUNET_MESH_connect)
1457 * @param tunnel connection to the other end
1458 * @param tunnel_ctx this is NULL
1459 * @param sender who sent the message
1460 * @param message the actual message
1461 * @param atsi performance data for the connection
1462 * @return GNUNET_OK to keep the connection open,
1463 * GNUNET_SYSERR to close it (signal serious error)
1466 client_handle_reset (void *cls,
1467 struct GNUNET_MESH_Tunnel *tunnel,
1469 const struct GNUNET_PeerIdentity *sender,
1470 const struct GNUNET_MessageHeader *message,
1471 const struct GNUNET_ATS_Information*atsi)
1473 // struct GNUNET_STREAM_Socket *socket = cls;
1480 * Common message handler for handling TRANSMIT_CLOSE messages
1482 * @param socket the socket through which the ack was received
1483 * @param tunnel connection to the other end
1484 * @param sender who sent the message
1485 * @param msg the transmit close message
1486 * @param atsi performance data for the connection
1487 * @return GNUNET_OK to keep the connection open,
1488 * GNUNET_SYSERR to close it (signal serious error)
1491 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1492 struct GNUNET_MESH_Tunnel *tunnel,
1493 const struct GNUNET_PeerIdentity *sender,
1494 const struct GNUNET_STREAM_MessageHeader *msg,
1495 const struct GNUNET_ATS_Information*atsi)
1497 struct GNUNET_STREAM_MessageHeader *reply;
1499 switch (socket->state)
1501 case STATE_ESTABLISHED:
1502 socket->state = STATE_RECEIVE_CLOSED;
1504 /* Send TRANSMIT_CLOSE_ACK */
1505 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1506 reply->header.type =
1507 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1508 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1509 queue_message (socket, reply, NULL, NULL);
1513 /* FIXME: Call statistics? */
1521 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1523 * @param cls the socket (set from GNUNET_MESH_connect)
1524 * @param tunnel connection to the other end
1525 * @param tunnel_ctx this is NULL
1526 * @param sender who sent the message
1527 * @param message the actual message
1528 * @param atsi performance data for the connection
1529 * @return GNUNET_OK to keep the connection open,
1530 * GNUNET_SYSERR to close it (signal serious error)
1533 client_handle_transmit_close (void *cls,
1534 struct GNUNET_MESH_Tunnel *tunnel,
1536 const struct GNUNET_PeerIdentity *sender,
1537 const struct GNUNET_MessageHeader *message,
1538 const struct GNUNET_ATS_Information*atsi)
1540 struct GNUNET_STREAM_Socket *socket = cls;
1542 return handle_transmit_close (socket,
1545 (struct GNUNET_STREAM_MessageHeader *)message,
1551 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_*_CLOSE_ACK messages
1553 * @param socket the socket
1554 * @param tunnel connection to the other end
1555 * @param sender who sent the message
1556 * @param message the actual message
1557 * @param atsi performance data for the connection
1558 * @param operation the close operation which is being ACK'ed
1559 * @return GNUNET_OK to keep the connection open,
1560 * GNUNET_SYSERR to close it (signal serious error)
1563 handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
1564 struct GNUNET_MESH_Tunnel *tunnel,
1565 const struct GNUNET_PeerIdentity *sender,
1566 const struct GNUNET_STREAM_MessageHeader *message,
1567 const struct GNUNET_ATS_Information *atsi,
1570 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
1572 shutdown_handle = socket->shutdown_handle;
1573 if (NULL == shutdown_handle)
1575 LOG (GNUNET_ERROR_TYPE_DEBUG,
1576 "%s: Received CLOSE_ACK when shutdown handle is NULL\n",
1577 GNUNET_i2s (&socket->other_peer));
1584 switch (socket->state)
1586 case STATE_CLOSE_WAIT:
1587 if (SHUT_RDWR != shutdown_handle->operation)
1589 LOG (GNUNET_ERROR_TYPE_DEBUG,
1590 "%s: Received CLOSE_ACK when shutdown handle is not for "
1592 GNUNET_i2s (&socket->other_peer));
1596 LOG (GNUNET_ERROR_TYPE_DEBUG,
1597 "%s: Received CLOSE_ACK from %s\n",
1598 GNUNET_i2s (&socket->other_peer),
1599 GNUNET_i2s (&socket->other_peer));
1600 socket->state = STATE_CLOSED;
1603 LOG (GNUNET_ERROR_TYPE_DEBUG,
1604 "%s: Received CLOSE_ACK when in it not expected\n",
1605 GNUNET_i2s (&socket->other_peer));
1611 switch (socket->state)
1613 case STATE_RECEIVE_CLOSE_WAIT:
1614 if (SHUT_RD != shutdown_handle->operation)
1616 LOG (GNUNET_ERROR_TYPE_DEBUG,
1617 "%s: Received RECEIVE_CLOSE_ACK when shutdown handle "
1618 "is not for SHUT_RD\n",
1619 GNUNET_i2s (&socket->other_peer));
1623 LOG (GNUNET_ERROR_TYPE_DEBUG,
1624 "%s: Received RECEIVE_CLOSE_ACK from %s\n",
1625 GNUNET_i2s (&socket->other_peer),
1626 GNUNET_i2s (&socket->other_peer));
1627 socket->state = STATE_RECEIVE_CLOSED;
1630 LOG (GNUNET_ERROR_TYPE_DEBUG,
1631 "%s: Received RECEIVE_CLOSE_ACK when in it not expected\n",
1632 GNUNET_i2s (&socket->other_peer));
1638 switch (socket->state)
1640 case STATE_TRANSMIT_CLOSE_WAIT:
1641 if (SHUT_WR != shutdown_handle->operation)
1643 LOG (GNUNET_ERROR_TYPE_DEBUG,
1644 "%s: Received TRANSMIT_CLOSE_ACK when shutdown handle "
1645 "is not for SHUT_WR\n",
1646 GNUNET_i2s (&socket->other_peer));
1650 LOG (GNUNET_ERROR_TYPE_DEBUG,
1651 "%s: Received TRANSMIT_CLOSE_ACK from %s\n",
1652 GNUNET_i2s (&socket->other_peer),
1653 GNUNET_i2s (&socket->other_peer));
1654 socket->state = STATE_TRANSMIT_CLOSED;
1657 LOG (GNUNET_ERROR_TYPE_DEBUG,
1658 "%s: Received TRANSMIT_CLOSE_ACK when in it not expected\n",
1659 GNUNET_i2s (&socket->other_peer));
1668 if (NULL != shutdown_handle->completion_cb) /* Shutdown completion */
1669 shutdown_handle->completion_cb(shutdown_handle->completion_cls,
1671 GNUNET_free (shutdown_handle); /* Free shutdown handle */
1672 socket->shutdown_handle = NULL;
1673 if (GNUNET_SCHEDULER_NO_TASK
1674 != shutdown_handle->close_msg_retransmission_task_id)
1676 GNUNET_SCHEDULER_cancel
1677 (shutdown_handle->close_msg_retransmission_task_id);
1678 shutdown_handle->close_msg_retransmission_task_id =
1679 GNUNET_SCHEDULER_NO_TASK;
1686 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1688 * @param cls the socket (set from GNUNET_MESH_connect)
1689 * @param tunnel connection to the other end
1690 * @param tunnel_ctx this is NULL
1691 * @param sender who sent the message
1692 * @param message the actual message
1693 * @param atsi performance data for the connection
1694 * @return GNUNET_OK to keep the connection open,
1695 * GNUNET_SYSERR to close it (signal serious error)
1698 client_handle_transmit_close_ack (void *cls,
1699 struct GNUNET_MESH_Tunnel *tunnel,
1701 const struct GNUNET_PeerIdentity *sender,
1702 const struct GNUNET_MessageHeader *message,
1703 const struct GNUNET_ATS_Information*atsi)
1705 struct GNUNET_STREAM_Socket *socket = cls;
1707 return handle_generic_close_ack (socket,
1710 (const struct GNUNET_STREAM_MessageHeader *)
1718 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1720 * @param socket the socket
1721 * @param tunnel connection to the other end
1722 * @param sender who sent the message
1723 * @param message the actual message
1724 * @param atsi performance data for the connection
1725 * @return GNUNET_OK to keep the connection open,
1726 * GNUNET_SYSERR to close it (signal serious error)
1729 handle_receive_close (struct GNUNET_STREAM_Socket *socket,
1730 struct GNUNET_MESH_Tunnel *tunnel,
1731 const struct GNUNET_PeerIdentity *sender,
1732 const struct GNUNET_STREAM_MessageHeader *message,
1733 const struct GNUNET_ATS_Information *atsi)
1735 struct GNUNET_STREAM_MessageHeader *receive_close_ack;
1737 switch (socket->state)
1741 case STATE_HELLO_WAIT:
1742 LOG (GNUNET_ERROR_TYPE_DEBUG,
1743 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1744 GNUNET_i2s (&socket->other_peer));
1750 LOG (GNUNET_ERROR_TYPE_DEBUG,
1751 "%s: Received RECEIVE_CLOSE from %s\n",
1752 GNUNET_i2s (&socket->other_peer),
1753 GNUNET_i2s (&socket->other_peer));
1755 GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1756 receive_close_ack->header.size =
1757 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1758 receive_close_ack->header.type =
1759 htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK);
1760 queue_message (socket,
1765 /* FIXME: Handle the case where write handle is present; the write operation
1766 should be deemed as finised and the write continuation callback
1767 has to be called with the stream status GNUNET_STREAM_SHUTDOWN */
1773 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1775 * @param cls the socket (set from GNUNET_MESH_connect)
1776 * @param tunnel connection to the other end
1777 * @param tunnel_ctx this is NULL
1778 * @param sender who sent the message
1779 * @param message the actual message
1780 * @param atsi performance data for the connection
1781 * @return GNUNET_OK to keep the connection open,
1782 * GNUNET_SYSERR to close it (signal serious error)
1785 client_handle_receive_close (void *cls,
1786 struct GNUNET_MESH_Tunnel *tunnel,
1788 const struct GNUNET_PeerIdentity *sender,
1789 const struct GNUNET_MessageHeader *message,
1790 const struct GNUNET_ATS_Information*atsi)
1792 struct GNUNET_STREAM_Socket *socket = cls;
1795 handle_receive_close (socket,
1798 (const struct GNUNET_STREAM_MessageHeader *) message,
1804 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1806 * @param cls the socket (set from GNUNET_MESH_connect)
1807 * @param tunnel connection to the other end
1808 * @param tunnel_ctx this is NULL
1809 * @param sender who sent the message
1810 * @param message the actual message
1811 * @param atsi performance data for the connection
1812 * @return GNUNET_OK to keep the connection open,
1813 * GNUNET_SYSERR to close it (signal serious error)
1816 client_handle_receive_close_ack (void *cls,
1817 struct GNUNET_MESH_Tunnel *tunnel,
1819 const struct GNUNET_PeerIdentity *sender,
1820 const struct GNUNET_MessageHeader *message,
1821 const struct GNUNET_ATS_Information*atsi)
1823 struct GNUNET_STREAM_Socket *socket = cls;
1825 return handle_generic_close_ack (socket,
1828 (const struct GNUNET_STREAM_MessageHeader *)
1836 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1838 * @param socket the socket
1839 * @param tunnel connection to the other end
1840 * @param sender who sent the message
1841 * @param message the actual message
1842 * @param atsi performance data for the connection
1843 * @return GNUNET_OK to keep the connection open,
1844 * GNUNET_SYSERR to close it (signal serious error)
1847 handle_close (struct GNUNET_STREAM_Socket *socket,
1848 struct GNUNET_MESH_Tunnel *tunnel,
1849 const struct GNUNET_PeerIdentity *sender,
1850 const struct GNUNET_STREAM_MessageHeader *message,
1851 const struct GNUNET_ATS_Information*atsi)
1853 struct GNUNET_STREAM_MessageHeader *close_ack;
1855 switch (socket->state)
1859 case STATE_HELLO_WAIT:
1860 LOG (GNUNET_ERROR_TYPE_DEBUG,
1861 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1862 GNUNET_i2s (&socket->other_peer));
1868 LOG (GNUNET_ERROR_TYPE_DEBUG,
1869 "%s: Received CLOSE from %s\n",
1870 GNUNET_i2s (&socket->other_peer),
1871 GNUNET_i2s (&socket->other_peer));
1872 close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1873 close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1874 close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK);
1875 queue_message (socket,
1879 if (socket->state == STATE_CLOSED)
1882 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1883 socket->receive_buffer = NULL;
1884 socket->receive_buffer_size = 0;
1890 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1892 * @param cls the socket (set from GNUNET_MESH_connect)
1893 * @param tunnel connection to the other end
1894 * @param tunnel_ctx this is NULL
1895 * @param sender who sent the message
1896 * @param message the actual message
1897 * @param atsi performance data for the connection
1898 * @return GNUNET_OK to keep the connection open,
1899 * GNUNET_SYSERR to close it (signal serious error)
1902 client_handle_close (void *cls,
1903 struct GNUNET_MESH_Tunnel *tunnel,
1905 const struct GNUNET_PeerIdentity *sender,
1906 const struct GNUNET_MessageHeader *message,
1907 const struct GNUNET_ATS_Information*atsi)
1909 struct GNUNET_STREAM_Socket *socket = cls;
1911 return handle_close (socket,
1914 (const struct GNUNET_STREAM_MessageHeader *) message,
1920 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1922 * @param cls the socket (set from GNUNET_MESH_connect)
1923 * @param tunnel connection to the other end
1924 * @param tunnel_ctx this is NULL
1925 * @param sender who sent the message
1926 * @param message the actual message
1927 * @param atsi performance data for the connection
1928 * @return GNUNET_OK to keep the connection open,
1929 * GNUNET_SYSERR to close it (signal serious error)
1932 client_handle_close_ack (void *cls,
1933 struct GNUNET_MESH_Tunnel *tunnel,
1935 const struct GNUNET_PeerIdentity *sender,
1936 const struct GNUNET_MessageHeader *message,
1937 const struct GNUNET_ATS_Information *atsi)
1939 struct GNUNET_STREAM_Socket *socket = cls;
1941 return handle_generic_close_ack (socket,
1944 (const struct GNUNET_STREAM_MessageHeader *)
1950 /*****************************/
1951 /* Server's Message Handlers */
1952 /*****************************/
1955 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1957 * @param cls the closure
1958 * @param tunnel connection to the other end
1959 * @param tunnel_ctx the socket
1960 * @param sender who sent the message
1961 * @param message the actual message
1962 * @param atsi performance data for the connection
1963 * @return GNUNET_OK to keep the connection open,
1964 * GNUNET_SYSERR to close it (signal serious error)
1967 server_handle_data (void *cls,
1968 struct GNUNET_MESH_Tunnel *tunnel,
1970 const struct GNUNET_PeerIdentity *sender,
1971 const struct GNUNET_MessageHeader *message,
1972 const struct GNUNET_ATS_Information*atsi)
1974 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1976 return handle_data (socket,
1979 (const struct GNUNET_STREAM_DataMessage *)message,
1985 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1987 * @param cls the closure
1988 * @param tunnel connection to the other end
1989 * @param tunnel_ctx the socket
1990 * @param sender who sent the message
1991 * @param message the actual message
1992 * @param atsi performance data for the connection
1993 * @return GNUNET_OK to keep the connection open,
1994 * GNUNET_SYSERR to close it (signal serious error)
1997 server_handle_hello (void *cls,
1998 struct GNUNET_MESH_Tunnel *tunnel,
2000 const struct GNUNET_PeerIdentity *sender,
2001 const struct GNUNET_MessageHeader *message,
2002 const struct GNUNET_ATS_Information*atsi)
2004 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2005 struct GNUNET_STREAM_HelloAckMessage *reply;
2007 if (0 != memcmp (sender,
2008 &socket->other_peer,
2009 sizeof (struct GNUNET_PeerIdentity)))
2011 LOG (GNUNET_ERROR_TYPE_DEBUG,
2012 "%s: Received HELLO from non-confirming peer\n",
2013 GNUNET_i2s (&socket->other_peer));
2017 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO ==
2018 ntohs (message->type));
2019 GNUNET_assert (socket->tunnel == tunnel);
2020 LOG (GNUNET_ERROR_TYPE_DEBUG,
2021 "%s: Received HELLO from %s\n",
2022 GNUNET_i2s (&socket->other_peer),
2023 GNUNET_i2s (&socket->other_peer));
2025 if (STATE_INIT == socket->state)
2027 reply = generate_hello_ack_msg (socket);
2028 queue_message (socket,
2030 &set_state_hello_wait,
2035 LOG (GNUNET_ERROR_TYPE_DEBUG,
2036 "%s: Client sent HELLO when in state %d\n",
2037 GNUNET_i2s (&socket->other_peer),
2039 /* FIXME: Send RESET? */
2047 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
2049 * @param cls the closure
2050 * @param tunnel connection to the other end
2051 * @param tunnel_ctx the socket
2052 * @param sender who sent the message
2053 * @param message the actual message
2054 * @param atsi performance data for the connection
2055 * @return GNUNET_OK to keep the connection open,
2056 * GNUNET_SYSERR to close it (signal serious error)
2059 server_handle_hello_ack (void *cls,
2060 struct GNUNET_MESH_Tunnel *tunnel,
2062 const struct GNUNET_PeerIdentity *sender,
2063 const struct GNUNET_MessageHeader *message,
2064 const struct GNUNET_ATS_Information*atsi)
2066 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2067 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
2069 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
2070 ntohs (message->type));
2071 GNUNET_assert (socket->tunnel == tunnel);
2072 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
2073 if (STATE_HELLO_WAIT == socket->state)
2075 LOG (GNUNET_ERROR_TYPE_DEBUG,
2076 "%s: Received HELLO_ACK from %s\n",
2077 GNUNET_i2s (&socket->other_peer),
2078 GNUNET_i2s (&socket->other_peer));
2079 socket->read_sequence_number = ntohl (ack_message->sequence_number);
2080 LOG (GNUNET_ERROR_TYPE_DEBUG,
2081 "%s: Read sequence number %u\n",
2082 GNUNET_i2s (&socket->other_peer),
2083 (unsigned int) socket->read_sequence_number);
2084 socket->receiver_window_available =
2085 ntohl (ack_message->receiver_window_size);
2086 /* Attain ESTABLISHED state */
2087 set_state_established (NULL, socket);
2091 LOG (GNUNET_ERROR_TYPE_DEBUG,
2092 "Client sent HELLO_ACK when in state %d\n", socket->state);
2093 /* FIXME: Send RESET? */
2101 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
2103 * @param cls the closure
2104 * @param tunnel connection to the other end
2105 * @param tunnel_ctx the socket
2106 * @param sender who sent the message
2107 * @param message the actual message
2108 * @param atsi performance data for the connection
2109 * @return GNUNET_OK to keep the connection open,
2110 * GNUNET_SYSERR to close it (signal serious error)
2113 server_handle_reset (void *cls,
2114 struct GNUNET_MESH_Tunnel *tunnel,
2116 const struct GNUNET_PeerIdentity *sender,
2117 const struct GNUNET_MessageHeader *message,
2118 const struct GNUNET_ATS_Information*atsi)
2120 // struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2127 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
2129 * @param cls the closure
2130 * @param tunnel connection to the other end
2131 * @param tunnel_ctx the socket
2132 * @param sender who sent the message
2133 * @param message the actual message
2134 * @param atsi performance data for the connection
2135 * @return GNUNET_OK to keep the connection open,
2136 * GNUNET_SYSERR to close it (signal serious error)
2139 server_handle_transmit_close (void *cls,
2140 struct GNUNET_MESH_Tunnel *tunnel,
2142 const struct GNUNET_PeerIdentity *sender,
2143 const struct GNUNET_MessageHeader *message,
2144 const struct GNUNET_ATS_Information*atsi)
2146 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2148 return handle_transmit_close (socket,
2151 (struct GNUNET_STREAM_MessageHeader *)message,
2157 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
2159 * @param cls the closure
2160 * @param tunnel connection to the other end
2161 * @param tunnel_ctx the socket
2162 * @param sender who sent the message
2163 * @param message the actual message
2164 * @param atsi performance data for the connection
2165 * @return GNUNET_OK to keep the connection open,
2166 * GNUNET_SYSERR to close it (signal serious error)
2169 server_handle_transmit_close_ack (void *cls,
2170 struct GNUNET_MESH_Tunnel *tunnel,
2172 const struct GNUNET_PeerIdentity *sender,
2173 const struct GNUNET_MessageHeader *message,
2174 const struct GNUNET_ATS_Information*atsi)
2176 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2178 return handle_generic_close_ack (socket,
2181 (const struct GNUNET_STREAM_MessageHeader *)
2189 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
2191 * @param cls the closure
2192 * @param tunnel connection to the other end
2193 * @param tunnel_ctx the socket
2194 * @param sender who sent the message
2195 * @param message the actual message
2196 * @param atsi performance data for the connection
2197 * @return GNUNET_OK to keep the connection open,
2198 * GNUNET_SYSERR to close it (signal serious error)
2201 server_handle_receive_close (void *cls,
2202 struct GNUNET_MESH_Tunnel *tunnel,
2204 const struct GNUNET_PeerIdentity *sender,
2205 const struct GNUNET_MessageHeader *message,
2206 const struct GNUNET_ATS_Information*atsi)
2208 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2211 handle_receive_close (socket,
2214 (const struct GNUNET_STREAM_MessageHeader *) message,
2220 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
2222 * @param cls the closure
2223 * @param tunnel connection to the other end
2224 * @param tunnel_ctx the socket
2225 * @param sender who sent the message
2226 * @param message the actual message
2227 * @param atsi performance data for the connection
2228 * @return GNUNET_OK to keep the connection open,
2229 * GNUNET_SYSERR to close it (signal serious error)
2232 server_handle_receive_close_ack (void *cls,
2233 struct GNUNET_MESH_Tunnel *tunnel,
2235 const struct GNUNET_PeerIdentity *sender,
2236 const struct GNUNET_MessageHeader *message,
2237 const struct GNUNET_ATS_Information*atsi)
2239 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2241 return handle_generic_close_ack (socket,
2244 (const struct GNUNET_STREAM_MessageHeader *)
2252 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2254 * @param cls the listen socket (from GNUNET_MESH_connect in
2255 * GNUNET_STREAM_listen)
2256 * @param tunnel connection to the other end
2257 * @param tunnel_ctx the socket
2258 * @param sender who sent the message
2259 * @param message the actual message
2260 * @param atsi performance data for the connection
2261 * @return GNUNET_OK to keep the connection open,
2262 * GNUNET_SYSERR to close it (signal serious error)
2265 server_handle_close (void *cls,
2266 struct GNUNET_MESH_Tunnel *tunnel,
2268 const struct GNUNET_PeerIdentity *sender,
2269 const struct GNUNET_MessageHeader *message,
2270 const struct GNUNET_ATS_Information*atsi)
2272 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2274 return handle_close (socket,
2277 (const struct GNUNET_STREAM_MessageHeader *) message,
2283 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2285 * @param cls the closure
2286 * @param tunnel connection to the other end
2287 * @param tunnel_ctx the socket
2288 * @param sender who sent the message
2289 * @param message the actual message
2290 * @param atsi performance data for the connection
2291 * @return GNUNET_OK to keep the connection open,
2292 * GNUNET_SYSERR to close it (signal serious error)
2295 server_handle_close_ack (void *cls,
2296 struct GNUNET_MESH_Tunnel *tunnel,
2298 const struct GNUNET_PeerIdentity *sender,
2299 const struct GNUNET_MessageHeader *message,
2300 const struct GNUNET_ATS_Information*atsi)
2302 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2304 return handle_generic_close_ack (socket,
2307 (const struct GNUNET_STREAM_MessageHeader *)
2315 * Handler for DATA_ACK messages
2317 * @param socket the socket through which the ack was received
2318 * @param tunnel connection to the other end
2319 * @param sender who sent the message
2320 * @param ack the acknowledgment message
2321 * @param atsi performance data for the connection
2322 * @return GNUNET_OK to keep the connection open,
2323 * GNUNET_SYSERR to close it (signal serious error)
2326 handle_ack (struct GNUNET_STREAM_Socket *socket,
2327 struct GNUNET_MESH_Tunnel *tunnel,
2328 const struct GNUNET_PeerIdentity *sender,
2329 const struct GNUNET_STREAM_AckMessage *ack,
2330 const struct GNUNET_ATS_Information*atsi)
2332 unsigned int packet;
2333 int need_retransmission;
2336 if (0 != memcmp (sender,
2337 &socket->other_peer,
2338 sizeof (struct GNUNET_PeerIdentity)))
2340 LOG (GNUNET_ERROR_TYPE_DEBUG,
2341 "%s: Received ACK from non-confirming peer\n",
2342 GNUNET_i2s (&socket->other_peer));
2346 switch (socket->state)
2348 case (STATE_ESTABLISHED):
2349 case (STATE_RECEIVE_CLOSED):
2350 case (STATE_RECEIVE_CLOSE_WAIT):
2351 if (NULL == socket->write_handle)
2353 LOG (GNUNET_ERROR_TYPE_DEBUG,
2354 "%s: Received DATA_ACK when write_handle is NULL\n",
2355 GNUNET_i2s (&socket->other_peer));
2358 /* FIXME: increment in the base sequence number is breaking current flow
2360 if (!((socket->write_sequence_number
2361 - ntohl (ack->base_sequence_number)) < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
2363 LOG (GNUNET_ERROR_TYPE_DEBUG,
2364 "%s: Received DATA_ACK with unexpected base sequence number\n",
2365 GNUNET_i2s (&socket->other_peer));
2366 LOG (GNUNET_ERROR_TYPE_DEBUG,
2367 "%s: Current write sequence: %u; Ack's base sequence: %u\n",
2368 GNUNET_i2s (&socket->other_peer),
2369 socket->write_sequence_number,
2370 ntohl (ack->base_sequence_number));
2373 /* FIXME: include the case when write_handle is cancelled - ignore the
2376 LOG (GNUNET_ERROR_TYPE_DEBUG,
2377 "%s: Received DATA_ACK from %s\n",
2378 GNUNET_i2s (&socket->other_peer),
2379 GNUNET_i2s (&socket->other_peer));
2381 /* Cancel the retransmission task */
2382 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
2384 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
2385 socket->retransmission_timeout_task_id =
2386 GNUNET_SCHEDULER_NO_TASK;
2389 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2391 if (NULL == socket->write_handle->messages[packet]) break;
2392 if (ntohl (ack->base_sequence_number)
2393 >= ntohl (socket->write_handle->messages[packet]->sequence_number))
2394 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
2399 ackbitmap_is_bit_set (&socket->write_handle->ack_bitmap,
2400 ntohl (socket->write_handle->messages[packet]->sequence_number)
2401 - ntohl (ack->base_sequence_number)))
2402 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
2407 /* Update the receive window remaining
2408 FIXME : Should update with the value from a data ack with greater
2410 socket->receiver_window_available =
2411 ntohl (ack->receive_window_remaining);
2413 /* Check if we have received all acknowledgements */
2414 need_retransmission = GNUNET_NO;
2415 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2417 if (NULL == socket->write_handle->messages[packet]) break;
2418 if (GNUNET_YES != ackbitmap_is_bit_set
2419 (&socket->write_handle->ack_bitmap,packet))
2421 need_retransmission = GNUNET_YES;
2425 if (GNUNET_YES == need_retransmission)
2427 write_data (socket);
2429 else /* We have to call the write continuation callback now */
2431 struct GNUNET_STREAM_IOWriteHandle *write_handle;
2433 /* Free the packets */
2434 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2436 GNUNET_free_non_null (socket->write_handle->messages[packet]);
2438 write_handle = socket->write_handle;
2439 socket->write_handle = NULL;
2440 if (NULL != write_handle->write_cont)
2441 write_handle->write_cont (write_handle->write_cont_cls,
2443 write_handle->size);
2444 /* We are done with the write handle - Freeing it */
2445 GNUNET_free (write_handle);
2446 LOG (GNUNET_ERROR_TYPE_DEBUG,
2447 "%s: Write completion callback completed\n",
2448 GNUNET_i2s (&socket->other_peer));
2459 * Handler for DATA_ACK messages
2461 * @param cls the 'struct GNUNET_STREAM_Socket'
2462 * @param tunnel connection to the other end
2463 * @param tunnel_ctx unused
2464 * @param sender who sent the message
2465 * @param message the actual message
2466 * @param atsi performance data for the connection
2467 * @return GNUNET_OK to keep the connection open,
2468 * GNUNET_SYSERR to close it (signal serious error)
2471 client_handle_ack (void *cls,
2472 struct GNUNET_MESH_Tunnel *tunnel,
2474 const struct GNUNET_PeerIdentity *sender,
2475 const struct GNUNET_MessageHeader *message,
2476 const struct GNUNET_ATS_Information*atsi)
2478 struct GNUNET_STREAM_Socket *socket = cls;
2479 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2481 return handle_ack (socket, tunnel, sender, ack, atsi);
2486 * Handler for DATA_ACK messages
2488 * @param cls the server's listen socket
2489 * @param tunnel connection to the other end
2490 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2491 * @param sender who sent the message
2492 * @param message the actual message
2493 * @param atsi performance data for the connection
2494 * @return GNUNET_OK to keep the connection open,
2495 * GNUNET_SYSERR to close it (signal serious error)
2498 server_handle_ack (void *cls,
2499 struct GNUNET_MESH_Tunnel *tunnel,
2501 const struct GNUNET_PeerIdentity *sender,
2502 const struct GNUNET_MessageHeader *message,
2503 const struct GNUNET_ATS_Information*atsi)
2505 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2506 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2508 return handle_ack (socket, tunnel, sender, ack, atsi);
2513 * For client message handlers, the stream socket is in the
2516 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2517 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2518 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2519 sizeof (struct GNUNET_STREAM_AckMessage) },
2520 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2521 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2522 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2523 sizeof (struct GNUNET_STREAM_MessageHeader)},
2524 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2525 sizeof (struct GNUNET_STREAM_MessageHeader)},
2526 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2527 sizeof (struct GNUNET_STREAM_MessageHeader)},
2528 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2529 sizeof (struct GNUNET_STREAM_MessageHeader)},
2530 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2531 sizeof (struct GNUNET_STREAM_MessageHeader)},
2532 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2533 sizeof (struct GNUNET_STREAM_MessageHeader)},
2534 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2535 sizeof (struct GNUNET_STREAM_MessageHeader)},
2541 * For server message handlers, the stream socket is in the
2542 * tunnel context, and the listen socket in the closure argument.
2544 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2545 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2546 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2547 sizeof (struct GNUNET_STREAM_AckMessage) },
2548 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2549 sizeof (struct GNUNET_STREAM_MessageHeader)},
2550 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2551 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2552 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2553 sizeof (struct GNUNET_STREAM_MessageHeader)},
2554 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2555 sizeof (struct GNUNET_STREAM_MessageHeader)},
2556 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2557 sizeof (struct GNUNET_STREAM_MessageHeader)},
2558 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2559 sizeof (struct GNUNET_STREAM_MessageHeader)},
2560 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2561 sizeof (struct GNUNET_STREAM_MessageHeader)},
2562 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2563 sizeof (struct GNUNET_STREAM_MessageHeader)},
2564 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2565 sizeof (struct GNUNET_STREAM_MessageHeader)},
2571 * Function called when our target peer is connected to our tunnel
2573 * @param cls the socket for which this tunnel is created
2574 * @param peer the peer identity of the target
2575 * @param atsi performance data for the connection
2578 mesh_peer_connect_callback (void *cls,
2579 const struct GNUNET_PeerIdentity *peer,
2580 const struct GNUNET_ATS_Information * atsi)
2582 struct GNUNET_STREAM_Socket *socket = cls;
2583 struct GNUNET_STREAM_MessageHeader *message;
2585 if (0 != memcmp (peer,
2586 &socket->other_peer,
2587 sizeof (struct GNUNET_PeerIdentity)))
2589 LOG (GNUNET_ERROR_TYPE_DEBUG,
2590 "%s: A peer which is not our target has connected to our tunnel\n",
2595 LOG (GNUNET_ERROR_TYPE_DEBUG,
2596 "%s: Target peer %s connected\n",
2597 GNUNET_i2s (&socket->other_peer),
2598 GNUNET_i2s (&socket->other_peer));
2600 /* Set state to INIT */
2601 socket->state = STATE_INIT;
2603 /* Send HELLO message */
2604 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2605 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
2606 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2607 queue_message (socket,
2609 &set_state_hello_wait,
2612 /* Call open callback */
2613 if (NULL == socket->open_cb)
2615 LOG (GNUNET_ERROR_TYPE_DEBUG,
2616 "STREAM_open callback is NULL\n");
2622 * Function called when our target peer is disconnected from our tunnel
2624 * @param cls the socket associated which this tunnel
2625 * @param peer the peer identity of the target
2628 mesh_peer_disconnect_callback (void *cls,
2629 const struct GNUNET_PeerIdentity *peer)
2631 struct GNUNET_STREAM_Socket *socket=cls;
2633 /* If the state is SHUTDOWN its ok; else set the state of the socket to SYSERR */
2634 LOG (GNUNET_ERROR_TYPE_DEBUG,
2635 "%s: Other peer %s disconnected \n",
2636 GNUNET_i2s (&socket->other_peer),
2637 GNUNET_i2s (&socket->other_peer));
2642 * Method called whenever a peer creates a tunnel to us
2644 * @param cls closure
2645 * @param tunnel new handle to the tunnel
2646 * @param initiator peer that started the tunnel
2647 * @param atsi performance information for the tunnel
2648 * @return initial tunnel context for the tunnel
2649 * (can be NULL -- that's not an error)
2652 new_tunnel_notify (void *cls,
2653 struct GNUNET_MESH_Tunnel *tunnel,
2654 const struct GNUNET_PeerIdentity *initiator,
2655 const struct GNUNET_ATS_Information *atsi)
2657 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2658 struct GNUNET_STREAM_Socket *socket;
2660 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2661 from the same peer again until the socket is closed */
2663 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2664 socket->other_peer = *initiator;
2665 socket->tunnel = tunnel;
2666 socket->session_id = 0; /* FIXME */
2667 socket->state = STATE_INIT;
2668 socket->lsocket = lsocket;
2670 LOG (GNUNET_ERROR_TYPE_DEBUG,
2671 "%s: Peer %s initiated tunnel to us\n",
2672 GNUNET_i2s (&socket->other_peer),
2673 GNUNET_i2s (&socket->other_peer));
2675 /* FIXME: Copy MESH handle from lsocket to socket */
2682 * Function called whenever an inbound tunnel is destroyed. Should clean up
2683 * any associated state. This function is NOT called if the client has
2684 * explicitly asked for the tunnel to be destroyed using
2685 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2688 * @param cls closure (set from GNUNET_MESH_connect)
2689 * @param tunnel connection to the other end (henceforth invalid)
2690 * @param tunnel_ctx place where local state associated
2691 * with the tunnel is stored
2694 tunnel_cleaner (void *cls,
2695 const struct GNUNET_MESH_Tunnel *tunnel,
2698 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2700 if (tunnel != socket->tunnel)
2704 LOG (GNUNET_ERROR_TYPE_DEBUG,
2705 "%s: Peer %s has terminated connection abruptly\n",
2706 GNUNET_i2s (&socket->other_peer),
2707 GNUNET_i2s (&socket->other_peer));
2709 socket->status = GNUNET_STREAM_SHUTDOWN;
2711 /* Clear Transmit handles */
2712 if (NULL != socket->transmit_handle)
2714 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2715 socket->transmit_handle = NULL;
2717 if (NULL != socket->ack_transmit_handle)
2719 GNUNET_MESH_notify_transmit_ready_cancel (socket->ack_transmit_handle);
2720 GNUNET_free (socket->ack_msg);
2721 socket->ack_msg = NULL;
2722 socket->ack_transmit_handle = NULL;
2724 /* Stop Tasks using socket->tunnel */
2725 if (GNUNET_SCHEDULER_NO_TASK != socket->ack_task_id)
2727 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2728 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2730 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
2732 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
2733 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
2735 /* FIXME: Cancel all other tasks using socket->tunnel */
2736 socket->tunnel = NULL;
2746 * Tries to open a stream to the target peer
2748 * @param cfg configuration to use
2749 * @param target the target peer to which the stream has to be opened
2750 * @param app_port the application port number which uniquely identifies this
2752 * @param open_cb this function will be called after stream has be established
2753 * @param open_cb_cls the closure for open_cb
2754 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2755 * @return if successful it returns the stream socket; NULL if stream cannot be
2758 struct GNUNET_STREAM_Socket *
2759 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2760 const struct GNUNET_PeerIdentity *target,
2761 GNUNET_MESH_ApplicationType app_port,
2762 GNUNET_STREAM_OpenCallback open_cb,
2766 struct GNUNET_STREAM_Socket *socket;
2767 enum GNUNET_STREAM_Option option;
2768 GNUNET_MESH_ApplicationType ports[] = {app_port, 0};
2769 va_list vargs; /* Variable arguments */
2771 LOG (GNUNET_ERROR_TYPE_DEBUG,
2773 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2774 socket->other_peer = *target;
2775 socket->open_cb = open_cb;
2776 socket->open_cls = open_cb_cls;
2778 socket->retransmit_timeout =
2779 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
2780 va_start (vargs, open_cb_cls); /* Parse variable args */
2782 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2785 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2786 /* Expect struct GNUNET_TIME_Relative */
2787 socket->retransmit_timeout = va_arg (vargs,
2788 struct GNUNET_TIME_Relative);
2790 case GNUNET_STREAM_OPTION_END:
2793 } while (GNUNET_STREAM_OPTION_END != option);
2794 va_end (vargs); /* End of variable args parsing */
2795 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2796 RECEIVE_BUFFER_SIZE, /* QUEUE size as parameter? */
2798 NULL, /* No inbound tunnel handler */
2799 NULL, /* No in-tunnel cleaner */
2800 client_message_handlers,
2801 ports); /* We don't get inbound tunnels */
2802 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2804 GNUNET_free (socket);
2808 /* Now create the mesh tunnel to target */
2809 LOG (GNUNET_ERROR_TYPE_DEBUG,
2810 "Creating MESH Tunnel\n");
2811 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2812 NULL, /* Tunnel context */
2813 &mesh_peer_connect_callback,
2814 &mesh_peer_disconnect_callback,
2816 GNUNET_assert (NULL != socket->tunnel);
2817 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2818 &socket->other_peer);
2820 LOG (GNUNET_ERROR_TYPE_DEBUG,
2821 "%s() END\n", __func__);
2827 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
2829 * @param socket the stream socket
2830 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
2831 * @param completion_cb the callback that will be called upon successful
2832 * shutdown of given operation
2833 * @param completion_cls the closure for the completion callback
2834 * @return the shutdown handle
2836 struct GNUNET_STREAM_ShutdownHandle *
2837 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2839 GNUNET_STREAM_ShutdownCompletion completion_cb,
2840 void *completion_cls)
2842 struct GNUNET_STREAM_ShutdownHandle *handle;
2843 struct GNUNET_STREAM_MessageHeader *msg;
2845 GNUNET_assert (NULL == socket->shutdown_handle);
2847 handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle));
2848 handle->socket = socket;
2849 handle->completion_cb = completion_cb;
2850 handle->completion_cls = completion_cls;
2851 socket->shutdown_handle = handle;
2853 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2854 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2858 handle->operation = SHUT_RD;
2859 if (NULL != socket->read_handle)
2860 LOG (GNUNET_ERROR_TYPE_WARNING,
2861 "Existing read handle should be cancelled before shutting"
2863 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
2864 queue_message (socket,
2866 &set_state_receive_close_wait,
2870 handle->operation = SHUT_WR;
2871 if (NULL != socket->write_handle)
2872 LOG (GNUNET_ERROR_TYPE_WARNING,
2873 "Existing write handle should be cancelled before shutting"
2875 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
2876 queue_message (socket,
2878 &set_state_transmit_close_wait,
2882 handle->operation = SHUT_RDWR;
2883 if (NULL != socket->write_handle)
2884 LOG (GNUNET_ERROR_TYPE_WARNING,
2885 "Existing write handle should be cancelled before shutting"
2887 if (NULL != socket->read_handle)
2888 LOG (GNUNET_ERROR_TYPE_WARNING,
2889 "Existing read handle should be cancelled before shutting"
2891 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
2892 queue_message (socket,
2894 &set_state_close_wait,
2898 LOG (GNUNET_ERROR_TYPE_WARNING,
2899 "GNUNET_STREAM_shutdown called with invalid value for "
2900 "parameter operation -- Ignoring\n");
2902 GNUNET_free (handle);
2905 handle->close_msg_retransmission_task_id =
2906 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2907 &close_msg_retransmission_task,
2914 * Cancels a pending shutdown
2916 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
2919 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle)
2921 if (GNUNET_SCHEDULER_NO_TASK != handle->close_msg_retransmission_task_id)
2922 GNUNET_SCHEDULER_cancel (handle->close_msg_retransmission_task_id);
2923 GNUNET_free (handle);
2931 * @param socket the stream socket
2934 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
2936 struct MessageQueue *head;
2938 if (NULL != socket->read_handle)
2940 LOG (GNUNET_ERROR_TYPE_WARNING,
2941 "Closing STREAM socket when a read handle is pending\n");
2943 if (NULL != socket->write_handle)
2945 LOG (GNUNET_ERROR_TYPE_WARNING,
2946 "Closing STREAM socket when a write handle is pending\n");
2947 GNUNET_STREAM_io_write_cancel (socket->write_handle);
2948 //socket->write_handle = NULL;
2951 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
2953 /* socket closed with read task pending!? */
2955 GNUNET_SCHEDULER_cancel (socket->read_task_id);
2956 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
2959 /* Terminate the ack'ing tasks if they are still present */
2960 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
2962 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2963 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2966 /* Clear Transmit handles */
2967 if (NULL != socket->transmit_handle)
2969 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2970 socket->transmit_handle = NULL;
2972 if (NULL != socket->ack_transmit_handle)
2974 GNUNET_MESH_notify_transmit_ready_cancel (socket->ack_transmit_handle);
2975 GNUNET_free (socket->ack_msg);
2976 socket->ack_msg = NULL;
2977 socket->ack_transmit_handle = NULL;
2980 /* Clear existing message queue */
2981 while (NULL != (head = socket->queue_head)) {
2982 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2985 GNUNET_free (head->message);
2989 /* Close associated tunnel */
2990 if (NULL != socket->tunnel)
2992 GNUNET_MESH_tunnel_destroy (socket->tunnel);
2993 socket->tunnel = NULL;
2996 /* Close mesh connection */
2997 if (NULL != socket->mesh && NULL == socket->lsocket)
2999 GNUNET_MESH_disconnect (socket->mesh);
3000 socket->mesh = NULL;
3003 /* Release receive buffer */
3004 if (NULL != socket->receive_buffer)
3006 GNUNET_free (socket->receive_buffer);
3009 GNUNET_free (socket);
3014 * Listens for stream connections for a specific application ports
3016 * @param cfg the configuration to use
3017 * @param app_port the application port for which new streams will be accepted
3018 * @param listen_cb this function will be called when a peer tries to establish
3020 * @param listen_cb_cls closure for listen_cb
3021 * @return listen socket, NULL for any error
3023 struct GNUNET_STREAM_ListenSocket *
3024 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
3025 GNUNET_MESH_ApplicationType app_port,
3026 GNUNET_STREAM_ListenCallback listen_cb,
3027 void *listen_cb_cls)
3029 /* FIXME: Add variable args for passing configration options? */
3030 struct GNUNET_STREAM_ListenSocket *lsocket;
3031 GNUNET_MESH_ApplicationType ports[] = {app_port, 0};
3033 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
3034 lsocket->port = app_port;
3035 lsocket->listen_cb = listen_cb;
3036 lsocket->listen_cb_cls = listen_cb_cls;
3037 lsocket->mesh = GNUNET_MESH_connect (cfg,
3038 RECEIVE_BUFFER_SIZE, /* FIXME: QUEUE size as parameter? */
3039 lsocket, /* Closure */
3042 server_message_handlers,
3044 GNUNET_assert (NULL != lsocket->mesh);
3050 * Closes the listen socket
3052 * @param lsocket the listen socket
3055 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
3057 /* Close MESH connection */
3058 GNUNET_assert (NULL != lsocket->mesh);
3059 GNUNET_MESH_disconnect (lsocket->mesh);
3061 GNUNET_free (lsocket);
3066 * Tries to write the given data to the stream. The maximum size of data that
3067 * can be written as part of a write operation is (64 * (64000 - sizeof (struct
3068 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
3069 * violation, however only the said number of maximum bytes will be written.
3071 * @param socket the socket representing a stream
3072 * @param data the data buffer from where the data is written into the stream
3073 * @param size the number of bytes to be written from the data buffer
3074 * @param timeout the timeout period
3075 * @param write_cont the function to call upon writing some bytes into the
3077 * @param write_cont_cls the closure
3079 * @return handle to cancel the operation; if a previous write is pending or
3080 * the stream has been shutdown for this operation then write_cont is
3081 * immediately called and NULL is returned.
3083 struct GNUNET_STREAM_IOWriteHandle *
3084 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
3087 struct GNUNET_TIME_Relative timeout,
3088 GNUNET_STREAM_CompletionContinuation write_cont,
3089 void *write_cont_cls)
3091 unsigned int num_needed_packets;
3092 unsigned int packet;
3093 struct GNUNET_STREAM_IOWriteHandle *io_handle;
3094 uint32_t packet_size;
3095 uint32_t payload_size;
3096 struct GNUNET_STREAM_DataMessage *data_msg;
3098 struct GNUNET_TIME_Relative ack_deadline;
3100 LOG (GNUNET_ERROR_TYPE_DEBUG,
3103 /* Return NULL if there is already a write request pending */
3104 if (NULL != socket->write_handle)
3110 switch (socket->state)
3112 case STATE_TRANSMIT_CLOSED:
3113 case STATE_TRANSMIT_CLOSE_WAIT:
3115 case STATE_CLOSE_WAIT:
3116 if (NULL != write_cont)
3117 write_cont (write_cont_cls, GNUNET_STREAM_SHUTDOWN, 0);
3118 LOG (GNUNET_ERROR_TYPE_DEBUG,
3119 "%s() END\n", __func__);
3123 case STATE_HELLO_WAIT:
3124 if (NULL != write_cont)
3125 /* FIXME: GNUNET_STREAM_SYSERR?? */
3126 write_cont (write_cont_cls, GNUNET_STREAM_SYSERR, 0);
3127 LOG (GNUNET_ERROR_TYPE_DEBUG,
3128 "%s() END\n", __func__);
3130 case STATE_ESTABLISHED:
3131 case STATE_RECEIVE_CLOSED:
3132 case STATE_RECEIVE_CLOSE_WAIT:
3136 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
3137 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
3138 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
3139 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
3140 io_handle->socket = socket;
3141 io_handle->write_cont = write_cont;
3142 io_handle->write_cont_cls = write_cont_cls;
3143 io_handle->size = size;
3145 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3146 determined from RTT */
3147 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
3148 /* Divide the given buffer into packets for sending */
3149 for (packet=0; packet < num_needed_packets; packet++)
3151 if ((packet + 1) * max_payload_size < size)
3153 payload_size = max_payload_size;
3154 packet_size = MAX_PACKET_SIZE;
3158 payload_size = size - packet * max_payload_size;
3159 packet_size = payload_size + sizeof (struct
3160 GNUNET_STREAM_DataMessage);
3162 io_handle->messages[packet] = GNUNET_malloc (packet_size);
3163 io_handle->messages[packet]->header.header.size = htons (packet_size);
3164 io_handle->messages[packet]->header.header.type =
3165 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
3166 io_handle->messages[packet]->sequence_number =
3167 htonl (socket->write_sequence_number++);
3168 io_handle->messages[packet]->offset = htonl (socket->write_offset);
3170 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3171 determined from RTT */
3172 io_handle->messages[packet]->ack_deadline =
3173 GNUNET_TIME_relative_hton (ack_deadline);
3174 data_msg = io_handle->messages[packet];
3175 /* Copy data from given buffer to the packet */
3176 memcpy (&data_msg[1],
3179 sweep += payload_size;
3180 socket->write_offset += payload_size;
3182 socket->write_handle = io_handle;
3183 write_data (socket);
3185 LOG (GNUNET_ERROR_TYPE_DEBUG,
3186 "%s() END\n", __func__);
3194 * Tries to read data from the stream.
3196 * @param socket the socket representing a stream
3197 * @param timeout the timeout period
3198 * @param proc function to call with data (once only)
3199 * @param proc_cls the closure for proc
3201 * @return handle to cancel the operation; if the stream has been shutdown for
3202 * this type of opeartion then the DataProcessor is immediately
3203 * called with GNUNET_STREAM_SHUTDOWN as status and NULL if returned
3205 struct GNUNET_STREAM_IOReadHandle *
3206 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
3207 struct GNUNET_TIME_Relative timeout,
3208 GNUNET_STREAM_DataProcessor proc,
3211 struct GNUNET_STREAM_IOReadHandle *read_handle;
3213 LOG (GNUNET_ERROR_TYPE_DEBUG,
3215 GNUNET_i2s (&socket->other_peer),
3218 /* Return NULL if there is already a read handle; the user has to cancel that
3219 first before continuing or has to wait until it is completed */
3220 if (NULL != socket->read_handle) return NULL;
3222 GNUNET_assert (NULL != proc);
3224 switch (socket->state)
3226 case STATE_RECEIVE_CLOSED:
3227 case STATE_RECEIVE_CLOSE_WAIT:
3229 case STATE_CLOSE_WAIT:
3230 proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
3231 LOG (GNUNET_ERROR_TYPE_DEBUG,
3233 GNUNET_i2s (&socket->other_peer),
3240 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
3241 read_handle->proc = proc;
3242 read_handle->proc_cls = proc_cls;
3243 socket->read_handle = read_handle;
3245 /* Check if we have a packet at bitmap 0 */
3246 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
3249 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
3254 /* Setup the read timeout task */
3255 socket->read_io_timeout_task_id =
3256 GNUNET_SCHEDULER_add_delayed (timeout,
3259 LOG (GNUNET_ERROR_TYPE_DEBUG,
3261 GNUNET_i2s (&socket->other_peer),
3268 * Cancel pending write operation.
3270 * @param ioh handle to operation to cancel
3273 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
3275 struct GNUNET_STREAM_Socket *socket = ioh->socket;
3276 unsigned int packet;
3278 GNUNET_assert (NULL != socket->write_handle);
3279 GNUNET_assert (socket->write_handle == ioh);
3281 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
3283 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
3284 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
3287 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
3289 if (NULL == ioh->messages[packet]) break;
3290 GNUNET_free (ioh->messages[packet]);
3293 GNUNET_free (socket->write_handle);
3294 socket->write_handle = NULL;
3300 * Cancel pending read operation.
3302 * @param ioh handle to operation to cancel
3305 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)