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
38 #include "gnunet_common.h"
39 #include "gnunet_util_lib.h"
40 #include "gnunet_lockmanager_service.h"
41 #include "gnunet_statistics_service.h"
42 #include "gnunet_stream_lib.h"
46 * Generic logging shorthand
48 #define LOG(kind,...) \
49 GNUNET_log_from (kind, "stream-api", __VA_ARGS__)
52 * Debug logging shorthand
54 #define LOG_DEBUG(...) \
55 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
58 * Time in relative seconds shorthand
60 #define TIME_REL_SECS(sec) \
61 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
64 * The maximum packet size of a stream packet
66 #define DEFAULT_MAX_PAYLOAD_SIZE 64000
71 #define RECEIVE_BUFFER_SIZE 4096000
74 * states in the Protocol
79 * Client initialization state
84 * Listener initialization state
89 * Pre-connection establishment state
94 * State where a connection has been established
99 * State where the socket is closed on our side and waiting to be ACK'ed
101 STATE_RECEIVE_CLOSE_WAIT,
104 * State where the socket is closed for reading
106 STATE_RECEIVE_CLOSED,
109 * State where the socket is closed on our side and waiting to be ACK'ed
111 STATE_TRANSMIT_CLOSE_WAIT,
114 * State where the socket is closed for writing
116 STATE_TRANSMIT_CLOSED,
119 * State where the socket is closed on our side and waiting to be ACK'ed
124 * State where the socket is closed
131 * Functions of this type are called when a message is written
133 * @param cls the closure from queue_message
134 * @param socket the socket the written message was bound to
136 typedef void (*SendFinishCallback) (void *cls,
137 struct GNUNET_STREAM_Socket *socket);
141 * The send message queue
148 struct GNUNET_STREAM_MessageHeader *message;
151 * Callback to be called when the message is sent
153 SendFinishCallback finish_cb;
156 * The closure for finish_cb
161 * The next message in queue. Should be NULL in the last message
163 struct MessageQueue *next;
166 * The next message in queue. Should be NULL in the first message
168 struct MessageQueue *prev;
173 * The STREAM Socket Handler
175 struct GNUNET_STREAM_Socket
180 struct GNUNET_MESH_Handle *mesh;
183 * Handle to statistics
185 struct GNUNET_STATISTICS_Handle *stat_handle;
188 * The mesh tunnel handle
190 struct GNUNET_MESH_Tunnel *tunnel;
193 * Stream open closure
198 * Stream open callback
200 GNUNET_STREAM_OpenCallback open_cb;
203 * The current transmit handle (if a pending transmit request exists)
205 struct GNUNET_MESH_TransmitHandle *transmit_handle;
208 * The current message associated with the transmit handle
210 struct MessageQueue *queue_head;
213 * The queue tail, should always point to the last message in queue
215 struct MessageQueue *queue_tail;
218 * The write IO_handle associated with this socket
220 struct GNUNET_STREAM_WriteHandle *write_handle;
223 * The read IO_handle associated with this socket
225 struct GNUNET_STREAM_ReadHandle *read_handle;
228 * The shutdown handle associated with this socket
230 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
233 * Buffer for storing received messages
235 void *receive_buffer;
238 * The listen socket from which this socket is derived. Should be NULL if it
239 * is not a derived socket
241 struct GNUNET_STREAM_ListenSocket *lsocket;
244 * The peer identity of the peer at the other end of the stream
246 struct GNUNET_PeerIdentity other_peer;
249 * The Acknowledgement Bitmap
251 GNUNET_STREAM_AckBitmap ack_bitmap;
254 * Task identifier for retransmission task after timeout
256 GNUNET_SCHEDULER_TaskIdentifier data_retransmission_task_id;
259 * Task identifier for retransmission of control messages
261 GNUNET_SCHEDULER_TaskIdentifier control_retransmission_task_id;
264 * The task for sending timely Acks
266 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
269 * Retransmission timeout
271 struct GNUNET_TIME_Relative retransmit_timeout;
274 * Time when the Acknowledgement was queued
276 struct GNUNET_TIME_Absolute ack_time_registered;
279 * Queued Acknowledgement deadline
281 struct GNUNET_TIME_Relative ack_time_deadline;
284 * Mesh transmit timeout
286 struct GNUNET_TIME_Relative mesh_retry_timeout;
289 * Data retransmission timeout
291 struct GNUNET_TIME_Relative data_retransmit_timeout;
294 * The state of the protocol associated with this socket
299 * Whether testing mode is active or not
309 * Is transmission closed
314 * The application port number (type: uint32_t)
316 GNUNET_MESH_ApplicationType app_port;
319 * The write sequence number to be set incase of testing
321 uint32_t testing_set_write_sequence_number_value;
324 * Write sequence number. Set to random when sending HELLO(client) and
327 uint32_t write_sequence_number;
330 * Read sequence number. This number's value is determined during handshake
332 uint32_t read_sequence_number;
335 * The receiver buffer size
337 uint32_t receive_buffer_size;
340 * The receiver buffer boundaries
342 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
345 * receiver's available buffer after the last acknowledged packet
347 uint32_t receiver_window_available;
350 * The offset pointer used during write operation
352 uint32_t write_offset;
355 * The offset after which we are expecting data
357 uint32_t read_offset;
360 * The offset upto which user has read from the received buffer
362 uint32_t copy_offset;
365 * The maximum size of the data message payload this stream handle can send
367 uint16_t max_payload_size;
372 * A socket for listening
374 struct GNUNET_STREAM_ListenSocket
379 struct GNUNET_MESH_Handle *mesh;
382 * Handle to statistics
384 struct GNUNET_STATISTICS_Handle *stat_handle;
389 struct GNUNET_CONFIGURATION_Handle *cfg;
392 * Handle to the lock manager service
394 struct GNUNET_LOCKMANAGER_Handle *lockmanager;
397 * The active LockingRequest from lockmanager
399 struct GNUNET_LOCKMANAGER_LockingRequest *locking_request;
402 * Callback to call after acquring a lock and listening
404 GNUNET_STREAM_ListenSuccessCallback listen_ok_cb;
407 * The callback function which is called after successful opening socket
409 GNUNET_STREAM_ListenCallback listen_cb;
412 * The call back closure
419 GNUNET_MESH_ApplicationType port;
422 * The id of the lockmanager timeout task
424 GNUNET_SCHEDULER_TaskIdentifier lockmanager_acquire_timeout_task;
427 * The retransmit timeout
429 struct GNUNET_TIME_Relative retransmit_timeout;
437 * Whether testing mode is active or not
442 * The write sequence number to be set incase of testing
444 uint32_t testing_set_write_sequence_number_value;
447 * The maximum size of the data message payload this stream handle can send
449 uint16_t max_payload_size;
455 * The IO Write Handle
457 struct GNUNET_STREAM_WriteHandle
460 * The socket to which this write handle is associated
462 struct GNUNET_STREAM_Socket *socket;
465 * The write continuation callback
467 GNUNET_STREAM_CompletionContinuation write_cont;
470 * Write continuation closure
472 void *write_cont_cls;
475 * The packet_buffers associated with this Handle
477 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
480 * The bitmap of this IOHandle; Corresponding bit for a message is set when
481 * it has been acknowledged by the receiver
483 GNUNET_STREAM_AckBitmap ack_bitmap;
486 * Number of bytes in this write handle
491 * Number of packets already transmitted from this IO handle. Retransmitted
492 * packets are not taken into account here. This is used to determine which
493 * packets account for retransmission and which packets occupy buffer space at
496 unsigned int packets_sent;
499 * The maximum of the base numbers of the received acks
501 uint32_t max_ack_base_num;
509 struct GNUNET_STREAM_ReadHandle
512 * The socket to which this read handle is associated
514 struct GNUNET_STREAM_Socket *socket;
517 * Callback for the read processor
519 GNUNET_STREAM_DataProcessor proc;
522 * The closure pointer for the read processor callback
527 * Task identifier for the read io timeout task
529 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
532 * Task scheduled to continue a read operation.
534 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
537 * Task scheduled from GNUNET_STREAM_read() to lookup the ACK bitmap and call
538 * the read processor task
540 GNUNET_SCHEDULER_TaskIdentifier probe_data_availability_task_id;
545 * Handle for Shutdown
547 struct GNUNET_STREAM_ShutdownHandle
550 * The socket associated with this shutdown handle
552 struct GNUNET_STREAM_Socket *socket;
555 * Shutdown completion callback
557 GNUNET_STREAM_ShutdownCompletion completion_cb;
560 * Closure for completion callback
562 void *completion_cls;
565 * Close message retransmission task id
567 GNUNET_SCHEDULER_TaskIdentifier close_msg_retransmission_task_id;
570 * Task scheduled to call the shutdown continuation callback
572 GNUNET_SCHEDULER_TaskIdentifier call_cont_task_id;
575 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
582 * Default value in seconds for various timeouts
584 static const unsigned int default_timeout = 10;
587 * The domain name for locks we use here
589 static const char *locking_domain = "GNUNET_STREAM_APPLOCK";
592 * Callback function for sending queued message
594 * @param cls closure the socket
595 * @param size number of bytes available in buf
596 * @param buf where the callee should write the message
597 * @return number of bytes written to buf
600 send_message_notify (void *cls, size_t size, void *buf)
602 struct GNUNET_STREAM_Socket *socket = cls;
603 struct MessageQueue *head;
606 socket->transmit_handle = NULL; /* Remove the transmit handle */
607 head = socket->queue_head;
609 return 0; /* just to be safe */
610 if (0 == size) /* request timed out */
612 socket->mesh_retry_timeout = GNUNET_TIME_STD_BACKOFF
613 (socket->mesh_retry_timeout);
614 LOG (GNUNET_ERROR_TYPE_DEBUG,
615 "%s: Message sending to MESH timed out. Retrying in %s \n",
616 GNUNET_i2s (&socket->other_peer),
617 GNUNET_STRINGS_relative_time_to_string (socket->mesh_retry_timeout,
619 socket->transmit_handle =
620 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
621 GNUNET_NO, /* Corking */
622 socket->mesh_retry_timeout,
624 ntohs (head->message->header.size),
625 &send_message_notify,
629 ret = ntohs (head->message->header.size);
630 GNUNET_assert (size >= ret);
631 memcpy (buf, head->message, ret);
632 if (NULL != head->finish_cb)
634 head->finish_cb (head->finish_cb_cls, socket);
636 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
639 GNUNET_free (head->message);
641 if (NULL != socket->transmit_handle)
642 return ret; /* 'finish_cb' might have triggered message already! */
643 head = socket->queue_head;
644 if (NULL != head) /* more pending messages to send */
646 socket->mesh_retry_timeout = GNUNET_TIME_UNIT_ZERO;
647 socket->transmit_handle =
648 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
649 GNUNET_NO, /* Corking */
650 socket->mesh_retry_timeout,
652 ntohs (head->message->header.size),
653 &send_message_notify,
661 * Queues a message for sending using the mesh connection of a socket
663 * @param socket the socket whose mesh connection is used
664 * @param message the message to be sent
665 * @param finish_cb the callback to be called when the message is sent
666 * @param finish_cb_cls the closure for the callback
667 * @param urgent set to GNUNET_YES to add the message to the beginning of the
668 * queue; GNUNET_NO to add at the tail
671 queue_message (struct GNUNET_STREAM_Socket *socket,
672 struct GNUNET_STREAM_MessageHeader *message,
673 SendFinishCallback finish_cb,
677 struct MessageQueue *queue_entity;
680 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
681 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 "%s: Queueing message of type %d and size %d\n",
684 GNUNET_i2s (&socket->other_peer),
685 ntohs (message->header.type),
686 ntohs (message->header.size));
687 GNUNET_assert (NULL != message);
688 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
689 queue_entity->message = message;
690 queue_entity->finish_cb = finish_cb;
691 queue_entity->finish_cb_cls = finish_cb_cls;
692 if (GNUNET_YES == urgent)
694 GNUNET_CONTAINER_DLL_insert (socket->queue_head, socket->queue_tail,
696 if (NULL != socket->transmit_handle)
698 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
699 socket->transmit_handle = NULL;
703 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
706 if (NULL == socket->transmit_handle)
708 socket->mesh_retry_timeout = GNUNET_TIME_UNIT_ZERO;
709 socket->transmit_handle =
710 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
711 GNUNET_NO, /* Corking */
712 socket->mesh_retry_timeout,
714 ntohs (message->header.size),
715 &send_message_notify,
722 * Copies a message and queues it for sending using the mesh connection of
725 * @param socket the socket whose mesh connection is used
726 * @param message the message to be sent
727 * @param finish_cb the callback to be called when the message is sent
728 * @param finish_cb_cls the closure for the callback
731 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
732 const struct GNUNET_STREAM_MessageHeader *message,
733 SendFinishCallback finish_cb,
736 struct GNUNET_STREAM_MessageHeader *msg_copy;
739 size = ntohs (message->header.size);
740 msg_copy = GNUNET_malloc (size);
741 memcpy (msg_copy, message, size);
742 queue_message (socket, msg_copy, finish_cb, finish_cb_cls, GNUNET_NO);
747 * Writes data using the given socket. The amount of data written is limited by
748 * the receiver_window_size
750 * @param socket the socket to use
753 write_data (struct GNUNET_STREAM_Socket *socket);
757 * Task for retransmitting data messages if they aren't ACK before their ack
760 * @param cls the socket
761 * @param tc the Task context
764 data_retransmission_task (void *cls,
765 const struct GNUNET_SCHEDULER_TaskContext *tc)
767 struct GNUNET_STREAM_Socket *socket = cls;
769 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
770 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
772 LOG (GNUNET_ERROR_TYPE_DEBUG,
773 "%s: Retransmitting DATA...\n", GNUNET_i2s (&socket->other_peer));
779 * Task for sending ACK message
781 * @param cls the socket
782 * @param tc the Task context
786 const struct GNUNET_SCHEDULER_TaskContext *tc)
788 struct GNUNET_STREAM_Socket *socket = cls;
789 struct GNUNET_STREAM_AckMessage *ack_msg;
791 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
792 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
794 /* Create the ACK Message */
795 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
796 ack_msg->header.header.size = htons (sizeof (struct
797 GNUNET_STREAM_AckMessage));
798 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
799 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
800 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
801 ack_msg->receive_window_remaining =
802 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
803 /* Queue up ACK for immediate sending */
804 queue_message (socket, &ack_msg->header, NULL, NULL, GNUNET_YES);
809 * Retransmission task for shutdown messages
811 * @param cls the shutdown handle
812 * @param tc the Task Context
815 close_msg_retransmission_task (void *cls,
816 const struct GNUNET_SCHEDULER_TaskContext *tc)
818 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle = cls;
819 struct GNUNET_STREAM_MessageHeader *msg;
820 struct GNUNET_STREAM_Socket *socket;
822 shutdown_handle->close_msg_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
823 GNUNET_assert (NULL != shutdown_handle);
824 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
826 socket = shutdown_handle->socket;
827 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
828 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
829 switch (shutdown_handle->operation)
832 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
835 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
838 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
842 shutdown_handle->close_msg_retransmission_task_id =
843 GNUNET_SCHEDULER_NO_TASK;
846 queue_message (socket, msg, NULL, NULL, GNUNET_NO);
847 shutdown_handle->close_msg_retransmission_task_id =
848 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
849 &close_msg_retransmission_task,
855 * Function to modify a bit in GNUNET_STREAM_AckBitmap
857 * @param bitmap the bitmap to modify
858 * @param bit the bit number to modify
859 * @param value GNUNET_YES to on, GNUNET_NO to off
862 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
866 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
867 if (GNUNET_YES == value)
868 *bitmap |= (1LL << bit);
870 *bitmap &= ~(1LL << bit);
875 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
877 * @param bitmap address of the bitmap that has to be checked
878 * @param bit the bit number to check
879 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
882 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
885 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
886 return 0 != (*bitmap & (1LL << bit));
891 * Writes data using the given socket. The amount of data written is limited by
892 * the receiver_window_size
894 * @param socket the socket to use
897 write_data (struct GNUNET_STREAM_Socket *socket)
899 struct GNUNET_STREAM_WriteHandle *io_handle = socket->write_handle;
902 for (packet=0; packet < io_handle->packets_sent; packet++)
904 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
907 LOG (GNUNET_ERROR_TYPE_DEBUG,
908 "%s: Retransmitting DATA message with sequence %u\n",
909 GNUNET_i2s (&socket->other_peer),
910 ntohl (io_handle->messages[packet]->sequence_number));
911 copy_and_queue_message (socket,
912 &io_handle->messages[packet]->header,
917 /* Now send new packets if there is enough buffer space */
918 while ((packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH) &&
919 (NULL != io_handle->messages[packet]) &&
920 (socket->receiver_window_available
921 >= ntohs (io_handle->messages[packet]->header.header.size)))
923 socket->receiver_window_available -=
924 ntohs (io_handle->messages[packet]->header.header.size);
925 LOG (GNUNET_ERROR_TYPE_DEBUG,
926 "%s: Placing DATA message with sequence %u in send queue\n",
927 GNUNET_i2s (&socket->other_peer),
928 ntohl (io_handle->messages[packet]->sequence_number));
929 copy_and_queue_message (socket,
930 &io_handle->messages[packet]->header,
935 io_handle->packets_sent = packet;
936 if (GNUNET_SCHEDULER_NO_TASK == socket->data_retransmission_task_id)
938 socket->data_retransmit_timeout = GNUNET_TIME_STD_BACKOFF
939 (socket->data_retransmit_timeout);
940 socket->data_retransmission_task_id =
941 GNUNET_SCHEDULER_add_delayed (socket->data_retransmit_timeout,
942 &data_retransmission_task,
949 * Cleansup the sockets read handle
951 * @param socket the socket whose read handle has to be cleanedup
954 cleanup_read_handle (struct GNUNET_STREAM_Socket *socket)
956 struct GNUNET_STREAM_ReadHandle *read_handle;
958 read_handle = socket->read_handle;
959 /* Read io time task should be there; if it is already executed then this
960 read handle is not valid; However upon scheduler shutdown the read io task
961 may be executed before */
962 if (GNUNET_SCHEDULER_NO_TASK != read_handle->read_io_timeout_task_id)
963 GNUNET_SCHEDULER_cancel (read_handle->read_io_timeout_task_id);
964 /* reading task may be present; if so we have to stop it */
965 if (GNUNET_SCHEDULER_NO_TASK != read_handle->read_task_id)
966 GNUNET_SCHEDULER_cancel (read_handle->read_task_id);
967 if (GNUNET_SCHEDULER_NO_TASK != read_handle->probe_data_availability_task_id)
968 GNUNET_SCHEDULER_cancel (read_handle->probe_data_availability_task_id);
969 GNUNET_free (read_handle);
970 socket->read_handle = NULL;
975 * Task for calling the read processor
977 * @param cls the socket
978 * @param tc the task context
981 call_read_processor (void *cls,
982 const struct GNUNET_SCHEDULER_TaskContext *tc)
984 struct GNUNET_STREAM_Socket *socket = cls;
985 struct GNUNET_STREAM_ReadHandle *read_handle;
986 GNUNET_STREAM_DataProcessor proc;
989 size_t valid_read_size;
991 uint32_t sequence_increase;
992 uint32_t offset_increase;
994 read_handle = socket->read_handle;
995 GNUNET_assert (NULL != read_handle);
996 read_handle->read_task_id = GNUNET_SCHEDULER_NO_TASK;
997 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
999 if (NULL == socket->receive_buffer)
1001 GNUNET_assert (NULL != read_handle->proc);
1002 /* Check the bitmap for any holes */
1003 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1005 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
1009 /* We only call read processor if we have the first packet */
1010 GNUNET_assert (0 < packet);
1012 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
1013 GNUNET_assert (0 != valid_read_size);
1014 proc = read_handle->proc;
1015 proc_cls = read_handle->proc_cls;
1016 cleanup_read_handle (socket);
1017 /* Call the data processor */
1018 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Calling read processor\n",
1019 GNUNET_i2s (&socket->other_peer));
1020 read_size = proc (proc_cls, GNUNET_STREAM_OK,
1021 socket->receive_buffer + socket->copy_offset,
1023 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Read processor read %d bytes\n",
1024 GNUNET_i2s (&socket->other_peer), read_size);
1025 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Read processor completed successfully\n",
1026 GNUNET_i2s (&socket->other_peer));
1027 GNUNET_assert (read_size <= valid_read_size);
1028 socket->copy_offset += read_size;
1029 /* Determine upto which packet we can remove from the buffer */
1030 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1032 if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
1037 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
1040 /* If no packets can be removed we can't move the buffer */
1043 sequence_increase = packet;
1044 LOG (GNUNET_ERROR_TYPE_DEBUG,
1045 "%s: Sequence increase after read processor completion: %u\n",
1046 GNUNET_i2s (&socket->other_peer), sequence_increase);
1047 /* Shift the data in the receive buffer */
1048 socket->receive_buffer =
1049 memmove (socket->receive_buffer,
1050 socket->receive_buffer
1051 + socket->receive_buffer_boundaries[sequence_increase-1],
1052 socket->receive_buffer_size
1053 - socket->receive_buffer_boundaries[sequence_increase-1]);
1054 /* Shift the bitmap */
1055 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
1056 /* Set read_sequence_number */
1057 socket->read_sequence_number += sequence_increase;
1058 /* Set read_offset */
1059 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
1060 socket->read_offset += offset_increase;
1061 /* Fix copy_offset */
1062 GNUNET_assert (offset_increase <= socket->copy_offset);
1063 socket->copy_offset -= offset_increase;
1064 /* Fix relative boundaries */
1065 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1067 if (packet < (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase))
1069 uint32_t ahead_buffer_boundary;
1071 ahead_buffer_boundary =
1072 socket->receive_buffer_boundaries[packet + sequence_increase];
1073 if (0 == ahead_buffer_boundary)
1074 socket->receive_buffer_boundaries[packet] = 0;
1077 GNUNET_assert (offset_increase < ahead_buffer_boundary);
1078 socket->receive_buffer_boundaries[packet] =
1079 ahead_buffer_boundary - offset_increase;
1083 socket->receive_buffer_boundaries[packet] = 0;
1089 * Cancels the existing read io handle
1091 * @param cls the closure from the SCHEDULER call
1092 * @param tc the task context
1095 read_io_timeout (void *cls,
1096 const struct GNUNET_SCHEDULER_TaskContext *tc)
1098 struct GNUNET_STREAM_Socket *socket = cls;
1099 struct GNUNET_STREAM_ReadHandle *read_handle;
1100 GNUNET_STREAM_DataProcessor proc;
1103 read_handle = socket->read_handle;
1104 GNUNET_assert (NULL != read_handle);
1105 read_handle->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
1106 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1108 if (read_handle->read_task_id != GNUNET_SCHEDULER_NO_TASK)
1110 LOG (GNUNET_ERROR_TYPE_DEBUG,
1111 "%s: Read task timedout - Cancelling it\n",
1112 GNUNET_i2s (&socket->other_peer));
1113 GNUNET_SCHEDULER_cancel (read_handle->read_task_id);
1114 read_handle->read_task_id = GNUNET_SCHEDULER_NO_TASK;
1116 proc = read_handle->proc;
1117 proc_cls = read_handle->proc_cls;
1118 GNUNET_free (read_handle);
1119 socket->read_handle = NULL;
1120 /* Call the read processor to signal timeout */
1122 GNUNET_STREAM_TIMEOUT,
1129 * Handler for DATA messages; Same for both client and server
1131 * @param socket the socket through which the ack was received
1132 * @param tunnel connection to the other end
1133 * @param sender who sent the message
1134 * @param msg the data message
1135 * @param atsi performance data for the connection
1136 * @return GNUNET_OK to keep the connection open,
1137 * GNUNET_SYSERR to close it (signal serious error)
1140 handle_data (struct GNUNET_STREAM_Socket *socket,
1141 struct GNUNET_MESH_Tunnel *tunnel,
1142 const struct GNUNET_PeerIdentity *sender,
1143 const struct GNUNET_STREAM_DataMessage *msg,
1144 const struct GNUNET_ATS_Information*atsi)
1146 const void *payload;
1147 struct GNUNET_TIME_Relative ack_deadline_rel;
1148 uint32_t bytes_needed;
1149 uint32_t relative_offset;
1150 uint32_t relative_sequence_number;
1153 size = htons (msg->header.header.size);
1154 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
1156 GNUNET_break_op (0);
1157 return GNUNET_SYSERR;
1159 if (0 != memcmp (sender, &socket->other_peer,
1160 sizeof (struct GNUNET_PeerIdentity)))
1162 LOG (GNUNET_ERROR_TYPE_DEBUG,
1163 "%s: Received DATA from non-confirming peer\n",
1164 GNUNET_i2s (&socket->other_peer));
1167 switch (socket->state)
1169 case STATE_ESTABLISHED:
1170 case STATE_TRANSMIT_CLOSED:
1171 case STATE_TRANSMIT_CLOSE_WAIT:
1172 /* check if the message's sequence number is in the range we are
1174 relative_sequence_number =
1175 ntohl (msg->sequence_number) - socket->read_sequence_number;
1176 if ( relative_sequence_number >= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1178 LOG (GNUNET_ERROR_TYPE_DEBUG,
1179 "%s: Ignoring received message with sequence number %u\n",
1180 GNUNET_i2s (&socket->other_peer),
1181 ntohl (msg->sequence_number));
1182 /* Start ACK sending task if one is not already present */
1183 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1185 socket->ack_task_id =
1186 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1187 (msg->ack_deadline),
1193 /* Check if we have already seen this message */
1194 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1195 relative_sequence_number))
1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 "%s: Ignoring already received message with sequence number %u\n",
1199 GNUNET_i2s (&socket->other_peer),
1200 ntohl (msg->sequence_number));
1201 /* Start ACK sending task if one is not already present */
1202 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1204 socket->ack_task_id =
1205 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1206 (msg->ack_deadline), &ack_task, socket);
1210 LOG (GNUNET_ERROR_TYPE_DEBUG,
1211 "%s: Receiving DATA with sequence number: %u and size: %d from %s\n",
1212 GNUNET_i2s (&socket->other_peer), ntohl (msg->sequence_number),
1213 ntohs (msg->header.header.size), GNUNET_i2s (&socket->other_peer));
1214 /* Check if we have to allocate the buffer */
1215 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1216 relative_offset = ntohl (msg->offset) - socket->read_offset;
1217 bytes_needed = relative_offset + size;
1218 if (bytes_needed > socket->receive_buffer_size)
1220 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1222 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1224 socket->receive_buffer_size = bytes_needed;
1228 LOG (GNUNET_ERROR_TYPE_DEBUG,
1229 "%s: Cannot accommodate packet %d as buffer is full\n",
1230 GNUNET_i2s (&socket->other_peer), ntohl (msg->sequence_number));
1234 /* Copy Data to buffer */
1236 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1237 memcpy (socket->receive_buffer + relative_offset, payload, size);
1238 socket->receive_buffer_boundaries[relative_sequence_number] =
1239 relative_offset + size;
1240 /* Modify the ACK bitmap */
1241 ackbitmap_modify_bit (&socket->ack_bitmap, relative_sequence_number,
1243 /* Start ACK sending task if one is not already present */
1244 ack_deadline_rel = GNUNET_TIME_relative_ntoh (msg->ack_deadline);
1245 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1248 GNUNET_TIME_relative_min (ack_deadline_rel,
1249 GNUNET_TIME_relative_multiply
1250 (GNUNET_TIME_UNIT_SECONDS, 300));
1251 socket->ack_task_id =
1252 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1253 (msg->ack_deadline), &ack_task, socket);
1254 socket->ack_time_registered = GNUNET_TIME_absolute_get ();
1255 socket->ack_time_deadline = ack_deadline_rel;
1259 struct GNUNET_TIME_Relative ack_time_past;
1260 struct GNUNET_TIME_Relative ack_time_remaining;
1261 struct GNUNET_TIME_Relative ack_time_min;
1263 GNUNET_TIME_absolute_get_duration (socket->ack_time_registered);
1264 ack_time_remaining = GNUNET_TIME_relative_subtract
1265 (socket->ack_time_deadline, ack_time_past);
1266 ack_time_min = GNUNET_TIME_relative_min (ack_time_remaining,
1268 if (0 == memcmp(&ack_deadline_rel, &ack_time_min,
1269 sizeof (struct GNUNET_TIME_Relative)))
1271 ack_deadline_rel = ack_time_min;
1272 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
1273 socket->ack_task_id = GNUNET_SCHEDULER_add_delayed (ack_deadline_rel,
1275 socket->ack_time_registered = GNUNET_TIME_absolute_get ();
1276 socket->ack_time_deadline = ack_deadline_rel;
1279 if ((NULL != socket->read_handle) /* A read handle is waiting */
1280 /* There is no current read task */
1281 && (GNUNET_SCHEDULER_NO_TASK == socket->read_handle->read_task_id)
1282 /* We have the first packet */
1283 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap, 0)))
1285 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Scheduling read processor\n",
1286 GNUNET_i2s (&socket->other_peer));
1287 socket->read_handle->read_task_id =
1288 GNUNET_SCHEDULER_add_now (&call_read_processor, socket);
1292 LOG (GNUNET_ERROR_TYPE_DEBUG,
1293 "%s: Received data message when it cannot be handled\n",
1294 GNUNET_i2s (&socket->other_peer));
1302 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1304 * @param cls the socket (set from GNUNET_MESH_connect)
1305 * @param tunnel connection to the other end
1306 * @param tunnel_ctx place to store local state associated with the tunnel
1307 * @param sender who sent the message
1308 * @param message the actual message
1309 * @param atsi performance data for the connection
1310 * @return GNUNET_OK to keep the connection open,
1311 * GNUNET_SYSERR to close it (signal serious error)
1314 client_handle_data (void *cls,
1315 struct GNUNET_MESH_Tunnel *tunnel,
1317 const struct GNUNET_PeerIdentity *sender,
1318 const struct GNUNET_MessageHeader *message,
1319 const struct GNUNET_ATS_Information*atsi)
1321 struct GNUNET_STREAM_Socket *socket = cls;
1323 return handle_data (socket, tunnel, sender,
1324 (const struct GNUNET_STREAM_DataMessage *) message, atsi);
1329 * Callback to set state to ESTABLISHED
1331 * @param cls the closure NULL;
1332 * @param socket the socket to requiring state change
1335 set_state_established (void *cls,
1336 struct GNUNET_STREAM_Socket *socket)
1338 LOG (GNUNET_ERROR_TYPE_DEBUG,
1339 "%s: Attaining ESTABLISHED state\n",
1340 GNUNET_i2s (&socket->other_peer));
1341 socket->write_offset = 0;
1342 socket->read_offset = 0;
1343 socket->state = STATE_ESTABLISHED;
1344 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
1345 socket->control_retransmission_task_id);
1346 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
1347 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
1348 if (NULL != socket->lsocket)
1350 LOG (GNUNET_ERROR_TYPE_DEBUG,
1351 "%s: Calling listen callback\n",
1352 GNUNET_i2s (&socket->other_peer));
1353 if (GNUNET_SYSERR ==
1354 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1356 &socket->other_peer))
1358 socket->state = STATE_CLOSED;
1359 /* FIXME: We should close in a decent way (send RST) */
1360 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1361 GNUNET_free (socket);
1365 socket->open_cb (socket->open_cls, socket);
1370 * Callback to set state to HELLO_WAIT
1372 * @param cls the closure from queue_message
1373 * @param socket the socket to requiring state change
1376 set_state_hello_wait (void *cls,
1377 struct GNUNET_STREAM_Socket *socket)
1379 GNUNET_assert (STATE_INIT == socket->state);
1380 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "%s: Attaining HELLO_WAIT state\n",
1382 GNUNET_i2s (&socket->other_peer));
1383 socket->state = STATE_HELLO_WAIT;
1388 * Callback to set state to CLOSE_WAIT
1390 * @param cls the closure from queue_message
1391 * @param socket the socket requiring state change
1394 set_state_close_wait (void *cls,
1395 struct GNUNET_STREAM_Socket *socket)
1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1398 "%s: Attaing CLOSE_WAIT state\n",
1399 GNUNET_i2s (&socket->other_peer));
1400 socket->state = STATE_CLOSE_WAIT;
1401 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1402 socket->receive_buffer = NULL;
1403 socket->receive_buffer_size = 0;
1408 * Callback to set state to RECEIVE_CLOSE_WAIT
1410 * @param cls the closure from queue_message
1411 * @param socket the socket requiring state change
1414 set_state_receive_close_wait (void *cls,
1415 struct GNUNET_STREAM_Socket *socket)
1417 LOG (GNUNET_ERROR_TYPE_DEBUG,
1418 "%s: Attaing RECEIVE_CLOSE_WAIT state\n",
1419 GNUNET_i2s (&socket->other_peer));
1420 socket->state = STATE_RECEIVE_CLOSE_WAIT;
1421 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1422 socket->receive_buffer = NULL;
1423 socket->receive_buffer_size = 0;
1428 * Callback to set state to TRANSMIT_CLOSE_WAIT
1430 * @param cls the closure from queue_message
1431 * @param socket the socket requiring state change
1434 set_state_transmit_close_wait (void *cls,
1435 struct GNUNET_STREAM_Socket *socket)
1437 LOG (GNUNET_ERROR_TYPE_DEBUG,
1438 "%s: Attaing TRANSMIT_CLOSE_WAIT state\n",
1439 GNUNET_i2s (&socket->other_peer));
1440 socket->state = STATE_TRANSMIT_CLOSE_WAIT;
1445 * Callback to set state to CLOSED
1447 * @param cls the closure from queue_message
1448 * @param socket the socket requiring state change
1451 set_state_closed (void *cls,
1452 struct GNUNET_STREAM_Socket *socket)
1454 socket->state = STATE_CLOSED;
1459 * Returns GNUNET_MESSAGE_TYPE_STREAM_HELLO
1461 * @return the generate hello message
1463 static struct GNUNET_STREAM_MessageHeader *
1464 generate_hello (void)
1466 struct GNUNET_STREAM_MessageHeader *msg;
1468 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1469 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
1470 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1476 * Returns a new HelloAckMessage. Also sets the write sequence number for the
1479 * @param socket the socket for which this HelloAckMessage has to be generated
1480 * @param generate_seq GNUNET_YES to generate the write sequence number,
1481 * GNUNET_NO to use the existing sequence number
1482 * @return the HelloAckMessage
1484 static struct GNUNET_STREAM_HelloAckMessage *
1485 generate_hello_ack (struct GNUNET_STREAM_Socket *socket,
1488 struct GNUNET_STREAM_HelloAckMessage *msg;
1490 if (GNUNET_YES == generate_seq)
1492 if (GNUNET_YES == socket->testing_active)
1493 socket->write_sequence_number =
1494 socket->testing_set_write_sequence_number_value;
1496 socket->write_sequence_number =
1497 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1498 LOG_DEBUG ("%s: write sequence number %u\n",
1499 GNUNET_i2s (&socket->other_peer),
1500 (unsigned int) socket->write_sequence_number);
1502 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1503 msg->header.header.size =
1504 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1505 msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1506 msg->sequence_number = htonl (socket->write_sequence_number);
1507 msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1513 * Task for retransmitting control messages if they aren't ACK'ed before a
1516 * @param cls the socket
1517 * @param tc the Task context
1520 control_retransmission_task (void *cls,
1521 const struct GNUNET_SCHEDULER_TaskContext *tc)
1523 struct GNUNET_STREAM_Socket *socket = cls;
1525 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
1526 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
1528 LOG_DEBUG ("%s: Retransmitting a control message\n",
1529 GNUNET_i2s (&socket->other_peer));
1530 switch (socket->state)
1538 case STATE_HELLO_WAIT:
1539 if (NULL == socket->lsocket) /* We are client */
1540 queue_message (socket, generate_hello (), NULL, NULL, GNUNET_NO);
1542 queue_message (socket,
1543 (struct GNUNET_STREAM_MessageHeader *)
1544 generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
1546 socket->control_retransmission_task_id =
1547 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
1548 &control_retransmission_task, socket);
1550 case STATE_ESTABLISHED:
1551 if (NULL == socket->lsocket)
1552 queue_message (socket,
1553 (struct GNUNET_STREAM_MessageHeader *)
1554 generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
1566 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1568 * @param cls the socket (set from GNUNET_MESH_connect)
1569 * @param tunnel connection to the other end
1570 * @param tunnel_ctx this is NULL
1571 * @param sender who sent the message
1572 * @param message the actual message
1573 * @param atsi performance data for the connection
1574 * @return GNUNET_OK to keep the connection open,
1575 * GNUNET_SYSERR to close it (signal serious error)
1578 client_handle_hello_ack (void *cls,
1579 struct GNUNET_MESH_Tunnel *tunnel,
1581 const struct GNUNET_PeerIdentity *sender,
1582 const struct GNUNET_MessageHeader *message,
1583 const struct GNUNET_ATS_Information*atsi)
1585 struct GNUNET_STREAM_Socket *socket = cls;
1586 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1587 struct GNUNET_STREAM_HelloAckMessage *reply;
1589 if (0 != memcmp (sender, &socket->other_peer,
1590 sizeof (struct GNUNET_PeerIdentity)))
1592 LOG (GNUNET_ERROR_TYPE_DEBUG,
1593 "%s: Received HELLO_ACK from non-confirming peer\n",
1594 GNUNET_i2s (&socket->other_peer));
1597 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1598 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received HELLO_ACK from %s\n",
1599 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1600 GNUNET_assert (socket->tunnel == tunnel);
1601 switch (socket->state)
1603 case STATE_HELLO_WAIT:
1604 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1605 LOG (GNUNET_ERROR_TYPE_DEBUG,
1606 "%s: Read sequence number %u\n",
1607 GNUNET_i2s (&socket->other_peer),
1608 (unsigned int) socket->read_sequence_number);
1609 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1610 reply = generate_hello_ack (socket, GNUNET_YES);
1611 queue_message (socket, &reply->header, &set_state_established,
1614 case STATE_ESTABLISHED:
1615 // call statistics (# ACKs ignored++)
1616 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
1617 socket->control_retransmission_task_id);
1618 socket->control_retransmission_task_id =
1619 GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
1622 LOG_DEBUG ("%s: Server %s sent HELLO_ACK when in state %d\n",
1623 GNUNET_i2s (&socket->other_peer),
1624 GNUNET_i2s (&socket->other_peer), socket->state);
1625 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1626 return GNUNET_SYSERR;
1632 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1634 * @param cls the socket (set from GNUNET_MESH_connect)
1635 * @param tunnel connection to the other end
1636 * @param tunnel_ctx this is NULL
1637 * @param sender who sent the message
1638 * @param message the actual message
1639 * @param atsi performance data for the connection
1640 * @return GNUNET_OK to keep the connection open,
1641 * GNUNET_SYSERR to close it (signal serious error)
1644 client_handle_reset (void *cls,
1645 struct GNUNET_MESH_Tunnel *tunnel,
1647 const struct GNUNET_PeerIdentity *sender,
1648 const struct GNUNET_MessageHeader *message,
1649 const struct GNUNET_ATS_Information*atsi)
1651 // struct GNUNET_STREAM_Socket *socket = cls;
1658 * Frees the socket's receive buffers, marks the socket as receive closed and
1659 * calls the DataProcessor with GNUNET_STREAM_SHUTDOWN status if a read handle
1662 * @param socket the socket
1665 do_receive_shutdown (struct GNUNET_STREAM_Socket *socket)
1667 socket->receive_closed = GNUNET_YES;
1668 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1669 socket->receive_buffer = NULL;
1670 socket->receive_buffer_size = 0;
1671 if (NULL != socket->read_handle)
1673 GNUNET_STREAM_DataProcessor proc;
1676 proc = socket->read_handle->proc;
1677 proc_cls = socket->read_handle->proc_cls;
1678 GNUNET_STREAM_read_cancel (socket->read_handle);
1679 socket->read_handle = NULL;
1681 proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
1687 * Marks the socket as transmit closed and calls the CompletionContinuation with
1688 * GNUNET_STREAM_SHUTDOWN status if a write handle is present
1690 * @param socket the socket
1693 do_transmit_shutdown (struct GNUNET_STREAM_Socket *socket)
1695 socket->transmit_closed = GNUNET_YES;
1696 /* If write handle is present call it with GNUNET_STREAM_SHUTDOWN to signal
1697 that that stream has been shutdown */
1698 if (NULL != socket->write_handle)
1700 GNUNET_STREAM_CompletionContinuation wc;
1703 wc = socket->write_handle->write_cont;
1704 wc_cls = socket->write_handle->write_cont_cls;
1705 GNUNET_STREAM_write_cancel (socket->write_handle);
1706 socket->write_handle = NULL;
1709 GNUNET_STREAM_SHUTDOWN, 0);
1715 * Common message handler for handling TRANSMIT_CLOSE messages
1717 * @param socket the socket through which the ack was received
1718 * @param tunnel connection to the other end
1719 * @param sender who sent the message
1720 * @param msg the transmit close message
1721 * @param atsi performance data for the connection
1722 * @return GNUNET_OK to keep the connection open,
1723 * GNUNET_SYSERR to close it (signal serious error)
1726 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1727 struct GNUNET_MESH_Tunnel *tunnel,
1728 const struct GNUNET_PeerIdentity *sender,
1729 const struct GNUNET_STREAM_MessageHeader *msg,
1730 const struct GNUNET_ATS_Information*atsi)
1732 struct GNUNET_STREAM_MessageHeader *reply;
1734 switch (socket->state)
1738 case STATE_HELLO_WAIT:
1739 LOG (GNUNET_ERROR_TYPE_DEBUG,
1740 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1741 GNUNET_i2s (&socket->other_peer));
1746 /* Send TRANSMIT_CLOSE_ACK */
1747 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1748 reply->header.type =
1749 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1750 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1751 queue_message (socket, reply, NULL, NULL, GNUNET_NO);
1752 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received TRANSMIT_CLOSE from %s\n",
1753 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1754 switch(socket->state)
1756 case STATE_RECEIVE_CLOSED:
1757 case STATE_RECEIVE_CLOSE_WAIT:
1758 case STATE_CLOSE_WAIT:
1764 do_receive_shutdown (socket);
1765 if (GNUNET_YES == socket->transmit_closed)
1766 socket->state = STATE_CLOSED;
1768 socket->state = STATE_RECEIVE_CLOSED;
1774 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1776 * @param cls the socket (set from GNUNET_MESH_connect)
1777 * @param tunnel connection to the other end
1778 * @param tunnel_ctx this is NULL
1779 * @param sender who sent the message
1780 * @param message the actual message
1781 * @param atsi performance data for the connection
1782 * @return GNUNET_OK to keep the connection open,
1783 * GNUNET_SYSERR to close it (signal serious error)
1786 client_handle_transmit_close (void *cls,
1787 struct GNUNET_MESH_Tunnel *tunnel,
1789 const struct GNUNET_PeerIdentity *sender,
1790 const struct GNUNET_MessageHeader *message,
1791 const struct GNUNET_ATS_Information*atsi)
1793 struct GNUNET_STREAM_Socket *socket = cls;
1795 return handle_transmit_close (socket,
1798 (struct GNUNET_STREAM_MessageHeader *)message,
1804 * Task for calling the shutdown continuation callback
1806 * @param cls the socket
1807 * @param tc the scheduler task context
1810 call_cont_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1812 struct GNUNET_STREAM_Socket *socket = cls;
1814 GNUNET_assert (NULL != socket->shutdown_handle);
1815 socket->shutdown_handle->call_cont_task_id = GNUNET_SCHEDULER_NO_TASK;
1816 if (NULL != socket->shutdown_handle->completion_cb)
1817 socket->shutdown_handle->completion_cb
1818 (socket->shutdown_handle->completion_cls,
1819 socket->shutdown_handle->operation);
1820 GNUNET_free (socket->shutdown_handle);
1821 socket->shutdown_handle = NULL;
1826 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_*_CLOSE_ACK messages
1828 * @param socket the socket
1829 * @param tunnel connection to the other end
1830 * @param sender who sent the message
1831 * @param message the actual message
1832 * @param atsi performance data for the connection
1833 * @param operation the close operation which is being ACK'ed
1834 * @return GNUNET_OK to keep the connection open,
1835 * GNUNET_SYSERR to close it (signal serious error)
1838 handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
1839 struct GNUNET_MESH_Tunnel *tunnel,
1840 const struct GNUNET_PeerIdentity *sender,
1841 const struct GNUNET_STREAM_MessageHeader *message,
1842 const struct GNUNET_ATS_Information *atsi,
1845 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
1847 shutdown_handle = socket->shutdown_handle;
1848 if (NULL == shutdown_handle)
1850 /* This may happen when the shudown handle is cancelled */
1851 LOG (GNUNET_ERROR_TYPE_DEBUG,
1852 "%s: Received CLOSE_ACK when shutdown handle is NULL\n",
1853 GNUNET_i2s (&socket->other_peer));
1859 switch (socket->state)
1861 case STATE_CLOSE_WAIT:
1862 if (SHUT_RDWR != shutdown_handle->operation)
1864 LOG (GNUNET_ERROR_TYPE_DEBUG,
1865 "%s: Received CLOSE_ACK when shutdown handle is not for "
1866 "SHUT_RDWR\n", GNUNET_i2s (&socket->other_peer));
1869 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received CLOSE_ACK from %s\n",
1870 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1871 socket->state = STATE_CLOSED;
1874 LOG (GNUNET_ERROR_TYPE_DEBUG,
1875 "%s: Received CLOSE_ACK when it is not expected\n",
1876 GNUNET_i2s (&socket->other_peer));
1881 switch (socket->state)
1883 case STATE_RECEIVE_CLOSE_WAIT:
1884 if (SHUT_RD != shutdown_handle->operation)
1886 LOG (GNUNET_ERROR_TYPE_DEBUG,
1887 "%s: Received RECEIVE_CLOSE_ACK when shutdown handle "
1888 "is not for SHUT_RD\n", GNUNET_i2s (&socket->other_peer));
1891 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received RECEIVE_CLOSE_ACK from %s\n",
1892 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1893 socket->state = STATE_RECEIVE_CLOSED;
1896 LOG (GNUNET_ERROR_TYPE_DEBUG,
1897 "%s: Received RECEIVE_CLOSE_ACK when it is not expected\n",
1898 GNUNET_i2s (&socket->other_peer));
1903 switch (socket->state)
1905 case STATE_TRANSMIT_CLOSE_WAIT:
1906 if (SHUT_WR != shutdown_handle->operation)
1908 LOG (GNUNET_ERROR_TYPE_DEBUG,
1909 "%s: Received TRANSMIT_CLOSE_ACK when shutdown handle "
1910 "is not for SHUT_WR\n",
1911 GNUNET_i2s (&socket->other_peer));
1914 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received TRANSMIT_CLOSE_ACK from %s\n",
1915 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1916 socket->state = STATE_TRANSMIT_CLOSED;
1919 LOG (GNUNET_ERROR_TYPE_DEBUG,
1920 "%s: Received TRANSMIT_CLOSE_ACK when it is not expected\n",
1921 GNUNET_i2s (&socket->other_peer));
1928 shutdown_handle->call_cont_task_id = GNUNET_SCHEDULER_add_now
1929 (&call_cont_task, socket);
1930 if (GNUNET_SCHEDULER_NO_TASK
1931 != shutdown_handle->close_msg_retransmission_task_id)
1933 GNUNET_SCHEDULER_cancel
1934 (shutdown_handle->close_msg_retransmission_task_id);
1935 shutdown_handle->close_msg_retransmission_task_id =
1936 GNUNET_SCHEDULER_NO_TASK;
1943 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1945 * @param cls the socket (set from GNUNET_MESH_connect)
1946 * @param tunnel connection to the other end
1947 * @param tunnel_ctx this is NULL
1948 * @param sender who sent the message
1949 * @param message the actual message
1950 * @param atsi performance data for the connection
1951 * @return GNUNET_OK to keep the connection open,
1952 * GNUNET_SYSERR to close it (signal serious error)
1955 client_handle_transmit_close_ack (void *cls,
1956 struct GNUNET_MESH_Tunnel *tunnel,
1958 const struct GNUNET_PeerIdentity *sender,
1959 const struct GNUNET_MessageHeader *message,
1960 const struct GNUNET_ATS_Information*atsi)
1962 struct GNUNET_STREAM_Socket *socket = cls;
1964 return handle_generic_close_ack (socket,
1967 (const struct GNUNET_STREAM_MessageHeader *)
1975 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1977 * @param socket the socket
1978 * @param tunnel connection to the other end
1979 * @param sender who sent the message
1980 * @param message the actual message
1981 * @param atsi performance data for the connection
1982 * @return GNUNET_OK to keep the connection open,
1983 * GNUNET_SYSERR to close it (signal serious error)
1986 handle_receive_close (struct GNUNET_STREAM_Socket *socket,
1987 struct GNUNET_MESH_Tunnel *tunnel,
1988 const struct GNUNET_PeerIdentity *sender,
1989 const struct GNUNET_STREAM_MessageHeader *message,
1990 const struct GNUNET_ATS_Information *atsi)
1992 struct GNUNET_STREAM_MessageHeader *receive_close_ack;
1994 switch (socket->state)
1998 case STATE_HELLO_WAIT:
1999 LOG (GNUNET_ERROR_TYPE_DEBUG,
2000 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
2001 GNUNET_i2s (&socket->other_peer));
2006 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received RECEIVE_CLOSE from %s\n",
2007 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
2009 GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2010 receive_close_ack->header.size =
2011 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2012 receive_close_ack->header.type =
2013 htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK);
2014 queue_message (socket, receive_close_ack, NULL, NULL, GNUNET_NO);
2015 switch (socket->state)
2017 case STATE_TRANSMIT_CLOSED:
2018 case STATE_TRANSMIT_CLOSE_WAIT:
2020 case STATE_CLOSE_WAIT:
2025 do_transmit_shutdown (socket);
2026 if (GNUNET_YES == socket->receive_closed)
2027 socket->state = STATE_CLOSED;
2029 socket->state = STATE_TRANSMIT_CLOSED;
2035 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
2037 * @param cls the socket (set from GNUNET_MESH_connect)
2038 * @param tunnel connection to the other end
2039 * @param tunnel_ctx this is NULL
2040 * @param sender who sent the message
2041 * @param message the actual message
2042 * @param atsi performance data for the connection
2043 * @return GNUNET_OK to keep the connection open,
2044 * GNUNET_SYSERR to close it (signal serious error)
2047 client_handle_receive_close (void *cls,
2048 struct GNUNET_MESH_Tunnel *tunnel,
2050 const struct GNUNET_PeerIdentity *sender,
2051 const struct GNUNET_MessageHeader *message,
2052 const struct GNUNET_ATS_Information*atsi)
2054 struct GNUNET_STREAM_Socket *socket = cls;
2057 handle_receive_close (socket,
2060 (const struct GNUNET_STREAM_MessageHeader *) message,
2066 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
2068 * @param cls the socket (set from GNUNET_MESH_connect)
2069 * @param tunnel connection to the other end
2070 * @param tunnel_ctx this is NULL
2071 * @param sender who sent the message
2072 * @param message the actual message
2073 * @param atsi performance data for the connection
2074 * @return GNUNET_OK to keep the connection open,
2075 * GNUNET_SYSERR to close it (signal serious error)
2078 client_handle_receive_close_ack (void *cls,
2079 struct GNUNET_MESH_Tunnel *tunnel,
2081 const struct GNUNET_PeerIdentity *sender,
2082 const struct GNUNET_MessageHeader *message,
2083 const struct GNUNET_ATS_Information*atsi)
2085 struct GNUNET_STREAM_Socket *socket = cls;
2087 return handle_generic_close_ack (socket,
2090 (const struct GNUNET_STREAM_MessageHeader *)
2098 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2100 * @param socket the socket
2101 * @param tunnel connection to the other end
2102 * @param sender who sent the message
2103 * @param message the actual message
2104 * @param atsi performance data for the connection
2105 * @return GNUNET_OK to keep the connection open,
2106 * GNUNET_SYSERR to close it (signal serious error)
2109 handle_close (struct GNUNET_STREAM_Socket *socket,
2110 struct GNUNET_MESH_Tunnel *tunnel,
2111 const struct GNUNET_PeerIdentity *sender,
2112 const struct GNUNET_STREAM_MessageHeader *message,
2113 const struct GNUNET_ATS_Information*atsi)
2115 struct GNUNET_STREAM_MessageHeader *close_ack;
2117 switch (socket->state)
2121 case STATE_HELLO_WAIT:
2122 LOG (GNUNET_ERROR_TYPE_DEBUG,
2123 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
2124 GNUNET_i2s (&socket->other_peer));
2129 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received CLOSE from %s\n",
2130 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
2131 close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2132 close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2133 close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK);
2134 queue_message (socket, close_ack, &set_state_closed, NULL, GNUNET_NO);
2135 if ((STATE_CLOSED == socket->state) || (STATE_CLOSE_WAIT == socket->state))
2137 if (GNUNET_NO == socket->transmit_closed)
2138 do_transmit_shutdown (socket);
2139 if (GNUNET_NO == socket->receive_closed)
2140 do_receive_shutdown (socket);
2146 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2148 * @param cls the socket (set from GNUNET_MESH_connect)
2149 * @param tunnel connection to the other end
2150 * @param tunnel_ctx this is NULL
2151 * @param sender who sent the message
2152 * @param message the actual message
2153 * @param atsi performance data for the connection
2154 * @return GNUNET_OK to keep the connection open,
2155 * GNUNET_SYSERR to close it (signal serious error)
2158 client_handle_close (void *cls,
2159 struct GNUNET_MESH_Tunnel *tunnel,
2161 const struct GNUNET_PeerIdentity *sender,
2162 const struct GNUNET_MessageHeader *message,
2163 const struct GNUNET_ATS_Information*atsi)
2165 struct GNUNET_STREAM_Socket *socket = cls;
2167 return handle_close (socket,
2170 (const struct GNUNET_STREAM_MessageHeader *) message,
2176 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2178 * @param cls the socket (set from GNUNET_MESH_connect)
2179 * @param tunnel connection to the other end
2180 * @param tunnel_ctx this is NULL
2181 * @param sender who sent the message
2182 * @param message the actual message
2183 * @param atsi performance data for the connection
2184 * @return GNUNET_OK to keep the connection open,
2185 * GNUNET_SYSERR to close it (signal serious error)
2188 client_handle_close_ack (void *cls,
2189 struct GNUNET_MESH_Tunnel *tunnel,
2191 const struct GNUNET_PeerIdentity *sender,
2192 const struct GNUNET_MessageHeader *message,
2193 const struct GNUNET_ATS_Information *atsi)
2195 struct GNUNET_STREAM_Socket *socket = cls;
2197 return handle_generic_close_ack (socket,
2200 (const struct GNUNET_STREAM_MessageHeader *)
2206 /*****************************/
2207 /* Server's Message Handlers */
2208 /*****************************/
2211 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
2213 * @param cls the closure
2214 * @param tunnel connection to the other end
2215 * @param tunnel_ctx the socket
2216 * @param sender who sent the message
2217 * @param message the actual message
2218 * @param atsi performance data for the connection
2219 * @return GNUNET_OK to keep the connection open,
2220 * GNUNET_SYSERR to close it (signal serious error)
2223 server_handle_data (void *cls,
2224 struct GNUNET_MESH_Tunnel *tunnel,
2226 const struct GNUNET_PeerIdentity *sender,
2227 const struct GNUNET_MessageHeader *message,
2228 const struct GNUNET_ATS_Information*atsi)
2230 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2232 return handle_data (socket,
2235 (const struct GNUNET_STREAM_DataMessage *)message,
2241 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
2243 * @param cls the closure
2244 * @param tunnel connection to the other end
2245 * @param tunnel_ctx the socket
2246 * @param sender who sent the message
2247 * @param message the actual message
2248 * @param atsi performance data for the connection
2249 * @return GNUNET_OK to keep the connection open,
2250 * GNUNET_SYSERR to close it (signal serious error)
2253 server_handle_hello (void *cls,
2254 struct GNUNET_MESH_Tunnel *tunnel,
2256 const struct GNUNET_PeerIdentity *sender,
2257 const struct GNUNET_MessageHeader *message,
2258 const struct GNUNET_ATS_Information*atsi)
2260 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2261 struct GNUNET_STREAM_HelloAckMessage *reply;
2263 if (0 != memcmp (sender,
2264 &socket->other_peer,
2265 sizeof (struct GNUNET_PeerIdentity)))
2267 LOG_DEBUG ("%s: Received HELLO from non-confirming peer\n",
2268 GNUNET_i2s (&socket->other_peer));
2271 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO == ntohs (message->type));
2272 GNUNET_assert (socket->tunnel == tunnel);
2273 LOG_DEBUG ("%s: Received HELLO from %s\n", GNUNET_i2s (&socket->other_peer),
2274 GNUNET_i2s (&socket->other_peer));
2275 switch (socket->state)
2278 reply = generate_hello_ack (socket, GNUNET_YES);
2279 queue_message (socket, &reply->header, &set_state_hello_wait, NULL,
2281 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
2282 socket->control_retransmission_task_id);
2283 socket->control_retransmission_task_id =
2284 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2285 &control_retransmission_task, socket);
2287 case STATE_HELLO_WAIT:
2288 /* Perhaps our HELLO_ACK was lost */
2289 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
2290 socket->control_retransmission_task_id);
2291 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
2292 socket->control_retransmission_task_id =
2293 GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
2296 LOG_DEBUG( "%s: Client sent HELLO when in state %d\n",
2297 GNUNET_i2s (&socket->other_peer), socket->state);
2298 /* FIXME: Send RESET? */
2305 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
2307 * @param cls the closure
2308 * @param tunnel connection to the other end
2309 * @param tunnel_ctx the socket
2310 * @param sender who sent the message
2311 * @param message the actual message
2312 * @param atsi performance data for the connection
2313 * @return GNUNET_OK to keep the connection open,
2314 * GNUNET_SYSERR to close it (signal serious error)
2317 server_handle_hello_ack (void *cls,
2318 struct GNUNET_MESH_Tunnel *tunnel,
2320 const struct GNUNET_PeerIdentity *sender,
2321 const struct GNUNET_MessageHeader *message,
2322 const struct GNUNET_ATS_Information*atsi)
2324 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2325 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
2327 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
2328 ntohs (message->type));
2329 GNUNET_assert (socket->tunnel == tunnel);
2330 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
2331 switch (socket->state)
2333 case STATE_HELLO_WAIT:
2334 LOG (GNUNET_ERROR_TYPE_DEBUG,
2335 "%s: Received HELLO_ACK from %s\n",
2336 GNUNET_i2s (&socket->other_peer),
2337 GNUNET_i2s (&socket->other_peer));
2338 socket->read_sequence_number = ntohl (ack_message->sequence_number);
2339 LOG (GNUNET_ERROR_TYPE_DEBUG,
2340 "%s: Read sequence number %u\n",
2341 GNUNET_i2s (&socket->other_peer),
2342 (unsigned int) socket->read_sequence_number);
2343 socket->receiver_window_available =
2344 ntohl (ack_message->receiver_window_size);
2345 set_state_established (NULL, socket);
2348 LOG (GNUNET_ERROR_TYPE_DEBUG,
2349 "Client sent HELLO_ACK when in state %d\n", socket->state);
2356 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
2358 * @param cls the closure
2359 * @param tunnel connection to the other end
2360 * @param tunnel_ctx the socket
2361 * @param sender who sent the message
2362 * @param message the actual message
2363 * @param atsi performance data for the connection
2364 * @return GNUNET_OK to keep the connection open,
2365 * GNUNET_SYSERR to close it (signal serious error)
2368 server_handle_reset (void *cls,
2369 struct GNUNET_MESH_Tunnel *tunnel,
2371 const struct GNUNET_PeerIdentity *sender,
2372 const struct GNUNET_MessageHeader *message,
2373 const struct GNUNET_ATS_Information*atsi)
2375 // struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2382 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
2384 * @param cls the closure
2385 * @param tunnel connection to the other end
2386 * @param tunnel_ctx the socket
2387 * @param sender who sent the message
2388 * @param message the actual message
2389 * @param atsi performance data for the connection
2390 * @return GNUNET_OK to keep the connection open,
2391 * GNUNET_SYSERR to close it (signal serious error)
2394 server_handle_transmit_close (void *cls,
2395 struct GNUNET_MESH_Tunnel *tunnel,
2397 const struct GNUNET_PeerIdentity *sender,
2398 const struct GNUNET_MessageHeader *message,
2399 const struct GNUNET_ATS_Information*atsi)
2401 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2403 return handle_transmit_close (socket,
2406 (struct GNUNET_STREAM_MessageHeader *)message,
2412 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
2414 * @param cls the closure
2415 * @param tunnel connection to the other end
2416 * @param tunnel_ctx the socket
2417 * @param sender who sent the message
2418 * @param message the actual message
2419 * @param atsi performance data for the connection
2420 * @return GNUNET_OK to keep the connection open,
2421 * GNUNET_SYSERR to close it (signal serious error)
2424 server_handle_transmit_close_ack (void *cls,
2425 struct GNUNET_MESH_Tunnel *tunnel,
2427 const struct GNUNET_PeerIdentity *sender,
2428 const struct GNUNET_MessageHeader *message,
2429 const struct GNUNET_ATS_Information*atsi)
2431 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2433 return handle_generic_close_ack (socket,
2436 (const struct GNUNET_STREAM_MessageHeader *)
2444 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
2446 * @param cls the closure
2447 * @param tunnel connection to the other end
2448 * @param tunnel_ctx the socket
2449 * @param sender who sent the message
2450 * @param message the actual message
2451 * @param atsi performance data for the connection
2452 * @return GNUNET_OK to keep the connection open,
2453 * GNUNET_SYSERR to close it (signal serious error)
2456 server_handle_receive_close (void *cls,
2457 struct GNUNET_MESH_Tunnel *tunnel,
2459 const struct GNUNET_PeerIdentity *sender,
2460 const struct GNUNET_MessageHeader *message,
2461 const struct GNUNET_ATS_Information*atsi)
2463 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2466 handle_receive_close (socket,
2469 (const struct GNUNET_STREAM_MessageHeader *) message,
2475 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
2477 * @param cls the closure
2478 * @param tunnel connection to the other end
2479 * @param tunnel_ctx the socket
2480 * @param sender who sent the message
2481 * @param message the actual message
2482 * @param atsi performance data for the connection
2483 * @return GNUNET_OK to keep the connection open,
2484 * GNUNET_SYSERR to close it (signal serious error)
2487 server_handle_receive_close_ack (void *cls,
2488 struct GNUNET_MESH_Tunnel *tunnel,
2490 const struct GNUNET_PeerIdentity *sender,
2491 const struct GNUNET_MessageHeader *message,
2492 const struct GNUNET_ATS_Information*atsi)
2494 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2496 return handle_generic_close_ack (socket,
2499 (const struct GNUNET_STREAM_MessageHeader *)
2507 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2509 * @param cls the listen socket (from GNUNET_MESH_connect in
2510 * GNUNET_STREAM_listen)
2511 * @param tunnel connection to the other end
2512 * @param tunnel_ctx the socket
2513 * @param sender who sent the message
2514 * @param message the actual message
2515 * @param atsi performance data for the connection
2516 * @return GNUNET_OK to keep the connection open,
2517 * GNUNET_SYSERR to close it (signal serious error)
2520 server_handle_close (void *cls,
2521 struct GNUNET_MESH_Tunnel *tunnel,
2523 const struct GNUNET_PeerIdentity *sender,
2524 const struct GNUNET_MessageHeader *message,
2525 const struct GNUNET_ATS_Information*atsi)
2527 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2529 return handle_close (socket,
2532 (const struct GNUNET_STREAM_MessageHeader *) message,
2538 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2540 * @param cls the closure
2541 * @param tunnel connection to the other end
2542 * @param tunnel_ctx the socket
2543 * @param sender who sent the message
2544 * @param message the actual message
2545 * @param atsi performance data for the connection
2546 * @return GNUNET_OK to keep the connection open,
2547 * GNUNET_SYSERR to close it (signal serious error)
2550 server_handle_close_ack (void *cls,
2551 struct GNUNET_MESH_Tunnel *tunnel,
2553 const struct GNUNET_PeerIdentity *sender,
2554 const struct GNUNET_MessageHeader *message,
2555 const struct GNUNET_ATS_Information*atsi)
2557 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2559 return handle_generic_close_ack (socket,
2562 (const struct GNUNET_STREAM_MessageHeader *)
2570 * Handler for DATA_ACK messages
2572 * @param socket the socket through which the ack was received
2573 * @param tunnel connection to the other end
2574 * @param sender who sent the message
2575 * @param ack the acknowledgment message
2576 * @param atsi performance data for the connection
2577 * @return GNUNET_OK to keep the connection open,
2578 * GNUNET_SYSERR to close it (signal serious error)
2581 handle_ack (struct GNUNET_STREAM_Socket *socket,
2582 struct GNUNET_MESH_Tunnel *tunnel,
2583 const struct GNUNET_PeerIdentity *sender,
2584 const struct GNUNET_STREAM_AckMessage *ack,
2585 const struct GNUNET_ATS_Information*atsi)
2587 struct GNUNET_STREAM_WriteHandle *write_handle;
2588 uint64_t ack_bitmap;
2589 unsigned int packet;
2590 int need_retransmission;
2591 uint32_t sequence_difference;
2593 if (0 != memcmp (sender,
2594 &socket->other_peer,
2595 sizeof (struct GNUNET_PeerIdentity)))
2597 LOG (GNUNET_ERROR_TYPE_DEBUG,
2598 "%s: Received ACK from non-confirming peer\n",
2599 GNUNET_i2s (&socket->other_peer));
2602 switch (socket->state)
2604 case (STATE_ESTABLISHED):
2605 case (STATE_RECEIVE_CLOSED):
2606 case (STATE_RECEIVE_CLOSE_WAIT):
2607 if (NULL == socket->write_handle)
2609 LOG (GNUNET_ERROR_TYPE_DEBUG,
2610 "%s: Received DATA_ACK when write_handle is NULL\n",
2611 GNUNET_i2s (&socket->other_peer));
2614 sequence_difference =
2615 socket->write_sequence_number - ntohl (ack->base_sequence_number);
2616 if (!(sequence_difference <= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
2618 LOG (GNUNET_ERROR_TYPE_DEBUG,
2619 "%s: Received DATA_ACK with unexpected base sequence number\n",
2620 GNUNET_i2s (&socket->other_peer));
2621 LOG (GNUNET_ERROR_TYPE_DEBUG,
2622 "%s: Current write sequence: %u; Ack's base sequence: %u\n",
2623 GNUNET_i2s (&socket->other_peer),
2624 socket->write_sequence_number,
2625 ntohl (ack->base_sequence_number));
2628 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received DATA_ACK from %s\n",
2629 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
2630 /* Cancel the retransmission task */
2631 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
2633 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
2634 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
2635 socket->data_retransmit_timeout = GNUNET_TIME_UNIT_SECONDS;
2637 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2639 if (NULL == socket->write_handle->messages[packet])
2641 /* BS: Base sequence from ack; PS: sequence num of current packet */
2642 sequence_difference = ntohl (ack->base_sequence_number)
2643 - ntohl (socket->write_handle->messages[packet]->sequence_number);
2644 if (0 == sequence_difference)
2645 break; /* The message in our handle is not yet received */
2646 /* case where BS = PS + GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
2647 /* sequence_difference <= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
2648 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
2649 packet, GNUNET_YES);
2651 if (((ntohl (ack->base_sequence_number)
2652 - (socket->write_handle->max_ack_base_num))
2653 <= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
2655 socket->write_handle->max_ack_base_num = ntohl (ack->base_sequence_number);
2656 socket->receiver_window_available =
2657 ntohl (ack->receive_window_remaining);
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2661 "Ignoring to modify receive window available as base: %u, max_ack_base: %u\n",
2662 ntohl (ack->base_sequence_number),
2663 socket->write_handle->max_ack_base_num);
2664 if ((packet == GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
2665 || ((packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
2666 && (NULL == socket->write_handle->messages[packet])))
2667 goto call_write_cont_cb;
2668 GNUNET_assert (ntohl
2669 (socket->write_handle->messages[packet]->sequence_number)
2670 == ntohl (ack->base_sequence_number));
2671 /* Update our bitmap */
2672 ack_bitmap = GNUNET_ntohll (ack->bitmap);
2673 for (; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2675 if (NULL == socket->write_handle->messages[packet]) break;
2676 if (ackbitmap_is_bit_set (&ack_bitmap, ntohl
2677 (socket->write_handle->messages[packet]->sequence_number)
2678 - ntohl (ack->base_sequence_number)))
2679 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap, packet, GNUNET_YES);
2681 /* Check if we have received all acknowledgements */
2682 need_retransmission = GNUNET_NO;
2683 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2685 if (NULL == socket->write_handle->messages[packet]) break;
2686 if (GNUNET_YES != ackbitmap_is_bit_set
2687 (&socket->write_handle->ack_bitmap,packet))
2689 need_retransmission = GNUNET_YES;
2693 if (GNUNET_YES == need_retransmission)
2695 write_data (socket);
2700 /* Free the packets */
2701 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2703 GNUNET_free_non_null (socket->write_handle->messages[packet]);
2705 write_handle = socket->write_handle;
2706 socket->write_handle = NULL;
2707 if (NULL != write_handle->write_cont)
2708 write_handle->write_cont (write_handle->write_cont_cls,
2710 write_handle->size);
2711 /* We are done with the write handle - Freeing it */
2712 GNUNET_free (write_handle);
2713 LOG (GNUNET_ERROR_TYPE_DEBUG,
2714 "%s: Write completion callback completed\n",
2715 GNUNET_i2s (&socket->other_peer));
2725 * Handler for DATA_ACK messages
2727 * @param cls the 'struct GNUNET_STREAM_Socket'
2728 * @param tunnel connection to the other end
2729 * @param tunnel_ctx unused
2730 * @param sender who sent the message
2731 * @param message the actual message
2732 * @param atsi performance data for the connection
2733 * @return GNUNET_OK to keep the connection open,
2734 * GNUNET_SYSERR to close it (signal serious error)
2737 client_handle_ack (void *cls,
2738 struct GNUNET_MESH_Tunnel *tunnel,
2740 const struct GNUNET_PeerIdentity *sender,
2741 const struct GNUNET_MessageHeader *message,
2742 const struct GNUNET_ATS_Information*atsi)
2744 struct GNUNET_STREAM_Socket *socket = cls;
2745 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2747 return handle_ack (socket, tunnel, sender, ack, atsi);
2752 * Handler for DATA_ACK messages
2754 * @param cls the server's listen socket
2755 * @param tunnel connection to the other end
2756 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2757 * @param sender who sent the message
2758 * @param message the actual message
2759 * @param atsi performance data for the connection
2760 * @return GNUNET_OK to keep the connection open,
2761 * GNUNET_SYSERR to close it (signal serious error)
2764 server_handle_ack (void *cls,
2765 struct GNUNET_MESH_Tunnel *tunnel,
2767 const struct GNUNET_PeerIdentity *sender,
2768 const struct GNUNET_MessageHeader *message,
2769 const struct GNUNET_ATS_Information*atsi)
2771 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2772 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2774 return handle_ack (socket, tunnel, sender, ack, atsi);
2779 * For client message handlers, the stream socket is in the
2782 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2783 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2784 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2785 sizeof (struct GNUNET_STREAM_AckMessage) },
2786 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2787 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2788 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2789 sizeof (struct GNUNET_STREAM_MessageHeader)},
2790 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2791 sizeof (struct GNUNET_STREAM_MessageHeader)},
2792 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2793 sizeof (struct GNUNET_STREAM_MessageHeader)},
2794 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2795 sizeof (struct GNUNET_STREAM_MessageHeader)},
2796 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2797 sizeof (struct GNUNET_STREAM_MessageHeader)},
2798 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2799 sizeof (struct GNUNET_STREAM_MessageHeader)},
2800 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2801 sizeof (struct GNUNET_STREAM_MessageHeader)},
2807 * For server message handlers, the stream socket is in the
2808 * tunnel context, and the listen socket in the closure argument.
2810 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2811 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2812 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2813 sizeof (struct GNUNET_STREAM_AckMessage) },
2814 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2815 sizeof (struct GNUNET_STREAM_MessageHeader)},
2816 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2817 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2818 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2819 sizeof (struct GNUNET_STREAM_MessageHeader)},
2820 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2821 sizeof (struct GNUNET_STREAM_MessageHeader)},
2822 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2823 sizeof (struct GNUNET_STREAM_MessageHeader)},
2824 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2825 sizeof (struct GNUNET_STREAM_MessageHeader)},
2826 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2827 sizeof (struct GNUNET_STREAM_MessageHeader)},
2828 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2829 sizeof (struct GNUNET_STREAM_MessageHeader)},
2830 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2831 sizeof (struct GNUNET_STREAM_MessageHeader)},
2837 * Function called when our target peer is connected to our tunnel
2839 * @param cls the socket for which this tunnel is created
2840 * @param peer the peer identity of the target
2841 * @param atsi performance data for the connection
2844 mesh_peer_connect_callback (void *cls,
2845 const struct GNUNET_PeerIdentity *peer,
2846 const struct GNUNET_ATS_Information * atsi)
2848 struct GNUNET_STREAM_Socket *socket = cls;
2849 struct GNUNET_STREAM_MessageHeader *message;
2851 if (0 != memcmp (peer,
2852 &socket->other_peer,
2853 sizeof (struct GNUNET_PeerIdentity)))
2855 LOG (GNUNET_ERROR_TYPE_DEBUG,
2856 "%s: A peer which is not our target has connected to our tunnel\n",
2860 LOG (GNUNET_ERROR_TYPE_DEBUG,
2861 "%s: Target peer %s connected\n",
2862 GNUNET_i2s (&socket->other_peer),
2863 GNUNET_i2s (&socket->other_peer));
2864 /* Set state to INIT */
2865 socket->state = STATE_INIT;
2866 /* Send HELLO message */
2867 message = generate_hello ();
2868 queue_message (socket, message, &set_state_hello_wait, NULL, GNUNET_NO);
2869 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
2870 socket->control_retransmission_task_id);
2871 socket->control_retransmission_task_id =
2872 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2873 &control_retransmission_task, socket);
2878 * Function called when our target peer is disconnected from our tunnel
2880 * @param cls the socket associated which this tunnel
2881 * @param peer the peer identity of the target
2884 mesh_peer_disconnect_callback (void *cls,
2885 const struct GNUNET_PeerIdentity *peer)
2887 struct GNUNET_STREAM_Socket *socket=cls;
2889 /* If the state is SHUTDOWN its ok; else set the state of the socket to SYSERR */
2890 LOG (GNUNET_ERROR_TYPE_DEBUG,
2891 "%s: Other peer %s disconnected \n",
2892 GNUNET_i2s (&socket->other_peer),
2893 GNUNET_i2s (&socket->other_peer));
2898 * Method called whenever a peer creates a tunnel to us
2900 * @param cls closure
2901 * @param tunnel new handle to the tunnel
2902 * @param initiator peer that started the tunnel
2903 * @param atsi performance information for the tunnel
2904 * @return initial tunnel context for the tunnel
2905 * (can be NULL -- that's not an error)
2908 new_tunnel_notify (void *cls,
2909 struct GNUNET_MESH_Tunnel *tunnel,
2910 const struct GNUNET_PeerIdentity *initiator,
2911 const struct GNUNET_ATS_Information *atsi)
2913 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2914 struct GNUNET_STREAM_Socket *socket;
2916 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2917 from the same peer again until the socket is closed */
2919 if (GNUNET_NO == lsocket->listening)
2921 GNUNET_MESH_tunnel_destroy (tunnel);
2924 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2925 socket->other_peer = *initiator;
2926 socket->tunnel = tunnel;
2927 socket->state = STATE_INIT;
2928 socket->lsocket = lsocket;
2929 socket->stat_handle = lsocket->stat_handle;
2930 socket->retransmit_timeout = lsocket->retransmit_timeout;
2931 socket->testing_active = lsocket->testing_active;
2932 socket->testing_set_write_sequence_number_value =
2933 lsocket->testing_set_write_sequence_number_value;
2934 socket->max_payload_size = lsocket->max_payload_size;
2935 LOG (GNUNET_ERROR_TYPE_DEBUG,
2936 "%s: Peer %s initiated tunnel to us\n",
2937 GNUNET_i2s (&socket->other_peer),
2938 GNUNET_i2s (&socket->other_peer));
2939 if (NULL != socket->stat_handle)
2941 GNUNET_STATISTICS_update (socket->stat_handle,
2942 "total inbound connections received",
2944 GNUNET_STATISTICS_update (socket->stat_handle,
2945 "inbound connections", 1, GNUNET_NO);
2953 * Function called whenever an inbound tunnel is destroyed. Should clean up
2954 * any associated state. This function is NOT called if the client has
2955 * explicitly asked for the tunnel to be destroyed using
2956 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2959 * @param cls closure (set from GNUNET_MESH_connect)
2960 * @param tunnel connection to the other end (henceforth invalid)
2961 * @param tunnel_ctx place where local state associated
2962 * with the tunnel is stored
2965 tunnel_cleaner (void *cls,
2966 const struct GNUNET_MESH_Tunnel *tunnel,
2969 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2970 struct MessageQueue *head;
2972 GNUNET_assert (tunnel == socket->tunnel);
2974 LOG (GNUNET_ERROR_TYPE_DEBUG,
2975 "%s: Peer %s has terminated connection abruptly\n",
2976 GNUNET_i2s (&socket->other_peer),
2977 GNUNET_i2s (&socket->other_peer));
2978 if (NULL != socket->stat_handle)
2980 GNUNET_STATISTICS_update (socket->stat_handle,
2981 "connections terminated abruptly", 1, GNUNET_NO);
2982 GNUNET_STATISTICS_update (socket->stat_handle,
2983 "inbound connections", -1, GNUNET_NO);
2985 /* Clear Transmit handles */
2986 if (NULL != socket->transmit_handle)
2988 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2989 socket->transmit_handle = NULL;
2991 /* Stop Tasks using socket->tunnel */
2992 if (GNUNET_SCHEDULER_NO_TASK != socket->ack_task_id)
2994 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2995 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2997 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
2999 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
3000 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
3002 /* Terminate the control retransmission tasks */
3003 if (GNUNET_SCHEDULER_NO_TASK != socket->control_retransmission_task_id)
3005 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
3006 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
3008 /* Clear existing message queue */
3009 while (NULL != (head = socket->queue_head)) {
3010 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
3013 GNUNET_free (head->message);
3016 socket->tunnel = NULL;
3021 * Callback to signal timeout on lockmanager lock acquire
3023 * @param cls the ListenSocket
3024 * @param tc the scheduler task context
3027 lockmanager_acquire_timeout (void *cls,
3028 const struct GNUNET_SCHEDULER_TaskContext *tc)
3030 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
3031 GNUNET_STREAM_ListenCallback listen_cb;
3032 void *listen_cb_cls;
3034 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
3035 listen_cb = lsocket->listen_cb;
3036 listen_cb_cls = lsocket->listen_cb_cls;
3037 if (NULL != listen_cb)
3038 listen_cb (listen_cb_cls, NULL, NULL);
3043 * Callback to notify us on the status changes on app_port lock
3045 * @param cls the ListenSocket
3046 * @param domain the domain name of the lock
3047 * @param lock the app_port
3048 * @param status the current status of the lock
3051 lock_status_change_cb (void *cls, const char *domain, uint32_t lock,
3052 enum GNUNET_LOCKMANAGER_Status status)
3054 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
3056 GNUNET_assert (lock == (uint32_t) lsocket->port);
3057 if (GNUNET_LOCKMANAGER_SUCCESS == status)
3059 lsocket->listening = GNUNET_YES;
3060 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
3062 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
3063 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
3065 if (NULL == lsocket->mesh)
3067 GNUNET_MESH_ApplicationType ports[] = {lsocket->port, 0};
3069 lsocket->mesh = GNUNET_MESH_connect (lsocket->cfg,
3070 lsocket, /* Closure */
3073 server_message_handlers,
3075 GNUNET_assert (NULL != lsocket->mesh);
3076 if (NULL != lsocket->listen_ok_cb)
3078 (void) lsocket->listen_ok_cb ();
3082 if (GNUNET_LOCKMANAGER_RELEASE == status)
3083 lsocket->listening = GNUNET_NO;
3093 * Tries to open a stream to the target peer
3095 * @param cfg configuration to use
3096 * @param target the target peer to which the stream has to be opened
3097 * @param app_port the application port number which uniquely identifies this
3099 * @param open_cb this function will be called after stream has be established;
3101 * @param open_cb_cls the closure for open_cb
3102 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
3103 * @return if successful it returns the stream socket; NULL if stream cannot be
3106 struct GNUNET_STREAM_Socket *
3107 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
3108 const struct GNUNET_PeerIdentity *target,
3109 GNUNET_MESH_ApplicationType app_port,
3110 GNUNET_STREAM_OpenCallback open_cb,
3114 struct GNUNET_STREAM_Socket *socket;
3115 enum GNUNET_STREAM_Option option;
3116 GNUNET_MESH_ApplicationType ports[] = {app_port, 0};
3118 uint16_t payload_size;
3120 LOG (GNUNET_ERROR_TYPE_DEBUG,
3122 GNUNET_assert (NULL != open_cb);
3123 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
3124 socket->other_peer = *target;
3125 socket->open_cb = open_cb;
3126 socket->open_cls = open_cb_cls;
3128 socket->retransmit_timeout = TIME_REL_SECS (default_timeout);
3129 socket->testing_active = GNUNET_NO;
3130 socket->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
3131 va_start (vargs, open_cb_cls); /* Parse variable args */
3133 option = va_arg (vargs, enum GNUNET_STREAM_Option);
3136 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
3137 /* Expect struct GNUNET_TIME_Relative */
3138 socket->retransmit_timeout = va_arg (vargs,
3139 struct GNUNET_TIME_Relative);
3141 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
3142 socket->testing_active = GNUNET_YES;
3143 socket->testing_set_write_sequence_number_value = va_arg (vargs,
3146 case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
3147 GNUNET_break (0); /* Option irrelevant in STREAM_open */
3149 case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
3150 GNUNET_break (0); /* Option irrelevant in STREAM_open */
3152 case GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE:
3153 payload_size = (uint16_t) va_arg (vargs, unsigned int);
3154 GNUNET_assert (0 != payload_size);
3155 if (payload_size < socket->max_payload_size)
3156 socket->max_payload_size = payload_size;
3158 case GNUNET_STREAM_OPTION_END:
3161 } while (GNUNET_STREAM_OPTION_END != option);
3162 va_end (vargs); /* End of variable args parsing */
3163 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
3165 NULL, /* No inbound tunnel handler */
3166 NULL, /* No in-tunnel cleaner */
3167 client_message_handlers,
3168 ports); /* We don't get inbound tunnels */
3169 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
3171 GNUNET_free (socket);
3174 /* Now create the mesh tunnel to target */
3175 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating MESH Tunnel\n");
3176 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
3177 NULL, /* Tunnel context */
3178 &mesh_peer_connect_callback,
3179 &mesh_peer_disconnect_callback,
3181 GNUNET_assert (NULL != socket->tunnel);
3182 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
3183 &socket->other_peer);
3184 socket->stat_handle = GNUNET_STATISTICS_create ("stream", cfg);
3185 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
3191 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
3193 * @param socket the stream socket
3194 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
3195 * @param completion_cb the callback that will be called upon successful
3196 * shutdown of given operation
3197 * @param completion_cls the closure for the completion callback
3198 * @return the shutdown handle
3200 struct GNUNET_STREAM_ShutdownHandle *
3201 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
3203 GNUNET_STREAM_ShutdownCompletion completion_cb,
3204 void *completion_cls)
3206 struct GNUNET_STREAM_ShutdownHandle *handle;
3207 struct GNUNET_STREAM_MessageHeader *msg;
3209 GNUNET_assert (NULL == socket->shutdown_handle);
3210 handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle));
3211 handle->socket = socket;
3212 handle->completion_cb = completion_cb;
3213 handle->completion_cls = completion_cls;
3214 socket->shutdown_handle = handle;
3215 if ( ((GNUNET_YES == socket->receive_closed) && (SHUT_RD == operation))
3216 || ((GNUNET_YES == socket->transmit_closed) && (SHUT_WR == operation))
3217 || ((GNUNET_YES == socket->transmit_closed)
3218 && (GNUNET_YES == socket->receive_closed)
3219 && (SHUT_RDWR == operation)) )
3221 handle->operation = operation;
3222 handle->call_cont_task_id = GNUNET_SCHEDULER_add_now (&call_cont_task,
3226 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
3227 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
3231 handle->operation = SHUT_RD;
3232 if (NULL != socket->read_handle)
3233 LOG (GNUNET_ERROR_TYPE_WARNING,
3234 "Existing read handle should be cancelled before shutting"
3236 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
3237 queue_message (socket, msg, &set_state_receive_close_wait, NULL,
3239 socket->receive_closed = GNUNET_YES;
3242 handle->operation = SHUT_WR;
3243 if (NULL != socket->write_handle)
3244 LOG (GNUNET_ERROR_TYPE_WARNING,
3245 "Existing write handle should be cancelled before shutting"
3247 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
3248 queue_message (socket, msg, &set_state_transmit_close_wait, NULL,
3250 socket->transmit_closed = GNUNET_YES;
3253 handle->operation = SHUT_RDWR;
3254 if (NULL != socket->write_handle)
3255 LOG (GNUNET_ERROR_TYPE_WARNING,
3256 "Existing write handle should be cancelled before shutting"
3258 if (NULL != socket->read_handle)
3259 LOG (GNUNET_ERROR_TYPE_WARNING,
3260 "Existing read handle should be cancelled before shutting"
3262 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
3263 queue_message (socket, msg, &set_state_close_wait, NULL, GNUNET_NO);
3264 socket->transmit_closed = GNUNET_YES;
3265 socket->receive_closed = GNUNET_YES;
3268 LOG (GNUNET_ERROR_TYPE_WARNING,
3269 "GNUNET_STREAM_shutdown called with invalid value for "
3270 "parameter operation -- Ignoring\n");
3272 GNUNET_free (handle);
3275 handle->close_msg_retransmission_task_id =
3276 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
3277 &close_msg_retransmission_task,
3284 * Cancels a pending shutdown. Note that the shutdown messages may already be
3285 * sent and the stream is shutdown already for the operation given to
3286 * GNUNET_STREAM_shutdown(). This function only clears up any retranmissions of
3287 * shutdown messages and frees the shutdown handle.
3289 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
3292 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle)
3294 if (GNUNET_SCHEDULER_NO_TASK != handle->close_msg_retransmission_task_id)
3295 GNUNET_SCHEDULER_cancel (handle->close_msg_retransmission_task_id);
3296 if (GNUNET_SCHEDULER_NO_TASK != handle->call_cont_task_id)
3297 GNUNET_SCHEDULER_cancel (handle->call_cont_task_id);
3298 handle->socket->shutdown_handle = NULL;
3299 GNUNET_free (handle);
3306 * @param socket the stream socket
3309 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
3311 struct MessageQueue *head;
3313 if (NULL != socket->read_handle)
3315 LOG (GNUNET_ERROR_TYPE_WARNING,
3316 "Closing STREAM socket when a read handle is pending\n");
3317 GNUNET_STREAM_read_cancel (socket->read_handle);
3319 if (NULL != socket->write_handle)
3321 LOG (GNUNET_ERROR_TYPE_WARNING,
3322 "Closing STREAM socket when a write handle is pending\n");
3323 GNUNET_STREAM_write_cancel (socket->write_handle);
3324 //socket->write_handle = NULL;
3326 /* Terminate the ack'ing task if they are still present */
3327 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
3329 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
3330 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
3332 /* Terminate the control retransmission tasks */
3333 if (GNUNET_SCHEDULER_NO_TASK != socket->control_retransmission_task_id)
3335 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
3337 /* Clear Transmit handles */
3338 if (NULL != socket->transmit_handle)
3340 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
3341 socket->transmit_handle = NULL;
3343 /* Clear existing message queue */
3344 while (NULL != (head = socket->queue_head)) {
3345 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
3348 GNUNET_free (head->message);
3351 /* Close associated tunnel */
3352 if (NULL != socket->tunnel)
3354 GNUNET_MESH_tunnel_destroy (socket->tunnel);
3355 socket->tunnel = NULL;
3357 /* Close mesh connection */
3358 if ((NULL != socket->mesh) && (NULL == socket->lsocket))
3360 GNUNET_MESH_disconnect (socket->mesh);
3361 socket->mesh = NULL;
3363 /* Close statistics connection */
3364 if ( (NULL != socket->stat_handle) && (NULL == socket->lsocket) )
3365 GNUNET_STATISTICS_destroy (socket->stat_handle, GNUNET_YES);
3366 /* Release receive buffer */
3367 if (NULL != socket->receive_buffer)
3369 GNUNET_free (socket->receive_buffer);
3371 GNUNET_free (socket);
3376 * Listens for stream connections for a specific application ports
3378 * @param cfg the configuration to use
3379 * @param app_port the application port for which new streams will be accepted
3380 * @param listen_cb this function will be called when a peer tries to establish
3382 * @param listen_cb_cls closure for listen_cb
3383 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
3384 * @return listen socket, NULL for any error
3386 struct GNUNET_STREAM_ListenSocket *
3387 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
3388 GNUNET_MESH_ApplicationType app_port,
3389 GNUNET_STREAM_ListenCallback listen_cb,
3390 void *listen_cb_cls,
3393 struct GNUNET_STREAM_ListenSocket *lsocket;
3394 struct GNUNET_TIME_Relative listen_timeout;
3395 enum GNUNET_STREAM_Option option;
3397 uint16_t payload_size;
3399 GNUNET_assert (NULL != listen_cb);
3400 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
3401 lsocket->cfg = GNUNET_CONFIGURATION_dup (cfg);
3402 lsocket->lockmanager = GNUNET_LOCKMANAGER_connect (lsocket->cfg);
3403 if (NULL == lsocket->lockmanager)
3405 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3406 GNUNET_free (lsocket);
3409 lsocket->listening = GNUNET_NO;/* We listen when we get a lock on app_port */
3411 lsocket->retransmit_timeout = TIME_REL_SECS (default_timeout);
3412 lsocket->testing_active = GNUNET_NO;
3413 lsocket->listen_ok_cb = NULL;
3414 lsocket->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
3415 listen_timeout = TIME_REL_SECS (60); /* A minute for listen timeout */
3416 va_start (vargs, listen_cb_cls);
3418 option = va_arg (vargs, enum GNUNET_STREAM_Option);
3421 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
3422 lsocket->retransmit_timeout = va_arg (vargs,
3423 struct GNUNET_TIME_Relative);
3425 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
3426 lsocket->testing_active = GNUNET_YES;
3427 lsocket->testing_set_write_sequence_number_value = va_arg (vargs,
3430 case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
3431 listen_timeout = GNUNET_TIME_relative_multiply
3432 (GNUNET_TIME_UNIT_MILLISECONDS, va_arg (vargs, uint32_t));
3434 case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
3435 lsocket->listen_ok_cb = va_arg (vargs,
3436 GNUNET_STREAM_ListenSuccessCallback);
3438 case GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE:
3439 payload_size = (uint16_t) va_arg (vargs, unsigned int);
3440 GNUNET_assert (0 != payload_size);
3441 if (payload_size < lsocket->max_payload_size)
3442 lsocket->max_payload_size = payload_size;
3444 case GNUNET_STREAM_OPTION_END:
3447 } while (GNUNET_STREAM_OPTION_END != option);
3449 lsocket->port = app_port;
3450 lsocket->listen_cb = listen_cb;
3451 lsocket->listen_cb_cls = listen_cb_cls;
3452 lsocket->locking_request =
3453 GNUNET_LOCKMANAGER_acquire_lock (lsocket->lockmanager, locking_domain,
3454 (uint32_t) lsocket->port,
3455 &lock_status_change_cb, lsocket);
3456 lsocket->lockmanager_acquire_timeout_task =
3457 GNUNET_SCHEDULER_add_delayed (listen_timeout,
3458 &lockmanager_acquire_timeout, lsocket);
3459 lsocket->stat_handle = GNUNET_STATISTICS_create ("stream",
3466 * Closes the listen socket
3468 * @param lsocket the listen socket
3471 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
3473 /* Close MESH connection */
3474 if (NULL != lsocket->mesh)
3475 GNUNET_MESH_disconnect (lsocket->mesh);
3476 if (NULL != lsocket->stat_handle)
3477 GNUNET_STATISTICS_destroy (lsocket->stat_handle, GNUNET_YES);
3478 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3479 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
3480 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
3481 if (NULL != lsocket->locking_request)
3482 GNUNET_LOCKMANAGER_cancel_request (lsocket->locking_request);
3483 if (NULL != lsocket->lockmanager)
3484 GNUNET_LOCKMANAGER_disconnect (lsocket->lockmanager);
3485 GNUNET_free (lsocket);
3490 * Tries to write the given data to the stream. The maximum size of data that
3491 * can be written per a write operation is ~ 4MB (64 * (64000 - sizeof (struct
3492 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
3493 * violation, however only the said number of maximum bytes will be written.
3495 * @param socket the socket representing a stream
3496 * @param data the data buffer from where the data is written into the stream
3497 * @param size the number of bytes to be written from the data buffer
3498 * @param timeout the timeout period
3499 * @param write_cont the function to call upon writing some bytes into the
3501 * @param write_cont_cls the closure
3503 * @return handle to cancel the operation; if a previous write is pending NULL
3504 * is returned. If the stream has been shutdown for this operation or
3505 * is broken then write_cont is immediately called and NULL is
3508 struct GNUNET_STREAM_WriteHandle *
3509 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
3512 struct GNUNET_TIME_Relative timeout,
3513 GNUNET_STREAM_CompletionContinuation write_cont,
3514 void *write_cont_cls)
3516 struct GNUNET_STREAM_WriteHandle *io_handle;
3517 struct GNUNET_STREAM_DataMessage *data_msg;
3519 struct GNUNET_TIME_Relative ack_deadline;
3520 unsigned int num_needed_packets;
3521 unsigned int packet;
3522 uint32_t packet_size;
3523 uint32_t payload_size;
3524 uint16_t max_data_packet_size;
3526 LOG (GNUNET_ERROR_TYPE_DEBUG,
3528 if (NULL != socket->write_handle)
3533 if (NULL == socket->tunnel)
3535 if (NULL != write_cont)
3536 write_cont (write_cont_cls, GNUNET_STREAM_SYSERR, 0);
3539 switch (socket->state)
3541 case STATE_TRANSMIT_CLOSED:
3542 case STATE_TRANSMIT_CLOSE_WAIT:
3544 case STATE_CLOSE_WAIT:
3545 if (NULL != write_cont)
3546 write_cont (write_cont_cls, GNUNET_STREAM_SHUTDOWN, 0);
3547 LOG (GNUNET_ERROR_TYPE_DEBUG,
3548 "%s() END\n", __func__);
3552 case STATE_HELLO_WAIT:
3553 if (NULL != write_cont)
3554 write_cont (write_cont_cls, GNUNET_STREAM_SYSERR, 0);
3555 LOG (GNUNET_ERROR_TYPE_DEBUG,
3556 "%s() END\n", __func__);
3558 case STATE_ESTABLISHED:
3559 case STATE_RECEIVE_CLOSED:
3560 case STATE_RECEIVE_CLOSE_WAIT:
3563 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * socket->max_payload_size < size)
3564 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * socket->max_payload_size;
3565 num_needed_packets =
3566 (size + (socket->max_payload_size - 1)) / socket->max_payload_size;
3567 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_WriteHandle));
3568 io_handle->socket = socket;
3569 io_handle->write_cont = write_cont;
3570 io_handle->write_cont_cls = write_cont_cls;
3571 io_handle->size = size;
3572 io_handle->packets_sent = 0;
3574 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3575 determined from RTT */
3576 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
3577 /* Divide the given buffer into packets for sending */
3578 max_data_packet_size =
3579 socket->max_payload_size + sizeof (struct GNUNET_STREAM_DataMessage);
3580 io_handle->max_ack_base_num = socket->write_sequence_number;
3581 for (packet=0; packet < num_needed_packets; packet++)
3583 if ((packet + 1) * socket->max_payload_size < size)
3585 payload_size = socket->max_payload_size;
3586 packet_size = max_data_packet_size;
3590 payload_size = size - packet * socket->max_payload_size;
3592 payload_size + sizeof (struct GNUNET_STREAM_DataMessage);
3594 io_handle->messages[packet] = GNUNET_malloc (packet_size);
3595 io_handle->messages[packet]->header.header.size = htons (packet_size);
3596 io_handle->messages[packet]->header.header.type =
3597 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
3598 io_handle->messages[packet]->sequence_number =
3599 htonl (socket->write_sequence_number++);
3600 io_handle->messages[packet]->offset = htonl (socket->write_offset);
3601 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3602 determined from RTT */
3603 io_handle->messages[packet]->ack_deadline =
3604 GNUNET_TIME_relative_hton (ack_deadline);
3605 data_msg = io_handle->messages[packet];
3606 /* Copy data from given buffer to the packet */
3607 memcpy (&data_msg[1], sweep, payload_size);
3608 sweep += payload_size;
3609 socket->write_offset += payload_size;
3611 /* ack the last data message. FIXME: remove when we figure out how to do this
3613 io_handle->messages[num_needed_packets - 1]->ack_deadline =
3614 GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO);
3615 socket->data_retransmit_timeout = GNUNET_TIME_UNIT_SECONDS;
3616 socket->write_handle = io_handle;
3617 write_data (socket);
3618 LOG (GNUNET_ERROR_TYPE_DEBUG,
3619 "%s() END\n", __func__);
3625 * Function to check the ACK bitmap for any received messages and call the data processor
3627 * @param cls the socket
3628 * @param tc the scheduler task context
3631 probe_data_availability (void *cls,
3632 const struct GNUNET_SCHEDULER_TaskContext *tc)
3634 struct GNUNET_STREAM_Socket *socket = cls;
3636 GNUNET_assert (NULL != socket->read_handle);
3637 socket->read_handle->probe_data_availability_task_id =
3638 GNUNET_SCHEDULER_NO_TASK;
3639 if (GNUNET_SCHEDULER_NO_TASK != socket->read_handle->read_task_id)
3640 return; /* A task to call read processor is present */
3641 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
3643 socket->read_handle->read_task_id
3644 = GNUNET_SCHEDULER_add_now (&call_read_processor, socket);
3650 * Tries to read data from the stream. Should not be called when another read
3651 * handle is present; the existing read handle should be canceled with
3652 * GNUNET_STREAM_read_cancel(). Only one read handle per socket is present at
3655 * @param socket the socket representing a stream
3656 * @param timeout the timeout period
3657 * @param proc function to call with data (once only)
3658 * @param proc_cls the closure for proc
3659 * @return handle to cancel the operation; NULL is returned if the stream has
3660 * been shutdown for this type of opeartion (the DataProcessor is
3661 * immediately called with GNUNET_STREAM_SHUTDOWN as status)
3663 struct GNUNET_STREAM_ReadHandle *
3664 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
3665 struct GNUNET_TIME_Relative timeout,
3666 GNUNET_STREAM_DataProcessor proc,
3669 struct GNUNET_STREAM_ReadHandle *read_handle;
3671 LOG (GNUNET_ERROR_TYPE_DEBUG,
3673 GNUNET_i2s (&socket->other_peer),
3675 /* Only one read handle is permitted at any time; cancel the existing or wait
3676 for it to complete */
3677 GNUNET_assert (NULL == socket->read_handle);
3678 GNUNET_assert (NULL != proc);
3679 if (GNUNET_YES == socket->receive_closed)
3681 switch (socket->state)
3683 case STATE_RECEIVE_CLOSED:
3684 case STATE_RECEIVE_CLOSE_WAIT:
3686 case STATE_CLOSE_WAIT:
3687 LOG (GNUNET_ERROR_TYPE_DEBUG,
3689 GNUNET_i2s (&socket->other_peer),
3691 proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
3696 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ReadHandle));
3697 read_handle->proc = proc;
3698 read_handle->proc_cls = proc_cls;
3699 read_handle->socket = socket;
3700 socket->read_handle = read_handle;
3701 read_handle->probe_data_availability_task_id =
3702 GNUNET_SCHEDULER_add_now (&probe_data_availability, socket);
3703 read_handle->read_io_timeout_task_id =
3704 GNUNET_SCHEDULER_add_delayed (timeout, &read_io_timeout, socket);
3705 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: %s() END\n",
3706 GNUNET_i2s (&socket->other_peer), __func__);
3712 * Cancels pending write operation. Also cancels packet retransmissions which
3713 * may have resulted otherwise.
3715 * CAUTION: Normally a write operation is considered successful if the data
3716 * given to it is sent and acknowledged by the receiver. As data is divided
3717 * into packets, it is possible that not all packets are received by the
3718 * receiver. Any missing packets are then retransmitted till the receiver
3719 * acknowledges all packets or until a timeout . During this scenario if the
3720 * write operation is cancelled all such retransmissions are also
3721 * cancelled. This may leave the receiver's receive buffer incompletely filled
3722 * as some missing packets are never retransmitted. So this operation should be
3723 * used before shutting down transmission from our side or before closing the
3726 * @param wh write operation handle to cancel
3729 GNUNET_STREAM_write_cancel (struct GNUNET_STREAM_WriteHandle *wh)
3731 struct GNUNET_STREAM_Socket *socket = wh->socket;
3732 unsigned int packet;
3734 GNUNET_assert (NULL != socket->write_handle);
3735 GNUNET_assert (socket->write_handle == wh);
3736 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
3738 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
3739 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
3741 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
3743 if (NULL == wh->messages[packet]) break;
3744 GNUNET_free (wh->messages[packet]);
3746 GNUNET_free (socket->write_handle);
3747 socket->write_handle = NULL;
3752 * Cancel pending read operation.
3754 * @param rh read operation handle to cancel
3757 GNUNET_STREAM_read_cancel (struct GNUNET_STREAM_ReadHandle *rh)
3759 struct GNUNET_STREAM_Socket *socket;
3761 socket = rh->socket;
3762 GNUNET_assert (NULL != socket->read_handle);
3763 GNUNET_assert (rh == socket->read_handle);
3764 cleanup_read_handle (socket);
3767 /* end of stream_api.c */