2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * Checks for matching the sender and socket->other_peer in server
26 * Add code for write io timeout
28 * Include retransmission for control messages
32 * @file stream/stream_api.c
33 * @brief Implementation of the stream library
34 * @author Sree Harsha Totakura
39 #include "gnunet_common.h"
40 #include "gnunet_crypto_lib.h"
41 #include "gnunet_lockmanager_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
178 * Retransmission timeout
180 struct GNUNET_TIME_Relative retransmit_timeout;
183 * The Acknowledgement Bitmap
185 GNUNET_STREAM_AckBitmap ack_bitmap;
188 * Time when the Acknowledgement was queued
190 struct GNUNET_TIME_Absolute ack_time_registered;
193 * Queued Acknowledgement deadline
195 struct GNUNET_TIME_Relative ack_time_deadline;
200 struct GNUNET_MESH_Handle *mesh;
203 * The mesh tunnel handle
205 struct GNUNET_MESH_Tunnel *tunnel;
208 * Stream open closure
213 * Stream open callback
215 GNUNET_STREAM_OpenCallback open_cb;
218 * The current transmit handle (if a pending transmit request exists)
220 struct GNUNET_MESH_TransmitHandle *transmit_handle;
223 * The current message associated with the transmit handle
225 struct MessageQueue *queue_head;
228 * The queue tail, should always point to the last message in queue
230 struct MessageQueue *queue_tail;
233 * The write IO_handle associated with this socket
235 struct GNUNET_STREAM_IOWriteHandle *write_handle;
238 * The read IO_handle associated with this socket
240 struct GNUNET_STREAM_IOReadHandle *read_handle;
243 * The shutdown handle associated with this socket
245 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
248 * Buffer for storing received messages
250 void *receive_buffer;
253 * The listen socket from which this socket is derived. Should be NULL if it
254 * is not a derived socket
256 struct GNUNET_STREAM_ListenSocket *lsocket;
259 * The peer identity of the peer at the other end of the stream
261 struct GNUNET_PeerIdentity other_peer;
264 * Task identifier for the read io timeout task
266 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
269 * Task identifier for retransmission task after timeout
271 GNUNET_SCHEDULER_TaskIdentifier data_retransmission_task_id;
274 * Task identifier for retransmission of control messages
276 GNUNET_SCHEDULER_TaskIdentifier control_retransmission_task_id;
279 * The task for sending timely Acks
281 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
284 * Task scheduled to continue a read operation.
286 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
289 * The state of the protocol associated with this socket
294 * The status of the socket
296 enum GNUNET_STREAM_Status status;
299 * The number of previous timeouts; FIXME: currently not used
301 unsigned int retries;
304 * The application port number (type: uint32_t)
306 GNUNET_MESH_ApplicationType app_port;
309 * Whether testing mode is active or not
314 * The write sequence number to be set incase of testing
316 uint32_t testing_set_write_sequence_number_value;
319 * The session id associated with this stream connection
320 * FIXME: Not used currently, may be removed
325 * Write sequence number. Set to random when sending HELLO(client) and
328 uint32_t write_sequence_number;
331 * Read sequence number. This number's value is determined during handshake
333 uint32_t read_sequence_number;
336 * The receiver buffer size
338 uint32_t receive_buffer_size;
341 * The receiver buffer boundaries
343 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
346 * receiver's available buffer after the last acknowledged packet
348 uint32_t receiver_window_available;
351 * The offset pointer used during write operation
353 uint32_t write_offset;
356 * The offset after which we are expecting data
358 uint32_t read_offset;
361 * The offset upto which user has read from the received buffer
363 uint32_t copy_offset;
366 * The maximum size of the data message payload this stream handle can send
368 uint16_t max_payload_size;
373 * A socket for listening
375 struct GNUNET_STREAM_ListenSocket
380 struct GNUNET_MESH_Handle *mesh;
385 struct GNUNET_CONFIGURATION_Handle *cfg;
388 * Handle to the lock manager service
390 struct GNUNET_LOCKMANAGER_Handle *lockmanager;
393 * The active LockingRequest from lockmanager
395 struct GNUNET_LOCKMANAGER_LockingRequest *locking_request;
398 * Callback to call after acquring a lock and listening
400 GNUNET_STREAM_ListenSuccessCallback listen_ok_cb;
403 * The callback function which is called after successful opening socket
405 GNUNET_STREAM_ListenCallback listen_cb;
408 * The call back closure
415 GNUNET_MESH_ApplicationType port;
418 * The id of the lockmanager timeout task
420 GNUNET_SCHEDULER_TaskIdentifier lockmanager_acquire_timeout_task;
423 * The retransmit timeout
425 struct GNUNET_TIME_Relative retransmit_timeout;
433 * Whether testing mode is active or not
438 * The write sequence number to be set incase of testing
440 uint32_t testing_set_write_sequence_number_value;
443 * The maximum size of the data message payload this stream handle can send
445 uint16_t max_payload_size;
451 * The IO Write Handle
453 struct GNUNET_STREAM_IOWriteHandle
456 * The socket to which this write handle is associated
458 struct GNUNET_STREAM_Socket *socket;
461 * The packet_buffers associated with this Handle
463 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
466 * The write continuation callback
468 GNUNET_STREAM_CompletionContinuation write_cont;
471 * Write continuation closure
473 void *write_cont_cls;
476 * The bitmap of this IOHandle; Corresponding bit for a message is set when
477 * it has been acknowledged by the receiver
479 GNUNET_STREAM_AckBitmap ack_bitmap;
482 * Number of bytes in this write handle
491 struct GNUNET_STREAM_IOReadHandle
494 * The socket to which this read handle is associated
496 struct GNUNET_STREAM_Socket *socket;
499 * Callback for the read processor
501 GNUNET_STREAM_DataProcessor proc;
504 * The closure pointer for the read processor callback
511 * Handle for Shutdown
513 struct GNUNET_STREAM_ShutdownHandle
516 * The socket associated with this shutdown handle
518 struct GNUNET_STREAM_Socket *socket;
521 * Shutdown completion callback
523 GNUNET_STREAM_ShutdownCompletion completion_cb;
526 * Closure for completion callback
528 void *completion_cls;
531 * Close message retransmission task id
533 GNUNET_SCHEDULER_TaskIdentifier close_msg_retransmission_task_id;
536 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
543 * Default value in seconds for various timeouts
545 static const unsigned int default_timeout = 10;
548 * The domain name for locks we use here
550 static const char *locking_domain = "GNUNET_STREAM_APPLOCK";
554 * Callback function for sending queued message
556 * @param cls closure the socket
557 * @param size number of bytes available in buf
558 * @param buf where the callee should write the message
559 * @return number of bytes written to buf
562 send_message_notify (void *cls, size_t size, void *buf)
564 struct GNUNET_STREAM_Socket *socket = cls;
565 struct MessageQueue *head;
568 socket->transmit_handle = NULL; /* Remove the transmit handle */
569 head = socket->queue_head;
571 return 0; /* just to be safe */
572 if (0 == size) /* request timed out */
575 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "%s: Message sending timed out. Retry %d \n",
577 GNUNET_i2s (&socket->other_peer),
579 socket->transmit_handle =
580 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
581 GNUNET_NO, /* Corking */
582 /* FIXME: exponential backoff */
583 socket->retransmit_timeout,
585 ntohs (head->message->header.size),
586 &send_message_notify,
590 ret = ntohs (head->message->header.size);
591 GNUNET_assert (size >= ret);
592 memcpy (buf, head->message, ret);
593 if (NULL != head->finish_cb)
595 head->finish_cb (head->finish_cb_cls, socket);
597 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
600 GNUNET_free (head->message);
602 head = socket->queue_head;
603 if (NULL != head) /* more pending messages to send */
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,
621 * Queues a message for sending using the mesh connection of a socket
623 * @param socket the socket whose mesh connection is used
624 * @param message the message to be sent
625 * @param finish_cb the callback to be called when the message is sent
626 * @param finish_cb_cls the closure for the callback
627 * @param urgent set to GNUNET_YES to add the message to the beginning of the
628 * queue; GNUNET_NO to add at the tail
631 queue_message (struct GNUNET_STREAM_Socket *socket,
632 struct GNUNET_STREAM_MessageHeader *message,
633 SendFinishCallback finish_cb,
637 struct MessageQueue *queue_entity;
640 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
641 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
642 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 "%s: Queueing message of type %d and size %d\n",
644 GNUNET_i2s (&socket->other_peer),
645 ntohs (message->header.type),
646 ntohs (message->header.size));
647 GNUNET_assert (NULL != message);
648 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
649 queue_entity->message = message;
650 queue_entity->finish_cb = finish_cb;
651 queue_entity->finish_cb_cls = finish_cb_cls;
652 if (GNUNET_YES == urgent)
654 GNUNET_CONTAINER_DLL_insert (socket->queue_head, socket->queue_tail,
656 if (NULL != socket->transmit_handle)
658 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
659 socket->transmit_handle = NULL;
663 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
666 if (NULL == socket->transmit_handle)
669 socket->transmit_handle =
670 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
671 GNUNET_NO, /* Corking */
672 socket->retransmit_timeout,
674 ntohs (message->header.size),
675 &send_message_notify,
682 * Copies a message and queues it for sending using the mesh connection of
685 * @param socket the socket whose mesh connection is used
686 * @param message the message to be sent
687 * @param finish_cb the callback to be called when the message is sent
688 * @param finish_cb_cls the closure for the callback
691 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
692 const struct GNUNET_STREAM_MessageHeader *message,
693 SendFinishCallback finish_cb,
696 struct GNUNET_STREAM_MessageHeader *msg_copy;
699 size = ntohs (message->header.size);
700 msg_copy = GNUNET_malloc (size);
701 memcpy (msg_copy, message, size);
702 queue_message (socket, msg_copy, finish_cb, finish_cb_cls, GNUNET_NO);
707 * Writes data using the given socket. The amount of data written is limited by
708 * the receiver_window_size
710 * @param socket the socket to use
713 write_data (struct GNUNET_STREAM_Socket *socket);
717 * Task for retransmitting data messages if they aren't ACK before their ack
720 * @param cls the socket
721 * @param tc the Task context
724 data_retransmission_task (void *cls,
725 const struct GNUNET_SCHEDULER_TaskContext *tc)
727 struct GNUNET_STREAM_Socket *socket = cls;
729 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
731 LOG (GNUNET_ERROR_TYPE_DEBUG,
732 "%s: Retransmitting DATA...\n", GNUNET_i2s (&socket->other_peer));
733 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
739 * Task for sending ACK message
741 * @param cls the socket
742 * @param tc the Task context
746 const struct GNUNET_SCHEDULER_TaskContext *tc)
748 struct GNUNET_STREAM_Socket *socket = cls;
749 struct GNUNET_STREAM_AckMessage *ack_msg;
751 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
755 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
756 /* Create the ACK Message */
757 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
758 ack_msg->header.header.size = htons (sizeof (struct
759 GNUNET_STREAM_AckMessage));
760 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
761 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
762 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
763 ack_msg->receive_window_remaining =
764 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
765 /* Queue up ACK for immediate sending */
766 queue_message (socket, &ack_msg->header, NULL, NULL, GNUNET_YES);
771 * Retransmission task for shutdown messages
773 * @param cls the shutdown handle
774 * @param tc the Task Context
777 close_msg_retransmission_task (void *cls,
778 const struct GNUNET_SCHEDULER_TaskContext *tc)
780 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle = cls;
781 struct GNUNET_STREAM_MessageHeader *msg;
782 struct GNUNET_STREAM_Socket *socket;
784 GNUNET_assert (NULL != shutdown_handle);
785 socket = shutdown_handle->socket;
787 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
788 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
789 switch (shutdown_handle->operation)
792 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
795 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
798 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
802 shutdown_handle->close_msg_retransmission_task_id =
803 GNUNET_SCHEDULER_NO_TASK;
806 queue_message (socket, msg, NULL, NULL, GNUNET_NO);
807 shutdown_handle->close_msg_retransmission_task_id =
808 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
809 &close_msg_retransmission_task,
815 * Function to modify a bit in GNUNET_STREAM_AckBitmap
817 * @param bitmap the bitmap to modify
818 * @param bit the bit number to modify
819 * @param value GNUNET_YES to on, GNUNET_NO to off
822 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
826 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
827 if (GNUNET_YES == value)
828 *bitmap |= (1LL << bit);
830 *bitmap &= ~(1LL << bit);
835 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
837 * @param bitmap address of the bitmap that has to be checked
838 * @param bit the bit number to check
839 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
842 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
845 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
846 return 0 != (*bitmap & (1LL << bit));
851 * Writes data using the given socket. The amount of data written is limited by
852 * the receiver_window_size
854 * @param socket the socket to use
857 write_data (struct GNUNET_STREAM_Socket *socket)
859 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
860 int packet; /* Although an int, should never be negative */
864 /* Find the last acknowledged packet */
865 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
867 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
870 else if (NULL == io_handle->messages[packet])
873 /* Resend packets which weren't ack'ed */
874 for (packet=0; packet < ack_packet; packet++)
876 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
879 LOG (GNUNET_ERROR_TYPE_DEBUG,
880 "%s: Placing DATA message with sequence %u in send queue\n",
881 GNUNET_i2s (&socket->other_peer),
882 ntohl (io_handle->messages[packet]->sequence_number));
883 copy_and_queue_message (socket,
884 &io_handle->messages[packet]->header,
889 packet = ack_packet + 1;
890 /* Now send new packets if there is enough buffer space */
891 while ( (NULL != io_handle->messages[packet]) &&
892 (socket->receiver_window_available
893 >= ntohs (io_handle->messages[packet]->header.header.size)) &&
894 (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
896 socket->receiver_window_available -=
897 ntohs (io_handle->messages[packet]->header.header.size);
898 LOG (GNUNET_ERROR_TYPE_DEBUG,
899 "%s: Placing DATA message with sequence %u in send queue\n",
900 GNUNET_i2s (&socket->other_peer),
901 ntohl (io_handle->messages[packet]->sequence_number));
902 copy_and_queue_message (socket,
903 &io_handle->messages[packet]->header,
908 if (GNUNET_SCHEDULER_NO_TASK == socket->data_retransmission_task_id)
909 socket->data_retransmission_task_id =
910 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
911 (GNUNET_TIME_UNIT_SECONDS, 8),
912 &data_retransmission_task,
918 * Task for calling the read processor
920 * @param cls the socket
921 * @param tc the task context
924 call_read_processor (void *cls,
925 const struct GNUNET_SCHEDULER_TaskContext *tc)
927 struct GNUNET_STREAM_Socket *socket = cls;
929 size_t valid_read_size;
931 uint32_t sequence_increase;
932 uint32_t offset_increase;
934 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
935 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
938 if (NULL == socket->receive_buffer)
941 GNUNET_assert (NULL != socket->read_handle);
942 GNUNET_assert (NULL != socket->read_handle->proc);
944 /* Check the bitmap for any holes */
945 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
947 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
951 /* We only call read processor if we have the first packet */
952 GNUNET_assert (0 < packet);
954 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
955 GNUNET_assert (0 != valid_read_size);
956 /* Cancel the read_io_timeout_task */
957 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task_id);
958 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
959 /* Call the data processor */
960 LOG (GNUNET_ERROR_TYPE_DEBUG,
961 "%s: Calling read processor\n",
962 GNUNET_i2s (&socket->other_peer));
964 socket->read_handle->proc (socket->read_handle->proc_cls,
966 socket->receive_buffer + socket->copy_offset,
968 LOG (GNUNET_ERROR_TYPE_DEBUG,
969 "%s: Read processor read %d bytes\n",
970 GNUNET_i2s (&socket->other_peer), read_size);
971 LOG (GNUNET_ERROR_TYPE_DEBUG,
972 "%s: Read processor completed successfully\n",
973 GNUNET_i2s (&socket->other_peer));
974 /* Free the read handle */
975 GNUNET_free (socket->read_handle);
976 socket->read_handle = NULL;
977 GNUNET_assert (read_size <= valid_read_size);
978 socket->copy_offset += read_size;
979 /* Determine upto which packet we can remove from the buffer */
980 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
982 if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
984 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
988 /* If no packets can be removed we can't move the buffer */
989 if (0 == packet) return;
990 sequence_increase = packet;
991 LOG (GNUNET_ERROR_TYPE_DEBUG,
992 "%s: Sequence increase after read processor completion: %u\n",
993 GNUNET_i2s (&socket->other_peer), sequence_increase);
995 /* Shift the data in the receive buffer */
996 socket->receive_buffer =
997 memmove (socket->receive_buffer,
998 socket->receive_buffer
999 + socket->receive_buffer_boundaries[sequence_increase-1],
1000 socket->receive_buffer_size
1001 - socket->receive_buffer_boundaries[sequence_increase-1]);
1002 /* Shift the bitmap */
1003 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
1004 /* Set read_sequence_number */
1005 socket->read_sequence_number += sequence_increase;
1006 /* Set read_offset */
1007 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
1008 socket->read_offset += offset_increase;
1009 /* Fix copy_offset */
1010 GNUNET_assert (offset_increase <= socket->copy_offset);
1011 socket->copy_offset -= offset_increase;
1012 /* Fix relative boundaries */
1013 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1015 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
1017 uint32_t ahead_buffer_boundary;
1019 ahead_buffer_boundary =
1020 socket->receive_buffer_boundaries[packet + sequence_increase];
1021 if (0 == ahead_buffer_boundary)
1022 socket->receive_buffer_boundaries[packet] = 0;
1025 GNUNET_assert (offset_increase < ahead_buffer_boundary);
1026 socket->receive_buffer_boundaries[packet] =
1027 ahead_buffer_boundary - offset_increase;
1031 socket->receive_buffer_boundaries[packet] = 0;
1037 * Cancels the existing read io handle
1039 * @param cls the closure from the SCHEDULER call
1040 * @param tc the task context
1043 read_io_timeout (void *cls,
1044 const struct GNUNET_SCHEDULER_TaskContext *tc)
1046 struct GNUNET_STREAM_Socket *socket = cls;
1047 GNUNET_STREAM_DataProcessor proc;
1050 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
1051 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
1053 LOG (GNUNET_ERROR_TYPE_DEBUG,
1054 "%s: Read task timedout - Cancelling it\n",
1055 GNUNET_i2s (&socket->other_peer));
1056 GNUNET_SCHEDULER_cancel (socket->read_task_id);
1057 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
1059 GNUNET_assert (NULL != socket->read_handle);
1060 proc = socket->read_handle->proc;
1061 proc_cls = socket->read_handle->proc_cls;
1062 GNUNET_free (socket->read_handle);
1063 socket->read_handle = NULL;
1064 /* Call the read processor to signal timeout */
1066 GNUNET_STREAM_TIMEOUT,
1073 * Handler for DATA messages; Same for both client and server
1075 * @param socket the socket through which the ack was received
1076 * @param tunnel connection to the other end
1077 * @param sender who sent the message
1078 * @param msg the data message
1079 * @param atsi performance data for the connection
1080 * @return GNUNET_OK to keep the connection open,
1081 * GNUNET_SYSERR to close it (signal serious error)
1084 handle_data (struct GNUNET_STREAM_Socket *socket,
1085 struct GNUNET_MESH_Tunnel *tunnel,
1086 const struct GNUNET_PeerIdentity *sender,
1087 const struct GNUNET_STREAM_DataMessage *msg,
1088 const struct GNUNET_ATS_Information*atsi)
1090 const void *payload;
1091 struct GNUNET_TIME_Relative ack_deadline_rel;
1092 uint32_t bytes_needed;
1093 uint32_t relative_offset;
1094 uint32_t relative_sequence_number;
1097 size = htons (msg->header.header.size);
1098 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
1100 GNUNET_break_op (0);
1101 return GNUNET_SYSERR;
1103 if (0 != memcmp (sender, &socket->other_peer,
1104 sizeof (struct GNUNET_PeerIdentity)))
1106 LOG (GNUNET_ERROR_TYPE_DEBUG,
1107 "%s: Received DATA from non-confirming peer\n",
1108 GNUNET_i2s (&socket->other_peer));
1111 switch (socket->state)
1113 case STATE_ESTABLISHED:
1114 case STATE_TRANSMIT_CLOSED:
1115 case STATE_TRANSMIT_CLOSE_WAIT:
1116 /* check if the message's sequence number is in the range we are
1118 relative_sequence_number =
1119 ntohl (msg->sequence_number) - socket->read_sequence_number;
1120 if ( relative_sequence_number > GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1122 LOG (GNUNET_ERROR_TYPE_DEBUG,
1123 "%s: Ignoring received message with sequence number %u\n",
1124 GNUNET_i2s (&socket->other_peer),
1125 ntohl (msg->sequence_number));
1126 /* Start ACK sending task if one is not already present */
1127 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1129 socket->ack_task_id =
1130 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1131 (msg->ack_deadline),
1137 /* Check if we have already seen this message */
1138 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1139 relative_sequence_number))
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 "%s: Ignoring already 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), &ack_task, socket);
1154 LOG (GNUNET_ERROR_TYPE_DEBUG,
1155 "%s: Receiving DATA with sequence number: %u and size: %d from %s\n",
1156 GNUNET_i2s (&socket->other_peer), ntohl (msg->sequence_number),
1157 ntohs (msg->header.header.size), GNUNET_i2s (&socket->other_peer));
1158 /* Check if we have to allocate the buffer */
1159 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1160 relative_offset = ntohl (msg->offset) - socket->read_offset;
1161 bytes_needed = relative_offset + size;
1162 if (bytes_needed > socket->receive_buffer_size)
1164 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1166 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1168 socket->receive_buffer_size = bytes_needed;
1172 LOG (GNUNET_ERROR_TYPE_DEBUG,
1173 "%s: Cannot accommodate packet %d as buffer is full\n",
1174 GNUNET_i2s (&socket->other_peer), ntohl (msg->sequence_number));
1178 /* Copy Data to buffer */
1180 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1181 memcpy (socket->receive_buffer + relative_offset, payload, size);
1182 socket->receive_buffer_boundaries[relative_sequence_number] =
1183 relative_offset + size;
1184 /* Modify the ACK bitmap */
1185 ackbitmap_modify_bit (&socket->ack_bitmap, relative_sequence_number,
1187 /* Start ACK sending task if one is not already present */
1188 ack_deadline_rel = GNUNET_TIME_relative_ntoh (msg->ack_deadline);
1189 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1192 GNUNET_TIME_relative_min (ack_deadline_rel,
1193 GNUNET_TIME_relative_multiply
1194 (GNUNET_TIME_UNIT_SECONDS, 300));
1195 socket->ack_task_id =
1196 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1197 (msg->ack_deadline), &ack_task, socket);
1198 socket->ack_time_registered = GNUNET_TIME_absolute_get ();
1199 socket->ack_time_deadline = ack_deadline_rel;
1203 struct GNUNET_TIME_Relative ack_time_past;
1204 struct GNUNET_TIME_Relative ack_time_remaining;
1205 struct GNUNET_TIME_Relative ack_time_min;
1207 GNUNET_TIME_absolute_get_duration (socket->ack_time_registered);
1208 ack_time_remaining = GNUNET_TIME_relative_subtract
1209 (socket->ack_time_deadline, ack_time_past);
1210 ack_time_min = GNUNET_TIME_relative_min (ack_time_remaining,
1212 if (0 == memcmp(&ack_deadline_rel, &ack_time_min,
1213 sizeof (struct GNUNET_TIME_Relative)))
1215 ack_deadline_rel = ack_time_min;
1216 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
1217 socket->ack_task_id = GNUNET_SCHEDULER_add_delayed (ack_deadline_rel,
1219 socket->ack_time_registered = GNUNET_TIME_absolute_get ();
1220 socket->ack_time_deadline = ack_deadline_rel;
1223 if ((NULL != socket->read_handle) /* A read handle is waiting */
1224 /* There is no current read task */
1225 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
1226 /* We have the first packet */
1227 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap, 0)))
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Scheduling read processor\n",
1230 GNUNET_i2s (&socket->other_peer));
1231 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
1236 LOG (GNUNET_ERROR_TYPE_DEBUG,
1237 "%s: Received data message when it cannot be handled\n",
1238 GNUNET_i2s (&socket->other_peer));
1246 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1248 * @param cls the socket (set from GNUNET_MESH_connect)
1249 * @param tunnel connection to the other end
1250 * @param tunnel_ctx place to store local state associated with the tunnel
1251 * @param sender who sent the message
1252 * @param message the actual message
1253 * @param atsi performance data for the connection
1254 * @return GNUNET_OK to keep the connection open,
1255 * GNUNET_SYSERR to close it (signal serious error)
1258 client_handle_data (void *cls,
1259 struct GNUNET_MESH_Tunnel *tunnel,
1261 const struct GNUNET_PeerIdentity *sender,
1262 const struct GNUNET_MessageHeader *message,
1263 const struct GNUNET_ATS_Information*atsi)
1265 struct GNUNET_STREAM_Socket *socket = cls;
1267 return handle_data (socket, tunnel, sender,
1268 (const struct GNUNET_STREAM_DataMessage *) message, atsi);
1273 * Callback to set state to ESTABLISHED
1275 * @param cls the closure NULL;
1276 * @param socket the socket to requiring state change
1279 set_state_established (void *cls,
1280 struct GNUNET_STREAM_Socket *socket)
1282 LOG (GNUNET_ERROR_TYPE_DEBUG,
1283 "%s: Attaining ESTABLISHED state\n",
1284 GNUNET_i2s (&socket->other_peer));
1285 socket->write_offset = 0;
1286 socket->read_offset = 0;
1287 socket->state = STATE_ESTABLISHED;
1288 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
1289 socket->control_retransmission_task_id);
1290 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
1291 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
1292 if (NULL != socket->lsocket)
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "%s: Calling listen callback\n",
1296 GNUNET_i2s (&socket->other_peer));
1297 if (GNUNET_SYSERR ==
1298 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1300 &socket->other_peer))
1302 socket->state = STATE_CLOSED;
1303 /* FIXME: We should close in a decent way (send RST) */
1304 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1305 GNUNET_free (socket);
1309 socket->open_cb (socket->open_cls, socket);
1314 * Callback to set state to HELLO_WAIT
1316 * @param cls the closure from queue_message
1317 * @param socket the socket to requiring state change
1320 set_state_hello_wait (void *cls,
1321 struct GNUNET_STREAM_Socket *socket)
1323 GNUNET_assert (STATE_INIT == socket->state);
1324 LOG (GNUNET_ERROR_TYPE_DEBUG,
1325 "%s: Attaining HELLO_WAIT state\n",
1326 GNUNET_i2s (&socket->other_peer));
1327 socket->state = STATE_HELLO_WAIT;
1332 * Callback to set state to CLOSE_WAIT
1334 * @param cls the closure from queue_message
1335 * @param socket the socket requiring state change
1338 set_state_close_wait (void *cls,
1339 struct GNUNET_STREAM_Socket *socket)
1341 LOG (GNUNET_ERROR_TYPE_DEBUG,
1342 "%s: Attaing CLOSE_WAIT state\n",
1343 GNUNET_i2s (&socket->other_peer));
1344 socket->state = STATE_CLOSE_WAIT;
1345 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1346 socket->receive_buffer = NULL;
1347 socket->receive_buffer_size = 0;
1352 * Callback to set state to RECEIVE_CLOSE_WAIT
1354 * @param cls the closure from queue_message
1355 * @param socket the socket requiring state change
1358 set_state_receive_close_wait (void *cls,
1359 struct GNUNET_STREAM_Socket *socket)
1361 LOG (GNUNET_ERROR_TYPE_DEBUG,
1362 "%s: Attaing RECEIVE_CLOSE_WAIT state\n",
1363 GNUNET_i2s (&socket->other_peer));
1364 socket->state = STATE_RECEIVE_CLOSE_WAIT;
1365 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1366 socket->receive_buffer = NULL;
1367 socket->receive_buffer_size = 0;
1372 * Callback to set state to TRANSMIT_CLOSE_WAIT
1374 * @param cls the closure from queue_message
1375 * @param socket the socket requiring state change
1378 set_state_transmit_close_wait (void *cls,
1379 struct GNUNET_STREAM_Socket *socket)
1381 LOG (GNUNET_ERROR_TYPE_DEBUG,
1382 "%s: Attaing TRANSMIT_CLOSE_WAIT state\n",
1383 GNUNET_i2s (&socket->other_peer));
1384 socket->state = STATE_TRANSMIT_CLOSE_WAIT;
1389 * Callback to set state to CLOSED
1391 * @param cls the closure from queue_message
1392 * @param socket the socket requiring state change
1395 set_state_closed (void *cls,
1396 struct GNUNET_STREAM_Socket *socket)
1398 socket->state = STATE_CLOSED;
1403 * Returns GNUNET_MESSAGE_TYPE_STREAM_HELLO
1405 * @return the generate hello message
1407 static struct GNUNET_STREAM_MessageHeader *
1408 generate_hello (void)
1410 struct GNUNET_STREAM_MessageHeader *msg;
1412 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1413 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
1414 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1420 * Returns a new HelloAckMessage. Also sets the write sequence number for the
1423 * @param socket the socket for which this HelloAckMessage has to be generated
1424 * @param generate_seq GNUNET_YES to generate the write sequence number,
1425 * GNUNET_NO to use the existing sequence number
1426 * @return the HelloAckMessage
1428 static struct GNUNET_STREAM_HelloAckMessage *
1429 generate_hello_ack (struct GNUNET_STREAM_Socket *socket,
1432 struct GNUNET_STREAM_HelloAckMessage *msg;
1434 if (GNUNET_YES == generate_seq)
1436 if (GNUNET_YES == socket->testing_active)
1437 socket->write_sequence_number =
1438 socket->testing_set_write_sequence_number_value;
1440 socket->write_sequence_number =
1441 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1442 LOG_DEBUG ("%s: write sequence number %u\n",
1443 GNUNET_i2s (&socket->other_peer),
1444 (unsigned int) socket->write_sequence_number);
1446 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1447 msg->header.header.size =
1448 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1449 msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1450 msg->sequence_number = htonl (socket->write_sequence_number);
1451 msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1457 * Task for retransmitting control messages if they aren't ACK'ed before a
1460 * @param cls the socket
1461 * @param tc the Task context
1464 control_retransmission_task (void *cls,
1465 const struct GNUNET_SCHEDULER_TaskContext *tc)
1467 struct GNUNET_STREAM_Socket *socket = cls;
1469 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
1471 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
1472 LOG_DEBUG ("%s: Retransmitting a control message\n",
1473 GNUNET_i2s (&socket->other_peer));
1474 switch (socket->state)
1482 case STATE_HELLO_WAIT:
1483 if (NULL == socket->lsocket) /* We are client */
1484 queue_message (socket, generate_hello (), NULL, NULL, GNUNET_NO);
1486 queue_message (socket,
1487 (struct GNUNET_STREAM_MessageHeader *)
1488 generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
1490 socket->control_retransmission_task_id =
1491 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
1492 &control_retransmission_task, socket);
1494 case STATE_ESTABLISHED:
1495 if (NULL == socket->lsocket)
1496 queue_message (socket,
1497 (struct GNUNET_STREAM_MessageHeader *)
1498 generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
1509 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1511 * @param cls the socket (set from GNUNET_MESH_connect)
1512 * @param tunnel connection to the other end
1513 * @param tunnel_ctx this is NULL
1514 * @param sender who sent the message
1515 * @param message the actual message
1516 * @param atsi performance data for the connection
1517 * @return GNUNET_OK to keep the connection open,
1518 * GNUNET_SYSERR to close it (signal serious error)
1521 client_handle_hello_ack (void *cls,
1522 struct GNUNET_MESH_Tunnel *tunnel,
1524 const struct GNUNET_PeerIdentity *sender,
1525 const struct GNUNET_MessageHeader *message,
1526 const struct GNUNET_ATS_Information*atsi)
1528 struct GNUNET_STREAM_Socket *socket = cls;
1529 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1530 struct GNUNET_STREAM_HelloAckMessage *reply;
1532 if (0 != memcmp (sender, &socket->other_peer,
1533 sizeof (struct GNUNET_PeerIdentity)))
1535 LOG (GNUNET_ERROR_TYPE_DEBUG,
1536 "%s: Received HELLO_ACK from non-confirming peer\n",
1537 GNUNET_i2s (&socket->other_peer));
1540 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1541 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received HELLO_ACK from %s\n",
1542 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1543 GNUNET_assert (socket->tunnel == tunnel);
1544 switch (socket->state)
1546 case STATE_HELLO_WAIT:
1547 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1548 LOG (GNUNET_ERROR_TYPE_DEBUG,
1549 "%s: Read sequence number %u\n",
1550 GNUNET_i2s (&socket->other_peer),
1551 (unsigned int) socket->read_sequence_number);
1552 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1553 reply = generate_hello_ack (socket, GNUNET_YES);
1554 queue_message (socket, &reply->header, &set_state_established,
1557 case STATE_ESTABLISHED:
1558 // call statistics (# ACKs ignored++)
1559 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
1560 socket->control_retransmission_task_id);
1561 socket->control_retransmission_task_id =
1562 GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
1565 LOG_DEBUG ("%s: Server %s sent HELLO_ACK when in state %d\n",
1566 GNUNET_i2s (&socket->other_peer),
1567 GNUNET_i2s (&socket->other_peer), socket->state);
1568 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1569 return GNUNET_SYSERR;
1575 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1577 * @param cls the socket (set from GNUNET_MESH_connect)
1578 * @param tunnel connection to the other end
1579 * @param tunnel_ctx this is NULL
1580 * @param sender who sent the message
1581 * @param message the actual message
1582 * @param atsi performance data for the connection
1583 * @return GNUNET_OK to keep the connection open,
1584 * GNUNET_SYSERR to close it (signal serious error)
1587 client_handle_reset (void *cls,
1588 struct GNUNET_MESH_Tunnel *tunnel,
1590 const struct GNUNET_PeerIdentity *sender,
1591 const struct GNUNET_MessageHeader *message,
1592 const struct GNUNET_ATS_Information*atsi)
1594 // struct GNUNET_STREAM_Socket *socket = cls;
1601 * Common message handler for handling TRANSMIT_CLOSE messages
1603 * @param socket the socket through which the ack was received
1604 * @param tunnel connection to the other end
1605 * @param sender who sent the message
1606 * @param msg the transmit close message
1607 * @param atsi performance data for the connection
1608 * @return GNUNET_OK to keep the connection open,
1609 * GNUNET_SYSERR to close it (signal serious error)
1612 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1613 struct GNUNET_MESH_Tunnel *tunnel,
1614 const struct GNUNET_PeerIdentity *sender,
1615 const struct GNUNET_STREAM_MessageHeader *msg,
1616 const struct GNUNET_ATS_Information*atsi)
1618 struct GNUNET_STREAM_MessageHeader *reply;
1620 switch (socket->state)
1622 case STATE_ESTABLISHED:
1623 socket->state = STATE_RECEIVE_CLOSED;
1624 /* Send TRANSMIT_CLOSE_ACK */
1625 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1626 reply->header.type =
1627 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1628 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1629 queue_message (socket, reply, NULL, NULL, GNUNET_NO);
1632 /* FIXME: Call statistics? */
1640 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1642 * @param cls the socket (set from GNUNET_MESH_connect)
1643 * @param tunnel connection to the other end
1644 * @param tunnel_ctx this is NULL
1645 * @param sender who sent the message
1646 * @param message the actual message
1647 * @param atsi performance data for the connection
1648 * @return GNUNET_OK to keep the connection open,
1649 * GNUNET_SYSERR to close it (signal serious error)
1652 client_handle_transmit_close (void *cls,
1653 struct GNUNET_MESH_Tunnel *tunnel,
1655 const struct GNUNET_PeerIdentity *sender,
1656 const struct GNUNET_MessageHeader *message,
1657 const struct GNUNET_ATS_Information*atsi)
1659 struct GNUNET_STREAM_Socket *socket = cls;
1661 return handle_transmit_close (socket,
1664 (struct GNUNET_STREAM_MessageHeader *)message,
1670 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_*_CLOSE_ACK messages
1672 * @param socket the socket
1673 * @param tunnel connection to the other end
1674 * @param sender who sent the message
1675 * @param message the actual message
1676 * @param atsi performance data for the connection
1677 * @param operation the close operation which is being ACK'ed
1678 * @return GNUNET_OK to keep the connection open,
1679 * GNUNET_SYSERR to close it (signal serious error)
1682 handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
1683 struct GNUNET_MESH_Tunnel *tunnel,
1684 const struct GNUNET_PeerIdentity *sender,
1685 const struct GNUNET_STREAM_MessageHeader *message,
1686 const struct GNUNET_ATS_Information *atsi,
1689 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
1691 shutdown_handle = socket->shutdown_handle;
1692 if (NULL == shutdown_handle)
1694 LOG (GNUNET_ERROR_TYPE_DEBUG,
1695 "%s: Received CLOSE_ACK when shutdown handle is NULL\n",
1696 GNUNET_i2s (&socket->other_peer));
1702 switch (socket->state)
1704 case STATE_CLOSE_WAIT:
1705 if (SHUT_RDWR != shutdown_handle->operation)
1707 LOG (GNUNET_ERROR_TYPE_DEBUG,
1708 "%s: Received CLOSE_ACK when shutdown handle is not for "
1709 "SHUT_RDWR\n", GNUNET_i2s (&socket->other_peer));
1712 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received CLOSE_ACK from %s\n",
1713 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1714 socket->state = STATE_CLOSED;
1717 LOG (GNUNET_ERROR_TYPE_DEBUG,
1718 "%s: Received CLOSE_ACK when in it not expected\n",
1719 GNUNET_i2s (&socket->other_peer));
1724 switch (socket->state)
1726 case STATE_RECEIVE_CLOSE_WAIT:
1727 if (SHUT_RD != shutdown_handle->operation)
1729 LOG (GNUNET_ERROR_TYPE_DEBUG,
1730 "%s: Received RECEIVE_CLOSE_ACK when shutdown handle "
1731 "is not for SHUT_RD\n", GNUNET_i2s (&socket->other_peer));
1734 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received RECEIVE_CLOSE_ACK from %s\n",
1735 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1736 socket->state = STATE_RECEIVE_CLOSED;
1739 LOG (GNUNET_ERROR_TYPE_DEBUG,
1740 "%s: Received RECEIVE_CLOSE_ACK when in it not expected\n",
1741 GNUNET_i2s (&socket->other_peer));
1746 switch (socket->state)
1748 case STATE_TRANSMIT_CLOSE_WAIT:
1749 if (SHUT_WR != shutdown_handle->operation)
1751 LOG (GNUNET_ERROR_TYPE_DEBUG,
1752 "%s: Received TRANSMIT_CLOSE_ACK when shutdown handle "
1753 "is not for SHUT_WR\n",
1754 GNUNET_i2s (&socket->other_peer));
1757 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received TRANSMIT_CLOSE_ACK from %s\n",
1758 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1759 socket->state = STATE_TRANSMIT_CLOSED;
1762 LOG (GNUNET_ERROR_TYPE_DEBUG,
1763 "%s: Received TRANSMIT_CLOSE_ACK when in it not expected\n",
1764 GNUNET_i2s (&socket->other_peer));
1771 if (NULL != shutdown_handle->completion_cb) /* Shutdown completion */
1772 shutdown_handle->completion_cb(shutdown_handle->completion_cls,
1774 if (GNUNET_SCHEDULER_NO_TASK
1775 != shutdown_handle->close_msg_retransmission_task_id)
1777 GNUNET_SCHEDULER_cancel
1778 (shutdown_handle->close_msg_retransmission_task_id);
1779 shutdown_handle->close_msg_retransmission_task_id =
1780 GNUNET_SCHEDULER_NO_TASK;
1782 GNUNET_free (shutdown_handle); /* Free shutdown handle */
1783 socket->shutdown_handle = NULL;
1789 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1791 * @param cls the socket (set from GNUNET_MESH_connect)
1792 * @param tunnel connection to the other end
1793 * @param tunnel_ctx this is NULL
1794 * @param sender who sent the message
1795 * @param message the actual message
1796 * @param atsi performance data for the connection
1797 * @return GNUNET_OK to keep the connection open,
1798 * GNUNET_SYSERR to close it (signal serious error)
1801 client_handle_transmit_close_ack (void *cls,
1802 struct GNUNET_MESH_Tunnel *tunnel,
1804 const struct GNUNET_PeerIdentity *sender,
1805 const struct GNUNET_MessageHeader *message,
1806 const struct GNUNET_ATS_Information*atsi)
1808 struct GNUNET_STREAM_Socket *socket = cls;
1810 return handle_generic_close_ack (socket,
1813 (const struct GNUNET_STREAM_MessageHeader *)
1821 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1823 * @param socket the socket
1824 * @param tunnel connection to the other end
1825 * @param sender who sent the message
1826 * @param message the actual message
1827 * @param atsi performance data for the connection
1828 * @return GNUNET_OK to keep the connection open,
1829 * GNUNET_SYSERR to close it (signal serious error)
1832 handle_receive_close (struct GNUNET_STREAM_Socket *socket,
1833 struct GNUNET_MESH_Tunnel *tunnel,
1834 const struct GNUNET_PeerIdentity *sender,
1835 const struct GNUNET_STREAM_MessageHeader *message,
1836 const struct GNUNET_ATS_Information *atsi)
1838 struct GNUNET_STREAM_MessageHeader *receive_close_ack;
1840 switch (socket->state)
1844 case STATE_HELLO_WAIT:
1845 LOG (GNUNET_ERROR_TYPE_DEBUG,
1846 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1847 GNUNET_i2s (&socket->other_peer));
1853 LOG (GNUNET_ERROR_TYPE_DEBUG,
1854 "%s: Received RECEIVE_CLOSE from %s\n",
1855 GNUNET_i2s (&socket->other_peer),
1856 GNUNET_i2s (&socket->other_peer));
1858 GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1859 receive_close_ack->header.size =
1860 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1861 receive_close_ack->header.type =
1862 htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK);
1863 queue_message (socket, receive_close_ack, &set_state_closed,
1865 /* FIXME: Handle the case where write handle is present; the write operation
1866 should be deemed as finised and the write continuation callback
1867 has to be called with the stream status GNUNET_STREAM_SHUTDOWN */
1873 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1875 * @param cls the socket (set from GNUNET_MESH_connect)
1876 * @param tunnel connection to the other end
1877 * @param tunnel_ctx this is NULL
1878 * @param sender who sent the message
1879 * @param message the actual message
1880 * @param atsi performance data for the connection
1881 * @return GNUNET_OK to keep the connection open,
1882 * GNUNET_SYSERR to close it (signal serious error)
1885 client_handle_receive_close (void *cls,
1886 struct GNUNET_MESH_Tunnel *tunnel,
1888 const struct GNUNET_PeerIdentity *sender,
1889 const struct GNUNET_MessageHeader *message,
1890 const struct GNUNET_ATS_Information*atsi)
1892 struct GNUNET_STREAM_Socket *socket = cls;
1895 handle_receive_close (socket,
1898 (const struct GNUNET_STREAM_MessageHeader *) message,
1904 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1906 * @param cls the socket (set from GNUNET_MESH_connect)
1907 * @param tunnel connection to the other end
1908 * @param tunnel_ctx this is NULL
1909 * @param sender who sent the message
1910 * @param message the actual message
1911 * @param atsi performance data for the connection
1912 * @return GNUNET_OK to keep the connection open,
1913 * GNUNET_SYSERR to close it (signal serious error)
1916 client_handle_receive_close_ack (void *cls,
1917 struct GNUNET_MESH_Tunnel *tunnel,
1919 const struct GNUNET_PeerIdentity *sender,
1920 const struct GNUNET_MessageHeader *message,
1921 const struct GNUNET_ATS_Information*atsi)
1923 struct GNUNET_STREAM_Socket *socket = cls;
1925 return handle_generic_close_ack (socket,
1928 (const struct GNUNET_STREAM_MessageHeader *)
1936 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1938 * @param socket the socket
1939 * @param tunnel connection to the other end
1940 * @param sender who sent the message
1941 * @param message the actual message
1942 * @param atsi performance data for the connection
1943 * @return GNUNET_OK to keep the connection open,
1944 * GNUNET_SYSERR to close it (signal serious error)
1947 handle_close (struct GNUNET_STREAM_Socket *socket,
1948 struct GNUNET_MESH_Tunnel *tunnel,
1949 const struct GNUNET_PeerIdentity *sender,
1950 const struct GNUNET_STREAM_MessageHeader *message,
1951 const struct GNUNET_ATS_Information*atsi)
1953 struct GNUNET_STREAM_MessageHeader *close_ack;
1955 switch (socket->state)
1959 case STATE_HELLO_WAIT:
1960 LOG (GNUNET_ERROR_TYPE_DEBUG,
1961 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1962 GNUNET_i2s (&socket->other_peer));
1968 LOG (GNUNET_ERROR_TYPE_DEBUG,
1969 "%s: Received CLOSE from %s\n",
1970 GNUNET_i2s (&socket->other_peer),
1971 GNUNET_i2s (&socket->other_peer));
1972 close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1973 close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1974 close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK);
1975 queue_message (socket, close_ack, &set_state_closed, NULL, GNUNET_NO);
1976 if (socket->state == STATE_CLOSED)
1979 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1980 socket->receive_buffer = NULL;
1981 socket->receive_buffer_size = 0;
1987 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1989 * @param cls the socket (set from GNUNET_MESH_connect)
1990 * @param tunnel connection to the other end
1991 * @param tunnel_ctx this is NULL
1992 * @param sender who sent the message
1993 * @param message the actual message
1994 * @param atsi performance data for the connection
1995 * @return GNUNET_OK to keep the connection open,
1996 * GNUNET_SYSERR to close it (signal serious error)
1999 client_handle_close (void *cls,
2000 struct GNUNET_MESH_Tunnel *tunnel,
2002 const struct GNUNET_PeerIdentity *sender,
2003 const struct GNUNET_MessageHeader *message,
2004 const struct GNUNET_ATS_Information*atsi)
2006 struct GNUNET_STREAM_Socket *socket = cls;
2008 return handle_close (socket,
2011 (const struct GNUNET_STREAM_MessageHeader *) message,
2017 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2019 * @param cls the socket (set from GNUNET_MESH_connect)
2020 * @param tunnel connection to the other end
2021 * @param tunnel_ctx this is NULL
2022 * @param sender who sent the message
2023 * @param message the actual message
2024 * @param atsi performance data for the connection
2025 * @return GNUNET_OK to keep the connection open,
2026 * GNUNET_SYSERR to close it (signal serious error)
2029 client_handle_close_ack (void *cls,
2030 struct GNUNET_MESH_Tunnel *tunnel,
2032 const struct GNUNET_PeerIdentity *sender,
2033 const struct GNUNET_MessageHeader *message,
2034 const struct GNUNET_ATS_Information *atsi)
2036 struct GNUNET_STREAM_Socket *socket = cls;
2038 return handle_generic_close_ack (socket,
2041 (const struct GNUNET_STREAM_MessageHeader *)
2047 /*****************************/
2048 /* Server's Message Handlers */
2049 /*****************************/
2052 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
2054 * @param cls the closure
2055 * @param tunnel connection to the other end
2056 * @param tunnel_ctx the socket
2057 * @param sender who sent the message
2058 * @param message the actual message
2059 * @param atsi performance data for the connection
2060 * @return GNUNET_OK to keep the connection open,
2061 * GNUNET_SYSERR to close it (signal serious error)
2064 server_handle_data (void *cls,
2065 struct GNUNET_MESH_Tunnel *tunnel,
2067 const struct GNUNET_PeerIdentity *sender,
2068 const struct GNUNET_MessageHeader *message,
2069 const struct GNUNET_ATS_Information*atsi)
2071 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2073 return handle_data (socket,
2076 (const struct GNUNET_STREAM_DataMessage *)message,
2082 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
2084 * @param cls the closure
2085 * @param tunnel connection to the other end
2086 * @param tunnel_ctx the socket
2087 * @param sender who sent the message
2088 * @param message the actual message
2089 * @param atsi performance data for the connection
2090 * @return GNUNET_OK to keep the connection open,
2091 * GNUNET_SYSERR to close it (signal serious error)
2094 server_handle_hello (void *cls,
2095 struct GNUNET_MESH_Tunnel *tunnel,
2097 const struct GNUNET_PeerIdentity *sender,
2098 const struct GNUNET_MessageHeader *message,
2099 const struct GNUNET_ATS_Information*atsi)
2101 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2102 struct GNUNET_STREAM_HelloAckMessage *reply;
2104 if (0 != memcmp (sender,
2105 &socket->other_peer,
2106 sizeof (struct GNUNET_PeerIdentity)))
2108 LOG_DEBUG ("%s: Received HELLO from non-confirming peer\n",
2109 GNUNET_i2s (&socket->other_peer));
2112 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO == ntohs (message->type));
2113 GNUNET_assert (socket->tunnel == tunnel);
2114 LOG_DEBUG ("%s: Received HELLO from %s\n", GNUNET_i2s (&socket->other_peer),
2115 GNUNET_i2s (&socket->other_peer));
2116 switch (socket->status)
2119 reply = generate_hello_ack (socket, GNUNET_YES);
2120 queue_message (socket, &reply->header, &set_state_hello_wait, NULL,
2122 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
2123 socket->control_retransmission_task_id);
2124 socket->control_retransmission_task_id =
2125 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2126 &control_retransmission_task, socket);
2128 case STATE_HELLO_WAIT:
2129 /* Perhaps our HELLO_ACK was lost */
2130 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
2131 socket->control_retransmission_task_id);
2132 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
2133 socket->control_retransmission_task_id =
2134 GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
2137 LOG_DEBUG( "%s: Client sent HELLO when in state %d\n",
2138 GNUNET_i2s (&socket->other_peer), socket->state);
2139 /* FIXME: Send RESET? */
2146 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
2148 * @param cls the closure
2149 * @param tunnel connection to the other end
2150 * @param tunnel_ctx the socket
2151 * @param sender who sent the message
2152 * @param message the actual message
2153 * @param atsi performance data for the connection
2154 * @return GNUNET_OK to keep the connection open,
2155 * GNUNET_SYSERR to close it (signal serious error)
2158 server_handle_hello_ack (void *cls,
2159 struct GNUNET_MESH_Tunnel *tunnel,
2161 const struct GNUNET_PeerIdentity *sender,
2162 const struct GNUNET_MessageHeader *message,
2163 const struct GNUNET_ATS_Information*atsi)
2165 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2166 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
2168 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
2169 ntohs (message->type));
2170 GNUNET_assert (socket->tunnel == tunnel);
2171 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
2172 switch (socket->state)
2174 case STATE_HELLO_WAIT:
2175 LOG (GNUNET_ERROR_TYPE_DEBUG,
2176 "%s: Received HELLO_ACK from %s\n",
2177 GNUNET_i2s (&socket->other_peer),
2178 GNUNET_i2s (&socket->other_peer));
2179 socket->read_sequence_number = ntohl (ack_message->sequence_number);
2180 LOG (GNUNET_ERROR_TYPE_DEBUG,
2181 "%s: Read sequence number %u\n",
2182 GNUNET_i2s (&socket->other_peer),
2183 (unsigned int) socket->read_sequence_number);
2184 socket->receiver_window_available =
2185 ntohl (ack_message->receiver_window_size);
2186 set_state_established (NULL, socket);
2189 LOG (GNUNET_ERROR_TYPE_DEBUG,
2190 "Client sent HELLO_ACK when in state %d\n", socket->state);
2197 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
2199 * @param cls the closure
2200 * @param tunnel connection to the other end
2201 * @param tunnel_ctx the socket
2202 * @param sender who sent the message
2203 * @param message the actual message
2204 * @param atsi performance data for the connection
2205 * @return GNUNET_OK to keep the connection open,
2206 * GNUNET_SYSERR to close it (signal serious error)
2209 server_handle_reset (void *cls,
2210 struct GNUNET_MESH_Tunnel *tunnel,
2212 const struct GNUNET_PeerIdentity *sender,
2213 const struct GNUNET_MessageHeader *message,
2214 const struct GNUNET_ATS_Information*atsi)
2216 // struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2223 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
2225 * @param cls the closure
2226 * @param tunnel connection to the other end
2227 * @param tunnel_ctx the socket
2228 * @param sender who sent the message
2229 * @param message the actual message
2230 * @param atsi performance data for the connection
2231 * @return GNUNET_OK to keep the connection open,
2232 * GNUNET_SYSERR to close it (signal serious error)
2235 server_handle_transmit_close (void *cls,
2236 struct GNUNET_MESH_Tunnel *tunnel,
2238 const struct GNUNET_PeerIdentity *sender,
2239 const struct GNUNET_MessageHeader *message,
2240 const struct GNUNET_ATS_Information*atsi)
2242 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2244 return handle_transmit_close (socket,
2247 (struct GNUNET_STREAM_MessageHeader *)message,
2253 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
2255 * @param cls the closure
2256 * @param tunnel connection to the other end
2257 * @param tunnel_ctx the socket
2258 * @param sender who sent the message
2259 * @param message the actual message
2260 * @param atsi performance data for the connection
2261 * @return GNUNET_OK to keep the connection open,
2262 * GNUNET_SYSERR to close it (signal serious error)
2265 server_handle_transmit_close_ack (void *cls,
2266 struct GNUNET_MESH_Tunnel *tunnel,
2268 const struct GNUNET_PeerIdentity *sender,
2269 const struct GNUNET_MessageHeader *message,
2270 const struct GNUNET_ATS_Information*atsi)
2272 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2274 return handle_generic_close_ack (socket,
2277 (const struct GNUNET_STREAM_MessageHeader *)
2285 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
2287 * @param cls the closure
2288 * @param tunnel connection to the other end
2289 * @param tunnel_ctx the socket
2290 * @param sender who sent the message
2291 * @param message the actual message
2292 * @param atsi performance data for the connection
2293 * @return GNUNET_OK to keep the connection open,
2294 * GNUNET_SYSERR to close it (signal serious error)
2297 server_handle_receive_close (void *cls,
2298 struct GNUNET_MESH_Tunnel *tunnel,
2300 const struct GNUNET_PeerIdentity *sender,
2301 const struct GNUNET_MessageHeader *message,
2302 const struct GNUNET_ATS_Information*atsi)
2304 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2307 handle_receive_close (socket,
2310 (const struct GNUNET_STREAM_MessageHeader *) message,
2316 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
2318 * @param cls the closure
2319 * @param tunnel connection to the other end
2320 * @param tunnel_ctx the socket
2321 * @param sender who sent the message
2322 * @param message the actual message
2323 * @param atsi performance data for the connection
2324 * @return GNUNET_OK to keep the connection open,
2325 * GNUNET_SYSERR to close it (signal serious error)
2328 server_handle_receive_close_ack (void *cls,
2329 struct GNUNET_MESH_Tunnel *tunnel,
2331 const struct GNUNET_PeerIdentity *sender,
2332 const struct GNUNET_MessageHeader *message,
2333 const struct GNUNET_ATS_Information*atsi)
2335 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2337 return handle_generic_close_ack (socket,
2340 (const struct GNUNET_STREAM_MessageHeader *)
2348 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2350 * @param cls the listen socket (from GNUNET_MESH_connect in
2351 * GNUNET_STREAM_listen)
2352 * @param tunnel connection to the other end
2353 * @param tunnel_ctx the socket
2354 * @param sender who sent the message
2355 * @param message the actual message
2356 * @param atsi performance data for the connection
2357 * @return GNUNET_OK to keep the connection open,
2358 * GNUNET_SYSERR to close it (signal serious error)
2361 server_handle_close (void *cls,
2362 struct GNUNET_MESH_Tunnel *tunnel,
2364 const struct GNUNET_PeerIdentity *sender,
2365 const struct GNUNET_MessageHeader *message,
2366 const struct GNUNET_ATS_Information*atsi)
2368 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2370 return handle_close (socket,
2373 (const struct GNUNET_STREAM_MessageHeader *) message,
2379 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2381 * @param cls the closure
2382 * @param tunnel connection to the other end
2383 * @param tunnel_ctx the socket
2384 * @param sender who sent the message
2385 * @param message the actual message
2386 * @param atsi performance data for the connection
2387 * @return GNUNET_OK to keep the connection open,
2388 * GNUNET_SYSERR to close it (signal serious error)
2391 server_handle_close_ack (void *cls,
2392 struct GNUNET_MESH_Tunnel *tunnel,
2394 const struct GNUNET_PeerIdentity *sender,
2395 const struct GNUNET_MessageHeader *message,
2396 const struct GNUNET_ATS_Information*atsi)
2398 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2400 return handle_generic_close_ack (socket,
2403 (const struct GNUNET_STREAM_MessageHeader *)
2411 * Handler for DATA_ACK messages
2413 * @param socket the socket through which the ack was received
2414 * @param tunnel connection to the other end
2415 * @param sender who sent the message
2416 * @param ack the acknowledgment message
2417 * @param atsi performance data for the connection
2418 * @return GNUNET_OK to keep the connection open,
2419 * GNUNET_SYSERR to close it (signal serious error)
2422 handle_ack (struct GNUNET_STREAM_Socket *socket,
2423 struct GNUNET_MESH_Tunnel *tunnel,
2424 const struct GNUNET_PeerIdentity *sender,
2425 const struct GNUNET_STREAM_AckMessage *ack,
2426 const struct GNUNET_ATS_Information*atsi)
2428 unsigned int packet;
2429 int need_retransmission;
2430 uint32_t sequence_difference;
2432 if (0 != memcmp (sender,
2433 &socket->other_peer,
2434 sizeof (struct GNUNET_PeerIdentity)))
2436 LOG (GNUNET_ERROR_TYPE_DEBUG,
2437 "%s: Received ACK from non-confirming peer\n",
2438 GNUNET_i2s (&socket->other_peer));
2441 switch (socket->state)
2443 case (STATE_ESTABLISHED):
2444 case (STATE_RECEIVE_CLOSED):
2445 case (STATE_RECEIVE_CLOSE_WAIT):
2446 if (NULL == socket->write_handle)
2448 LOG (GNUNET_ERROR_TYPE_DEBUG,
2449 "%s: Received DATA_ACK when write_handle is NULL\n",
2450 GNUNET_i2s (&socket->other_peer));
2453 if (!((socket->write_sequence_number
2454 - ntohl (ack->base_sequence_number)) < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
2456 LOG (GNUNET_ERROR_TYPE_DEBUG,
2457 "%s: Received DATA_ACK with unexpected base sequence number\n",
2458 GNUNET_i2s (&socket->other_peer));
2459 LOG (GNUNET_ERROR_TYPE_DEBUG,
2460 "%s: Current write sequence: %u; Ack's base sequence: %u\n",
2461 GNUNET_i2s (&socket->other_peer),
2462 socket->write_sequence_number,
2463 ntohl (ack->base_sequence_number));
2466 /* FIXME: include the case when write_handle is cancelled - ignore the
2468 LOG (GNUNET_ERROR_TYPE_DEBUG,
2469 "%s: Received DATA_ACK from %s\n",
2470 GNUNET_i2s (&socket->other_peer),
2471 GNUNET_i2s (&socket->other_peer));
2473 /* Cancel the retransmission task */
2474 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
2476 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
2477 socket->data_retransmission_task_id =
2478 GNUNET_SCHEDULER_NO_TASK;
2480 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2482 if (NULL == socket->write_handle->messages[packet]) break;
2483 /* BS: Base sequence from ack; PS: sequence num of current packet */
2484 sequence_difference = ntohl (ack->base_sequence_number)
2485 - ntohl (socket->write_handle->messages[packet]->sequence_number);
2486 /* case where BS = PS + GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
2487 if ((sequence_difference == GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
2488 || ((sequence_difference < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
2489 && (0 != sequence_difference))) /* case: BS > PS and BS != PS*/
2491 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap, packet,
2496 ackbitmap_is_bit_set (&socket->write_handle->ack_bitmap,
2497 -sequence_difference))/*inversion as PS >= BS */
2499 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap, packet,
2503 /* Update the receive window remaining
2504 FIXME : Should update with the value from a data ack with greater
2506 socket->receiver_window_available =
2507 ntohl (ack->receive_window_remaining);
2508 /* Check if we have received all acknowledgements */
2509 need_retransmission = GNUNET_NO;
2510 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2512 if (NULL == socket->write_handle->messages[packet]) break;
2513 if (GNUNET_YES != ackbitmap_is_bit_set
2514 (&socket->write_handle->ack_bitmap,packet))
2516 need_retransmission = GNUNET_YES;
2520 if (GNUNET_YES == need_retransmission)
2522 write_data (socket);
2524 else /* We have to call the write continuation callback now */
2526 struct GNUNET_STREAM_IOWriteHandle *write_handle;
2528 /* Free the packets */
2529 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2531 GNUNET_free_non_null (socket->write_handle->messages[packet]);
2533 write_handle = socket->write_handle;
2534 socket->write_handle = NULL;
2535 if (NULL != write_handle->write_cont)
2536 write_handle->write_cont (write_handle->write_cont_cls,
2538 write_handle->size);
2539 /* We are done with the write handle - Freeing it */
2540 GNUNET_free (write_handle);
2541 LOG (GNUNET_ERROR_TYPE_DEBUG,
2542 "%s: Write completion callback completed\n",
2543 GNUNET_i2s (&socket->other_peer));
2554 * Handler for DATA_ACK messages
2556 * @param cls the 'struct GNUNET_STREAM_Socket'
2557 * @param tunnel connection to the other end
2558 * @param tunnel_ctx unused
2559 * @param sender who sent the message
2560 * @param message the actual message
2561 * @param atsi performance data for the connection
2562 * @return GNUNET_OK to keep the connection open,
2563 * GNUNET_SYSERR to close it (signal serious error)
2566 client_handle_ack (void *cls,
2567 struct GNUNET_MESH_Tunnel *tunnel,
2569 const struct GNUNET_PeerIdentity *sender,
2570 const struct GNUNET_MessageHeader *message,
2571 const struct GNUNET_ATS_Information*atsi)
2573 struct GNUNET_STREAM_Socket *socket = cls;
2574 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2576 return handle_ack (socket, tunnel, sender, ack, atsi);
2581 * Handler for DATA_ACK messages
2583 * @param cls the server's listen socket
2584 * @param tunnel connection to the other end
2585 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2586 * @param sender who sent the message
2587 * @param message the actual message
2588 * @param atsi performance data for the connection
2589 * @return GNUNET_OK to keep the connection open,
2590 * GNUNET_SYSERR to close it (signal serious error)
2593 server_handle_ack (void *cls,
2594 struct GNUNET_MESH_Tunnel *tunnel,
2596 const struct GNUNET_PeerIdentity *sender,
2597 const struct GNUNET_MessageHeader *message,
2598 const struct GNUNET_ATS_Information*atsi)
2600 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2601 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2603 return handle_ack (socket, tunnel, sender, ack, atsi);
2608 * For client message handlers, the stream socket is in the
2611 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2612 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2613 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2614 sizeof (struct GNUNET_STREAM_AckMessage) },
2615 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2616 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2617 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2618 sizeof (struct GNUNET_STREAM_MessageHeader)},
2619 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2620 sizeof (struct GNUNET_STREAM_MessageHeader)},
2621 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2622 sizeof (struct GNUNET_STREAM_MessageHeader)},
2623 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2624 sizeof (struct GNUNET_STREAM_MessageHeader)},
2625 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2626 sizeof (struct GNUNET_STREAM_MessageHeader)},
2627 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2628 sizeof (struct GNUNET_STREAM_MessageHeader)},
2629 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2630 sizeof (struct GNUNET_STREAM_MessageHeader)},
2636 * For server message handlers, the stream socket is in the
2637 * tunnel context, and the listen socket in the closure argument.
2639 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2640 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2641 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2642 sizeof (struct GNUNET_STREAM_AckMessage) },
2643 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2644 sizeof (struct GNUNET_STREAM_MessageHeader)},
2645 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2646 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2647 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2648 sizeof (struct GNUNET_STREAM_MessageHeader)},
2649 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2650 sizeof (struct GNUNET_STREAM_MessageHeader)},
2651 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2652 sizeof (struct GNUNET_STREAM_MessageHeader)},
2653 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2654 sizeof (struct GNUNET_STREAM_MessageHeader)},
2655 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2656 sizeof (struct GNUNET_STREAM_MessageHeader)},
2657 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2658 sizeof (struct GNUNET_STREAM_MessageHeader)},
2659 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2660 sizeof (struct GNUNET_STREAM_MessageHeader)},
2666 * Function called when our target peer is connected to our tunnel
2668 * @param cls the socket for which this tunnel is created
2669 * @param peer the peer identity of the target
2670 * @param atsi performance data for the connection
2673 mesh_peer_connect_callback (void *cls,
2674 const struct GNUNET_PeerIdentity *peer,
2675 const struct GNUNET_ATS_Information * atsi)
2677 struct GNUNET_STREAM_Socket *socket = cls;
2678 struct GNUNET_STREAM_MessageHeader *message;
2680 if (0 != memcmp (peer,
2681 &socket->other_peer,
2682 sizeof (struct GNUNET_PeerIdentity)))
2684 LOG (GNUNET_ERROR_TYPE_DEBUG,
2685 "%s: A peer which is not our target has connected to our tunnel\n",
2689 LOG (GNUNET_ERROR_TYPE_DEBUG,
2690 "%s: Target peer %s connected\n",
2691 GNUNET_i2s (&socket->other_peer),
2692 GNUNET_i2s (&socket->other_peer));
2693 /* Set state to INIT */
2694 socket->state = STATE_INIT;
2695 /* Send HELLO message */
2696 message = generate_hello ();
2697 queue_message (socket, message, &set_state_hello_wait, NULL, GNUNET_NO);
2698 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
2699 socket->control_retransmission_task_id);
2700 socket->control_retransmission_task_id =
2701 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2702 &control_retransmission_task, socket);
2707 * Function called when our target peer is disconnected from our tunnel
2709 * @param cls the socket associated which this tunnel
2710 * @param peer the peer identity of the target
2713 mesh_peer_disconnect_callback (void *cls,
2714 const struct GNUNET_PeerIdentity *peer)
2716 struct GNUNET_STREAM_Socket *socket=cls;
2718 /* If the state is SHUTDOWN its ok; else set the state of the socket to SYSERR */
2719 LOG (GNUNET_ERROR_TYPE_DEBUG,
2720 "%s: Other peer %s disconnected \n",
2721 GNUNET_i2s (&socket->other_peer),
2722 GNUNET_i2s (&socket->other_peer));
2727 * Method called whenever a peer creates a tunnel to us
2729 * @param cls closure
2730 * @param tunnel new handle to the tunnel
2731 * @param initiator peer that started the tunnel
2732 * @param atsi performance information for the tunnel
2733 * @return initial tunnel context for the tunnel
2734 * (can be NULL -- that's not an error)
2737 new_tunnel_notify (void *cls,
2738 struct GNUNET_MESH_Tunnel *tunnel,
2739 const struct GNUNET_PeerIdentity *initiator,
2740 const struct GNUNET_ATS_Information *atsi)
2742 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2743 struct GNUNET_STREAM_Socket *socket;
2745 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2746 from the same peer again until the socket is closed */
2748 if (GNUNET_NO == lsocket->listening)
2750 GNUNET_MESH_tunnel_destroy (tunnel);
2753 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2754 socket->other_peer = *initiator;
2755 socket->tunnel = tunnel;
2756 socket->session_id = 0; /* FIXME */
2757 socket->state = STATE_INIT;
2758 socket->lsocket = lsocket;
2759 socket->retransmit_timeout = lsocket->retransmit_timeout;
2760 socket->testing_active = lsocket->testing_active;
2761 socket->testing_set_write_sequence_number_value =
2762 lsocket->testing_set_write_sequence_number_value;
2763 socket->max_payload_size = lsocket->max_payload_size;
2764 LOG (GNUNET_ERROR_TYPE_DEBUG,
2765 "%s: Peer %s initiated tunnel to us\n",
2766 GNUNET_i2s (&socket->other_peer),
2767 GNUNET_i2s (&socket->other_peer));
2768 /* FIXME: Copy MESH handle from lsocket to socket */
2774 * Function called whenever an inbound tunnel is destroyed. Should clean up
2775 * any associated state. This function is NOT called if the client has
2776 * explicitly asked for the tunnel to be destroyed using
2777 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2780 * @param cls closure (set from GNUNET_MESH_connect)
2781 * @param tunnel connection to the other end (henceforth invalid)
2782 * @param tunnel_ctx place where local state associated
2783 * with the tunnel is stored
2786 tunnel_cleaner (void *cls,
2787 const struct GNUNET_MESH_Tunnel *tunnel,
2790 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2792 if (tunnel != socket->tunnel)
2796 LOG (GNUNET_ERROR_TYPE_DEBUG,
2797 "%s: Peer %s has terminated connection abruptly\n",
2798 GNUNET_i2s (&socket->other_peer),
2799 GNUNET_i2s (&socket->other_peer));
2801 socket->status = GNUNET_STREAM_SHUTDOWN;
2803 /* Clear Transmit handles */
2804 if (NULL != socket->transmit_handle)
2806 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2807 socket->transmit_handle = NULL;
2809 /* Stop Tasks using socket->tunnel */
2810 if (GNUNET_SCHEDULER_NO_TASK != socket->ack_task_id)
2812 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2813 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2815 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
2817 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
2818 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
2820 /* FIXME: Cancel all other tasks using socket->tunnel */
2821 socket->tunnel = NULL;
2826 * Callback to signal timeout on lockmanager lock acquire
2828 * @param cls the ListenSocket
2829 * @param tc the scheduler task context
2832 lockmanager_acquire_timeout (void *cls,
2833 const struct GNUNET_SCHEDULER_TaskContext *tc)
2835 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2836 GNUNET_STREAM_ListenCallback listen_cb;
2837 void *listen_cb_cls;
2839 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2840 listen_cb = lsocket->listen_cb;
2841 listen_cb_cls = lsocket->listen_cb_cls;
2842 GNUNET_STREAM_listen_close (lsocket);
2843 if (NULL != listen_cb)
2844 listen_cb (listen_cb_cls, NULL, NULL);
2849 * Callback to notify us on the status changes on app_port lock
2851 * @param cls the ListenSocket
2852 * @param domain the domain name of the lock
2853 * @param lock the app_port
2854 * @param status the current status of the lock
2857 lock_status_change_cb (void *cls, const char *domain, uint32_t lock,
2858 enum GNUNET_LOCKMANAGER_Status status)
2860 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2862 GNUNET_assert (lock == (uint32_t) lsocket->port);
2863 if (GNUNET_LOCKMANAGER_SUCCESS == status)
2865 lsocket->listening = GNUNET_YES;
2866 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
2868 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
2869 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2871 if (NULL == lsocket->mesh)
2873 GNUNET_MESH_ApplicationType ports[] = {lsocket->port, 0};
2875 lsocket->mesh = GNUNET_MESH_connect (lsocket->cfg,
2876 lsocket, /* Closure */
2879 server_message_handlers,
2881 GNUNET_assert (NULL != lsocket->mesh);
2882 if (NULL != lsocket->listen_ok_cb)
2884 (void) lsocket->listen_ok_cb ();
2888 if (GNUNET_LOCKMANAGER_RELEASE == status)
2889 lsocket->listening = GNUNET_NO;
2899 * Tries to open a stream to the target peer
2901 * @param cfg configuration to use
2902 * @param target the target peer to which the stream has to be opened
2903 * @param app_port the application port number which uniquely identifies this
2905 * @param open_cb this function will be called after stream has be established;
2907 * @param open_cb_cls the closure for open_cb
2908 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2909 * @return if successful it returns the stream socket; NULL if stream cannot be
2912 struct GNUNET_STREAM_Socket *
2913 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2914 const struct GNUNET_PeerIdentity *target,
2915 GNUNET_MESH_ApplicationType app_port,
2916 GNUNET_STREAM_OpenCallback open_cb,
2920 struct GNUNET_STREAM_Socket *socket;
2921 enum GNUNET_STREAM_Option option;
2922 GNUNET_MESH_ApplicationType ports[] = {app_port, 0};
2924 uint16_t payload_size;
2926 LOG (GNUNET_ERROR_TYPE_DEBUG,
2928 GNUNET_assert (NULL != open_cb);
2929 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2930 socket->other_peer = *target;
2931 socket->open_cb = open_cb;
2932 socket->open_cls = open_cb_cls;
2934 socket->retransmit_timeout = TIME_REL_SECS (default_timeout);
2935 socket->testing_active = GNUNET_NO;
2936 socket->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
2937 va_start (vargs, open_cb_cls); /* Parse variable args */
2939 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2942 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2943 /* Expect struct GNUNET_TIME_Relative */
2944 socket->retransmit_timeout = va_arg (vargs,
2945 struct GNUNET_TIME_Relative);
2947 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
2948 socket->testing_active = GNUNET_YES;
2949 socket->testing_set_write_sequence_number_value = va_arg (vargs,
2952 case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
2953 GNUNET_break (0); /* Option irrelevant in STREAM_open */
2955 case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
2956 GNUNET_break (0); /* Option irrelevant in STREAM_open */
2958 case GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE:
2959 payload_size = (uint16_t) va_arg (vargs, unsigned int);
2960 GNUNET_assert (0 != payload_size);
2961 if (payload_size < socket->max_payload_size)
2962 socket->max_payload_size = payload_size;
2964 case GNUNET_STREAM_OPTION_END:
2967 } while (GNUNET_STREAM_OPTION_END != option);
2968 va_end (vargs); /* End of variable args parsing */
2969 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2971 NULL, /* No inbound tunnel handler */
2972 NULL, /* No in-tunnel cleaner */
2973 client_message_handlers,
2974 ports); /* We don't get inbound tunnels */
2975 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2977 GNUNET_free (socket);
2980 /* Now create the mesh tunnel to target */
2981 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating MESH Tunnel\n");
2982 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2983 NULL, /* Tunnel context */
2984 &mesh_peer_connect_callback,
2985 &mesh_peer_disconnect_callback,
2987 GNUNET_assert (NULL != socket->tunnel);
2988 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2989 &socket->other_peer);
2990 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
2996 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
2998 * @param socket the stream socket
2999 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
3000 * @param completion_cb the callback that will be called upon successful
3001 * shutdown of given operation
3002 * @param completion_cls the closure for the completion callback
3003 * @return the shutdown handle
3005 struct GNUNET_STREAM_ShutdownHandle *
3006 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
3008 GNUNET_STREAM_ShutdownCompletion completion_cb,
3009 void *completion_cls)
3011 struct GNUNET_STREAM_ShutdownHandle *handle;
3012 struct GNUNET_STREAM_MessageHeader *msg;
3014 GNUNET_assert (NULL == socket->shutdown_handle);
3016 handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle));
3017 handle->socket = socket;
3018 handle->completion_cb = completion_cb;
3019 handle->completion_cls = completion_cls;
3020 socket->shutdown_handle = handle;
3022 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
3023 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
3027 handle->operation = SHUT_RD;
3028 if (NULL != socket->read_handle)
3029 LOG (GNUNET_ERROR_TYPE_WARNING,
3030 "Existing read handle should be cancelled before shutting"
3032 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
3033 queue_message (socket, msg, &set_state_receive_close_wait, NULL,
3037 handle->operation = SHUT_WR;
3038 if (NULL != socket->write_handle)
3039 LOG (GNUNET_ERROR_TYPE_WARNING,
3040 "Existing write handle should be cancelled before shutting"
3042 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
3043 queue_message (socket, msg, &set_state_transmit_close_wait, NULL,
3047 handle->operation = SHUT_RDWR;
3048 if (NULL != socket->write_handle)
3049 LOG (GNUNET_ERROR_TYPE_WARNING,
3050 "Existing write handle should be cancelled before shutting"
3052 if (NULL != socket->read_handle)
3053 LOG (GNUNET_ERROR_TYPE_WARNING,
3054 "Existing read handle should be cancelled before shutting"
3056 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
3057 queue_message (socket, msg, &set_state_close_wait, NULL, GNUNET_NO);
3060 LOG (GNUNET_ERROR_TYPE_WARNING,
3061 "GNUNET_STREAM_shutdown called with invalid value for "
3062 "parameter operation -- Ignoring\n");
3064 GNUNET_free (handle);
3067 handle->close_msg_retransmission_task_id =
3068 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
3069 &close_msg_retransmission_task,
3076 * Cancels a pending shutdown
3078 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
3081 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle)
3083 if (GNUNET_SCHEDULER_NO_TASK != handle->close_msg_retransmission_task_id)
3084 GNUNET_SCHEDULER_cancel (handle->close_msg_retransmission_task_id);
3085 handle->socket->shutdown_handle = NULL;
3086 GNUNET_free (handle);
3093 * @param socket the stream socket
3096 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
3098 struct MessageQueue *head;
3100 if (NULL != socket->read_handle)
3102 LOG (GNUNET_ERROR_TYPE_WARNING,
3103 "Closing STREAM socket when a read handle is pending\n");
3104 GNUNET_STREAM_io_read_cancel (socket->read_handle);
3106 if (NULL != socket->write_handle)
3108 LOG (GNUNET_ERROR_TYPE_WARNING,
3109 "Closing STREAM socket when a write handle is pending\n");
3110 GNUNET_STREAM_io_write_cancel (socket->write_handle);
3111 //socket->write_handle = NULL;
3113 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
3115 /* socket closed with read task pending!? */
3117 GNUNET_SCHEDULER_cancel (socket->read_task_id);
3118 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
3120 /* Terminate the ack'ing tasks if they are still present */
3121 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
3123 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
3124 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
3126 /* Terminate the control retransmission tasks */
3127 if (GNUNET_SCHEDULER_NO_TASK != socket->control_retransmission_task_id)
3129 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
3131 /* Clear Transmit handles */
3132 if (NULL != socket->transmit_handle)
3134 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
3135 socket->transmit_handle = NULL;
3137 /* Clear existing message queue */
3138 while (NULL != (head = socket->queue_head)) {
3139 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
3142 GNUNET_free (head->message);
3145 /* Close associated tunnel */
3146 if (NULL != socket->tunnel)
3148 GNUNET_MESH_tunnel_destroy (socket->tunnel);
3149 socket->tunnel = NULL;
3151 /* Close mesh connection */
3152 if (NULL != socket->mesh && NULL == socket->lsocket)
3154 GNUNET_MESH_disconnect (socket->mesh);
3155 socket->mesh = NULL;
3157 /* Release receive buffer */
3158 if (NULL != socket->receive_buffer)
3160 GNUNET_free (socket->receive_buffer);
3162 GNUNET_free (socket);
3167 * Listens for stream connections for a specific application ports
3169 * @param cfg the configuration to use
3170 * @param app_port the application port for which new streams will be accepted
3171 * @param listen_cb this function will be called when a peer tries to establish
3173 * @param listen_cb_cls closure for listen_cb
3174 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
3175 * @return listen socket, NULL for any error
3177 struct GNUNET_STREAM_ListenSocket *
3178 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
3179 GNUNET_MESH_ApplicationType app_port,
3180 GNUNET_STREAM_ListenCallback listen_cb,
3181 void *listen_cb_cls,
3184 /* FIXME: Add variable args for passing configration options? */
3185 struct GNUNET_STREAM_ListenSocket *lsocket;
3186 struct GNUNET_TIME_Relative listen_timeout;
3187 enum GNUNET_STREAM_Option option;
3189 uint16_t payload_size;
3191 GNUNET_assert (NULL != listen_cb);
3192 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
3193 lsocket->cfg = GNUNET_CONFIGURATION_dup (cfg);
3194 lsocket->lockmanager = GNUNET_LOCKMANAGER_connect (lsocket->cfg);
3195 if (NULL == lsocket->lockmanager)
3197 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3198 GNUNET_free (lsocket);
3201 lsocket->listening = GNUNET_NO;/* We listen when we get a lock on app_port */
3203 lsocket->retransmit_timeout = TIME_REL_SECS (default_timeout);
3204 lsocket->testing_active = GNUNET_NO;
3205 lsocket->listen_ok_cb = NULL;
3206 lsocket->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
3207 listen_timeout = TIME_REL_SECS (60); /* A minute for listen timeout */
3208 va_start (vargs, listen_cb_cls);
3210 option = va_arg (vargs, enum GNUNET_STREAM_Option);
3213 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
3214 lsocket->retransmit_timeout = va_arg (vargs,
3215 struct GNUNET_TIME_Relative);
3217 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
3218 lsocket->testing_active = GNUNET_YES;
3219 lsocket->testing_set_write_sequence_number_value = va_arg (vargs,
3222 case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
3223 listen_timeout = GNUNET_TIME_relative_multiply
3224 (GNUNET_TIME_UNIT_MILLISECONDS, va_arg (vargs, uint32_t));
3226 case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
3227 lsocket->listen_ok_cb = va_arg (vargs,
3228 GNUNET_STREAM_ListenSuccessCallback);
3230 case GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE:
3231 payload_size = (uint16_t) va_arg (vargs, unsigned int);
3232 GNUNET_assert (0 != payload_size);
3233 if (payload_size < lsocket->max_payload_size)
3234 lsocket->max_payload_size = payload_size;
3236 case GNUNET_STREAM_OPTION_END:
3239 } while (GNUNET_STREAM_OPTION_END != option);
3241 lsocket->port = app_port;
3242 lsocket->listen_cb = listen_cb;
3243 lsocket->listen_cb_cls = listen_cb_cls;
3244 lsocket->locking_request =
3245 GNUNET_LOCKMANAGER_acquire_lock (lsocket->lockmanager, locking_domain,
3246 (uint32_t) lsocket->port,
3247 &lock_status_change_cb, lsocket);
3248 lsocket->lockmanager_acquire_timeout_task =
3249 GNUNET_SCHEDULER_add_delayed (listen_timeout,
3250 &lockmanager_acquire_timeout, lsocket);
3256 * Closes the listen socket
3258 * @param lsocket the listen socket
3261 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
3263 /* Close MESH connection */
3264 if (NULL != lsocket->mesh)
3265 GNUNET_MESH_disconnect (lsocket->mesh);
3266 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3267 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
3268 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
3269 if (NULL != lsocket->locking_request)
3270 GNUNET_LOCKMANAGER_cancel_request (lsocket->locking_request);
3271 if (NULL != lsocket->lockmanager)
3272 GNUNET_LOCKMANAGER_disconnect (lsocket->lockmanager);
3273 GNUNET_free (lsocket);
3278 * Tries to write the given data to the stream. The maximum size of data that
3279 * can be written as part of a write operation is (64 * (64000 - sizeof (struct
3280 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
3281 * violation, however only the said number of maximum bytes will be written.
3283 * @param socket the socket representing a stream
3284 * @param data the data buffer from where the data is written into the stream
3285 * @param size the number of bytes to be written from the data buffer
3286 * @param timeout the timeout period
3287 * @param write_cont the function to call upon writing some bytes into the
3289 * @param write_cont_cls the closure
3291 * @return handle to cancel the operation; if a previous write is pending or
3292 * the stream has been shutdown for this operation then write_cont is
3293 * immediately called and NULL is returned.
3295 struct GNUNET_STREAM_IOWriteHandle *
3296 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
3299 struct GNUNET_TIME_Relative timeout,
3300 GNUNET_STREAM_CompletionContinuation write_cont,
3301 void *write_cont_cls)
3303 struct GNUNET_STREAM_IOWriteHandle *io_handle;
3304 struct GNUNET_STREAM_DataMessage *data_msg;
3306 struct GNUNET_TIME_Relative ack_deadline;
3307 unsigned int num_needed_packets;
3308 unsigned int packet;
3309 uint32_t packet_size;
3310 uint32_t payload_size;
3311 uint16_t max_data_packet_size;
3313 LOG (GNUNET_ERROR_TYPE_DEBUG,
3315 if (NULL != socket->write_handle)
3320 switch (socket->state)
3322 case STATE_TRANSMIT_CLOSED:
3323 case STATE_TRANSMIT_CLOSE_WAIT:
3325 case STATE_CLOSE_WAIT:
3326 if (NULL != write_cont)
3327 write_cont (write_cont_cls, GNUNET_STREAM_SHUTDOWN, 0);
3328 LOG (GNUNET_ERROR_TYPE_DEBUG,
3329 "%s() END\n", __func__);
3333 case STATE_HELLO_WAIT:
3334 if (NULL != write_cont)
3335 /* FIXME: GNUNET_STREAM_SYSERR?? */
3336 write_cont (write_cont_cls, GNUNET_STREAM_SYSERR, 0);
3337 LOG (GNUNET_ERROR_TYPE_DEBUG,
3338 "%s() END\n", __func__);
3340 case STATE_ESTABLISHED:
3341 case STATE_RECEIVE_CLOSED:
3342 case STATE_RECEIVE_CLOSE_WAIT:
3345 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * socket->max_payload_size < size)
3346 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * socket->max_payload_size;
3347 num_needed_packets =
3348 (size + (socket->max_payload_size - 1)) / socket->max_payload_size;
3349 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
3350 io_handle->socket = socket;
3351 io_handle->write_cont = write_cont;
3352 io_handle->write_cont_cls = write_cont_cls;
3353 io_handle->size = size;
3355 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3356 determined from RTT */
3357 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
3358 /* Divide the given buffer into packets for sending */
3359 max_data_packet_size =
3360 socket->max_payload_size + sizeof (struct GNUNET_STREAM_DataMessage);
3361 for (packet=0; packet < num_needed_packets; packet++)
3363 if ((packet + 1) * socket->max_payload_size < size)
3365 payload_size = socket->max_payload_size;
3366 packet_size = max_data_packet_size;
3370 payload_size = size - packet * socket->max_payload_size;
3372 payload_size + sizeof (struct GNUNET_STREAM_DataMessage);
3374 io_handle->messages[packet] = GNUNET_malloc (packet_size);
3375 io_handle->messages[packet]->header.header.size = htons (packet_size);
3376 io_handle->messages[packet]->header.header.type =
3377 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
3378 io_handle->messages[packet]->sequence_number =
3379 htonl (socket->write_sequence_number++);
3380 io_handle->messages[packet]->offset = htonl (socket->write_offset);
3381 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3382 determined from RTT */
3383 io_handle->messages[packet]->ack_deadline =
3384 GNUNET_TIME_relative_hton (ack_deadline);
3385 data_msg = io_handle->messages[packet];
3386 /* Copy data from given buffer to the packet */
3387 memcpy (&data_msg[1], sweep, payload_size);
3388 sweep += payload_size;
3389 socket->write_offset += payload_size;
3391 /* ack the last data message. FIXME: remove when we figure out how to do this
3393 io_handle->messages[num_needed_packets - 1]->ack_deadline =
3394 GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO);
3395 socket->write_handle = io_handle;
3396 write_data (socket);
3397 LOG (GNUNET_ERROR_TYPE_DEBUG,
3398 "%s() END\n", __func__);
3404 * Tries to read data from the stream.
3406 * @param socket the socket representing a stream
3407 * @param timeout the timeout period
3408 * @param proc function to call with data (once only)
3409 * @param proc_cls the closure for proc
3411 * @return handle to cancel the operation; if the stream has been shutdown for
3412 * this type of opeartion then the DataProcessor is immediately
3413 * called with GNUNET_STREAM_SHUTDOWN as status and NULL if returned
3415 struct GNUNET_STREAM_IOReadHandle *
3416 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
3417 struct GNUNET_TIME_Relative timeout,
3418 GNUNET_STREAM_DataProcessor proc,
3421 struct GNUNET_STREAM_IOReadHandle *read_handle;
3423 LOG (GNUNET_ERROR_TYPE_DEBUG,
3425 GNUNET_i2s (&socket->other_peer),
3427 /* Return NULL if there is already a read handle; the user has to cancel that
3428 first before continuing or has to wait until it is completed */
3429 if (NULL != socket->read_handle)
3431 GNUNET_assert (NULL != proc);
3432 switch (socket->state)
3434 case STATE_RECEIVE_CLOSED:
3435 case STATE_RECEIVE_CLOSE_WAIT:
3437 case STATE_CLOSE_WAIT:
3438 proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
3439 LOG (GNUNET_ERROR_TYPE_DEBUG,
3441 GNUNET_i2s (&socket->other_peer),
3447 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
3448 read_handle->proc = proc;
3449 read_handle->proc_cls = proc_cls;
3450 read_handle->socket = socket;
3451 socket->read_handle = read_handle;
3452 /* Check if we have a packet at bitmap 0 */
3453 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
3456 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
3459 /* Setup the read timeout task */
3460 socket->read_io_timeout_task_id =
3461 GNUNET_SCHEDULER_add_delayed (timeout,
3464 LOG (GNUNET_ERROR_TYPE_DEBUG,
3466 GNUNET_i2s (&socket->other_peer),
3473 * Cancel pending write operation.
3475 * @param ioh handle to operation to cancel
3478 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
3480 struct GNUNET_STREAM_Socket *socket = ioh->socket;
3481 unsigned int packet;
3483 GNUNET_assert (NULL != socket->write_handle);
3484 GNUNET_assert (socket->write_handle == ioh);
3486 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
3488 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
3489 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
3492 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
3494 if (NULL == ioh->messages[packet]) break;
3495 GNUNET_free (ioh->messages[packet]);
3498 GNUNET_free (socket->write_handle);
3499 socket->write_handle = NULL;
3504 * Cancel pending read operation.
3506 * @param ioh handle to operation to cancel
3509 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)
3511 struct GNUNET_STREAM_Socket *socket;
3513 socket = ioh->socket;
3514 GNUNET_assert (NULL != socket->read_handle);
3515 GNUNET_assert (ioh == socket->read_handle);
3516 /* Read io time task should be there; if it is already executed then this
3517 read handle is not valid */
3518 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != socket->read_io_timeout_task_id);
3519 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task_id);
3520 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
3521 /* reading task may be present; if so we have to stop it */
3522 if (GNUNET_SCHEDULER_NO_TASK != socket->read_task_id)
3524 GNUNET_SCHEDULER_cancel (socket->read_task_id);
3525 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
3528 socket->read_handle = NULL;
3531 /* end of stream_api.c */