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_crypto_lib.h"
40 #include "gnunet_lockmanager_service.h"
41 #include "gnunet_statistics_service.h"
42 #include "gnunet_stream_lib.h"
43 #include "stream_protocol.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_IOWriteHandle *write_handle;
223 * The read IO_handle associated with this socket
225 struct GNUNET_STREAM_IOReadHandle *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 * The state of the protocol associated with this socket
289 * The status of the socket
291 enum GNUNET_STREAM_Status status;
294 * The number of previous timeouts; FIXME: currently not used
296 unsigned int retries;
299 * 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_IOWriteHandle
460 * The socket to which this write handle is associated
462 struct GNUNET_STREAM_Socket *socket;
465 * The packet_buffers associated with this Handle
467 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
470 * The write continuation callback
472 GNUNET_STREAM_CompletionContinuation write_cont;
475 * Write continuation closure
477 void *write_cont_cls;
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;
503 struct GNUNET_STREAM_IOReadHandle
506 * The socket to which this read handle is associated
508 struct GNUNET_STREAM_Socket *socket;
511 * Callback for the read processor
513 GNUNET_STREAM_DataProcessor proc;
516 * The closure pointer for the read processor callback
521 * Task identifier for the read io timeout task
523 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
526 * Task scheduled to continue a read operation.
528 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
533 * Handle for Shutdown
535 struct GNUNET_STREAM_ShutdownHandle
538 * The socket associated with this shutdown handle
540 struct GNUNET_STREAM_Socket *socket;
543 * Shutdown completion callback
545 GNUNET_STREAM_ShutdownCompletion completion_cb;
548 * Closure for completion callback
550 void *completion_cls;
553 * Close message retransmission task id
555 GNUNET_SCHEDULER_TaskIdentifier close_msg_retransmission_task_id;
558 * Task scheduled to call the shutdown continuation callback
560 GNUNET_SCHEDULER_TaskIdentifier call_cont_task_id;
563 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
570 * Default value in seconds for various timeouts
572 static const unsigned int default_timeout = 10;
575 * The domain name for locks we use here
577 static const char *locking_domain = "GNUNET_STREAM_APPLOCK";
581 * Callback function for sending queued message
583 * @param cls closure the socket
584 * @param size number of bytes available in buf
585 * @param buf where the callee should write the message
586 * @return number of bytes written to buf
589 send_message_notify (void *cls, size_t size, void *buf)
591 struct GNUNET_STREAM_Socket *socket = cls;
592 struct MessageQueue *head;
595 socket->transmit_handle = NULL; /* Remove the transmit handle */
596 head = socket->queue_head;
598 return 0; /* just to be safe */
599 if (0 == size) /* request timed out */
602 LOG (GNUNET_ERROR_TYPE_DEBUG,
603 "%s: Message sending timed out. Retry %d \n",
604 GNUNET_i2s (&socket->other_peer),
606 socket->transmit_handle =
607 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
608 GNUNET_NO, /* Corking */
609 /* FIXME: exponential backoff */
610 socket->retransmit_timeout,
612 ntohs (head->message->header.size),
613 &send_message_notify,
617 ret = ntohs (head->message->header.size);
618 GNUNET_assert (size >= ret);
619 memcpy (buf, head->message, ret);
620 if (NULL != head->finish_cb)
622 head->finish_cb (head->finish_cb_cls, socket);
624 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
627 GNUNET_free (head->message);
629 head = socket->queue_head;
630 if (NULL != head) /* more pending messages to send */
633 socket->transmit_handle =
634 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
635 GNUNET_NO, /* Corking */
636 /* FIXME: exponential backoff */
637 socket->retransmit_timeout,
639 ntohs (head->message->header.size),
640 &send_message_notify,
648 * Queues a message for sending using the mesh connection of a socket
650 * @param socket the socket whose mesh connection is used
651 * @param message the message to be sent
652 * @param finish_cb the callback to be called when the message is sent
653 * @param finish_cb_cls the closure for the callback
654 * @param urgent set to GNUNET_YES to add the message to the beginning of the
655 * queue; GNUNET_NO to add at the tail
658 queue_message (struct GNUNET_STREAM_Socket *socket,
659 struct GNUNET_STREAM_MessageHeader *message,
660 SendFinishCallback finish_cb,
664 struct MessageQueue *queue_entity;
667 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
668 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
669 LOG (GNUNET_ERROR_TYPE_DEBUG,
670 "%s: Queueing message of type %d and size %d\n",
671 GNUNET_i2s (&socket->other_peer),
672 ntohs (message->header.type),
673 ntohs (message->header.size));
674 GNUNET_assert (NULL != message);
675 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
676 queue_entity->message = message;
677 queue_entity->finish_cb = finish_cb;
678 queue_entity->finish_cb_cls = finish_cb_cls;
679 if (GNUNET_YES == urgent)
681 GNUNET_CONTAINER_DLL_insert (socket->queue_head, socket->queue_tail,
683 if (NULL != socket->transmit_handle)
685 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
686 socket->transmit_handle = NULL;
690 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
693 if (NULL == socket->transmit_handle)
696 socket->transmit_handle =
697 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
698 GNUNET_NO, /* Corking */
699 socket->retransmit_timeout,
701 ntohs (message->header.size),
702 &send_message_notify,
709 * Copies a message and queues it for sending using the mesh connection of
712 * @param socket the socket whose mesh connection is used
713 * @param message the message to be sent
714 * @param finish_cb the callback to be called when the message is sent
715 * @param finish_cb_cls the closure for the callback
718 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
719 const struct GNUNET_STREAM_MessageHeader *message,
720 SendFinishCallback finish_cb,
723 struct GNUNET_STREAM_MessageHeader *msg_copy;
726 size = ntohs (message->header.size);
727 msg_copy = GNUNET_malloc (size);
728 memcpy (msg_copy, message, size);
729 queue_message (socket, msg_copy, finish_cb, finish_cb_cls, GNUNET_NO);
734 * Writes data using the given socket. The amount of data written is limited by
735 * the receiver_window_size
737 * @param socket the socket to use
740 write_data (struct GNUNET_STREAM_Socket *socket);
744 * Task for retransmitting data messages if they aren't ACK before their ack
747 * @param cls the socket
748 * @param tc the Task context
751 data_retransmission_task (void *cls,
752 const struct GNUNET_SCHEDULER_TaskContext *tc)
754 struct GNUNET_STREAM_Socket *socket = cls;
756 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
757 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
759 LOG (GNUNET_ERROR_TYPE_DEBUG,
760 "%s: Retransmitting DATA...\n", GNUNET_i2s (&socket->other_peer));
766 * Task for sending ACK message
768 * @param cls the socket
769 * @param tc the Task context
773 const struct GNUNET_SCHEDULER_TaskContext *tc)
775 struct GNUNET_STREAM_Socket *socket = cls;
776 struct GNUNET_STREAM_AckMessage *ack_msg;
778 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
779 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
781 /* Create the ACK Message */
782 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
783 ack_msg->header.header.size = htons (sizeof (struct
784 GNUNET_STREAM_AckMessage));
785 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
786 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
787 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
788 ack_msg->receive_window_remaining =
789 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
790 /* Queue up ACK for immediate sending */
791 queue_message (socket, &ack_msg->header, NULL, NULL, GNUNET_YES);
796 * Retransmission task for shutdown messages
798 * @param cls the shutdown handle
799 * @param tc the Task Context
802 close_msg_retransmission_task (void *cls,
803 const struct GNUNET_SCHEDULER_TaskContext *tc)
805 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle = cls;
806 struct GNUNET_STREAM_MessageHeader *msg;
807 struct GNUNET_STREAM_Socket *socket;
809 shutdown_handle->close_msg_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
810 GNUNET_assert (NULL != shutdown_handle);
811 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
813 socket = shutdown_handle->socket;
814 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
815 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
816 switch (shutdown_handle->operation)
819 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
822 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
825 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
829 shutdown_handle->close_msg_retransmission_task_id =
830 GNUNET_SCHEDULER_NO_TASK;
833 queue_message (socket, msg, NULL, NULL, GNUNET_NO);
834 shutdown_handle->close_msg_retransmission_task_id =
835 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
836 &close_msg_retransmission_task,
842 * Function to modify a bit in GNUNET_STREAM_AckBitmap
844 * @param bitmap the bitmap to modify
845 * @param bit the bit number to modify
846 * @param value GNUNET_YES to on, GNUNET_NO to off
849 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
853 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
854 if (GNUNET_YES == value)
855 *bitmap |= (1LL << bit);
857 *bitmap &= ~(1LL << bit);
862 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
864 * @param bitmap address of the bitmap that has to be checked
865 * @param bit the bit number to check
866 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
869 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
872 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
873 return 0 != (*bitmap & (1LL << bit));
878 * Writes data using the given socket. The amount of data written is limited by
879 * the receiver_window_size
881 * @param socket the socket to use
884 write_data (struct GNUNET_STREAM_Socket *socket)
886 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
889 for (packet=0; packet < io_handle->packets_sent; packet++)
891 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
894 LOG (GNUNET_ERROR_TYPE_DEBUG,
895 "%s: Retransmitting DATA message with sequence %u\n",
896 GNUNET_i2s (&socket->other_peer),
897 ntohl (io_handle->messages[packet]->sequence_number));
898 copy_and_queue_message (socket,
899 &io_handle->messages[packet]->header,
904 /* Now send new packets if there is enough buffer space */
905 while ((packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH) &&
906 (NULL != io_handle->messages[packet]) &&
907 (socket->receiver_window_available
908 >= ntohs (io_handle->messages[packet]->header.header.size)))
910 socket->receiver_window_available -=
911 ntohs (io_handle->messages[packet]->header.header.size);
912 LOG (GNUNET_ERROR_TYPE_DEBUG,
913 "%s: Placing DATA message with sequence %u in send queue\n",
914 GNUNET_i2s (&socket->other_peer),
915 ntohl (io_handle->messages[packet]->sequence_number));
916 copy_and_queue_message (socket,
917 &io_handle->messages[packet]->header,
922 io_handle->packets_sent = packet;
923 // FIXME: 8s is not good, should use GNUNET_TIME_STD_BACKOFF...
924 if (GNUNET_SCHEDULER_NO_TASK == socket->data_retransmission_task_id)
925 socket->data_retransmission_task_id =
926 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
927 (GNUNET_TIME_UNIT_SECONDS, 8),
928 &data_retransmission_task,
934 * Task for calling the read processor
936 * @param cls the socket
937 * @param tc the task context
940 call_read_processor (void *cls,
941 const struct GNUNET_SCHEDULER_TaskContext *tc)
943 struct GNUNET_STREAM_Socket *socket = cls;
944 struct GNUNET_STREAM_IOReadHandle *read_handle;
946 size_t valid_read_size;
948 uint32_t sequence_increase;
949 uint32_t offset_increase;
951 read_handle = socket->read_handle;
952 GNUNET_assert (NULL != read_handle);
953 read_handle->read_task_id = GNUNET_SCHEDULER_NO_TASK;
954 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
956 if (NULL == socket->receive_buffer)
958 GNUNET_assert (NULL != socket->read_handle);
959 GNUNET_assert (NULL != socket->read_handle->proc);
960 /* Check the bitmap for any holes */
961 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
963 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
967 /* We only call read processor if we have the first packet */
968 GNUNET_assert (0 < packet);
970 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
971 GNUNET_assert (0 != valid_read_size);
972 /* Cancel the read_io_timeout_task */
973 GNUNET_SCHEDULER_cancel (read_handle->read_io_timeout_task_id);
974 read_handle->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
975 /* Call the data processor */
976 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Calling read processor\n",
977 GNUNET_i2s (&socket->other_peer));
979 socket->read_handle->proc (socket->read_handle->proc_cls,
981 socket->receive_buffer + socket->copy_offset,
983 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Read processor read %d bytes\n",
984 GNUNET_i2s (&socket->other_peer), read_size);
985 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Read processor completed successfully\n",
986 GNUNET_i2s (&socket->other_peer));
987 /* Free the read handle */
988 GNUNET_free (socket->read_handle);
989 socket->read_handle = NULL;
990 GNUNET_assert (read_size <= valid_read_size);
991 socket->copy_offset += read_size;
992 /* Determine upto which packet we can remove from the buffer */
993 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
995 if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
1000 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
1003 /* If no packets can be removed we can't move the buffer */
1006 sequence_increase = packet;
1007 LOG (GNUNET_ERROR_TYPE_DEBUG,
1008 "%s: Sequence increase after read processor completion: %u\n",
1009 GNUNET_i2s (&socket->other_peer), sequence_increase);
1010 /* Shift the data in the receive buffer */
1011 socket->receive_buffer =
1012 memmove (socket->receive_buffer,
1013 socket->receive_buffer
1014 + socket->receive_buffer_boundaries[sequence_increase-1],
1015 socket->receive_buffer_size
1016 - socket->receive_buffer_boundaries[sequence_increase-1]);
1017 /* Shift the bitmap */
1018 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
1019 /* Set read_sequence_number */
1020 socket->read_sequence_number += sequence_increase;
1021 /* Set read_offset */
1022 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
1023 socket->read_offset += offset_increase;
1024 /* Fix copy_offset */
1025 GNUNET_assert (offset_increase <= socket->copy_offset);
1026 socket->copy_offset -= offset_increase;
1027 /* Fix relative boundaries */
1028 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1030 if (packet < (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase))
1032 uint32_t ahead_buffer_boundary;
1034 ahead_buffer_boundary =
1035 socket->receive_buffer_boundaries[packet + sequence_increase];
1036 if (0 == ahead_buffer_boundary)
1037 socket->receive_buffer_boundaries[packet] = 0;
1040 GNUNET_assert (offset_increase < ahead_buffer_boundary);
1041 socket->receive_buffer_boundaries[packet] =
1042 ahead_buffer_boundary - offset_increase;
1046 socket->receive_buffer_boundaries[packet] = 0;
1052 * Cancels the existing read io handle
1054 * @param cls the closure from the SCHEDULER call
1055 * @param tc the task context
1058 read_io_timeout (void *cls,
1059 const struct GNUNET_SCHEDULER_TaskContext *tc)
1061 struct GNUNET_STREAM_Socket *socket = cls;
1062 struct GNUNET_STREAM_IOReadHandle *read_handle;
1063 GNUNET_STREAM_DataProcessor proc;
1066 read_handle = socket->read_handle;
1067 GNUNET_assert (NULL != read_handle);
1068 read_handle->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
1069 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1071 if (read_handle->read_task_id != GNUNET_SCHEDULER_NO_TASK)
1073 LOG (GNUNET_ERROR_TYPE_DEBUG,
1074 "%s: Read task timedout - Cancelling it\n",
1075 GNUNET_i2s (&socket->other_peer));
1076 GNUNET_SCHEDULER_cancel (read_handle->read_task_id);
1077 read_handle->read_task_id = GNUNET_SCHEDULER_NO_TASK;
1079 proc = read_handle->proc;
1080 proc_cls = read_handle->proc_cls;
1081 GNUNET_free (read_handle);
1082 socket->read_handle = NULL;
1083 /* Call the read processor to signal timeout */
1085 GNUNET_STREAM_TIMEOUT,
1092 * Handler for DATA messages; Same for both client and server
1094 * @param socket the socket through which the ack was received
1095 * @param tunnel connection to the other end
1096 * @param sender who sent the message
1097 * @param msg the data message
1098 * @param atsi performance data for the connection
1099 * @return GNUNET_OK to keep the connection open,
1100 * GNUNET_SYSERR to close it (signal serious error)
1103 handle_data (struct GNUNET_STREAM_Socket *socket,
1104 struct GNUNET_MESH_Tunnel *tunnel,
1105 const struct GNUNET_PeerIdentity *sender,
1106 const struct GNUNET_STREAM_DataMessage *msg,
1107 const struct GNUNET_ATS_Information*atsi)
1109 const void *payload;
1110 struct GNUNET_TIME_Relative ack_deadline_rel;
1111 uint32_t bytes_needed;
1112 uint32_t relative_offset;
1113 uint32_t relative_sequence_number;
1116 size = htons (msg->header.header.size);
1117 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
1119 GNUNET_break_op (0);
1120 return GNUNET_SYSERR;
1122 if (0 != memcmp (sender, &socket->other_peer,
1123 sizeof (struct GNUNET_PeerIdentity)))
1125 LOG (GNUNET_ERROR_TYPE_DEBUG,
1126 "%s: Received DATA from non-confirming peer\n",
1127 GNUNET_i2s (&socket->other_peer));
1130 switch (socket->state)
1132 case STATE_ESTABLISHED:
1133 case STATE_TRANSMIT_CLOSED:
1134 case STATE_TRANSMIT_CLOSE_WAIT:
1135 /* check if the message's sequence number is in the range we are
1137 relative_sequence_number =
1138 ntohl (msg->sequence_number) - socket->read_sequence_number;
1139 if ( relative_sequence_number >= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 "%s: Ignoring received message with sequence number %u\n",
1143 GNUNET_i2s (&socket->other_peer),
1144 ntohl (msg->sequence_number));
1145 /* Start ACK sending task if one is not already present */
1146 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1148 socket->ack_task_id =
1149 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1150 (msg->ack_deadline),
1156 /* Check if we have already seen this message */
1157 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1158 relative_sequence_number))
1160 LOG (GNUNET_ERROR_TYPE_DEBUG,
1161 "%s: Ignoring already received message with sequence number %u\n",
1162 GNUNET_i2s (&socket->other_peer),
1163 ntohl (msg->sequence_number));
1164 /* Start ACK sending task if one is not already present */
1165 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1167 socket->ack_task_id =
1168 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1169 (msg->ack_deadline), &ack_task, socket);
1173 LOG (GNUNET_ERROR_TYPE_DEBUG,
1174 "%s: Receiving DATA with sequence number: %u and size: %d from %s\n",
1175 GNUNET_i2s (&socket->other_peer), ntohl (msg->sequence_number),
1176 ntohs (msg->header.header.size), GNUNET_i2s (&socket->other_peer));
1177 /* Check if we have to allocate the buffer */
1178 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1179 relative_offset = ntohl (msg->offset) - socket->read_offset;
1180 bytes_needed = relative_offset + size;
1181 if (bytes_needed > socket->receive_buffer_size)
1183 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1185 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1187 socket->receive_buffer_size = bytes_needed;
1191 LOG (GNUNET_ERROR_TYPE_DEBUG,
1192 "%s: Cannot accommodate packet %d as buffer is full\n",
1193 GNUNET_i2s (&socket->other_peer), ntohl (msg->sequence_number));
1197 /* Copy Data to buffer */
1199 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1200 memcpy (socket->receive_buffer + relative_offset, payload, size);
1201 socket->receive_buffer_boundaries[relative_sequence_number] =
1202 relative_offset + size;
1203 /* Modify the ACK bitmap */
1204 ackbitmap_modify_bit (&socket->ack_bitmap, relative_sequence_number,
1206 /* Start ACK sending task if one is not already present */
1207 ack_deadline_rel = GNUNET_TIME_relative_ntoh (msg->ack_deadline);
1208 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1211 GNUNET_TIME_relative_min (ack_deadline_rel,
1212 GNUNET_TIME_relative_multiply
1213 (GNUNET_TIME_UNIT_SECONDS, 300));
1214 socket->ack_task_id =
1215 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1216 (msg->ack_deadline), &ack_task, socket);
1217 socket->ack_time_registered = GNUNET_TIME_absolute_get ();
1218 socket->ack_time_deadline = ack_deadline_rel;
1222 struct GNUNET_TIME_Relative ack_time_past;
1223 struct GNUNET_TIME_Relative ack_time_remaining;
1224 struct GNUNET_TIME_Relative ack_time_min;
1226 GNUNET_TIME_absolute_get_duration (socket->ack_time_registered);
1227 ack_time_remaining = GNUNET_TIME_relative_subtract
1228 (socket->ack_time_deadline, ack_time_past);
1229 ack_time_min = GNUNET_TIME_relative_min (ack_time_remaining,
1231 if (0 == memcmp(&ack_deadline_rel, &ack_time_min,
1232 sizeof (struct GNUNET_TIME_Relative)))
1234 ack_deadline_rel = ack_time_min;
1235 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
1236 socket->ack_task_id = GNUNET_SCHEDULER_add_delayed (ack_deadline_rel,
1238 socket->ack_time_registered = GNUNET_TIME_absolute_get ();
1239 socket->ack_time_deadline = ack_deadline_rel;
1242 if ((NULL != socket->read_handle) /* A read handle is waiting */
1243 /* There is no current read task */
1244 && (GNUNET_SCHEDULER_NO_TASK == socket->read_handle->read_task_id)
1245 /* We have the first packet */
1246 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap, 0)))
1248 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Scheduling read processor\n",
1249 GNUNET_i2s (&socket->other_peer));
1250 socket->read_handle->read_task_id =
1251 GNUNET_SCHEDULER_add_now (&call_read_processor, socket);
1255 LOG (GNUNET_ERROR_TYPE_DEBUG,
1256 "%s: Received data message when it cannot be handled\n",
1257 GNUNET_i2s (&socket->other_peer));
1265 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1267 * @param cls the socket (set from GNUNET_MESH_connect)
1268 * @param tunnel connection to the other end
1269 * @param tunnel_ctx place to store local state associated with the tunnel
1270 * @param sender who sent the message
1271 * @param message the actual message
1272 * @param atsi performance data for the connection
1273 * @return GNUNET_OK to keep the connection open,
1274 * GNUNET_SYSERR to close it (signal serious error)
1277 client_handle_data (void *cls,
1278 struct GNUNET_MESH_Tunnel *tunnel,
1280 const struct GNUNET_PeerIdentity *sender,
1281 const struct GNUNET_MessageHeader *message,
1282 const struct GNUNET_ATS_Information*atsi)
1284 struct GNUNET_STREAM_Socket *socket = cls;
1286 return handle_data (socket, tunnel, sender,
1287 (const struct GNUNET_STREAM_DataMessage *) message, atsi);
1292 * Callback to set state to ESTABLISHED
1294 * @param cls the closure NULL;
1295 * @param socket the socket to requiring state change
1298 set_state_established (void *cls,
1299 struct GNUNET_STREAM_Socket *socket)
1301 LOG (GNUNET_ERROR_TYPE_DEBUG,
1302 "%s: Attaining ESTABLISHED state\n",
1303 GNUNET_i2s (&socket->other_peer));
1304 socket->write_offset = 0;
1305 socket->read_offset = 0;
1306 socket->state = STATE_ESTABLISHED;
1307 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
1308 socket->control_retransmission_task_id);
1309 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
1310 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
1311 if (NULL != socket->lsocket)
1313 LOG (GNUNET_ERROR_TYPE_DEBUG,
1314 "%s: Calling listen callback\n",
1315 GNUNET_i2s (&socket->other_peer));
1316 if (GNUNET_SYSERR ==
1317 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1319 &socket->other_peer))
1321 socket->state = STATE_CLOSED;
1322 /* FIXME: We should close in a decent way (send RST) */
1323 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1324 GNUNET_free (socket);
1328 socket->open_cb (socket->open_cls, socket);
1333 * Callback to set state to HELLO_WAIT
1335 * @param cls the closure from queue_message
1336 * @param socket the socket to requiring state change
1339 set_state_hello_wait (void *cls,
1340 struct GNUNET_STREAM_Socket *socket)
1342 GNUNET_assert (STATE_INIT == socket->state);
1343 LOG (GNUNET_ERROR_TYPE_DEBUG,
1344 "%s: Attaining HELLO_WAIT state\n",
1345 GNUNET_i2s (&socket->other_peer));
1346 socket->state = STATE_HELLO_WAIT;
1351 * Callback to set state to CLOSE_WAIT
1353 * @param cls the closure from queue_message
1354 * @param socket the socket requiring state change
1357 set_state_close_wait (void *cls,
1358 struct GNUNET_STREAM_Socket *socket)
1360 LOG (GNUNET_ERROR_TYPE_DEBUG,
1361 "%s: Attaing CLOSE_WAIT state\n",
1362 GNUNET_i2s (&socket->other_peer));
1363 socket->state = STATE_CLOSE_WAIT;
1364 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1365 socket->receive_buffer = NULL;
1366 socket->receive_buffer_size = 0;
1371 * Callback to set state to RECEIVE_CLOSE_WAIT
1373 * @param cls the closure from queue_message
1374 * @param socket the socket requiring state change
1377 set_state_receive_close_wait (void *cls,
1378 struct GNUNET_STREAM_Socket *socket)
1380 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "%s: Attaing RECEIVE_CLOSE_WAIT state\n",
1382 GNUNET_i2s (&socket->other_peer));
1383 socket->state = STATE_RECEIVE_CLOSE_WAIT;
1384 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1385 socket->receive_buffer = NULL;
1386 socket->receive_buffer_size = 0;
1391 * Callback to set state to TRANSMIT_CLOSE_WAIT
1393 * @param cls the closure from queue_message
1394 * @param socket the socket requiring state change
1397 set_state_transmit_close_wait (void *cls,
1398 struct GNUNET_STREAM_Socket *socket)
1400 LOG (GNUNET_ERROR_TYPE_DEBUG,
1401 "%s: Attaing TRANSMIT_CLOSE_WAIT state\n",
1402 GNUNET_i2s (&socket->other_peer));
1403 socket->state = STATE_TRANSMIT_CLOSE_WAIT;
1408 * Callback to set state to CLOSED
1410 * @param cls the closure from queue_message
1411 * @param socket the socket requiring state change
1414 set_state_closed (void *cls,
1415 struct GNUNET_STREAM_Socket *socket)
1417 socket->state = STATE_CLOSED;
1422 * Returns GNUNET_MESSAGE_TYPE_STREAM_HELLO
1424 * @return the generate hello message
1426 static struct GNUNET_STREAM_MessageHeader *
1427 generate_hello (void)
1429 struct GNUNET_STREAM_MessageHeader *msg;
1431 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1432 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
1433 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1439 * Returns a new HelloAckMessage. Also sets the write sequence number for the
1442 * @param socket the socket for which this HelloAckMessage has to be generated
1443 * @param generate_seq GNUNET_YES to generate the write sequence number,
1444 * GNUNET_NO to use the existing sequence number
1445 * @return the HelloAckMessage
1447 static struct GNUNET_STREAM_HelloAckMessage *
1448 generate_hello_ack (struct GNUNET_STREAM_Socket *socket,
1451 struct GNUNET_STREAM_HelloAckMessage *msg;
1453 if (GNUNET_YES == generate_seq)
1455 if (GNUNET_YES == socket->testing_active)
1456 socket->write_sequence_number =
1457 socket->testing_set_write_sequence_number_value;
1459 socket->write_sequence_number =
1460 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1461 LOG_DEBUG ("%s: write sequence number %u\n",
1462 GNUNET_i2s (&socket->other_peer),
1463 (unsigned int) socket->write_sequence_number);
1465 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1466 msg->header.header.size =
1467 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1468 msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1469 msg->sequence_number = htonl (socket->write_sequence_number);
1470 msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1476 * Task for retransmitting control messages if they aren't ACK'ed before a
1479 * @param cls the socket
1480 * @param tc the Task context
1483 control_retransmission_task (void *cls,
1484 const struct GNUNET_SCHEDULER_TaskContext *tc)
1486 struct GNUNET_STREAM_Socket *socket = cls;
1488 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
1489 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
1491 LOG_DEBUG ("%s: Retransmitting a control message\n",
1492 GNUNET_i2s (&socket->other_peer));
1493 switch (socket->state)
1501 case STATE_HELLO_WAIT:
1502 if (NULL == socket->lsocket) /* We are client */
1503 queue_message (socket, generate_hello (), NULL, NULL, GNUNET_NO);
1505 queue_message (socket,
1506 (struct GNUNET_STREAM_MessageHeader *)
1507 generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
1509 socket->control_retransmission_task_id =
1510 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
1511 &control_retransmission_task, socket);
1513 case STATE_ESTABLISHED:
1514 if (NULL == socket->lsocket)
1515 queue_message (socket,
1516 (struct GNUNET_STREAM_MessageHeader *)
1517 generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
1529 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1531 * @param cls the socket (set from GNUNET_MESH_connect)
1532 * @param tunnel connection to the other end
1533 * @param tunnel_ctx this is NULL
1534 * @param sender who sent the message
1535 * @param message the actual message
1536 * @param atsi performance data for the connection
1537 * @return GNUNET_OK to keep the connection open,
1538 * GNUNET_SYSERR to close it (signal serious error)
1541 client_handle_hello_ack (void *cls,
1542 struct GNUNET_MESH_Tunnel *tunnel,
1544 const struct GNUNET_PeerIdentity *sender,
1545 const struct GNUNET_MessageHeader *message,
1546 const struct GNUNET_ATS_Information*atsi)
1548 struct GNUNET_STREAM_Socket *socket = cls;
1549 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1550 struct GNUNET_STREAM_HelloAckMessage *reply;
1552 if (0 != memcmp (sender, &socket->other_peer,
1553 sizeof (struct GNUNET_PeerIdentity)))
1555 LOG (GNUNET_ERROR_TYPE_DEBUG,
1556 "%s: Received HELLO_ACK from non-confirming peer\n",
1557 GNUNET_i2s (&socket->other_peer));
1560 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1561 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received HELLO_ACK from %s\n",
1562 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1563 GNUNET_assert (socket->tunnel == tunnel);
1564 switch (socket->state)
1566 case STATE_HELLO_WAIT:
1567 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1568 LOG (GNUNET_ERROR_TYPE_DEBUG,
1569 "%s: Read sequence number %u\n",
1570 GNUNET_i2s (&socket->other_peer),
1571 (unsigned int) socket->read_sequence_number);
1572 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1573 reply = generate_hello_ack (socket, GNUNET_YES);
1574 queue_message (socket, &reply->header, &set_state_established,
1577 case STATE_ESTABLISHED:
1578 // call statistics (# ACKs ignored++)
1579 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
1580 socket->control_retransmission_task_id);
1581 socket->control_retransmission_task_id =
1582 GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
1585 LOG_DEBUG ("%s: Server %s sent HELLO_ACK when in state %d\n",
1586 GNUNET_i2s (&socket->other_peer),
1587 GNUNET_i2s (&socket->other_peer), socket->state);
1588 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1589 return GNUNET_SYSERR;
1595 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1597 * @param cls the socket (set from GNUNET_MESH_connect)
1598 * @param tunnel connection to the other end
1599 * @param tunnel_ctx this is NULL
1600 * @param sender who sent the message
1601 * @param message the actual message
1602 * @param atsi performance data for the connection
1603 * @return GNUNET_OK to keep the connection open,
1604 * GNUNET_SYSERR to close it (signal serious error)
1607 client_handle_reset (void *cls,
1608 struct GNUNET_MESH_Tunnel *tunnel,
1610 const struct GNUNET_PeerIdentity *sender,
1611 const struct GNUNET_MessageHeader *message,
1612 const struct GNUNET_ATS_Information*atsi)
1614 // struct GNUNET_STREAM_Socket *socket = cls;
1621 * Common message handler for handling TRANSMIT_CLOSE messages
1623 * @param socket the socket through which the ack was received
1624 * @param tunnel connection to the other end
1625 * @param sender who sent the message
1626 * @param msg the transmit close message
1627 * @param atsi performance data for the connection
1628 * @return GNUNET_OK to keep the connection open,
1629 * GNUNET_SYSERR to close it (signal serious error)
1632 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1633 struct GNUNET_MESH_Tunnel *tunnel,
1634 const struct GNUNET_PeerIdentity *sender,
1635 const struct GNUNET_STREAM_MessageHeader *msg,
1636 const struct GNUNET_ATS_Information*atsi)
1638 struct GNUNET_STREAM_MessageHeader *reply;
1640 switch (socket->state)
1644 case STATE_HELLO_WAIT:
1645 LOG (GNUNET_ERROR_TYPE_DEBUG,
1646 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1647 GNUNET_i2s (&socket->other_peer));
1652 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received TRANSMIT_CLOSE from %s\n",
1653 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1654 socket->receive_closed = GNUNET_YES;
1655 if (GNUNET_YES == socket->transmit_closed)
1656 socket->state = STATE_CLOSED;
1658 socket->state = STATE_RECEIVE_CLOSED;
1659 /* Send TRANSMIT_CLOSE_ACK */
1660 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1661 reply->header.type =
1662 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1663 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1664 queue_message (socket, reply, NULL, NULL, GNUNET_NO);
1670 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1672 * @param cls the socket (set from GNUNET_MESH_connect)
1673 * @param tunnel connection to the other end
1674 * @param tunnel_ctx this is NULL
1675 * @param sender who sent the message
1676 * @param message the actual message
1677 * @param atsi performance data for the connection
1678 * @return GNUNET_OK to keep the connection open,
1679 * GNUNET_SYSERR to close it (signal serious error)
1682 client_handle_transmit_close (void *cls,
1683 struct GNUNET_MESH_Tunnel *tunnel,
1685 const struct GNUNET_PeerIdentity *sender,
1686 const struct GNUNET_MessageHeader *message,
1687 const struct GNUNET_ATS_Information*atsi)
1689 struct GNUNET_STREAM_Socket *socket = cls;
1691 return handle_transmit_close (socket,
1694 (struct GNUNET_STREAM_MessageHeader *)message,
1700 * Task for calling the shutdown continuation callback
1702 * @param cls the socket
1703 * @param tc the scheduler task context
1706 call_cont_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1708 struct GNUNET_STREAM_Socket *socket = cls;
1710 GNUNET_assert (NULL != socket->shutdown_handle);
1711 socket->shutdown_handle->call_cont_task_id = GNUNET_SCHEDULER_NO_TASK;
1712 if (NULL != socket->shutdown_handle->completion_cb)
1713 socket->shutdown_handle->completion_cb
1714 (socket->shutdown_handle->completion_cls,
1715 socket->shutdown_handle->operation);
1716 GNUNET_free (socket->shutdown_handle);
1717 socket->shutdown_handle = NULL;
1722 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_*_CLOSE_ACK messages
1724 * @param socket the socket
1725 * @param tunnel connection to the other end
1726 * @param sender who sent the message
1727 * @param message the actual message
1728 * @param atsi performance data for the connection
1729 * @param operation the close operation which is being ACK'ed
1730 * @return GNUNET_OK to keep the connection open,
1731 * GNUNET_SYSERR to close it (signal serious error)
1734 handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
1735 struct GNUNET_MESH_Tunnel *tunnel,
1736 const struct GNUNET_PeerIdentity *sender,
1737 const struct GNUNET_STREAM_MessageHeader *message,
1738 const struct GNUNET_ATS_Information *atsi,
1741 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
1743 shutdown_handle = socket->shutdown_handle;
1744 if (NULL == shutdown_handle)
1746 /* This may happen when the shudown handle is cancelled */
1747 LOG (GNUNET_ERROR_TYPE_DEBUG,
1748 "%s: Received CLOSE_ACK when shutdown handle is NULL\n",
1749 GNUNET_i2s (&socket->other_peer));
1755 switch (socket->state)
1757 case STATE_CLOSE_WAIT:
1758 if (SHUT_RDWR != shutdown_handle->operation)
1760 LOG (GNUNET_ERROR_TYPE_DEBUG,
1761 "%s: Received CLOSE_ACK when shutdown handle is not for "
1762 "SHUT_RDWR\n", GNUNET_i2s (&socket->other_peer));
1765 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received CLOSE_ACK from %s\n",
1766 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1767 socket->state = STATE_CLOSED;
1770 LOG (GNUNET_ERROR_TYPE_DEBUG,
1771 "%s: Received CLOSE_ACK when it is not expected\n",
1772 GNUNET_i2s (&socket->other_peer));
1777 switch (socket->state)
1779 case STATE_RECEIVE_CLOSE_WAIT:
1780 if (SHUT_RD != shutdown_handle->operation)
1782 LOG (GNUNET_ERROR_TYPE_DEBUG,
1783 "%s: Received RECEIVE_CLOSE_ACK when shutdown handle "
1784 "is not for SHUT_RD\n", GNUNET_i2s (&socket->other_peer));
1787 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received RECEIVE_CLOSE_ACK from %s\n",
1788 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1789 socket->state = STATE_RECEIVE_CLOSED;
1792 LOG (GNUNET_ERROR_TYPE_DEBUG,
1793 "%s: Received RECEIVE_CLOSE_ACK when it is not expected\n",
1794 GNUNET_i2s (&socket->other_peer));
1799 switch (socket->state)
1801 case STATE_TRANSMIT_CLOSE_WAIT:
1802 if (SHUT_WR != shutdown_handle->operation)
1804 LOG (GNUNET_ERROR_TYPE_DEBUG,
1805 "%s: Received TRANSMIT_CLOSE_ACK when shutdown handle "
1806 "is not for SHUT_WR\n",
1807 GNUNET_i2s (&socket->other_peer));
1810 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received TRANSMIT_CLOSE_ACK from %s\n",
1811 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1812 socket->state = STATE_TRANSMIT_CLOSED;
1815 LOG (GNUNET_ERROR_TYPE_DEBUG,
1816 "%s: Received TRANSMIT_CLOSE_ACK when it is not expected\n",
1817 GNUNET_i2s (&socket->other_peer));
1824 shutdown_handle->call_cont_task_id = GNUNET_SCHEDULER_add_now
1825 (&call_cont_task, socket);
1826 if (GNUNET_SCHEDULER_NO_TASK
1827 != shutdown_handle->close_msg_retransmission_task_id)
1829 GNUNET_SCHEDULER_cancel
1830 (shutdown_handle->close_msg_retransmission_task_id);
1831 shutdown_handle->close_msg_retransmission_task_id =
1832 GNUNET_SCHEDULER_NO_TASK;
1839 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1841 * @param cls the socket (set from GNUNET_MESH_connect)
1842 * @param tunnel connection to the other end
1843 * @param tunnel_ctx this is NULL
1844 * @param sender who sent the message
1845 * @param message the actual message
1846 * @param atsi performance data for the connection
1847 * @return GNUNET_OK to keep the connection open,
1848 * GNUNET_SYSERR to close it (signal serious error)
1851 client_handle_transmit_close_ack (void *cls,
1852 struct GNUNET_MESH_Tunnel *tunnel,
1854 const struct GNUNET_PeerIdentity *sender,
1855 const struct GNUNET_MessageHeader *message,
1856 const struct GNUNET_ATS_Information*atsi)
1858 struct GNUNET_STREAM_Socket *socket = cls;
1860 return handle_generic_close_ack (socket,
1863 (const struct GNUNET_STREAM_MessageHeader *)
1871 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1873 * @param socket the socket
1874 * @param tunnel connection to the other end
1875 * @param sender who sent the message
1876 * @param message the actual message
1877 * @param atsi performance data for the connection
1878 * @return GNUNET_OK to keep the connection open,
1879 * GNUNET_SYSERR to close it (signal serious error)
1882 handle_receive_close (struct GNUNET_STREAM_Socket *socket,
1883 struct GNUNET_MESH_Tunnel *tunnel,
1884 const struct GNUNET_PeerIdentity *sender,
1885 const struct GNUNET_STREAM_MessageHeader *message,
1886 const struct GNUNET_ATS_Information *atsi)
1888 struct GNUNET_STREAM_MessageHeader *receive_close_ack;
1890 switch (socket->state)
1894 case STATE_HELLO_WAIT:
1895 LOG (GNUNET_ERROR_TYPE_DEBUG,
1896 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1897 GNUNET_i2s (&socket->other_peer));
1902 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received RECEIVE_CLOSE from %s\n",
1903 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1904 socket->transmit_closed = GNUNET_YES;
1905 if (GNUNET_YES == socket->receive_closed)
1906 socket->state = STATE_CLOSED;
1908 socket->state = STATE_TRANSMIT_CLOSED;
1910 GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1911 receive_close_ack->header.size =
1912 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1913 receive_close_ack->header.type =
1914 htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK);
1915 queue_message (socket, receive_close_ack, NULL, NULL, GNUNET_NO);
1916 /* If write handle is present call it with GNUNET_STREAM_SHUTDOWN to signal
1917 that that stream has been shutdown */
1918 if (NULL != socket->write_handle)
1920 GNUNET_STREAM_CompletionContinuation wc;
1923 wc = socket->write_handle->write_cont;
1924 wc_cls = socket->write_handle->write_cont_cls;
1925 GNUNET_STREAM_io_write_cancel (socket->write_handle);
1926 socket->write_handle = NULL;
1929 GNUNET_STREAM_SHUTDOWN, 0);
1936 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1938 * @param cls the socket (set from GNUNET_MESH_connect)
1939 * @param tunnel connection to the other end
1940 * @param tunnel_ctx this is NULL
1941 * @param sender who sent the message
1942 * @param message the actual message
1943 * @param atsi performance data for the connection
1944 * @return GNUNET_OK to keep the connection open,
1945 * GNUNET_SYSERR to close it (signal serious error)
1948 client_handle_receive_close (void *cls,
1949 struct GNUNET_MESH_Tunnel *tunnel,
1951 const struct GNUNET_PeerIdentity *sender,
1952 const struct GNUNET_MessageHeader *message,
1953 const struct GNUNET_ATS_Information*atsi)
1955 struct GNUNET_STREAM_Socket *socket = cls;
1958 handle_receive_close (socket,
1961 (const struct GNUNET_STREAM_MessageHeader *) message,
1967 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1969 * @param cls the socket (set from GNUNET_MESH_connect)
1970 * @param tunnel connection to the other end
1971 * @param tunnel_ctx this is NULL
1972 * @param sender who sent the message
1973 * @param message the actual message
1974 * @param atsi performance data for the connection
1975 * @return GNUNET_OK to keep the connection open,
1976 * GNUNET_SYSERR to close it (signal serious error)
1979 client_handle_receive_close_ack (void *cls,
1980 struct GNUNET_MESH_Tunnel *tunnel,
1982 const struct GNUNET_PeerIdentity *sender,
1983 const struct GNUNET_MessageHeader *message,
1984 const struct GNUNET_ATS_Information*atsi)
1986 struct GNUNET_STREAM_Socket *socket = cls;
1988 return handle_generic_close_ack (socket,
1991 (const struct GNUNET_STREAM_MessageHeader *)
1999 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2001 * @param socket the socket
2002 * @param tunnel connection to the other end
2003 * @param sender who sent the message
2004 * @param message the actual message
2005 * @param atsi performance data for the connection
2006 * @return GNUNET_OK to keep the connection open,
2007 * GNUNET_SYSERR to close it (signal serious error)
2010 handle_close (struct GNUNET_STREAM_Socket *socket,
2011 struct GNUNET_MESH_Tunnel *tunnel,
2012 const struct GNUNET_PeerIdentity *sender,
2013 const struct GNUNET_STREAM_MessageHeader *message,
2014 const struct GNUNET_ATS_Information*atsi)
2016 struct GNUNET_STREAM_MessageHeader *close_ack;
2018 switch (socket->state)
2022 case STATE_HELLO_WAIT:
2023 LOG (GNUNET_ERROR_TYPE_DEBUG,
2024 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
2025 GNUNET_i2s (&socket->other_peer));
2030 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received CLOSE from %s\n",
2031 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
2032 close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2033 close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2034 close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK);
2035 queue_message (socket, close_ack, &set_state_closed, NULL, GNUNET_NO);
2036 if (STATE_CLOSED == socket->state)
2038 socket->receive_closed = GNUNET_YES;
2039 socket->transmit_closed = GNUNET_YES;
2040 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
2041 socket->receive_buffer = NULL;
2042 socket->receive_buffer_size = 0;
2043 /* If write handle is present call it with GNUNET_STREAM_SHUTDOWN to signal
2044 that that stream has been shutdown */
2045 if (NULL != socket->write_handle)
2047 GNUNET_STREAM_CompletionContinuation wc;
2050 wc = socket->write_handle->write_cont;
2051 wc_cls = socket->write_handle->write_cont_cls;
2052 GNUNET_STREAM_io_write_cancel (socket->write_handle);
2053 socket->write_handle = NULL;
2056 GNUNET_STREAM_SHUTDOWN, 0);
2063 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2065 * @param cls the socket (set from GNUNET_MESH_connect)
2066 * @param tunnel connection to the other end
2067 * @param tunnel_ctx this is NULL
2068 * @param sender who sent the message
2069 * @param message the actual message
2070 * @param atsi performance data for the connection
2071 * @return GNUNET_OK to keep the connection open,
2072 * GNUNET_SYSERR to close it (signal serious error)
2075 client_handle_close (void *cls,
2076 struct GNUNET_MESH_Tunnel *tunnel,
2078 const struct GNUNET_PeerIdentity *sender,
2079 const struct GNUNET_MessageHeader *message,
2080 const struct GNUNET_ATS_Information*atsi)
2082 struct GNUNET_STREAM_Socket *socket = cls;
2084 return handle_close (socket,
2087 (const struct GNUNET_STREAM_MessageHeader *) message,
2093 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2095 * @param cls the socket (set from GNUNET_MESH_connect)
2096 * @param tunnel connection to the other end
2097 * @param tunnel_ctx this is NULL
2098 * @param sender who sent the message
2099 * @param message the actual message
2100 * @param atsi performance data for the connection
2101 * @return GNUNET_OK to keep the connection open,
2102 * GNUNET_SYSERR to close it (signal serious error)
2105 client_handle_close_ack (void *cls,
2106 struct GNUNET_MESH_Tunnel *tunnel,
2108 const struct GNUNET_PeerIdentity *sender,
2109 const struct GNUNET_MessageHeader *message,
2110 const struct GNUNET_ATS_Information *atsi)
2112 struct GNUNET_STREAM_Socket *socket = cls;
2114 return handle_generic_close_ack (socket,
2117 (const struct GNUNET_STREAM_MessageHeader *)
2123 /*****************************/
2124 /* Server's Message Handlers */
2125 /*****************************/
2128 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
2130 * @param cls the closure
2131 * @param tunnel connection to the other end
2132 * @param tunnel_ctx the socket
2133 * @param sender who sent the message
2134 * @param message the actual message
2135 * @param atsi performance data for the connection
2136 * @return GNUNET_OK to keep the connection open,
2137 * GNUNET_SYSERR to close it (signal serious error)
2140 server_handle_data (void *cls,
2141 struct GNUNET_MESH_Tunnel *tunnel,
2143 const struct GNUNET_PeerIdentity *sender,
2144 const struct GNUNET_MessageHeader *message,
2145 const struct GNUNET_ATS_Information*atsi)
2147 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2149 return handle_data (socket,
2152 (const struct GNUNET_STREAM_DataMessage *)message,
2158 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
2160 * @param cls the closure
2161 * @param tunnel connection to the other end
2162 * @param tunnel_ctx the socket
2163 * @param sender who sent the message
2164 * @param message the actual message
2165 * @param atsi performance data for the connection
2166 * @return GNUNET_OK to keep the connection open,
2167 * GNUNET_SYSERR to close it (signal serious error)
2170 server_handle_hello (void *cls,
2171 struct GNUNET_MESH_Tunnel *tunnel,
2173 const struct GNUNET_PeerIdentity *sender,
2174 const struct GNUNET_MessageHeader *message,
2175 const struct GNUNET_ATS_Information*atsi)
2177 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2178 struct GNUNET_STREAM_HelloAckMessage *reply;
2180 if (0 != memcmp (sender,
2181 &socket->other_peer,
2182 sizeof (struct GNUNET_PeerIdentity)))
2184 LOG_DEBUG ("%s: Received HELLO from non-confirming peer\n",
2185 GNUNET_i2s (&socket->other_peer));
2188 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO == ntohs (message->type));
2189 GNUNET_assert (socket->tunnel == tunnel);
2190 LOG_DEBUG ("%s: Received HELLO from %s\n", GNUNET_i2s (&socket->other_peer),
2191 GNUNET_i2s (&socket->other_peer));
2192 switch (socket->state)
2195 reply = generate_hello_ack (socket, GNUNET_YES);
2196 queue_message (socket, &reply->header, &set_state_hello_wait, NULL,
2198 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
2199 socket->control_retransmission_task_id);
2200 socket->control_retransmission_task_id =
2201 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2202 &control_retransmission_task, socket);
2204 case STATE_HELLO_WAIT:
2205 /* Perhaps our HELLO_ACK was lost */
2206 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
2207 socket->control_retransmission_task_id);
2208 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
2209 socket->control_retransmission_task_id =
2210 GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
2213 LOG_DEBUG( "%s: Client sent HELLO when in state %d\n",
2214 GNUNET_i2s (&socket->other_peer), socket->state);
2215 /* FIXME: Send RESET? */
2222 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
2224 * @param cls the closure
2225 * @param tunnel connection to the other end
2226 * @param tunnel_ctx the socket
2227 * @param sender who sent the message
2228 * @param message the actual message
2229 * @param atsi performance data for the connection
2230 * @return GNUNET_OK to keep the connection open,
2231 * GNUNET_SYSERR to close it (signal serious error)
2234 server_handle_hello_ack (void *cls,
2235 struct GNUNET_MESH_Tunnel *tunnel,
2237 const struct GNUNET_PeerIdentity *sender,
2238 const struct GNUNET_MessageHeader *message,
2239 const struct GNUNET_ATS_Information*atsi)
2241 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2242 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
2244 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
2245 ntohs (message->type));
2246 GNUNET_assert (socket->tunnel == tunnel);
2247 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
2248 switch (socket->state)
2250 case STATE_HELLO_WAIT:
2251 LOG (GNUNET_ERROR_TYPE_DEBUG,
2252 "%s: Received HELLO_ACK from %s\n",
2253 GNUNET_i2s (&socket->other_peer),
2254 GNUNET_i2s (&socket->other_peer));
2255 socket->read_sequence_number = ntohl (ack_message->sequence_number);
2256 LOG (GNUNET_ERROR_TYPE_DEBUG,
2257 "%s: Read sequence number %u\n",
2258 GNUNET_i2s (&socket->other_peer),
2259 (unsigned int) socket->read_sequence_number);
2260 socket->receiver_window_available =
2261 ntohl (ack_message->receiver_window_size);
2262 set_state_established (NULL, socket);
2265 LOG (GNUNET_ERROR_TYPE_DEBUG,
2266 "Client sent HELLO_ACK when in state %d\n", socket->state);
2273 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
2275 * @param cls the closure
2276 * @param tunnel connection to the other end
2277 * @param tunnel_ctx the socket
2278 * @param sender who sent the message
2279 * @param message the actual message
2280 * @param atsi performance data for the connection
2281 * @return GNUNET_OK to keep the connection open,
2282 * GNUNET_SYSERR to close it (signal serious error)
2285 server_handle_reset (void *cls,
2286 struct GNUNET_MESH_Tunnel *tunnel,
2288 const struct GNUNET_PeerIdentity *sender,
2289 const struct GNUNET_MessageHeader *message,
2290 const struct GNUNET_ATS_Information*atsi)
2292 // struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2299 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
2301 * @param cls the closure
2302 * @param tunnel connection to the other end
2303 * @param tunnel_ctx the socket
2304 * @param sender who sent the message
2305 * @param message the actual message
2306 * @param atsi performance data for the connection
2307 * @return GNUNET_OK to keep the connection open,
2308 * GNUNET_SYSERR to close it (signal serious error)
2311 server_handle_transmit_close (void *cls,
2312 struct GNUNET_MESH_Tunnel *tunnel,
2314 const struct GNUNET_PeerIdentity *sender,
2315 const struct GNUNET_MessageHeader *message,
2316 const struct GNUNET_ATS_Information*atsi)
2318 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2320 return handle_transmit_close (socket,
2323 (struct GNUNET_STREAM_MessageHeader *)message,
2329 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
2331 * @param cls the closure
2332 * @param tunnel connection to the other end
2333 * @param tunnel_ctx the socket
2334 * @param sender who sent the message
2335 * @param message the actual message
2336 * @param atsi performance data for the connection
2337 * @return GNUNET_OK to keep the connection open,
2338 * GNUNET_SYSERR to close it (signal serious error)
2341 server_handle_transmit_close_ack (void *cls,
2342 struct GNUNET_MESH_Tunnel *tunnel,
2344 const struct GNUNET_PeerIdentity *sender,
2345 const struct GNUNET_MessageHeader *message,
2346 const struct GNUNET_ATS_Information*atsi)
2348 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2350 return handle_generic_close_ack (socket,
2353 (const struct GNUNET_STREAM_MessageHeader *)
2361 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
2363 * @param cls the closure
2364 * @param tunnel connection to the other end
2365 * @param tunnel_ctx the socket
2366 * @param sender who sent the message
2367 * @param message the actual message
2368 * @param atsi performance data for the connection
2369 * @return GNUNET_OK to keep the connection open,
2370 * GNUNET_SYSERR to close it (signal serious error)
2373 server_handle_receive_close (void *cls,
2374 struct GNUNET_MESH_Tunnel *tunnel,
2376 const struct GNUNET_PeerIdentity *sender,
2377 const struct GNUNET_MessageHeader *message,
2378 const struct GNUNET_ATS_Information*atsi)
2380 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2383 handle_receive_close (socket,
2386 (const struct GNUNET_STREAM_MessageHeader *) message,
2392 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
2394 * @param cls the closure
2395 * @param tunnel connection to the other end
2396 * @param tunnel_ctx the socket
2397 * @param sender who sent the message
2398 * @param message the actual message
2399 * @param atsi performance data for the connection
2400 * @return GNUNET_OK to keep the connection open,
2401 * GNUNET_SYSERR to close it (signal serious error)
2404 server_handle_receive_close_ack (void *cls,
2405 struct GNUNET_MESH_Tunnel *tunnel,
2407 const struct GNUNET_PeerIdentity *sender,
2408 const struct GNUNET_MessageHeader *message,
2409 const struct GNUNET_ATS_Information*atsi)
2411 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2413 return handle_generic_close_ack (socket,
2416 (const struct GNUNET_STREAM_MessageHeader *)
2424 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2426 * @param cls the listen socket (from GNUNET_MESH_connect in
2427 * GNUNET_STREAM_listen)
2428 * @param tunnel connection to the other end
2429 * @param tunnel_ctx the socket
2430 * @param sender who sent the message
2431 * @param message the actual message
2432 * @param atsi performance data for the connection
2433 * @return GNUNET_OK to keep the connection open,
2434 * GNUNET_SYSERR to close it (signal serious error)
2437 server_handle_close (void *cls,
2438 struct GNUNET_MESH_Tunnel *tunnel,
2440 const struct GNUNET_PeerIdentity *sender,
2441 const struct GNUNET_MessageHeader *message,
2442 const struct GNUNET_ATS_Information*atsi)
2444 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2446 return handle_close (socket,
2449 (const struct GNUNET_STREAM_MessageHeader *) message,
2455 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2457 * @param cls the closure
2458 * @param tunnel connection to the other end
2459 * @param tunnel_ctx the socket
2460 * @param sender who sent the message
2461 * @param message the actual message
2462 * @param atsi performance data for the connection
2463 * @return GNUNET_OK to keep the connection open,
2464 * GNUNET_SYSERR to close it (signal serious error)
2467 server_handle_close_ack (void *cls,
2468 struct GNUNET_MESH_Tunnel *tunnel,
2470 const struct GNUNET_PeerIdentity *sender,
2471 const struct GNUNET_MessageHeader *message,
2472 const struct GNUNET_ATS_Information*atsi)
2474 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2476 return handle_generic_close_ack (socket,
2479 (const struct GNUNET_STREAM_MessageHeader *)
2487 * Handler for DATA_ACK messages
2489 * @param socket the socket through which the ack was received
2490 * @param tunnel connection to the other end
2491 * @param sender who sent the message
2492 * @param ack the acknowledgment message
2493 * @param atsi performance data for the connection
2494 * @return GNUNET_OK to keep the connection open,
2495 * GNUNET_SYSERR to close it (signal serious error)
2498 handle_ack (struct GNUNET_STREAM_Socket *socket,
2499 struct GNUNET_MESH_Tunnel *tunnel,
2500 const struct GNUNET_PeerIdentity *sender,
2501 const struct GNUNET_STREAM_AckMessage *ack,
2502 const struct GNUNET_ATS_Information*atsi)
2504 unsigned int packet;
2505 int need_retransmission;
2506 uint32_t sequence_difference;
2508 if (0 != memcmp (sender,
2509 &socket->other_peer,
2510 sizeof (struct GNUNET_PeerIdentity)))
2512 LOG (GNUNET_ERROR_TYPE_DEBUG,
2513 "%s: Received ACK from non-confirming peer\n",
2514 GNUNET_i2s (&socket->other_peer));
2517 switch (socket->state)
2519 case (STATE_ESTABLISHED):
2520 case (STATE_RECEIVE_CLOSED):
2521 case (STATE_RECEIVE_CLOSE_WAIT):
2522 if (NULL == socket->write_handle)
2524 LOG (GNUNET_ERROR_TYPE_DEBUG,
2525 "%s: Received DATA_ACK when write_handle is NULL\n",
2526 GNUNET_i2s (&socket->other_peer));
2529 sequence_difference =
2530 socket->write_sequence_number - ntohl (ack->base_sequence_number);
2531 if (!(sequence_difference <= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
2533 LOG (GNUNET_ERROR_TYPE_DEBUG,
2534 "%s: Received DATA_ACK with unexpected base sequence number\n",
2535 GNUNET_i2s (&socket->other_peer));
2536 LOG (GNUNET_ERROR_TYPE_DEBUG,
2537 "%s: Current write sequence: %u; Ack's base sequence: %u\n",
2538 GNUNET_i2s (&socket->other_peer),
2539 socket->write_sequence_number,
2540 ntohl (ack->base_sequence_number));
2543 /* FIXME: include the case when write_handle is cancelled - ignore the
2545 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received DATA_ACK from %s\n",
2546 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
2547 /* Cancel the retransmission task */
2548 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
2550 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
2551 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
2553 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2555 if (NULL == socket->write_handle->messages[packet]) break;
2556 /* BS: Base sequence from ack; PS: sequence num of current packet */
2557 sequence_difference = ntohl (ack->base_sequence_number)
2558 - ntohl (socket->write_handle->messages[packet]->sequence_number);
2559 if ((0 == sequence_difference) ||
2560 (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH < sequence_difference))
2561 continue; /* The message in our handle is not yet received */
2562 /* case where BS = PS + GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
2563 /* sequence_difference <= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
2564 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
2565 packet, GNUNET_YES);
2567 /* Update the receive window remaining
2568 FIXME : Should update with the value from a data ack with greater
2570 socket->receiver_window_available =
2571 ntohl (ack->receive_window_remaining);
2572 /* Check if we have received all acknowledgements */
2573 need_retransmission = GNUNET_NO;
2574 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2576 if (NULL == socket->write_handle->messages[packet]) break;
2577 if (GNUNET_YES != ackbitmap_is_bit_set
2578 (&socket->write_handle->ack_bitmap,packet))
2580 need_retransmission = GNUNET_YES;
2584 if (GNUNET_YES == need_retransmission)
2586 write_data (socket);
2588 else /* We have to call the write continuation callback now */
2590 struct GNUNET_STREAM_IOWriteHandle *write_handle;
2592 /* Free the packets */
2593 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2595 GNUNET_free_non_null (socket->write_handle->messages[packet]);
2597 write_handle = socket->write_handle;
2598 socket->write_handle = NULL;
2599 if (NULL != write_handle->write_cont)
2600 write_handle->write_cont (write_handle->write_cont_cls,
2602 write_handle->size);
2603 /* We are done with the write handle - Freeing it */
2604 GNUNET_free (write_handle);
2605 LOG (GNUNET_ERROR_TYPE_DEBUG,
2606 "%s: Write completion callback completed\n",
2607 GNUNET_i2s (&socket->other_peer));
2618 * Handler for DATA_ACK messages
2620 * @param cls the 'struct GNUNET_STREAM_Socket'
2621 * @param tunnel connection to the other end
2622 * @param tunnel_ctx unused
2623 * @param sender who sent the message
2624 * @param message the actual message
2625 * @param atsi performance data for the connection
2626 * @return GNUNET_OK to keep the connection open,
2627 * GNUNET_SYSERR to close it (signal serious error)
2630 client_handle_ack (void *cls,
2631 struct GNUNET_MESH_Tunnel *tunnel,
2633 const struct GNUNET_PeerIdentity *sender,
2634 const struct GNUNET_MessageHeader *message,
2635 const struct GNUNET_ATS_Information*atsi)
2637 struct GNUNET_STREAM_Socket *socket = cls;
2638 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2640 return handle_ack (socket, tunnel, sender, ack, atsi);
2645 * Handler for DATA_ACK messages
2647 * @param cls the server's listen socket
2648 * @param tunnel connection to the other end
2649 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2650 * @param sender who sent the message
2651 * @param message the actual message
2652 * @param atsi performance data for the connection
2653 * @return GNUNET_OK to keep the connection open,
2654 * GNUNET_SYSERR to close it (signal serious error)
2657 server_handle_ack (void *cls,
2658 struct GNUNET_MESH_Tunnel *tunnel,
2660 const struct GNUNET_PeerIdentity *sender,
2661 const struct GNUNET_MessageHeader *message,
2662 const struct GNUNET_ATS_Information*atsi)
2664 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2665 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2667 return handle_ack (socket, tunnel, sender, ack, atsi);
2672 * For client message handlers, the stream socket is in the
2675 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2676 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2677 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2678 sizeof (struct GNUNET_STREAM_AckMessage) },
2679 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2680 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2681 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2682 sizeof (struct GNUNET_STREAM_MessageHeader)},
2683 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2684 sizeof (struct GNUNET_STREAM_MessageHeader)},
2685 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2686 sizeof (struct GNUNET_STREAM_MessageHeader)},
2687 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2688 sizeof (struct GNUNET_STREAM_MessageHeader)},
2689 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2690 sizeof (struct GNUNET_STREAM_MessageHeader)},
2691 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2692 sizeof (struct GNUNET_STREAM_MessageHeader)},
2693 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2694 sizeof (struct GNUNET_STREAM_MessageHeader)},
2700 * For server message handlers, the stream socket is in the
2701 * tunnel context, and the listen socket in the closure argument.
2703 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2704 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2705 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2706 sizeof (struct GNUNET_STREAM_AckMessage) },
2707 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2708 sizeof (struct GNUNET_STREAM_MessageHeader)},
2709 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2710 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2711 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2712 sizeof (struct GNUNET_STREAM_MessageHeader)},
2713 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2714 sizeof (struct GNUNET_STREAM_MessageHeader)},
2715 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2716 sizeof (struct GNUNET_STREAM_MessageHeader)},
2717 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2718 sizeof (struct GNUNET_STREAM_MessageHeader)},
2719 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2720 sizeof (struct GNUNET_STREAM_MessageHeader)},
2721 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2722 sizeof (struct GNUNET_STREAM_MessageHeader)},
2723 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2724 sizeof (struct GNUNET_STREAM_MessageHeader)},
2730 * Function called when our target peer is connected to our tunnel
2732 * @param cls the socket for which this tunnel is created
2733 * @param peer the peer identity of the target
2734 * @param atsi performance data for the connection
2737 mesh_peer_connect_callback (void *cls,
2738 const struct GNUNET_PeerIdentity *peer,
2739 const struct GNUNET_ATS_Information * atsi)
2741 struct GNUNET_STREAM_Socket *socket = cls;
2742 struct GNUNET_STREAM_MessageHeader *message;
2744 if (0 != memcmp (peer,
2745 &socket->other_peer,
2746 sizeof (struct GNUNET_PeerIdentity)))
2748 LOG (GNUNET_ERROR_TYPE_DEBUG,
2749 "%s: A peer which is not our target has connected to our tunnel\n",
2753 LOG (GNUNET_ERROR_TYPE_DEBUG,
2754 "%s: Target peer %s connected\n",
2755 GNUNET_i2s (&socket->other_peer),
2756 GNUNET_i2s (&socket->other_peer));
2757 /* Set state to INIT */
2758 socket->state = STATE_INIT;
2759 /* Send HELLO message */
2760 message = generate_hello ();
2761 queue_message (socket, message, &set_state_hello_wait, NULL, GNUNET_NO);
2762 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
2763 socket->control_retransmission_task_id);
2764 socket->control_retransmission_task_id =
2765 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2766 &control_retransmission_task, socket);
2771 * Function called when our target peer is disconnected from our tunnel
2773 * @param cls the socket associated which this tunnel
2774 * @param peer the peer identity of the target
2777 mesh_peer_disconnect_callback (void *cls,
2778 const struct GNUNET_PeerIdentity *peer)
2780 struct GNUNET_STREAM_Socket *socket=cls;
2782 /* If the state is SHUTDOWN its ok; else set the state of the socket to SYSERR */
2783 LOG (GNUNET_ERROR_TYPE_DEBUG,
2784 "%s: Other peer %s disconnected \n",
2785 GNUNET_i2s (&socket->other_peer),
2786 GNUNET_i2s (&socket->other_peer));
2791 * Method called whenever a peer creates a tunnel to us
2793 * @param cls closure
2794 * @param tunnel new handle to the tunnel
2795 * @param initiator peer that started the tunnel
2796 * @param atsi performance information for the tunnel
2797 * @return initial tunnel context for the tunnel
2798 * (can be NULL -- that's not an error)
2801 new_tunnel_notify (void *cls,
2802 struct GNUNET_MESH_Tunnel *tunnel,
2803 const struct GNUNET_PeerIdentity *initiator,
2804 const struct GNUNET_ATS_Information *atsi)
2806 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2807 struct GNUNET_STREAM_Socket *socket;
2809 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2810 from the same peer again until the socket is closed */
2812 if (GNUNET_NO == lsocket->listening)
2814 GNUNET_MESH_tunnel_destroy (tunnel);
2817 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2818 socket->other_peer = *initiator;
2819 socket->tunnel = tunnel;
2820 socket->state = STATE_INIT;
2821 socket->lsocket = lsocket;
2822 socket->stat_handle = lsocket->stat_handle;
2823 socket->retransmit_timeout = lsocket->retransmit_timeout;
2824 socket->testing_active = lsocket->testing_active;
2825 socket->testing_set_write_sequence_number_value =
2826 lsocket->testing_set_write_sequence_number_value;
2827 socket->max_payload_size = lsocket->max_payload_size;
2828 LOG (GNUNET_ERROR_TYPE_DEBUG,
2829 "%s: Peer %s initiated tunnel to us\n",
2830 GNUNET_i2s (&socket->other_peer),
2831 GNUNET_i2s (&socket->other_peer));
2832 if (NULL != socket->stat_handle)
2834 GNUNET_STATISTICS_update (socket->stat_handle,
2835 "total inbound connections received",
2837 GNUNET_STATISTICS_update (socket->stat_handle,
2838 "inbound connections", 1, GNUNET_NO);
2846 * Function called whenever an inbound tunnel is destroyed. Should clean up
2847 * any associated state. This function is NOT called if the client has
2848 * explicitly asked for the tunnel to be destroyed using
2849 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2852 * @param cls closure (set from GNUNET_MESH_connect)
2853 * @param tunnel connection to the other end (henceforth invalid)
2854 * @param tunnel_ctx place where local state associated
2855 * with the tunnel is stored
2858 tunnel_cleaner (void *cls,
2859 const struct GNUNET_MESH_Tunnel *tunnel,
2862 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2863 struct MessageQueue *head;
2865 GNUNET_assert (tunnel == socket->tunnel);
2867 LOG (GNUNET_ERROR_TYPE_DEBUG,
2868 "%s: Peer %s has terminated connection abruptly\n",
2869 GNUNET_i2s (&socket->other_peer),
2870 GNUNET_i2s (&socket->other_peer));
2871 if (NULL != socket->stat_handle)
2873 GNUNET_STATISTICS_update (socket->stat_handle,
2874 "connections terminated abruptly", 1, GNUNET_NO);
2875 GNUNET_STATISTICS_update (socket->stat_handle,
2876 "inbound connections", -1, GNUNET_NO);
2878 socket->status = GNUNET_STREAM_SHUTDOWN;
2879 /* Clear Transmit handles */
2880 if (NULL != socket->transmit_handle)
2882 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2883 socket->transmit_handle = NULL;
2885 /* Stop Tasks using socket->tunnel */
2886 if (GNUNET_SCHEDULER_NO_TASK != socket->ack_task_id)
2888 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2889 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2891 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
2893 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
2894 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
2896 /* Terminate the control retransmission tasks */
2897 if (GNUNET_SCHEDULER_NO_TASK != socket->control_retransmission_task_id)
2899 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
2900 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
2902 /* Clear Transmit handles */
2903 if (NULL != socket->transmit_handle)
2905 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2906 socket->transmit_handle = NULL;
2908 /* Clear existing message queue */
2909 while (NULL != (head = socket->queue_head)) {
2910 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2913 GNUNET_free (head->message);
2916 socket->tunnel = NULL;
2921 * Callback to signal timeout on lockmanager lock acquire
2923 * @param cls the ListenSocket
2924 * @param tc the scheduler task context
2927 lockmanager_acquire_timeout (void *cls,
2928 const struct GNUNET_SCHEDULER_TaskContext *tc)
2930 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2931 GNUNET_STREAM_ListenCallback listen_cb;
2932 void *listen_cb_cls;
2934 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2935 listen_cb = lsocket->listen_cb;
2936 listen_cb_cls = lsocket->listen_cb_cls;
2937 if (NULL != listen_cb)
2938 listen_cb (listen_cb_cls, NULL, NULL);
2943 * Callback to notify us on the status changes on app_port lock
2945 * @param cls the ListenSocket
2946 * @param domain the domain name of the lock
2947 * @param lock the app_port
2948 * @param status the current status of the lock
2951 lock_status_change_cb (void *cls, const char *domain, uint32_t lock,
2952 enum GNUNET_LOCKMANAGER_Status status)
2954 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2956 GNUNET_assert (lock == (uint32_t) lsocket->port);
2957 if (GNUNET_LOCKMANAGER_SUCCESS == status)
2959 lsocket->listening = GNUNET_YES;
2960 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
2962 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
2963 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2965 if (NULL == lsocket->mesh)
2967 GNUNET_MESH_ApplicationType ports[] = {lsocket->port, 0};
2969 lsocket->mesh = GNUNET_MESH_connect (lsocket->cfg,
2970 lsocket, /* Closure */
2973 server_message_handlers,
2975 GNUNET_assert (NULL != lsocket->mesh);
2976 if (NULL != lsocket->listen_ok_cb)
2978 (void) lsocket->listen_ok_cb ();
2982 if (GNUNET_LOCKMANAGER_RELEASE == status)
2983 lsocket->listening = GNUNET_NO;
2993 * Tries to open a stream to the target peer
2995 * @param cfg configuration to use
2996 * @param target the target peer to which the stream has to be opened
2997 * @param app_port the application port number which uniquely identifies this
2999 * @param open_cb this function will be called after stream has be established;
3001 * @param open_cb_cls the closure for open_cb
3002 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
3003 * @return if successful it returns the stream socket; NULL if stream cannot be
3006 struct GNUNET_STREAM_Socket *
3007 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
3008 const struct GNUNET_PeerIdentity *target,
3009 GNUNET_MESH_ApplicationType app_port,
3010 GNUNET_STREAM_OpenCallback open_cb,
3014 struct GNUNET_STREAM_Socket *socket;
3015 enum GNUNET_STREAM_Option option;
3016 GNUNET_MESH_ApplicationType ports[] = {app_port, 0};
3018 uint16_t payload_size;
3020 LOG (GNUNET_ERROR_TYPE_DEBUG,
3022 GNUNET_assert (NULL != open_cb);
3023 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
3024 socket->other_peer = *target;
3025 socket->open_cb = open_cb;
3026 socket->open_cls = open_cb_cls;
3028 socket->retransmit_timeout = TIME_REL_SECS (default_timeout);
3029 socket->testing_active = GNUNET_NO;
3030 socket->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
3031 va_start (vargs, open_cb_cls); /* Parse variable args */
3033 option = va_arg (vargs, enum GNUNET_STREAM_Option);
3036 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
3037 /* Expect struct GNUNET_TIME_Relative */
3038 socket->retransmit_timeout = va_arg (vargs,
3039 struct GNUNET_TIME_Relative);
3041 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
3042 socket->testing_active = GNUNET_YES;
3043 socket->testing_set_write_sequence_number_value = va_arg (vargs,
3046 case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
3047 GNUNET_break (0); /* Option irrelevant in STREAM_open */
3049 case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
3050 GNUNET_break (0); /* Option irrelevant in STREAM_open */
3052 case GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE:
3053 payload_size = (uint16_t) va_arg (vargs, unsigned int);
3054 GNUNET_assert (0 != payload_size);
3055 if (payload_size < socket->max_payload_size)
3056 socket->max_payload_size = payload_size;
3058 case GNUNET_STREAM_OPTION_END:
3061 } while (GNUNET_STREAM_OPTION_END != option);
3062 va_end (vargs); /* End of variable args parsing */
3063 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
3065 NULL, /* No inbound tunnel handler */
3066 NULL, /* No in-tunnel cleaner */
3067 client_message_handlers,
3068 ports); /* We don't get inbound tunnels */
3069 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
3071 GNUNET_free (socket);
3074 /* Now create the mesh tunnel to target */
3075 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating MESH Tunnel\n");
3076 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
3077 NULL, /* Tunnel context */
3078 &mesh_peer_connect_callback,
3079 &mesh_peer_disconnect_callback,
3081 GNUNET_assert (NULL != socket->tunnel);
3082 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
3083 &socket->other_peer);
3084 socket->stat_handle = GNUNET_STATISTICS_create ("stream", cfg);
3085 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
3091 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
3093 * @param socket the stream socket
3094 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
3095 * @param completion_cb the callback that will be called upon successful
3096 * shutdown of given operation
3097 * @param completion_cls the closure for the completion callback
3098 * @return the shutdown handle
3100 struct GNUNET_STREAM_ShutdownHandle *
3101 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
3103 GNUNET_STREAM_ShutdownCompletion completion_cb,
3104 void *completion_cls)
3106 struct GNUNET_STREAM_ShutdownHandle *handle;
3107 struct GNUNET_STREAM_MessageHeader *msg;
3109 GNUNET_assert (NULL == socket->shutdown_handle);
3110 handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle));
3111 handle->socket = socket;
3112 handle->completion_cb = completion_cb;
3113 handle->completion_cls = completion_cls;
3114 socket->shutdown_handle = handle;
3115 if ( ((GNUNET_YES == socket->receive_closed) && (SHUT_RD == operation))
3116 || ((GNUNET_YES == socket->transmit_closed) && (SHUT_WR == operation))
3117 || ((GNUNET_YES == socket->transmit_closed)
3118 && (GNUNET_YES == socket->receive_closed)
3119 && (SHUT_RDWR == operation)) )
3121 handle->operation = operation;
3122 handle->call_cont_task_id = GNUNET_SCHEDULER_add_now (&call_cont_task,
3126 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
3127 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
3131 handle->operation = SHUT_RD;
3132 if (NULL != socket->read_handle)
3133 LOG (GNUNET_ERROR_TYPE_WARNING,
3134 "Existing read handle should be cancelled before shutting"
3136 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
3137 queue_message (socket, msg, &set_state_receive_close_wait, NULL,
3139 socket->receive_closed = GNUNET_YES;
3142 handle->operation = SHUT_WR;
3143 if (NULL != socket->write_handle)
3144 LOG (GNUNET_ERROR_TYPE_WARNING,
3145 "Existing write handle should be cancelled before shutting"
3147 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
3148 queue_message (socket, msg, &set_state_transmit_close_wait, NULL,
3150 socket->transmit_closed = GNUNET_YES;
3153 handle->operation = SHUT_RDWR;
3154 if (NULL != socket->write_handle)
3155 LOG (GNUNET_ERROR_TYPE_WARNING,
3156 "Existing write handle should be cancelled before shutting"
3158 if (NULL != socket->read_handle)
3159 LOG (GNUNET_ERROR_TYPE_WARNING,
3160 "Existing read handle should be cancelled before shutting"
3162 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
3163 queue_message (socket, msg, &set_state_close_wait, NULL, GNUNET_NO);
3164 socket->transmit_closed = GNUNET_YES;
3165 socket->receive_closed = GNUNET_YES;
3168 LOG (GNUNET_ERROR_TYPE_WARNING,
3169 "GNUNET_STREAM_shutdown called with invalid value for "
3170 "parameter operation -- Ignoring\n");
3172 GNUNET_free (handle);
3175 handle->close_msg_retransmission_task_id =
3176 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
3177 &close_msg_retransmission_task,
3184 * Cancels a pending shutdown. Note that the shutdown messages may already be
3185 * sent and the stream is shutdown already for the operation given to
3186 * GNUNET_STREAM_shutdown(). This function only clears up any retranmissions of
3187 * shutdown messages and frees the shutdown handle.
3189 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
3192 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle)
3194 if (GNUNET_SCHEDULER_NO_TASK != handle->close_msg_retransmission_task_id)
3195 GNUNET_SCHEDULER_cancel (handle->close_msg_retransmission_task_id);
3196 if (GNUNET_SCHEDULER_NO_TASK != handle->call_cont_task_id)
3197 GNUNET_SCHEDULER_cancel (handle->call_cont_task_id);
3198 handle->socket->shutdown_handle = NULL;
3199 GNUNET_free (handle);
3206 * @param socket the stream socket
3209 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
3211 struct MessageQueue *head;
3213 if (NULL != socket->read_handle)
3215 LOG (GNUNET_ERROR_TYPE_WARNING,
3216 "Closing STREAM socket when a read handle is pending\n");
3217 GNUNET_STREAM_io_read_cancel (socket->read_handle);
3219 if (NULL != socket->write_handle)
3221 LOG (GNUNET_ERROR_TYPE_WARNING,
3222 "Closing STREAM socket when a write handle is pending\n");
3223 GNUNET_STREAM_io_write_cancel (socket->write_handle);
3224 //socket->write_handle = NULL;
3226 GNUNET_break (GNUNET_YES == socket->receive_closed);
3227 GNUNET_break (GNUNET_YES == socket->transmit_closed);
3228 /* Terminate the ack'ing task if they are still present */
3229 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
3231 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
3232 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
3234 /* Terminate the control retransmission tasks */
3235 if (GNUNET_SCHEDULER_NO_TASK != socket->control_retransmission_task_id)
3237 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
3239 /* Clear Transmit handles */
3240 if (NULL != socket->transmit_handle)
3242 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
3243 socket->transmit_handle = NULL;
3245 /* Clear existing message queue */
3246 while (NULL != (head = socket->queue_head)) {
3247 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
3250 GNUNET_free (head->message);
3253 /* Close associated tunnel */
3254 if (NULL != socket->tunnel)
3256 GNUNET_MESH_tunnel_destroy (socket->tunnel);
3257 socket->tunnel = NULL;
3259 /* Close mesh connection */
3260 if ((NULL != socket->mesh) && (NULL == socket->lsocket))
3262 GNUNET_MESH_disconnect (socket->mesh);
3263 socket->mesh = NULL;
3265 /* Close statistics connection */
3266 if ( (NULL != socket->stat_handle) && (NULL == socket->lsocket) )
3267 GNUNET_STATISTICS_destroy (socket->stat_handle, GNUNET_YES);
3268 /* Release receive buffer */
3269 if (NULL != socket->receive_buffer)
3271 GNUNET_free (socket->receive_buffer);
3273 GNUNET_free (socket);
3278 * Listens for stream connections for a specific application ports
3280 * @param cfg the configuration to use
3281 * @param app_port the application port for which new streams will be accepted
3282 * @param listen_cb this function will be called when a peer tries to establish
3284 * @param listen_cb_cls closure for listen_cb
3285 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
3286 * @return listen socket, NULL for any error
3288 struct GNUNET_STREAM_ListenSocket *
3289 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
3290 GNUNET_MESH_ApplicationType app_port,
3291 GNUNET_STREAM_ListenCallback listen_cb,
3292 void *listen_cb_cls,
3295 struct GNUNET_STREAM_ListenSocket *lsocket;
3296 struct GNUNET_TIME_Relative listen_timeout;
3297 enum GNUNET_STREAM_Option option;
3299 uint16_t payload_size;
3301 GNUNET_assert (NULL != listen_cb);
3302 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
3303 lsocket->cfg = GNUNET_CONFIGURATION_dup (cfg);
3304 lsocket->lockmanager = GNUNET_LOCKMANAGER_connect (lsocket->cfg);
3305 if (NULL == lsocket->lockmanager)
3307 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3308 GNUNET_free (lsocket);
3311 lsocket->listening = GNUNET_NO;/* We listen when we get a lock on app_port */
3313 lsocket->retransmit_timeout = TIME_REL_SECS (default_timeout);
3314 lsocket->testing_active = GNUNET_NO;
3315 lsocket->listen_ok_cb = NULL;
3316 lsocket->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
3317 listen_timeout = TIME_REL_SECS (60); /* A minute for listen timeout */
3318 va_start (vargs, listen_cb_cls);
3320 option = va_arg (vargs, enum GNUNET_STREAM_Option);
3323 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
3324 lsocket->retransmit_timeout = va_arg (vargs,
3325 struct GNUNET_TIME_Relative);
3327 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
3328 lsocket->testing_active = GNUNET_YES;
3329 lsocket->testing_set_write_sequence_number_value = va_arg (vargs,
3332 case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
3333 listen_timeout = GNUNET_TIME_relative_multiply
3334 (GNUNET_TIME_UNIT_MILLISECONDS, va_arg (vargs, uint32_t));
3336 case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
3337 lsocket->listen_ok_cb = va_arg (vargs,
3338 GNUNET_STREAM_ListenSuccessCallback);
3340 case GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE:
3341 payload_size = (uint16_t) va_arg (vargs, unsigned int);
3342 GNUNET_assert (0 != payload_size);
3343 if (payload_size < lsocket->max_payload_size)
3344 lsocket->max_payload_size = payload_size;
3346 case GNUNET_STREAM_OPTION_END:
3349 } while (GNUNET_STREAM_OPTION_END != option);
3351 lsocket->port = app_port;
3352 lsocket->listen_cb = listen_cb;
3353 lsocket->listen_cb_cls = listen_cb_cls;
3354 lsocket->locking_request =
3355 GNUNET_LOCKMANAGER_acquire_lock (lsocket->lockmanager, locking_domain,
3356 (uint32_t) lsocket->port,
3357 &lock_status_change_cb, lsocket);
3358 lsocket->lockmanager_acquire_timeout_task =
3359 GNUNET_SCHEDULER_add_delayed (listen_timeout,
3360 &lockmanager_acquire_timeout, lsocket);
3361 lsocket->stat_handle = GNUNET_STATISTICS_create ("stream",
3368 * Closes the listen socket
3370 * @param lsocket the listen socket
3373 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
3375 /* Close MESH connection */
3376 if (NULL != lsocket->mesh)
3377 GNUNET_MESH_disconnect (lsocket->mesh);
3378 if (NULL != lsocket->stat_handle)
3379 GNUNET_STATISTICS_destroy (lsocket->stat_handle, GNUNET_YES);
3380 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3381 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
3382 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
3383 if (NULL != lsocket->locking_request)
3384 GNUNET_LOCKMANAGER_cancel_request (lsocket->locking_request);
3385 if (NULL != lsocket->lockmanager)
3386 GNUNET_LOCKMANAGER_disconnect (lsocket->lockmanager);
3387 GNUNET_free (lsocket);
3392 * Tries to write the given data to the stream. The maximum size of data that
3393 * can be written as part of a write operation is (64 * (64000 - sizeof (struct
3394 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
3395 * violation, however only the said number of maximum bytes will be written.
3397 * @param socket the socket representing a stream
3398 * @param data the data buffer from where the data is written into the stream
3399 * @param size the number of bytes to be written from the data buffer
3400 * @param timeout the timeout period
3401 * @param write_cont the function to call upon writing some bytes into the
3403 * @param write_cont_cls the closure
3405 * @return handle to cancel the operation; if a previous write is pending or
3406 * the stream has been shutdown for this operation then write_cont is
3407 * immediately called and NULL is returned.
3409 struct GNUNET_STREAM_IOWriteHandle *
3410 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
3413 struct GNUNET_TIME_Relative timeout,
3414 GNUNET_STREAM_CompletionContinuation write_cont,
3415 void *write_cont_cls)
3417 struct GNUNET_STREAM_IOWriteHandle *io_handle;
3418 struct GNUNET_STREAM_DataMessage *data_msg;
3420 struct GNUNET_TIME_Relative ack_deadline;
3421 unsigned int num_needed_packets;
3422 unsigned int packet;
3423 uint32_t packet_size;
3424 uint32_t payload_size;
3425 uint16_t max_data_packet_size;
3427 LOG (GNUNET_ERROR_TYPE_DEBUG,
3429 if (NULL != socket->write_handle)
3434 switch (socket->state)
3436 case STATE_TRANSMIT_CLOSED:
3437 case STATE_TRANSMIT_CLOSE_WAIT:
3439 case STATE_CLOSE_WAIT:
3440 if (NULL != write_cont)
3441 write_cont (write_cont_cls, GNUNET_STREAM_SHUTDOWN, 0);
3442 LOG (GNUNET_ERROR_TYPE_DEBUG,
3443 "%s() END\n", __func__);
3447 case STATE_HELLO_WAIT:
3448 if (NULL != write_cont)
3449 /* FIXME: GNUNET_STREAM_SYSERR?? */
3450 write_cont (write_cont_cls, GNUNET_STREAM_SYSERR, 0);
3451 LOG (GNUNET_ERROR_TYPE_DEBUG,
3452 "%s() END\n", __func__);
3454 case STATE_ESTABLISHED:
3455 case STATE_RECEIVE_CLOSED:
3456 case STATE_RECEIVE_CLOSE_WAIT:
3459 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * socket->max_payload_size < size)
3460 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * socket->max_payload_size;
3461 num_needed_packets =
3462 (size + (socket->max_payload_size - 1)) / socket->max_payload_size;
3463 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
3464 io_handle->socket = socket;
3465 io_handle->write_cont = write_cont;
3466 io_handle->write_cont_cls = write_cont_cls;
3467 io_handle->size = size;
3468 io_handle->packets_sent = 0;
3470 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3471 determined from RTT */
3472 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
3473 /* Divide the given buffer into packets for sending */
3474 max_data_packet_size =
3475 socket->max_payload_size + sizeof (struct GNUNET_STREAM_DataMessage);
3476 for (packet=0; packet < num_needed_packets; packet++)
3478 if ((packet + 1) * socket->max_payload_size < size)
3480 payload_size = socket->max_payload_size;
3481 packet_size = max_data_packet_size;
3485 payload_size = size - packet * socket->max_payload_size;
3487 payload_size + sizeof (struct GNUNET_STREAM_DataMessage);
3489 io_handle->messages[packet] = GNUNET_malloc (packet_size);
3490 io_handle->messages[packet]->header.header.size = htons (packet_size);
3491 io_handle->messages[packet]->header.header.type =
3492 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
3493 io_handle->messages[packet]->sequence_number =
3494 htonl (socket->write_sequence_number++);
3495 io_handle->messages[packet]->offset = htonl (socket->write_offset);
3496 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3497 determined from RTT */
3498 io_handle->messages[packet]->ack_deadline =
3499 GNUNET_TIME_relative_hton (ack_deadline);
3500 data_msg = io_handle->messages[packet];
3501 /* Copy data from given buffer to the packet */
3502 memcpy (&data_msg[1], sweep, payload_size);
3503 sweep += payload_size;
3504 socket->write_offset += payload_size;
3506 /* ack the last data message. FIXME: remove when we figure out how to do this
3508 io_handle->messages[num_needed_packets - 1]->ack_deadline =
3509 GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO);
3510 socket->write_handle = io_handle;
3511 write_data (socket);
3512 LOG (GNUNET_ERROR_TYPE_DEBUG,
3513 "%s() END\n", __func__);
3519 * Tries to read data from the stream.
3521 * @param socket the socket representing a stream
3522 * @param timeout the timeout period
3523 * @param proc function to call with data (once only)
3524 * @param proc_cls the closure for proc
3526 * @return handle to cancel the operation; NULL is returned if: the stream has
3527 * been shutdown for this type of opeartion (the DataProcessor is
3528 * immediately called with GNUNET_STREAM_SHUTDOWN as status) OR another
3529 * read handle is present (only one read handle per socket is present
3532 struct GNUNET_STREAM_IOReadHandle *
3533 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
3534 struct GNUNET_TIME_Relative timeout,
3535 GNUNET_STREAM_DataProcessor proc,
3538 struct GNUNET_STREAM_IOReadHandle *read_handle;
3540 LOG (GNUNET_ERROR_TYPE_DEBUG,
3542 GNUNET_i2s (&socket->other_peer),
3544 /* Return NULL if there is already a read handle; the user has to cancel that
3545 first before continuing or has to wait until it is completed */
3546 if (NULL != socket->read_handle)
3548 GNUNET_assert (NULL != proc);
3549 switch (socket->state)
3551 case STATE_RECEIVE_CLOSED:
3552 case STATE_RECEIVE_CLOSE_WAIT:
3554 case STATE_CLOSE_WAIT:
3555 LOG (GNUNET_ERROR_TYPE_DEBUG,
3557 GNUNET_i2s (&socket->other_peer),
3559 proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
3564 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
3565 read_handle->proc = proc;
3566 read_handle->proc_cls = proc_cls;
3567 read_handle->socket = socket;
3568 socket->read_handle = read_handle;
3569 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
3571 read_handle->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
3573 read_handle->read_io_timeout_task_id =
3574 GNUNET_SCHEDULER_add_delayed (timeout, &read_io_timeout, socket);
3575 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: %s() END\n",
3576 GNUNET_i2s (&socket->other_peer), __func__);
3582 * Cancel pending write operation.
3584 * @param ioh handle to operation to cancel
3587 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
3589 struct GNUNET_STREAM_Socket *socket = ioh->socket;
3590 unsigned int packet;
3592 GNUNET_assert (NULL != socket->write_handle);
3593 GNUNET_assert (socket->write_handle == ioh);
3595 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
3597 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
3598 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
3601 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
3603 if (NULL == ioh->messages[packet]) break;
3604 GNUNET_free (ioh->messages[packet]);
3607 GNUNET_free (socket->write_handle);
3608 socket->write_handle = NULL;
3613 * Cancel pending read operation.
3615 * @param ioh handle to operation to cancel
3618 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)
3620 struct GNUNET_STREAM_Socket *socket;
3622 socket = ioh->socket;
3623 GNUNET_assert (NULL != socket->read_handle);
3624 GNUNET_assert (ioh == socket->read_handle);
3625 /* Read io time task should be there; if it is already executed then this
3626 read handle is not valid; However upon scheduler shutdown the read io task
3627 may be executed before */
3628 if (GNUNET_SCHEDULER_NO_TASK != ioh->read_io_timeout_task_id)
3629 GNUNET_SCHEDULER_cancel (ioh->read_io_timeout_task_id);
3630 /* reading task may be present; if so we have to stop it */
3631 if (GNUNET_SCHEDULER_NO_TASK != ioh->read_task_id)
3632 GNUNET_SCHEDULER_cancel (ioh->read_task_id);
3634 socket->read_handle = NULL;
3637 /* end of stream_api.c */