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 MAX_PACKET_SIZE 512//64000
71 #define RECEIVE_BUFFER_SIZE 4096000
74 * The maximum payload a data message packet can carry
76 static const size_t max_payload_size =
77 MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
80 * states in the Protocol
85 * Client initialization state
90 * Listener initialization state
95 * Pre-connection establishment state
100 * State where a connection has been established
105 * State where the socket is closed on our side and waiting to be ACK'ed
107 STATE_RECEIVE_CLOSE_WAIT,
110 * State where the socket is closed for reading
112 STATE_RECEIVE_CLOSED,
115 * State where the socket is closed on our side and waiting to be ACK'ed
117 STATE_TRANSMIT_CLOSE_WAIT,
120 * State where the socket is closed for writing
122 STATE_TRANSMIT_CLOSED,
125 * State where the socket is closed on our side and waiting to be ACK'ed
130 * State where the socket is closed
137 * Functions of this type are called when a message is written
139 * @param cls the closure from queue_message
140 * @param socket the socket the written message was bound to
142 typedef void (*SendFinishCallback) (void *cls,
143 struct GNUNET_STREAM_Socket *socket);
147 * The send message queue
154 struct GNUNET_STREAM_MessageHeader *message;
157 * Callback to be called when the message is sent
159 SendFinishCallback finish_cb;
162 * The closure for finish_cb
167 * The next message in queue. Should be NULL in the last message
169 struct MessageQueue *next;
172 * The next message in queue. Should be NULL in the first message
174 struct MessageQueue *prev;
179 * The STREAM Socket Handler
181 struct GNUNET_STREAM_Socket
184 * Retransmission timeout
186 struct GNUNET_TIME_Relative retransmit_timeout;
189 * The Acknowledgement Bitmap
191 GNUNET_STREAM_AckBitmap ack_bitmap;
194 * Time when the Acknowledgement was queued
196 struct GNUNET_TIME_Absolute ack_time_registered;
199 * Queued Acknowledgement deadline
201 struct GNUNET_TIME_Relative ack_time_deadline;
206 struct GNUNET_MESH_Handle *mesh;
209 * The mesh tunnel handle
211 struct GNUNET_MESH_Tunnel *tunnel;
214 * Stream open closure
219 * Stream open callback
221 GNUNET_STREAM_OpenCallback open_cb;
224 * The current transmit handle (if a pending transmit request exists)
226 struct GNUNET_MESH_TransmitHandle *transmit_handle;
229 * The current message associated with the transmit handle
231 struct MessageQueue *queue_head;
234 * The queue tail, should always point to the last message in queue
236 struct MessageQueue *queue_tail;
239 * The write IO_handle associated with this socket
241 struct GNUNET_STREAM_IOWriteHandle *write_handle;
244 * The read IO_handle associated with this socket
246 struct GNUNET_STREAM_IOReadHandle *read_handle;
249 * The shutdown handle associated with this socket
251 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
254 * Buffer for storing received messages
256 void *receive_buffer;
259 * The listen socket from which this socket is derived. Should be NULL if it
260 * is not a derived socket
262 struct GNUNET_STREAM_ListenSocket *lsocket;
265 * The peer identity of the peer at the other end of the stream
267 struct GNUNET_PeerIdentity other_peer;
270 * Task identifier for the read io timeout task
272 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
275 * Task identifier for retransmission task after timeout
277 GNUNET_SCHEDULER_TaskIdentifier data_retransmission_task_id;
280 * Task identifier for retransmission of control messages
282 GNUNET_SCHEDULER_TaskIdentifier control_retransmission_task_id;
285 * The task for sending timely Acks
287 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
290 * Task scheduled to continue a read operation.
292 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
295 * The state of the protocol associated with this socket
300 * The status of the socket
302 enum GNUNET_STREAM_Status status;
305 * The number of previous timeouts; FIXME: currently not used
307 unsigned int retries;
310 * The application port number (type: uint32_t)
312 GNUNET_MESH_ApplicationType app_port;
315 * Whether testing mode is active or not
320 * The write sequence number to be set incase of testing
322 uint32_t testing_set_write_sequence_number_value;
325 * The session id associated with this stream connection
326 * FIXME: Not used currently, may be removed
331 * Write sequence number. Set to random when sending HELLO(client) and
334 uint32_t write_sequence_number;
337 * Read sequence number. This number's value is determined during handshake
339 uint32_t read_sequence_number;
342 * The receiver buffer size
344 uint32_t receive_buffer_size;
347 * The receiver buffer boundaries
349 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
352 * receiver's available buffer after the last acknowledged packet
354 uint32_t receiver_window_available;
357 * The offset pointer used during write operation
359 uint32_t write_offset;
362 * The offset after which we are expecting data
364 uint32_t read_offset;
367 * The offset upto which user has read from the received buffer
369 uint32_t copy_offset;
374 * A socket for listening
376 struct GNUNET_STREAM_ListenSocket
381 struct GNUNET_MESH_Handle *mesh;
386 struct GNUNET_CONFIGURATION_Handle *cfg;
389 * Handle to the lock manager service
391 struct GNUNET_LOCKMANAGER_Handle *lockmanager;
394 * The active LockingRequest from lockmanager
396 struct GNUNET_LOCKMANAGER_LockingRequest *locking_request;
399 * Callback to call after acquring a lock and listening
401 GNUNET_STREAM_ListenSuccessCallback listen_ok_cb;
404 * The callback function which is called after successful opening socket
406 GNUNET_STREAM_ListenCallback listen_cb;
409 * The call back closure
416 GNUNET_MESH_ApplicationType port;
419 * The id of the lockmanager timeout task
421 GNUNET_SCHEDULER_TaskIdentifier lockmanager_acquire_timeout_task;
424 * The retransmit timeout
426 struct GNUNET_TIME_Relative retransmit_timeout;
434 * Whether testing mode is active or not
439 * The write sequence number to be set incase of testing
441 uint32_t testing_set_write_sequence_number_value;
446 * The IO Write Handle
448 struct GNUNET_STREAM_IOWriteHandle
451 * The socket to which this write handle is associated
453 struct GNUNET_STREAM_Socket *socket;
456 * The packet_buffers associated with this Handle
458 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
461 * The write continuation callback
463 GNUNET_STREAM_CompletionContinuation write_cont;
466 * Write continuation closure
468 void *write_cont_cls;
471 * The bitmap of this IOHandle; Corresponding bit for a message is set when
472 * it has been acknowledged by the receiver
474 GNUNET_STREAM_AckBitmap ack_bitmap;
477 * Number of bytes in this write handle
486 struct GNUNET_STREAM_IOReadHandle
489 * The socket to which this read handle is associated
491 struct GNUNET_STREAM_Socket *socket;
494 * Callback for the read processor
496 GNUNET_STREAM_DataProcessor proc;
499 * The closure pointer for the read processor callback
506 * Handle for Shutdown
508 struct GNUNET_STREAM_ShutdownHandle
511 * The socket associated with this shutdown handle
513 struct GNUNET_STREAM_Socket *socket;
516 * Shutdown completion callback
518 GNUNET_STREAM_ShutdownCompletion completion_cb;
521 * Closure for completion callback
523 void *completion_cls;
526 * Close message retransmission task id
528 GNUNET_SCHEDULER_TaskIdentifier close_msg_retransmission_task_id;
531 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
538 * Default value in seconds for various timeouts
540 static const unsigned int default_timeout = 10;
543 * The domain name for locks we use here
545 static const char *locking_domain = "GNUNET_STREAM_APPLOCK";
549 * Callback function for sending queued message
551 * @param cls closure the socket
552 * @param size number of bytes available in buf
553 * @param buf where the callee should write the message
554 * @return number of bytes written to buf
557 send_message_notify (void *cls, size_t size, void *buf)
559 struct GNUNET_STREAM_Socket *socket = cls;
560 struct MessageQueue *head;
563 socket->transmit_handle = NULL; /* Remove the transmit handle */
564 head = socket->queue_head;
566 return 0; /* just to be safe */
567 if (0 == size) /* request timed out */
570 LOG (GNUNET_ERROR_TYPE_DEBUG,
571 "%s: Message sending timed out. Retry %d \n",
572 GNUNET_i2s (&socket->other_peer),
574 socket->transmit_handle =
575 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
576 GNUNET_NO, /* Corking */
577 /* FIXME: exponential backoff */
578 socket->retransmit_timeout,
580 ntohs (head->message->header.size),
581 &send_message_notify,
585 ret = ntohs (head->message->header.size);
586 GNUNET_assert (size >= ret);
587 memcpy (buf, head->message, ret);
588 if (NULL != head->finish_cb)
590 head->finish_cb (head->finish_cb_cls, socket);
592 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
595 GNUNET_free (head->message);
597 head = socket->queue_head;
598 if (NULL != head) /* more pending messages to send */
601 socket->transmit_handle =
602 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
603 GNUNET_NO, /* Corking */
604 /* FIXME: exponential backoff */
605 socket->retransmit_timeout,
607 ntohs (head->message->header.size),
608 &send_message_notify,
616 * Queues a message for sending using the mesh connection of a socket
618 * @param socket the socket whose mesh connection is used
619 * @param message the message to be sent
620 * @param finish_cb the callback to be called when the message is sent
621 * @param finish_cb_cls the closure for the callback
622 * @param urgent set to GNUNET_YES to add the message to the beginning of the
623 * queue; GNUNET_NO to add at the tail
626 queue_message (struct GNUNET_STREAM_Socket *socket,
627 struct GNUNET_STREAM_MessageHeader *message,
628 SendFinishCallback finish_cb,
632 struct MessageQueue *queue_entity;
635 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
636 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
637 LOG (GNUNET_ERROR_TYPE_DEBUG,
638 "%s: Queueing message of type %d and size %d\n",
639 GNUNET_i2s (&socket->other_peer),
640 ntohs (message->header.type),
641 ntohs (message->header.size));
642 GNUNET_assert (NULL != message);
643 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
644 queue_entity->message = message;
645 queue_entity->finish_cb = finish_cb;
646 queue_entity->finish_cb_cls = finish_cb_cls;
647 if (GNUNET_YES == urgent)
649 GNUNET_CONTAINER_DLL_insert (socket->queue_head, socket->queue_tail,
651 if (NULL != socket->transmit_handle)
653 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
654 socket->transmit_handle = NULL;
658 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
661 if (NULL == socket->transmit_handle)
664 socket->transmit_handle =
665 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
666 GNUNET_NO, /* Corking */
667 socket->retransmit_timeout,
669 ntohs (message->header.size),
670 &send_message_notify,
677 * Copies a message and queues it for sending using the mesh connection of
680 * @param socket the socket whose mesh connection is used
681 * @param message the message to be sent
682 * @param finish_cb the callback to be called when the message is sent
683 * @param finish_cb_cls the closure for the callback
686 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
687 const struct GNUNET_STREAM_MessageHeader *message,
688 SendFinishCallback finish_cb,
691 struct GNUNET_STREAM_MessageHeader *msg_copy;
694 size = ntohs (message->header.size);
695 msg_copy = GNUNET_malloc (size);
696 memcpy (msg_copy, message, size);
697 queue_message (socket, msg_copy, finish_cb, finish_cb_cls, GNUNET_NO);
702 * Writes data using the given socket. The amount of data written is limited by
703 * the receiver_window_size
705 * @param socket the socket to use
708 write_data (struct GNUNET_STREAM_Socket *socket);
712 * Task for retransmitting data messages if they aren't ACK before their ack
715 * @param cls the socket
716 * @param tc the Task context
719 data_retransmission_task (void *cls,
720 const struct GNUNET_SCHEDULER_TaskContext *tc)
722 struct GNUNET_STREAM_Socket *socket = cls;
724 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
726 LOG (GNUNET_ERROR_TYPE_DEBUG,
727 "%s: Retransmitting DATA...\n", GNUNET_i2s (&socket->other_peer));
728 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
734 * Task for sending ACK message
736 * @param cls the socket
737 * @param tc the Task context
741 const struct GNUNET_SCHEDULER_TaskContext *tc)
743 struct GNUNET_STREAM_Socket *socket = cls;
744 struct GNUNET_STREAM_AckMessage *ack_msg;
746 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
750 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
751 /* Create the ACK Message */
752 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
753 ack_msg->header.header.size = htons (sizeof (struct
754 GNUNET_STREAM_AckMessage));
755 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
756 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
757 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
758 ack_msg->receive_window_remaining =
759 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
760 /* Queue up ACK for immediate sending */
761 queue_message (socket, &ack_msg->header, NULL, NULL, GNUNET_YES);
766 * Retransmission task for shutdown messages
768 * @param cls the shutdown handle
769 * @param tc the Task Context
772 close_msg_retransmission_task (void *cls,
773 const struct GNUNET_SCHEDULER_TaskContext *tc)
775 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle = cls;
776 struct GNUNET_STREAM_MessageHeader *msg;
777 struct GNUNET_STREAM_Socket *socket;
779 GNUNET_assert (NULL != shutdown_handle);
780 socket = shutdown_handle->socket;
782 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
783 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
784 switch (shutdown_handle->operation)
787 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
790 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
793 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
797 shutdown_handle->close_msg_retransmission_task_id =
798 GNUNET_SCHEDULER_NO_TASK;
801 queue_message (socket, msg, NULL, NULL, GNUNET_NO);
802 shutdown_handle->close_msg_retransmission_task_id =
803 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
804 &close_msg_retransmission_task,
810 * Function to modify a bit in GNUNET_STREAM_AckBitmap
812 * @param bitmap the bitmap to modify
813 * @param bit the bit number to modify
814 * @param value GNUNET_YES to on, GNUNET_NO to off
817 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
821 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
822 if (GNUNET_YES == value)
823 *bitmap |= (1LL << bit);
825 *bitmap &= ~(1LL << bit);
830 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
832 * @param bitmap address of the bitmap that has to be checked
833 * @param bit the bit number to check
834 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
837 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
840 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
841 return 0 != (*bitmap & (1LL << bit));
846 * Writes data using the given socket. The amount of data written is limited by
847 * the receiver_window_size
849 * @param socket the socket to use
852 write_data (struct GNUNET_STREAM_Socket *socket)
854 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
855 int packet; /* Although an int, should never be negative */
859 /* Find the last acknowledged packet */
860 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
862 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
865 else if (NULL == io_handle->messages[packet])
868 /* Resend packets which weren't ack'ed */
869 for (packet=0; packet < ack_packet; packet++)
871 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
874 LOG (GNUNET_ERROR_TYPE_DEBUG,
875 "%s: Placing DATA message with sequence %u in send queue\n",
876 GNUNET_i2s (&socket->other_peer),
877 ntohl (io_handle->messages[packet]->sequence_number));
878 copy_and_queue_message (socket,
879 &io_handle->messages[packet]->header,
884 packet = ack_packet + 1;
885 /* Now send new packets if there is enough buffer space */
886 while ( (NULL != io_handle->messages[packet]) &&
887 (socket->receiver_window_available
888 >= ntohs (io_handle->messages[packet]->header.header.size)) &&
889 (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
891 socket->receiver_window_available -=
892 ntohs (io_handle->messages[packet]->header.header.size);
893 LOG (GNUNET_ERROR_TYPE_DEBUG,
894 "%s: Placing DATA message with sequence %u in send queue\n",
895 GNUNET_i2s (&socket->other_peer),
896 ntohl (io_handle->messages[packet]->sequence_number));
897 copy_and_queue_message (socket,
898 &io_handle->messages[packet]->header,
903 if (GNUNET_SCHEDULER_NO_TASK == socket->data_retransmission_task_id)
904 socket->data_retransmission_task_id =
905 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
906 (GNUNET_TIME_UNIT_SECONDS, 8),
907 &data_retransmission_task,
913 * Task for calling the read processor
915 * @param cls the socket
916 * @param tc the task context
919 call_read_processor (void *cls,
920 const struct GNUNET_SCHEDULER_TaskContext *tc)
922 struct GNUNET_STREAM_Socket *socket = cls;
924 size_t valid_read_size;
926 uint32_t sequence_increase;
927 uint32_t offset_increase;
929 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
930 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
933 if (NULL == socket->receive_buffer)
936 GNUNET_assert (NULL != socket->read_handle);
937 GNUNET_assert (NULL != socket->read_handle->proc);
939 /* Check the bitmap for any holes */
940 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
942 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
946 /* We only call read processor if we have the first packet */
947 GNUNET_assert (0 < packet);
949 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
950 GNUNET_assert (0 != valid_read_size);
951 /* Cancel the read_io_timeout_task */
952 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task_id);
953 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
954 /* Call the data processor */
955 LOG (GNUNET_ERROR_TYPE_DEBUG,
956 "%s: Calling read processor\n",
957 GNUNET_i2s (&socket->other_peer));
959 socket->read_handle->proc (socket->read_handle->proc_cls,
961 socket->receive_buffer + socket->copy_offset,
963 LOG (GNUNET_ERROR_TYPE_DEBUG,
964 "%s: Read processor read %d bytes\n",
965 GNUNET_i2s (&socket->other_peer), read_size);
966 LOG (GNUNET_ERROR_TYPE_DEBUG,
967 "%s: Read processor completed successfully\n",
968 GNUNET_i2s (&socket->other_peer));
969 /* Free the read handle */
970 GNUNET_free (socket->read_handle);
971 socket->read_handle = NULL;
972 GNUNET_assert (read_size <= valid_read_size);
973 socket->copy_offset += read_size;
974 /* Determine upto which packet we can remove from the buffer */
975 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
977 if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
979 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
983 /* If no packets can be removed we can't move the buffer */
984 if (0 == packet) return;
985 sequence_increase = packet;
986 LOG (GNUNET_ERROR_TYPE_DEBUG,
987 "%s: Sequence increase after read processor completion: %u\n",
988 GNUNET_i2s (&socket->other_peer), sequence_increase);
990 /* Shift the data in the receive buffer */
991 socket->receive_buffer =
992 memmove (socket->receive_buffer,
993 socket->receive_buffer
994 + socket->receive_buffer_boundaries[sequence_increase-1],
995 socket->receive_buffer_size
996 - socket->receive_buffer_boundaries[sequence_increase-1]);
997 /* Shift the bitmap */
998 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
999 /* Set read_sequence_number */
1000 socket->read_sequence_number += sequence_increase;
1001 /* Set read_offset */
1002 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
1003 socket->read_offset += offset_increase;
1004 /* Fix copy_offset */
1005 GNUNET_assert (offset_increase <= socket->copy_offset);
1006 socket->copy_offset -= offset_increase;
1007 /* Fix relative boundaries */
1008 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1010 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
1012 uint32_t ahead_buffer_boundary;
1014 ahead_buffer_boundary =
1015 socket->receive_buffer_boundaries[packet + sequence_increase];
1016 if (0 == ahead_buffer_boundary)
1017 socket->receive_buffer_boundaries[packet] = 0;
1020 GNUNET_assert (offset_increase < ahead_buffer_boundary);
1021 socket->receive_buffer_boundaries[packet] =
1022 ahead_buffer_boundary - offset_increase;
1026 socket->receive_buffer_boundaries[packet] = 0;
1032 * Cancels the existing read io handle
1034 * @param cls the closure from the SCHEDULER call
1035 * @param tc the task context
1038 read_io_timeout (void *cls,
1039 const struct GNUNET_SCHEDULER_TaskContext *tc)
1041 struct GNUNET_STREAM_Socket *socket = cls;
1042 GNUNET_STREAM_DataProcessor proc;
1045 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
1046 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
1048 LOG (GNUNET_ERROR_TYPE_DEBUG,
1049 "%s: Read task timedout - Cancelling it\n",
1050 GNUNET_i2s (&socket->other_peer));
1051 GNUNET_SCHEDULER_cancel (socket->read_task_id);
1052 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
1054 GNUNET_assert (NULL != socket->read_handle);
1055 proc = socket->read_handle->proc;
1056 proc_cls = socket->read_handle->proc_cls;
1057 GNUNET_free (socket->read_handle);
1058 socket->read_handle = NULL;
1059 /* Call the read processor to signal timeout */
1061 GNUNET_STREAM_TIMEOUT,
1068 * Handler for DATA messages; Same for both client and server
1070 * @param socket the socket through which the ack was received
1071 * @param tunnel connection to the other end
1072 * @param sender who sent the message
1073 * @param msg the data message
1074 * @param atsi performance data for the connection
1075 * @return GNUNET_OK to keep the connection open,
1076 * GNUNET_SYSERR to close it (signal serious error)
1079 handle_data (struct GNUNET_STREAM_Socket *socket,
1080 struct GNUNET_MESH_Tunnel *tunnel,
1081 const struct GNUNET_PeerIdentity *sender,
1082 const struct GNUNET_STREAM_DataMessage *msg,
1083 const struct GNUNET_ATS_Information*atsi)
1085 const void *payload;
1086 uint32_t bytes_needed;
1087 uint32_t relative_offset;
1088 uint32_t relative_sequence_number;
1091 size = htons (msg->header.header.size);
1092 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
1094 GNUNET_break_op (0);
1095 return GNUNET_SYSERR;
1098 if (0 != memcmp (sender,
1099 &socket->other_peer,
1100 sizeof (struct GNUNET_PeerIdentity)))
1102 LOG (GNUNET_ERROR_TYPE_DEBUG,
1103 "%s: Received DATA from non-confirming peer\n",
1104 GNUNET_i2s (&socket->other_peer));
1108 switch (socket->state)
1110 case STATE_ESTABLISHED:
1111 case STATE_TRANSMIT_CLOSED:
1112 case STATE_TRANSMIT_CLOSE_WAIT:
1114 /* check if the message's sequence number is in the range we are
1116 relative_sequence_number =
1117 ntohl (msg->sequence_number) - socket->read_sequence_number;
1118 if ( relative_sequence_number > GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1120 LOG (GNUNET_ERROR_TYPE_DEBUG,
1121 "%s: Ignoring received message with sequence number %u\n",
1122 GNUNET_i2s (&socket->other_peer),
1123 ntohl (msg->sequence_number));
1124 /* Start ACK sending task if one is not already present */
1125 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1127 socket->ack_task_id =
1128 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1129 (msg->ack_deadline),
1136 /* Check if we have already seen this message */
1137 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1138 relative_sequence_number))
1140 LOG (GNUNET_ERROR_TYPE_DEBUG,
1141 "%s: Ignoring already received message with sequence number %u\n",
1142 GNUNET_i2s (&socket->other_peer),
1143 ntohl (msg->sequence_number));
1144 /* Start ACK sending task if one is not already present */
1145 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1147 socket->ack_task_id =
1148 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1149 (msg->ack_deadline),
1156 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 "%s: Receiving DATA with sequence number: %u and size: %d from %s\n",
1158 GNUNET_i2s (&socket->other_peer),
1159 ntohl (msg->sequence_number),
1160 ntohs (msg->header.header.size),
1161 GNUNET_i2s (&socket->other_peer));
1163 /* Check if we have to allocate the buffer */
1164 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1165 relative_offset = ntohl (msg->offset) - socket->read_offset;
1166 bytes_needed = relative_offset + size;
1167 if (bytes_needed > socket->receive_buffer_size)
1169 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1171 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1173 socket->receive_buffer_size = bytes_needed;
1177 LOG (GNUNET_ERROR_TYPE_DEBUG,
1178 "%s: Cannot accommodate packet %d as buffer is full\n",
1179 GNUNET_i2s (&socket->other_peer),
1180 ntohl (msg->sequence_number));
1185 /* Copy Data to buffer */
1187 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1188 memcpy (socket->receive_buffer + relative_offset,
1191 socket->receive_buffer_boundaries[relative_sequence_number] =
1192 relative_offset + size;
1194 /* Modify the ACK bitmap */
1195 ackbitmap_modify_bit (&socket->ack_bitmap,
1196 relative_sequence_number,
1199 /* Start ACK sending task if one is not already present */
1200 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1202 socket->ack_task_id =
1203 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1204 (msg->ack_deadline),
1209 if ((NULL != socket->read_handle) /* A read handle is waiting */
1210 /* There is no current read task */
1211 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
1212 /* We have the first packet */
1213 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
1216 LOG (GNUNET_ERROR_TYPE_DEBUG,
1217 "%s: Scheduling read processor\n",
1218 GNUNET_i2s (&socket->other_peer));
1220 socket->read_task_id =
1221 GNUNET_SCHEDULER_add_now (&call_read_processor,
1228 LOG (GNUNET_ERROR_TYPE_DEBUG,
1229 "%s: Received data message when it cannot be handled\n",
1230 GNUNET_i2s (&socket->other_peer));
1238 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1240 * @param cls the socket (set from GNUNET_MESH_connect)
1241 * @param tunnel connection to the other end
1242 * @param tunnel_ctx place to store local state associated with the tunnel
1243 * @param sender who sent the message
1244 * @param message the actual message
1245 * @param atsi performance data for the connection
1246 * @return GNUNET_OK to keep the connection open,
1247 * GNUNET_SYSERR to close it (signal serious error)
1250 client_handle_data (void *cls,
1251 struct GNUNET_MESH_Tunnel *tunnel,
1253 const struct GNUNET_PeerIdentity *sender,
1254 const struct GNUNET_MessageHeader *message,
1255 const struct GNUNET_ATS_Information*atsi)
1257 struct GNUNET_STREAM_Socket *socket = cls;
1259 return handle_data (socket,
1262 (const struct GNUNET_STREAM_DataMessage *) message,
1268 * Callback to set state to ESTABLISHED
1270 * @param cls the closure NULL;
1271 * @param socket the socket to requiring state change
1274 set_state_established (void *cls,
1275 struct GNUNET_STREAM_Socket *socket)
1277 LOG (GNUNET_ERROR_TYPE_DEBUG,
1278 "%s: Attaining ESTABLISHED state\n",
1279 GNUNET_i2s (&socket->other_peer));
1280 socket->write_offset = 0;
1281 socket->read_offset = 0;
1282 socket->state = STATE_ESTABLISHED;
1283 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
1284 socket->control_retransmission_task_id);
1285 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
1286 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
1287 if (NULL != socket->lsocket)
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 "%s: Calling listen callback\n",
1291 GNUNET_i2s (&socket->other_peer));
1292 if (GNUNET_SYSERR ==
1293 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1295 &socket->other_peer))
1297 socket->state = STATE_CLOSED;
1298 /* FIXME: We should close in a decent way (send RST) */
1299 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1300 GNUNET_free (socket);
1304 socket->open_cb (socket->open_cls, socket);
1309 * Callback to set state to HELLO_WAIT
1311 * @param cls the closure from queue_message
1312 * @param socket the socket to requiring state change
1315 set_state_hello_wait (void *cls,
1316 struct GNUNET_STREAM_Socket *socket)
1318 GNUNET_assert (STATE_INIT == socket->state);
1319 LOG (GNUNET_ERROR_TYPE_DEBUG,
1320 "%s: Attaining HELLO_WAIT state\n",
1321 GNUNET_i2s (&socket->other_peer));
1322 socket->state = STATE_HELLO_WAIT;
1327 * Callback to set state to CLOSE_WAIT
1329 * @param cls the closure from queue_message
1330 * @param socket the socket requiring state change
1333 set_state_close_wait (void *cls,
1334 struct GNUNET_STREAM_Socket *socket)
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "%s: Attaing CLOSE_WAIT state\n",
1338 GNUNET_i2s (&socket->other_peer));
1339 socket->state = STATE_CLOSE_WAIT;
1340 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1341 socket->receive_buffer = NULL;
1342 socket->receive_buffer_size = 0;
1347 * Callback to set state to RECEIVE_CLOSE_WAIT
1349 * @param cls the closure from queue_message
1350 * @param socket the socket requiring state change
1353 set_state_receive_close_wait (void *cls,
1354 struct GNUNET_STREAM_Socket *socket)
1356 LOG (GNUNET_ERROR_TYPE_DEBUG,
1357 "%s: Attaing RECEIVE_CLOSE_WAIT state\n",
1358 GNUNET_i2s (&socket->other_peer));
1359 socket->state = STATE_RECEIVE_CLOSE_WAIT;
1360 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1361 socket->receive_buffer = NULL;
1362 socket->receive_buffer_size = 0;
1367 * Callback to set state to TRANSMIT_CLOSE_WAIT
1369 * @param cls the closure from queue_message
1370 * @param socket the socket requiring state change
1373 set_state_transmit_close_wait (void *cls,
1374 struct GNUNET_STREAM_Socket *socket)
1376 LOG (GNUNET_ERROR_TYPE_DEBUG,
1377 "%s: Attaing TRANSMIT_CLOSE_WAIT state\n",
1378 GNUNET_i2s (&socket->other_peer));
1379 socket->state = STATE_TRANSMIT_CLOSE_WAIT;
1384 * Callback to set state to CLOSED
1386 * @param cls the closure from queue_message
1387 * @param socket the socket requiring state change
1390 set_state_closed (void *cls,
1391 struct GNUNET_STREAM_Socket *socket)
1393 socket->state = STATE_CLOSED;
1398 * Returns GNUNET_MESSAGE_TYPE_STREAM_HELLO
1400 * @return the generate hello message
1402 static struct GNUNET_STREAM_MessageHeader *
1403 generate_hello (void)
1405 struct GNUNET_STREAM_MessageHeader *msg;
1407 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1408 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
1409 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1415 * Returns a new HelloAckMessage. Also sets the write sequence number for the
1418 * @param socket the socket for which this HelloAckMessage has to be generated
1419 * @param generate_seq GNUNET_YES to generate the write sequence number,
1420 * GNUNET_NO to use the existing sequence number
1421 * @return the HelloAckMessage
1423 static struct GNUNET_STREAM_HelloAckMessage *
1424 generate_hello_ack (struct GNUNET_STREAM_Socket *socket,
1427 struct GNUNET_STREAM_HelloAckMessage *msg;
1429 if (GNUNET_YES == generate_seq)
1431 if (GNUNET_YES == socket->testing_active)
1432 socket->write_sequence_number =
1433 socket->testing_set_write_sequence_number_value;
1435 socket->write_sequence_number =
1436 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1437 LOG_DEBUG ("%s: write sequence number %u\n",
1438 GNUNET_i2s (&socket->other_peer),
1439 (unsigned int) socket->write_sequence_number);
1441 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1442 msg->header.header.size =
1443 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1444 msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1445 msg->sequence_number = htonl (socket->write_sequence_number);
1446 msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1452 * Task for retransmitting control messages if they aren't ACK'ed before a
1455 * @param cls the socket
1456 * @param tc the Task context
1459 control_retransmission_task (void *cls,
1460 const struct GNUNET_SCHEDULER_TaskContext *tc)
1462 struct GNUNET_STREAM_Socket *socket = cls;
1464 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
1466 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
1467 LOG_DEBUG ("%s: Retransmitting a control message\n",
1468 GNUNET_i2s (&socket->other_peer));
1469 switch (socket->status)
1477 case STATE_HELLO_WAIT:
1478 if (NULL == socket->lsocket) /* We are client */
1479 queue_message (socket, generate_hello (), NULL, NULL, GNUNET_NO);
1481 queue_message (socket,
1482 (struct GNUNET_STREAM_MessageHeader *)
1483 generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
1485 socket->control_retransmission_task_id =
1486 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
1487 &control_retransmission_task, socket);
1489 case STATE_ESTABLISHED:
1490 if (NULL == socket->lsocket)
1491 queue_message (socket,
1492 (struct GNUNET_STREAM_MessageHeader *)
1493 generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
1504 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1506 * @param cls the socket (set from GNUNET_MESH_connect)
1507 * @param tunnel connection to the other end
1508 * @param tunnel_ctx this is NULL
1509 * @param sender who sent the message
1510 * @param message the actual message
1511 * @param atsi performance data for the connection
1512 * @return GNUNET_OK to keep the connection open,
1513 * GNUNET_SYSERR to close it (signal serious error)
1516 client_handle_hello_ack (void *cls,
1517 struct GNUNET_MESH_Tunnel *tunnel,
1519 const struct GNUNET_PeerIdentity *sender,
1520 const struct GNUNET_MessageHeader *message,
1521 const struct GNUNET_ATS_Information*atsi)
1523 struct GNUNET_STREAM_Socket *socket = cls;
1524 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1525 struct GNUNET_STREAM_HelloAckMessage *reply;
1527 if (0 != memcmp (sender,
1528 &socket->other_peer,
1529 sizeof (struct GNUNET_PeerIdentity)))
1531 LOG (GNUNET_ERROR_TYPE_DEBUG,
1532 "%s: Received HELLO_ACK from non-confirming peer\n",
1533 GNUNET_i2s (&socket->other_peer));
1536 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1537 LOG (GNUNET_ERROR_TYPE_DEBUG,
1538 "%s: Received HELLO_ACK from %s\n",
1539 GNUNET_i2s (&socket->other_peer),
1540 GNUNET_i2s (&socket->other_peer));
1542 GNUNET_assert (socket->tunnel == tunnel);
1543 switch (socket->state)
1545 case STATE_HELLO_WAIT:
1546 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1547 LOG (GNUNET_ERROR_TYPE_DEBUG,
1548 "%s: Read sequence number %u\n",
1549 GNUNET_i2s (&socket->other_peer),
1550 (unsigned int) socket->read_sequence_number);
1551 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1552 reply = generate_hello_ack (socket, GNUNET_YES);
1553 queue_message (socket, &reply->header, &set_state_established,
1556 case STATE_ESTABLISHED:
1557 // call statistics (# ACKs ignored++)
1558 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
1559 socket->control_retransmission_task_id);
1560 socket->control_retransmission_task_id =
1561 GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
1564 LOG_DEBUG ("%s: Server %s sent HELLO_ACK when in state %d\n",
1565 GNUNET_i2s (&socket->other_peer),
1566 GNUNET_i2s (&socket->other_peer),
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;
1625 /* Send TRANSMIT_CLOSE_ACK */
1626 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1627 reply->header.type =
1628 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1629 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1630 queue_message (socket, reply, NULL, NULL, GNUNET_NO);
1634 /* FIXME: Call statistics? */
1642 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1644 * @param cls the socket (set from GNUNET_MESH_connect)
1645 * @param tunnel connection to the other end
1646 * @param tunnel_ctx this is NULL
1647 * @param sender who sent the message
1648 * @param message the actual message
1649 * @param atsi performance data for the connection
1650 * @return GNUNET_OK to keep the connection open,
1651 * GNUNET_SYSERR to close it (signal serious error)
1654 client_handle_transmit_close (void *cls,
1655 struct GNUNET_MESH_Tunnel *tunnel,
1657 const struct GNUNET_PeerIdentity *sender,
1658 const struct GNUNET_MessageHeader *message,
1659 const struct GNUNET_ATS_Information*atsi)
1661 struct GNUNET_STREAM_Socket *socket = cls;
1663 return handle_transmit_close (socket,
1666 (struct GNUNET_STREAM_MessageHeader *)message,
1672 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_*_CLOSE_ACK messages
1674 * @param socket the socket
1675 * @param tunnel connection to the other end
1676 * @param sender who sent the message
1677 * @param message the actual message
1678 * @param atsi performance data for the connection
1679 * @param operation the close operation which is being ACK'ed
1680 * @return GNUNET_OK to keep the connection open,
1681 * GNUNET_SYSERR to close it (signal serious error)
1684 handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
1685 struct GNUNET_MESH_Tunnel *tunnel,
1686 const struct GNUNET_PeerIdentity *sender,
1687 const struct GNUNET_STREAM_MessageHeader *message,
1688 const struct GNUNET_ATS_Information *atsi,
1691 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
1693 shutdown_handle = socket->shutdown_handle;
1694 if (NULL == shutdown_handle)
1696 LOG (GNUNET_ERROR_TYPE_DEBUG,
1697 "%s: Received CLOSE_ACK when shutdown handle is NULL\n",
1698 GNUNET_i2s (&socket->other_peer));
1705 switch (socket->state)
1707 case STATE_CLOSE_WAIT:
1708 if (SHUT_RDWR != shutdown_handle->operation)
1710 LOG (GNUNET_ERROR_TYPE_DEBUG,
1711 "%s: Received CLOSE_ACK when shutdown handle is not for "
1713 GNUNET_i2s (&socket->other_peer));
1717 LOG (GNUNET_ERROR_TYPE_DEBUG,
1718 "%s: Received CLOSE_ACK from %s\n",
1719 GNUNET_i2s (&socket->other_peer),
1720 GNUNET_i2s (&socket->other_peer));
1721 socket->state = STATE_CLOSED;
1724 LOG (GNUNET_ERROR_TYPE_DEBUG,
1725 "%s: Received CLOSE_ACK when in it not expected\n",
1726 GNUNET_i2s (&socket->other_peer));
1732 switch (socket->state)
1734 case STATE_RECEIVE_CLOSE_WAIT:
1735 if (SHUT_RD != shutdown_handle->operation)
1737 LOG (GNUNET_ERROR_TYPE_DEBUG,
1738 "%s: Received RECEIVE_CLOSE_ACK when shutdown handle "
1739 "is not for SHUT_RD\n",
1740 GNUNET_i2s (&socket->other_peer));
1744 LOG (GNUNET_ERROR_TYPE_DEBUG,
1745 "%s: Received RECEIVE_CLOSE_ACK from %s\n",
1746 GNUNET_i2s (&socket->other_peer),
1747 GNUNET_i2s (&socket->other_peer));
1748 socket->state = STATE_RECEIVE_CLOSED;
1751 LOG (GNUNET_ERROR_TYPE_DEBUG,
1752 "%s: Received RECEIVE_CLOSE_ACK when in it not expected\n",
1753 GNUNET_i2s (&socket->other_peer));
1759 switch (socket->state)
1761 case STATE_TRANSMIT_CLOSE_WAIT:
1762 if (SHUT_WR != shutdown_handle->operation)
1764 LOG (GNUNET_ERROR_TYPE_DEBUG,
1765 "%s: Received TRANSMIT_CLOSE_ACK when shutdown handle "
1766 "is not for SHUT_WR\n",
1767 GNUNET_i2s (&socket->other_peer));
1771 LOG (GNUNET_ERROR_TYPE_DEBUG,
1772 "%s: Received TRANSMIT_CLOSE_ACK from %s\n",
1773 GNUNET_i2s (&socket->other_peer),
1774 GNUNET_i2s (&socket->other_peer));
1775 socket->state = STATE_TRANSMIT_CLOSED;
1778 LOG (GNUNET_ERROR_TYPE_DEBUG,
1779 "%s: Received TRANSMIT_CLOSE_ACK when in it not expected\n",
1780 GNUNET_i2s (&socket->other_peer));
1789 if (NULL != shutdown_handle->completion_cb) /* Shutdown completion */
1790 shutdown_handle->completion_cb(shutdown_handle->completion_cls,
1792 if (GNUNET_SCHEDULER_NO_TASK
1793 != shutdown_handle->close_msg_retransmission_task_id)
1795 GNUNET_SCHEDULER_cancel
1796 (shutdown_handle->close_msg_retransmission_task_id);
1797 shutdown_handle->close_msg_retransmission_task_id =
1798 GNUNET_SCHEDULER_NO_TASK;
1800 GNUNET_free (shutdown_handle); /* Free shutdown handle */
1801 socket->shutdown_handle = NULL;
1807 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1809 * @param cls the socket (set from GNUNET_MESH_connect)
1810 * @param tunnel connection to the other end
1811 * @param tunnel_ctx this is NULL
1812 * @param sender who sent the message
1813 * @param message the actual message
1814 * @param atsi performance data for the connection
1815 * @return GNUNET_OK to keep the connection open,
1816 * GNUNET_SYSERR to close it (signal serious error)
1819 client_handle_transmit_close_ack (void *cls,
1820 struct GNUNET_MESH_Tunnel *tunnel,
1822 const struct GNUNET_PeerIdentity *sender,
1823 const struct GNUNET_MessageHeader *message,
1824 const struct GNUNET_ATS_Information*atsi)
1826 struct GNUNET_STREAM_Socket *socket = cls;
1828 return handle_generic_close_ack (socket,
1831 (const struct GNUNET_STREAM_MessageHeader *)
1839 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1841 * @param socket the socket
1842 * @param tunnel connection to the other end
1843 * @param sender who sent the message
1844 * @param message the actual message
1845 * @param atsi performance data for the connection
1846 * @return GNUNET_OK to keep the connection open,
1847 * GNUNET_SYSERR to close it (signal serious error)
1850 handle_receive_close (struct GNUNET_STREAM_Socket *socket,
1851 struct GNUNET_MESH_Tunnel *tunnel,
1852 const struct GNUNET_PeerIdentity *sender,
1853 const struct GNUNET_STREAM_MessageHeader *message,
1854 const struct GNUNET_ATS_Information *atsi)
1856 struct GNUNET_STREAM_MessageHeader *receive_close_ack;
1858 switch (socket->state)
1862 case STATE_HELLO_WAIT:
1863 LOG (GNUNET_ERROR_TYPE_DEBUG,
1864 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1865 GNUNET_i2s (&socket->other_peer));
1871 LOG (GNUNET_ERROR_TYPE_DEBUG,
1872 "%s: Received RECEIVE_CLOSE from %s\n",
1873 GNUNET_i2s (&socket->other_peer),
1874 GNUNET_i2s (&socket->other_peer));
1876 GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1877 receive_close_ack->header.size =
1878 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1879 receive_close_ack->header.type =
1880 htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK);
1881 queue_message (socket, receive_close_ack, &set_state_closed,
1883 /* FIXME: Handle the case where write handle is present; the write operation
1884 should be deemed as finised and the write continuation callback
1885 has to be called with the stream status GNUNET_STREAM_SHUTDOWN */
1891 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1893 * @param cls the socket (set from GNUNET_MESH_connect)
1894 * @param tunnel connection to the other end
1895 * @param tunnel_ctx this is NULL
1896 * @param sender who sent the message
1897 * @param message the actual message
1898 * @param atsi performance data for the connection
1899 * @return GNUNET_OK to keep the connection open,
1900 * GNUNET_SYSERR to close it (signal serious error)
1903 client_handle_receive_close (void *cls,
1904 struct GNUNET_MESH_Tunnel *tunnel,
1906 const struct GNUNET_PeerIdentity *sender,
1907 const struct GNUNET_MessageHeader *message,
1908 const struct GNUNET_ATS_Information*atsi)
1910 struct GNUNET_STREAM_Socket *socket = cls;
1913 handle_receive_close (socket,
1916 (const struct GNUNET_STREAM_MessageHeader *) message,
1922 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1924 * @param cls the socket (set from GNUNET_MESH_connect)
1925 * @param tunnel connection to the other end
1926 * @param tunnel_ctx this is NULL
1927 * @param sender who sent the message
1928 * @param message the actual message
1929 * @param atsi performance data for the connection
1930 * @return GNUNET_OK to keep the connection open,
1931 * GNUNET_SYSERR to close it (signal serious error)
1934 client_handle_receive_close_ack (void *cls,
1935 struct GNUNET_MESH_Tunnel *tunnel,
1937 const struct GNUNET_PeerIdentity *sender,
1938 const struct GNUNET_MessageHeader *message,
1939 const struct GNUNET_ATS_Information*atsi)
1941 struct GNUNET_STREAM_Socket *socket = cls;
1943 return handle_generic_close_ack (socket,
1946 (const struct GNUNET_STREAM_MessageHeader *)
1954 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1956 * @param socket the socket
1957 * @param tunnel connection to the other end
1958 * @param sender who sent the message
1959 * @param message the actual message
1960 * @param atsi performance data for the connection
1961 * @return GNUNET_OK to keep the connection open,
1962 * GNUNET_SYSERR to close it (signal serious error)
1965 handle_close (struct GNUNET_STREAM_Socket *socket,
1966 struct GNUNET_MESH_Tunnel *tunnel,
1967 const struct GNUNET_PeerIdentity *sender,
1968 const struct GNUNET_STREAM_MessageHeader *message,
1969 const struct GNUNET_ATS_Information*atsi)
1971 struct GNUNET_STREAM_MessageHeader *close_ack;
1973 switch (socket->state)
1977 case STATE_HELLO_WAIT:
1978 LOG (GNUNET_ERROR_TYPE_DEBUG,
1979 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1980 GNUNET_i2s (&socket->other_peer));
1986 LOG (GNUNET_ERROR_TYPE_DEBUG,
1987 "%s: Received CLOSE from %s\n",
1988 GNUNET_i2s (&socket->other_peer),
1989 GNUNET_i2s (&socket->other_peer));
1990 close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1991 close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1992 close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK);
1993 queue_message (socket, close_ack, &set_state_closed, NULL, GNUNET_NO);
1994 if (socket->state == STATE_CLOSED)
1997 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1998 socket->receive_buffer = NULL;
1999 socket->receive_buffer_size = 0;
2005 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2007 * @param cls the socket (set from GNUNET_MESH_connect)
2008 * @param tunnel connection to the other end
2009 * @param tunnel_ctx this is NULL
2010 * @param sender who sent the message
2011 * @param message the actual message
2012 * @param atsi performance data for the connection
2013 * @return GNUNET_OK to keep the connection open,
2014 * GNUNET_SYSERR to close it (signal serious error)
2017 client_handle_close (void *cls,
2018 struct GNUNET_MESH_Tunnel *tunnel,
2020 const struct GNUNET_PeerIdentity *sender,
2021 const struct GNUNET_MessageHeader *message,
2022 const struct GNUNET_ATS_Information*atsi)
2024 struct GNUNET_STREAM_Socket *socket = cls;
2026 return handle_close (socket,
2029 (const struct GNUNET_STREAM_MessageHeader *) message,
2035 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2037 * @param cls the socket (set from GNUNET_MESH_connect)
2038 * @param tunnel connection to the other end
2039 * @param tunnel_ctx this is NULL
2040 * @param sender who sent the message
2041 * @param message the actual message
2042 * @param atsi performance data for the connection
2043 * @return GNUNET_OK to keep the connection open,
2044 * GNUNET_SYSERR to close it (signal serious error)
2047 client_handle_close_ack (void *cls,
2048 struct GNUNET_MESH_Tunnel *tunnel,
2050 const struct GNUNET_PeerIdentity *sender,
2051 const struct GNUNET_MessageHeader *message,
2052 const struct GNUNET_ATS_Information *atsi)
2054 struct GNUNET_STREAM_Socket *socket = cls;
2056 return handle_generic_close_ack (socket,
2059 (const struct GNUNET_STREAM_MessageHeader *)
2065 /*****************************/
2066 /* Server's Message Handlers */
2067 /*****************************/
2070 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
2072 * @param cls the closure
2073 * @param tunnel connection to the other end
2074 * @param tunnel_ctx the socket
2075 * @param sender who sent the message
2076 * @param message the actual message
2077 * @param atsi performance data for the connection
2078 * @return GNUNET_OK to keep the connection open,
2079 * GNUNET_SYSERR to close it (signal serious error)
2082 server_handle_data (void *cls,
2083 struct GNUNET_MESH_Tunnel *tunnel,
2085 const struct GNUNET_PeerIdentity *sender,
2086 const struct GNUNET_MessageHeader *message,
2087 const struct GNUNET_ATS_Information*atsi)
2089 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2091 return handle_data (socket,
2094 (const struct GNUNET_STREAM_DataMessage *)message,
2100 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
2102 * @param cls the closure
2103 * @param tunnel connection to the other end
2104 * @param tunnel_ctx the socket
2105 * @param sender who sent the message
2106 * @param message the actual message
2107 * @param atsi performance data for the connection
2108 * @return GNUNET_OK to keep the connection open,
2109 * GNUNET_SYSERR to close it (signal serious error)
2112 server_handle_hello (void *cls,
2113 struct GNUNET_MESH_Tunnel *tunnel,
2115 const struct GNUNET_PeerIdentity *sender,
2116 const struct GNUNET_MessageHeader *message,
2117 const struct GNUNET_ATS_Information*atsi)
2119 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2120 struct GNUNET_STREAM_HelloAckMessage *reply;
2122 if (0 != memcmp (sender,
2123 &socket->other_peer,
2124 sizeof (struct GNUNET_PeerIdentity)))
2126 LOG_DEBUG ("%s: Received HELLO from non-confirming peer\n",
2127 GNUNET_i2s (&socket->other_peer));
2130 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO == ntohs (message->type));
2131 GNUNET_assert (socket->tunnel == tunnel);
2132 LOG_DEBUG ("%s: Received HELLO from %s\n", GNUNET_i2s (&socket->other_peer),
2133 GNUNET_i2s (&socket->other_peer));
2134 switch (socket->status)
2137 reply = generate_hello_ack (socket, GNUNET_YES);
2138 queue_message (socket, &reply->header, &set_state_hello_wait, NULL,
2140 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
2141 socket->control_retransmission_task_id);
2142 socket->control_retransmission_task_id =
2143 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2144 &control_retransmission_task, socket);
2146 case STATE_HELLO_WAIT:
2147 /* Perhaps our HELLO_ACK was lost */
2148 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
2149 socket->control_retransmission_task_id);
2150 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
2151 socket->control_retransmission_task_id =
2152 GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
2155 LOG_DEBUG( "%s: Client sent HELLO when in state %d\n",
2156 GNUNET_i2s (&socket->other_peer), socket->state);
2157 /* FIXME: Send RESET? */
2164 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
2166 * @param cls the closure
2167 * @param tunnel connection to the other end
2168 * @param tunnel_ctx the socket
2169 * @param sender who sent the message
2170 * @param message the actual message
2171 * @param atsi performance data for the connection
2172 * @return GNUNET_OK to keep the connection open,
2173 * GNUNET_SYSERR to close it (signal serious error)
2176 server_handle_hello_ack (void *cls,
2177 struct GNUNET_MESH_Tunnel *tunnel,
2179 const struct GNUNET_PeerIdentity *sender,
2180 const struct GNUNET_MessageHeader *message,
2181 const struct GNUNET_ATS_Information*atsi)
2183 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2184 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
2186 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
2187 ntohs (message->type));
2188 GNUNET_assert (socket->tunnel == tunnel);
2189 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
2190 switch (socket->state)
2192 case STATE_HELLO_WAIT:
2193 LOG (GNUNET_ERROR_TYPE_DEBUG,
2194 "%s: Received HELLO_ACK from %s\n",
2195 GNUNET_i2s (&socket->other_peer),
2196 GNUNET_i2s (&socket->other_peer));
2197 socket->read_sequence_number = ntohl (ack_message->sequence_number);
2198 LOG (GNUNET_ERROR_TYPE_DEBUG,
2199 "%s: Read sequence number %u\n",
2200 GNUNET_i2s (&socket->other_peer),
2201 (unsigned int) socket->read_sequence_number);
2202 socket->receiver_window_available =
2203 ntohl (ack_message->receiver_window_size);
2204 set_state_established (NULL, socket);
2207 LOG (GNUNET_ERROR_TYPE_DEBUG,
2208 "Client sent HELLO_ACK when in state %d\n", socket->state);
2215 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
2217 * @param cls the closure
2218 * @param tunnel connection to the other end
2219 * @param tunnel_ctx the socket
2220 * @param sender who sent the message
2221 * @param message the actual message
2222 * @param atsi performance data for the connection
2223 * @return GNUNET_OK to keep the connection open,
2224 * GNUNET_SYSERR to close it (signal serious error)
2227 server_handle_reset (void *cls,
2228 struct GNUNET_MESH_Tunnel *tunnel,
2230 const struct GNUNET_PeerIdentity *sender,
2231 const struct GNUNET_MessageHeader *message,
2232 const struct GNUNET_ATS_Information*atsi)
2234 // struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2241 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
2243 * @param cls the closure
2244 * @param tunnel connection to the other end
2245 * @param tunnel_ctx the socket
2246 * @param sender who sent the message
2247 * @param message the actual message
2248 * @param atsi performance data for the connection
2249 * @return GNUNET_OK to keep the connection open,
2250 * GNUNET_SYSERR to close it (signal serious error)
2253 server_handle_transmit_close (void *cls,
2254 struct GNUNET_MESH_Tunnel *tunnel,
2256 const struct GNUNET_PeerIdentity *sender,
2257 const struct GNUNET_MessageHeader *message,
2258 const struct GNUNET_ATS_Information*atsi)
2260 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2262 return handle_transmit_close (socket,
2265 (struct GNUNET_STREAM_MessageHeader *)message,
2271 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
2273 * @param cls the closure
2274 * @param tunnel connection to the other end
2275 * @param tunnel_ctx the socket
2276 * @param sender who sent the message
2277 * @param message the actual message
2278 * @param atsi performance data for the connection
2279 * @return GNUNET_OK to keep the connection open,
2280 * GNUNET_SYSERR to close it (signal serious error)
2283 server_handle_transmit_close_ack (void *cls,
2284 struct GNUNET_MESH_Tunnel *tunnel,
2286 const struct GNUNET_PeerIdentity *sender,
2287 const struct GNUNET_MessageHeader *message,
2288 const struct GNUNET_ATS_Information*atsi)
2290 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2292 return handle_generic_close_ack (socket,
2295 (const struct GNUNET_STREAM_MessageHeader *)
2303 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
2305 * @param cls the closure
2306 * @param tunnel connection to the other end
2307 * @param tunnel_ctx the socket
2308 * @param sender who sent the message
2309 * @param message the actual message
2310 * @param atsi performance data for the connection
2311 * @return GNUNET_OK to keep the connection open,
2312 * GNUNET_SYSERR to close it (signal serious error)
2315 server_handle_receive_close (void *cls,
2316 struct GNUNET_MESH_Tunnel *tunnel,
2318 const struct GNUNET_PeerIdentity *sender,
2319 const struct GNUNET_MessageHeader *message,
2320 const struct GNUNET_ATS_Information*atsi)
2322 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2325 handle_receive_close (socket,
2328 (const struct GNUNET_STREAM_MessageHeader *) message,
2334 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
2336 * @param cls the closure
2337 * @param tunnel connection to the other end
2338 * @param tunnel_ctx the socket
2339 * @param sender who sent the message
2340 * @param message the actual message
2341 * @param atsi performance data for the connection
2342 * @return GNUNET_OK to keep the connection open,
2343 * GNUNET_SYSERR to close it (signal serious error)
2346 server_handle_receive_close_ack (void *cls,
2347 struct GNUNET_MESH_Tunnel *tunnel,
2349 const struct GNUNET_PeerIdentity *sender,
2350 const struct GNUNET_MessageHeader *message,
2351 const struct GNUNET_ATS_Information*atsi)
2353 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2355 return handle_generic_close_ack (socket,
2358 (const struct GNUNET_STREAM_MessageHeader *)
2366 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2368 * @param cls the listen socket (from GNUNET_MESH_connect in
2369 * GNUNET_STREAM_listen)
2370 * @param tunnel connection to the other end
2371 * @param tunnel_ctx the socket
2372 * @param sender who sent the message
2373 * @param message the actual message
2374 * @param atsi performance data for the connection
2375 * @return GNUNET_OK to keep the connection open,
2376 * GNUNET_SYSERR to close it (signal serious error)
2379 server_handle_close (void *cls,
2380 struct GNUNET_MESH_Tunnel *tunnel,
2382 const struct GNUNET_PeerIdentity *sender,
2383 const struct GNUNET_MessageHeader *message,
2384 const struct GNUNET_ATS_Information*atsi)
2386 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2388 return handle_close (socket,
2391 (const struct GNUNET_STREAM_MessageHeader *) message,
2397 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2399 * @param cls the closure
2400 * @param tunnel connection to the other end
2401 * @param tunnel_ctx the socket
2402 * @param sender who sent the message
2403 * @param message the actual message
2404 * @param atsi performance data for the connection
2405 * @return GNUNET_OK to keep the connection open,
2406 * GNUNET_SYSERR to close it (signal serious error)
2409 server_handle_close_ack (void *cls,
2410 struct GNUNET_MESH_Tunnel *tunnel,
2412 const struct GNUNET_PeerIdentity *sender,
2413 const struct GNUNET_MessageHeader *message,
2414 const struct GNUNET_ATS_Information*atsi)
2416 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2418 return handle_generic_close_ack (socket,
2421 (const struct GNUNET_STREAM_MessageHeader *)
2429 * Handler for DATA_ACK messages
2431 * @param socket the socket through which the ack was received
2432 * @param tunnel connection to the other end
2433 * @param sender who sent the message
2434 * @param ack the acknowledgment message
2435 * @param atsi performance data for the connection
2436 * @return GNUNET_OK to keep the connection open,
2437 * GNUNET_SYSERR to close it (signal serious error)
2440 handle_ack (struct GNUNET_STREAM_Socket *socket,
2441 struct GNUNET_MESH_Tunnel *tunnel,
2442 const struct GNUNET_PeerIdentity *sender,
2443 const struct GNUNET_STREAM_AckMessage *ack,
2444 const struct GNUNET_ATS_Information*atsi)
2446 unsigned int packet;
2447 int need_retransmission;
2448 uint32_t sequence_difference;
2450 if (0 != memcmp (sender,
2451 &socket->other_peer,
2452 sizeof (struct GNUNET_PeerIdentity)))
2454 LOG (GNUNET_ERROR_TYPE_DEBUG,
2455 "%s: Received ACK from non-confirming peer\n",
2456 GNUNET_i2s (&socket->other_peer));
2459 switch (socket->state)
2461 case (STATE_ESTABLISHED):
2462 case (STATE_RECEIVE_CLOSED):
2463 case (STATE_RECEIVE_CLOSE_WAIT):
2464 if (NULL == socket->write_handle)
2466 LOG (GNUNET_ERROR_TYPE_DEBUG,
2467 "%s: Received DATA_ACK when write_handle is NULL\n",
2468 GNUNET_i2s (&socket->other_peer));
2471 if (!((socket->write_sequence_number
2472 - ntohl (ack->base_sequence_number)) < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
2474 LOG (GNUNET_ERROR_TYPE_DEBUG,
2475 "%s: Received DATA_ACK with unexpected base sequence number\n",
2476 GNUNET_i2s (&socket->other_peer));
2477 LOG (GNUNET_ERROR_TYPE_DEBUG,
2478 "%s: Current write sequence: %u; Ack's base sequence: %u\n",
2479 GNUNET_i2s (&socket->other_peer),
2480 socket->write_sequence_number,
2481 ntohl (ack->base_sequence_number));
2484 /* FIXME: include the case when write_handle is cancelled - ignore the
2486 LOG (GNUNET_ERROR_TYPE_DEBUG,
2487 "%s: Received DATA_ACK from %s\n",
2488 GNUNET_i2s (&socket->other_peer),
2489 GNUNET_i2s (&socket->other_peer));
2491 /* Cancel the retransmission task */
2492 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
2494 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
2495 socket->data_retransmission_task_id =
2496 GNUNET_SCHEDULER_NO_TASK;
2498 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2500 if (NULL == socket->write_handle->messages[packet]) break;
2501 /* BS: Base sequence from ack; PS: sequence num of current packet */
2502 sequence_difference = ntohl (ack->base_sequence_number)
2503 - ntohl (socket->write_handle->messages[packet]->sequence_number);
2504 /* case where BS = PS + GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
2505 if ((sequence_difference == GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
2506 || ((sequence_difference < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
2507 && (0 != sequence_difference))) /* case: BS > PS and BS != PS*/
2509 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap, packet,
2514 ackbitmap_is_bit_set (&socket->write_handle->ack_bitmap,
2515 -sequence_difference))/*inversion as PS >= BS */
2517 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap, packet,
2521 /* Update the receive window remaining
2522 FIXME : Should update with the value from a data ack with greater
2524 socket->receiver_window_available =
2525 ntohl (ack->receive_window_remaining);
2526 /* Check if we have received all acknowledgements */
2527 need_retransmission = GNUNET_NO;
2528 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2530 if (NULL == socket->write_handle->messages[packet]) break;
2531 if (GNUNET_YES != ackbitmap_is_bit_set
2532 (&socket->write_handle->ack_bitmap,packet))
2534 need_retransmission = GNUNET_YES;
2538 if (GNUNET_YES == need_retransmission)
2540 write_data (socket);
2542 else /* We have to call the write continuation callback now */
2544 struct GNUNET_STREAM_IOWriteHandle *write_handle;
2546 /* Free the packets */
2547 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2549 GNUNET_free_non_null (socket->write_handle->messages[packet]);
2551 write_handle = socket->write_handle;
2552 socket->write_handle = NULL;
2553 if (NULL != write_handle->write_cont)
2554 write_handle->write_cont (write_handle->write_cont_cls,
2556 write_handle->size);
2557 /* We are done with the write handle - Freeing it */
2558 GNUNET_free (write_handle);
2559 LOG (GNUNET_ERROR_TYPE_DEBUG,
2560 "%s: Write completion callback completed\n",
2561 GNUNET_i2s (&socket->other_peer));
2572 * Handler for DATA_ACK messages
2574 * @param cls the 'struct GNUNET_STREAM_Socket'
2575 * @param tunnel connection to the other end
2576 * @param tunnel_ctx unused
2577 * @param sender who sent the message
2578 * @param message the actual message
2579 * @param atsi performance data for the connection
2580 * @return GNUNET_OK to keep the connection open,
2581 * GNUNET_SYSERR to close it (signal serious error)
2584 client_handle_ack (void *cls,
2585 struct GNUNET_MESH_Tunnel *tunnel,
2587 const struct GNUNET_PeerIdentity *sender,
2588 const struct GNUNET_MessageHeader *message,
2589 const struct GNUNET_ATS_Information*atsi)
2591 struct GNUNET_STREAM_Socket *socket = cls;
2592 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2594 return handle_ack (socket, tunnel, sender, ack, atsi);
2599 * Handler for DATA_ACK messages
2601 * @param cls the server's listen socket
2602 * @param tunnel connection to the other end
2603 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2604 * @param sender who sent the message
2605 * @param message the actual message
2606 * @param atsi performance data for the connection
2607 * @return GNUNET_OK to keep the connection open,
2608 * GNUNET_SYSERR to close it (signal serious error)
2611 server_handle_ack (void *cls,
2612 struct GNUNET_MESH_Tunnel *tunnel,
2614 const struct GNUNET_PeerIdentity *sender,
2615 const struct GNUNET_MessageHeader *message,
2616 const struct GNUNET_ATS_Information*atsi)
2618 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2619 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2621 return handle_ack (socket, tunnel, sender, ack, atsi);
2626 * For client message handlers, the stream socket is in the
2629 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2630 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2631 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2632 sizeof (struct GNUNET_STREAM_AckMessage) },
2633 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2634 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2635 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2636 sizeof (struct GNUNET_STREAM_MessageHeader)},
2637 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2638 sizeof (struct GNUNET_STREAM_MessageHeader)},
2639 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2640 sizeof (struct GNUNET_STREAM_MessageHeader)},
2641 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2642 sizeof (struct GNUNET_STREAM_MessageHeader)},
2643 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2644 sizeof (struct GNUNET_STREAM_MessageHeader)},
2645 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2646 sizeof (struct GNUNET_STREAM_MessageHeader)},
2647 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2648 sizeof (struct GNUNET_STREAM_MessageHeader)},
2654 * For server message handlers, the stream socket is in the
2655 * tunnel context, and the listen socket in the closure argument.
2657 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2658 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2659 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2660 sizeof (struct GNUNET_STREAM_AckMessage) },
2661 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2662 sizeof (struct GNUNET_STREAM_MessageHeader)},
2663 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2664 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2665 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2666 sizeof (struct GNUNET_STREAM_MessageHeader)},
2667 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2668 sizeof (struct GNUNET_STREAM_MessageHeader)},
2669 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2670 sizeof (struct GNUNET_STREAM_MessageHeader)},
2671 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2672 sizeof (struct GNUNET_STREAM_MessageHeader)},
2673 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2674 sizeof (struct GNUNET_STREAM_MessageHeader)},
2675 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2676 sizeof (struct GNUNET_STREAM_MessageHeader)},
2677 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2678 sizeof (struct GNUNET_STREAM_MessageHeader)},
2684 * Function called when our target peer is connected to our tunnel
2686 * @param cls the socket for which this tunnel is created
2687 * @param peer the peer identity of the target
2688 * @param atsi performance data for the connection
2691 mesh_peer_connect_callback (void *cls,
2692 const struct GNUNET_PeerIdentity *peer,
2693 const struct GNUNET_ATS_Information * atsi)
2695 struct GNUNET_STREAM_Socket *socket = cls;
2696 struct GNUNET_STREAM_MessageHeader *message;
2698 if (0 != memcmp (peer,
2699 &socket->other_peer,
2700 sizeof (struct GNUNET_PeerIdentity)))
2702 LOG (GNUNET_ERROR_TYPE_DEBUG,
2703 "%s: A peer which is not our target has connected to our tunnel\n",
2707 LOG (GNUNET_ERROR_TYPE_DEBUG,
2708 "%s: Target peer %s connected\n",
2709 GNUNET_i2s (&socket->other_peer),
2710 GNUNET_i2s (&socket->other_peer));
2711 /* Set state to INIT */
2712 socket->state = STATE_INIT;
2713 /* Send HELLO message */
2714 message = generate_hello ();
2715 queue_message (socket, message, &set_state_hello_wait, NULL, GNUNET_NO);
2716 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
2717 socket->control_retransmission_task_id);
2718 socket->control_retransmission_task_id =
2719 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2720 &control_retransmission_task, socket);
2725 * Function called when our target peer is disconnected from our tunnel
2727 * @param cls the socket associated which this tunnel
2728 * @param peer the peer identity of the target
2731 mesh_peer_disconnect_callback (void *cls,
2732 const struct GNUNET_PeerIdentity *peer)
2734 struct GNUNET_STREAM_Socket *socket=cls;
2736 /* If the state is SHUTDOWN its ok; else set the state of the socket to SYSERR */
2737 LOG (GNUNET_ERROR_TYPE_DEBUG,
2738 "%s: Other peer %s disconnected \n",
2739 GNUNET_i2s (&socket->other_peer),
2740 GNUNET_i2s (&socket->other_peer));
2745 * Method called whenever a peer creates a tunnel to us
2747 * @param cls closure
2748 * @param tunnel new handle to the tunnel
2749 * @param initiator peer that started the tunnel
2750 * @param atsi performance information for the tunnel
2751 * @return initial tunnel context for the tunnel
2752 * (can be NULL -- that's not an error)
2755 new_tunnel_notify (void *cls,
2756 struct GNUNET_MESH_Tunnel *tunnel,
2757 const struct GNUNET_PeerIdentity *initiator,
2758 const struct GNUNET_ATS_Information *atsi)
2760 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2761 struct GNUNET_STREAM_Socket *socket;
2763 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2764 from the same peer again until the socket is closed */
2766 if (GNUNET_NO == lsocket->listening)
2768 // FIXME: socket uninitalized
2769 // FIXME: cannot use GNUNET_i2s twice in same call (static buffer)
2770 // LOG (GNUNET_ERROR_TYPE_DEBUG,
2771 // "%s: Destroying tunnel from peer %s as we don't have the lock\n",
2772 // GNUNET_i2s (&socket->other_peer),
2773 // GNUNET_i2s (&socket->other_peer));
2774 GNUNET_MESH_tunnel_destroy (tunnel);
2777 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2778 socket->other_peer = *initiator;
2779 socket->tunnel = tunnel;
2780 socket->session_id = 0; /* FIXME */
2781 socket->state = STATE_INIT;
2782 socket->lsocket = lsocket;
2783 socket->retransmit_timeout = lsocket->retransmit_timeout;
2784 socket->testing_active = lsocket->testing_active;
2785 socket->testing_set_write_sequence_number_value =
2786 lsocket->testing_set_write_sequence_number_value;
2787 LOG (GNUNET_ERROR_TYPE_DEBUG,
2788 "%s: Peer %s initiated tunnel to us\n",
2789 GNUNET_i2s (&socket->other_peer),
2790 GNUNET_i2s (&socket->other_peer));
2791 /* FIXME: Copy MESH handle from lsocket to socket */
2797 * Function called whenever an inbound tunnel is destroyed. Should clean up
2798 * any associated state. This function is NOT called if the client has
2799 * explicitly asked for the tunnel to be destroyed using
2800 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2803 * @param cls closure (set from GNUNET_MESH_connect)
2804 * @param tunnel connection to the other end (henceforth invalid)
2805 * @param tunnel_ctx place where local state associated
2806 * with the tunnel is stored
2809 tunnel_cleaner (void *cls,
2810 const struct GNUNET_MESH_Tunnel *tunnel,
2813 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2815 if (tunnel != socket->tunnel)
2819 LOG (GNUNET_ERROR_TYPE_DEBUG,
2820 "%s: Peer %s has terminated connection abruptly\n",
2821 GNUNET_i2s (&socket->other_peer),
2822 GNUNET_i2s (&socket->other_peer));
2824 socket->status = GNUNET_STREAM_SHUTDOWN;
2826 /* Clear Transmit handles */
2827 if (NULL != socket->transmit_handle)
2829 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2830 socket->transmit_handle = NULL;
2832 /* Stop Tasks using socket->tunnel */
2833 if (GNUNET_SCHEDULER_NO_TASK != socket->ack_task_id)
2835 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2836 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2838 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
2840 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
2841 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
2843 /* FIXME: Cancel all other tasks using socket->tunnel */
2844 socket->tunnel = NULL;
2849 * Callback to signal timeout on lockmanager lock acquire
2851 * @param cls the ListenSocket
2852 * @param tc the scheduler task context
2855 lockmanager_acquire_timeout (void *cls,
2856 const struct GNUNET_SCHEDULER_TaskContext *tc)
2858 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2859 GNUNET_STREAM_ListenCallback listen_cb;
2860 void *listen_cb_cls;
2862 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2863 listen_cb = lsocket->listen_cb;
2864 listen_cb_cls = lsocket->listen_cb_cls;
2865 GNUNET_STREAM_listen_close (lsocket);
2866 if (NULL != listen_cb)
2867 listen_cb (listen_cb_cls, NULL, NULL);
2872 * Callback to notify us on the status changes on app_port lock
2874 * @param cls the ListenSocket
2875 * @param domain the domain name of the lock
2876 * @param lock the app_port
2877 * @param status the current status of the lock
2880 lock_status_change_cb (void *cls, const char *domain, uint32_t lock,
2881 enum GNUNET_LOCKMANAGER_Status status)
2883 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2885 GNUNET_assert (lock == (uint32_t) lsocket->port);
2886 if (GNUNET_LOCKMANAGER_SUCCESS == status)
2888 lsocket->listening = GNUNET_YES;
2889 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
2891 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
2892 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2894 if (NULL == lsocket->mesh)
2896 GNUNET_MESH_ApplicationType ports[] = {lsocket->port, 0};
2898 lsocket->mesh = GNUNET_MESH_connect (lsocket->cfg,
2899 lsocket, /* Closure */
2902 server_message_handlers,
2904 GNUNET_assert (NULL != lsocket->mesh);
2905 if (NULL != lsocket->listen_ok_cb)
2907 (void) lsocket->listen_ok_cb ();
2911 if (GNUNET_LOCKMANAGER_RELEASE == status)
2912 lsocket->listening = GNUNET_NO;
2922 * Tries to open a stream to the target peer
2924 * @param cfg configuration to use
2925 * @param target the target peer to which the stream has to be opened
2926 * @param app_port the application port number which uniquely identifies this
2928 * @param open_cb this function will be called after stream has be established;
2930 * @param open_cb_cls the closure for open_cb
2931 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2932 * @return if successful it returns the stream socket; NULL if stream cannot be
2935 struct GNUNET_STREAM_Socket *
2936 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2937 const struct GNUNET_PeerIdentity *target,
2938 GNUNET_MESH_ApplicationType app_port,
2939 GNUNET_STREAM_OpenCallback open_cb,
2943 struct GNUNET_STREAM_Socket *socket;
2944 enum GNUNET_STREAM_Option option;
2945 GNUNET_MESH_ApplicationType ports[] = {app_port, 0};
2948 LOG (GNUNET_ERROR_TYPE_DEBUG,
2950 GNUNET_assert (NULL != open_cb);
2951 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2952 socket->other_peer = *target;
2953 socket->open_cb = open_cb;
2954 socket->open_cls = open_cb_cls;
2956 socket->retransmit_timeout = TIME_REL_SECS (default_timeout);
2957 socket->testing_active = GNUNET_NO;
2958 va_start (vargs, open_cb_cls); /* Parse variable args */
2960 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2963 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2964 /* Expect struct GNUNET_TIME_Relative */
2965 socket->retransmit_timeout = va_arg (vargs,
2966 struct GNUNET_TIME_Relative);
2968 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
2969 socket->testing_active = GNUNET_YES;
2970 socket->testing_set_write_sequence_number_value = va_arg (vargs,
2973 case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
2974 GNUNET_break (0); /* Option irrelevant in STREAM_open */
2976 case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
2977 GNUNET_break (0); /* Option irrelevant in STREAM_open */
2979 case GNUNET_STREAM_OPTION_END:
2982 } while (GNUNET_STREAM_OPTION_END != option);
2983 va_end (vargs); /* End of variable args parsing */
2984 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2986 NULL, /* No inbound tunnel handler */
2987 NULL, /* No in-tunnel cleaner */
2988 client_message_handlers,
2989 ports); /* We don't get inbound tunnels */
2990 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2992 GNUNET_free (socket);
2995 /* Now create the mesh tunnel to target */
2996 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating MESH Tunnel\n");
2997 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2998 NULL, /* Tunnel context */
2999 &mesh_peer_connect_callback,
3000 &mesh_peer_disconnect_callback,
3002 GNUNET_assert (NULL != socket->tunnel);
3003 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
3004 &socket->other_peer);
3005 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
3011 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
3013 * @param socket the stream socket
3014 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
3015 * @param completion_cb the callback that will be called upon successful
3016 * shutdown of given operation
3017 * @param completion_cls the closure for the completion callback
3018 * @return the shutdown handle
3020 struct GNUNET_STREAM_ShutdownHandle *
3021 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
3023 GNUNET_STREAM_ShutdownCompletion completion_cb,
3024 void *completion_cls)
3026 struct GNUNET_STREAM_ShutdownHandle *handle;
3027 struct GNUNET_STREAM_MessageHeader *msg;
3029 GNUNET_assert (NULL == socket->shutdown_handle);
3031 handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle));
3032 handle->socket = socket;
3033 handle->completion_cb = completion_cb;
3034 handle->completion_cls = completion_cls;
3035 socket->shutdown_handle = handle;
3037 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
3038 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
3042 handle->operation = SHUT_RD;
3043 if (NULL != socket->read_handle)
3044 LOG (GNUNET_ERROR_TYPE_WARNING,
3045 "Existing read handle should be cancelled before shutting"
3047 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
3048 queue_message (socket, msg, &set_state_receive_close_wait, NULL,
3052 handle->operation = SHUT_WR;
3053 if (NULL != socket->write_handle)
3054 LOG (GNUNET_ERROR_TYPE_WARNING,
3055 "Existing write handle should be cancelled before shutting"
3057 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
3058 queue_message (socket, msg, &set_state_transmit_close_wait, NULL,
3062 handle->operation = SHUT_RDWR;
3063 if (NULL != socket->write_handle)
3064 LOG (GNUNET_ERROR_TYPE_WARNING,
3065 "Existing write handle should be cancelled before shutting"
3067 if (NULL != socket->read_handle)
3068 LOG (GNUNET_ERROR_TYPE_WARNING,
3069 "Existing read handle should be cancelled before shutting"
3071 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
3072 queue_message (socket, msg, &set_state_close_wait, NULL, GNUNET_NO);
3075 LOG (GNUNET_ERROR_TYPE_WARNING,
3076 "GNUNET_STREAM_shutdown called with invalid value for "
3077 "parameter operation -- Ignoring\n");
3079 GNUNET_free (handle);
3082 handle->close_msg_retransmission_task_id =
3083 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
3084 &close_msg_retransmission_task,
3091 * Cancels a pending shutdown
3093 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
3096 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle)
3098 if (GNUNET_SCHEDULER_NO_TASK != handle->close_msg_retransmission_task_id)
3099 GNUNET_SCHEDULER_cancel (handle->close_msg_retransmission_task_id);
3100 handle->socket->shutdown_handle = NULL;
3101 GNUNET_free (handle);
3108 * @param socket the stream socket
3111 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
3113 struct MessageQueue *head;
3115 if (NULL != socket->read_handle)
3117 LOG (GNUNET_ERROR_TYPE_WARNING,
3118 "Closing STREAM socket when a read handle is pending\n");
3120 if (NULL != socket->write_handle)
3122 LOG (GNUNET_ERROR_TYPE_WARNING,
3123 "Closing STREAM socket when a write handle is pending\n");
3124 GNUNET_STREAM_io_write_cancel (socket->write_handle);
3125 //socket->write_handle = NULL;
3127 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
3129 /* socket closed with read task pending!? */
3131 GNUNET_SCHEDULER_cancel (socket->read_task_id);
3132 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
3134 /* Terminate the ack'ing tasks if they are still present */
3135 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
3137 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
3138 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
3140 /* Terminate the control retransmission tasks */
3141 if (GNUNET_SCHEDULER_NO_TASK != socket->control_retransmission_task_id)
3143 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
3145 /* Clear Transmit handles */
3146 if (NULL != socket->transmit_handle)
3148 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
3149 socket->transmit_handle = NULL;
3151 /* Clear existing message queue */
3152 while (NULL != (head = socket->queue_head)) {
3153 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
3156 GNUNET_free (head->message);
3160 /* Close associated tunnel */
3161 if (NULL != socket->tunnel)
3163 GNUNET_MESH_tunnel_destroy (socket->tunnel);
3164 socket->tunnel = NULL;
3167 /* Close mesh connection */
3168 if (NULL != socket->mesh && NULL == socket->lsocket)
3170 GNUNET_MESH_disconnect (socket->mesh);
3171 socket->mesh = NULL;
3174 /* Release receive buffer */
3175 if (NULL != socket->receive_buffer)
3177 GNUNET_free (socket->receive_buffer);
3180 GNUNET_free (socket);
3185 * Listens for stream connections for a specific application ports
3187 * @param cfg the configuration to use
3188 * @param app_port the application port for which new streams will be accepted
3189 * @param listen_cb this function will be called when a peer tries to establish
3191 * @param listen_cb_cls closure for listen_cb
3192 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
3193 * @return listen socket, NULL for any error
3195 struct GNUNET_STREAM_ListenSocket *
3196 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
3197 GNUNET_MESH_ApplicationType app_port,
3198 GNUNET_STREAM_ListenCallback listen_cb,
3199 void *listen_cb_cls,
3202 /* FIXME: Add variable args for passing configration options? */
3203 struct GNUNET_STREAM_ListenSocket *lsocket;
3204 struct GNUNET_TIME_Relative listen_timeout;
3205 enum GNUNET_STREAM_Option option;
3208 GNUNET_assert (NULL != listen_cb);
3209 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
3210 lsocket->cfg = GNUNET_CONFIGURATION_dup (cfg);
3211 lsocket->lockmanager = GNUNET_LOCKMANAGER_connect (lsocket->cfg);
3212 if (NULL == lsocket->lockmanager)
3214 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3215 GNUNET_free (lsocket);
3218 lsocket->listening = GNUNET_NO;/* We listen when we get a lock on app_port */
3220 lsocket->retransmit_timeout = TIME_REL_SECS (default_timeout);
3221 lsocket->testing_active = GNUNET_NO;
3222 lsocket->listen_ok_cb = NULL;
3223 listen_timeout = TIME_REL_SECS (60); /* A minute for listen timeout */
3224 va_start (vargs, listen_cb_cls);
3226 option = va_arg (vargs, enum GNUNET_STREAM_Option);
3229 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
3230 lsocket->retransmit_timeout = va_arg (vargs,
3231 struct GNUNET_TIME_Relative);
3233 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
3234 lsocket->testing_active = GNUNET_YES;
3235 lsocket->testing_set_write_sequence_number_value = va_arg (vargs,
3238 case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
3239 listen_timeout = GNUNET_TIME_relative_multiply
3240 (GNUNET_TIME_UNIT_MILLISECONDS, va_arg (vargs, uint32_t));
3242 case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
3243 lsocket->listen_ok_cb = va_arg (vargs,
3244 GNUNET_STREAM_ListenSuccessCallback);
3246 case GNUNET_STREAM_OPTION_END:
3249 } while (GNUNET_STREAM_OPTION_END != option);
3251 lsocket->port = app_port;
3252 lsocket->listen_cb = listen_cb;
3253 lsocket->listen_cb_cls = listen_cb_cls;
3254 lsocket->locking_request =
3255 GNUNET_LOCKMANAGER_acquire_lock (lsocket->lockmanager, locking_domain,
3256 (uint32_t) lsocket->port,
3257 &lock_status_change_cb, lsocket);
3258 lsocket->lockmanager_acquire_timeout_task =
3259 GNUNET_SCHEDULER_add_delayed (listen_timeout,
3260 &lockmanager_acquire_timeout, lsocket);
3266 * Closes the listen socket
3268 * @param lsocket the listen socket
3271 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
3273 /* Close MESH connection */
3274 if (NULL != lsocket->mesh)
3275 GNUNET_MESH_disconnect (lsocket->mesh);
3276 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3277 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
3278 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
3279 if (NULL != lsocket->locking_request)
3280 GNUNET_LOCKMANAGER_cancel_request (lsocket->locking_request);
3281 if (NULL != lsocket->lockmanager)
3282 GNUNET_LOCKMANAGER_disconnect (lsocket->lockmanager);
3283 GNUNET_free (lsocket);
3288 * Tries to write the given data to the stream. The maximum size of data that
3289 * can be written as part of a write operation is (64 * (64000 - sizeof (struct
3290 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
3291 * violation, however only the said number of maximum bytes will be written.
3293 * @param socket the socket representing a stream
3294 * @param data the data buffer from where the data is written into the stream
3295 * @param size the number of bytes to be written from the data buffer
3296 * @param timeout the timeout period
3297 * @param write_cont the function to call upon writing some bytes into the
3299 * @param write_cont_cls the closure
3301 * @return handle to cancel the operation; if a previous write is pending or
3302 * the stream has been shutdown for this operation then write_cont is
3303 * immediately called and NULL is returned.
3305 struct GNUNET_STREAM_IOWriteHandle *
3306 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
3309 struct GNUNET_TIME_Relative timeout,
3310 GNUNET_STREAM_CompletionContinuation write_cont,
3311 void *write_cont_cls)
3313 unsigned int num_needed_packets;
3314 unsigned int packet;
3315 struct GNUNET_STREAM_IOWriteHandle *io_handle;
3316 uint32_t packet_size;
3317 uint32_t payload_size;
3318 struct GNUNET_STREAM_DataMessage *data_msg;
3320 struct GNUNET_TIME_Relative ack_deadline;
3322 LOG (GNUNET_ERROR_TYPE_DEBUG,
3325 /* Return NULL if there is already a write request pending */
3326 if (NULL != socket->write_handle)
3332 switch (socket->state)
3334 case STATE_TRANSMIT_CLOSED:
3335 case STATE_TRANSMIT_CLOSE_WAIT:
3337 case STATE_CLOSE_WAIT:
3338 if (NULL != write_cont)
3339 write_cont (write_cont_cls, GNUNET_STREAM_SHUTDOWN, 0);
3340 LOG (GNUNET_ERROR_TYPE_DEBUG,
3341 "%s() END\n", __func__);
3345 case STATE_HELLO_WAIT:
3346 if (NULL != write_cont)
3347 /* FIXME: GNUNET_STREAM_SYSERR?? */
3348 write_cont (write_cont_cls, GNUNET_STREAM_SYSERR, 0);
3349 LOG (GNUNET_ERROR_TYPE_DEBUG,
3350 "%s() END\n", __func__);
3352 case STATE_ESTABLISHED:
3353 case STATE_RECEIVE_CLOSED:
3354 case STATE_RECEIVE_CLOSE_WAIT:
3358 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
3359 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
3360 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
3361 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
3362 io_handle->socket = socket;
3363 io_handle->write_cont = write_cont;
3364 io_handle->write_cont_cls = write_cont_cls;
3365 io_handle->size = size;
3367 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3368 determined from RTT */
3369 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
3370 /* Divide the given buffer into packets for sending */
3371 for (packet=0; packet < num_needed_packets; packet++)
3373 if ((packet + 1) * max_payload_size < size)
3375 payload_size = max_payload_size;
3376 packet_size = MAX_PACKET_SIZE;
3380 payload_size = size - packet * max_payload_size;
3381 packet_size = payload_size + sizeof (struct
3382 GNUNET_STREAM_DataMessage);
3384 io_handle->messages[packet] = GNUNET_malloc (packet_size);
3385 io_handle->messages[packet]->header.header.size = htons (packet_size);
3386 io_handle->messages[packet]->header.header.type =
3387 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
3388 io_handle->messages[packet]->sequence_number =
3389 htonl (socket->write_sequence_number++);
3390 io_handle->messages[packet]->offset = htonl (socket->write_offset);
3392 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3393 determined from RTT */
3394 io_handle->messages[packet]->ack_deadline =
3395 GNUNET_TIME_relative_hton (ack_deadline);
3396 data_msg = io_handle->messages[packet];
3397 /* Copy data from given buffer to the packet */
3398 memcpy (&data_msg[1],
3401 sweep += payload_size;
3402 socket->write_offset += payload_size;
3404 socket->write_handle = io_handle;
3405 write_data (socket);
3407 LOG (GNUNET_ERROR_TYPE_DEBUG,
3408 "%s() END\n", __func__);
3415 * Tries to read data from the stream.
3417 * @param socket the socket representing a stream
3418 * @param timeout the timeout period
3419 * @param proc function to call with data (once only)
3420 * @param proc_cls the closure for proc
3422 * @return handle to cancel the operation; if the stream has been shutdown for
3423 * this type of opeartion then the DataProcessor is immediately
3424 * called with GNUNET_STREAM_SHUTDOWN as status and NULL if returned
3426 struct GNUNET_STREAM_IOReadHandle *
3427 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
3428 struct GNUNET_TIME_Relative timeout,
3429 GNUNET_STREAM_DataProcessor proc,
3432 struct GNUNET_STREAM_IOReadHandle *read_handle;
3434 LOG (GNUNET_ERROR_TYPE_DEBUG,
3436 GNUNET_i2s (&socket->other_peer),
3438 /* Return NULL if there is already a read handle; the user has to cancel that
3439 first before continuing or has to wait until it is completed */
3440 if (NULL != socket->read_handle)
3442 GNUNET_assert (NULL != proc);
3443 switch (socket->state)
3445 case STATE_RECEIVE_CLOSED:
3446 case STATE_RECEIVE_CLOSE_WAIT:
3448 case STATE_CLOSE_WAIT:
3449 proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
3450 LOG (GNUNET_ERROR_TYPE_DEBUG,
3452 GNUNET_i2s (&socket->other_peer),
3458 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
3459 read_handle->proc = proc;
3460 read_handle->proc_cls = proc_cls;
3461 read_handle->socket = socket;
3462 socket->read_handle = read_handle;
3463 /* Check if we have a packet at bitmap 0 */
3464 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
3467 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
3470 /* Setup the read timeout task */
3471 socket->read_io_timeout_task_id =
3472 GNUNET_SCHEDULER_add_delayed (timeout,
3475 LOG (GNUNET_ERROR_TYPE_DEBUG,
3477 GNUNET_i2s (&socket->other_peer),
3484 * Cancel pending write operation.
3486 * @param ioh handle to operation to cancel
3489 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
3491 struct GNUNET_STREAM_Socket *socket = ioh->socket;
3492 unsigned int packet;
3494 GNUNET_assert (NULL != socket->write_handle);
3495 GNUNET_assert (socket->write_handle == ioh);
3497 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
3499 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
3500 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
3503 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
3505 if (NULL == ioh->messages[packet]) break;
3506 GNUNET_free (ioh->messages[packet]);
3509 GNUNET_free (socket->write_handle);
3510 socket->write_handle = NULL;
3515 * Cancel pending read operation.
3517 * @param ioh handle to operation to cancel
3520 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)
3522 struct GNUNET_STREAM_Socket *socket;
3524 socket = ioh->socket;
3525 GNUNET_assert (NULL != socket->read_handle);
3526 GNUNET_assert (ioh == socket->read_handle);
3527 /* Read io time task should be there; if it is already executed then this
3528 read handle is not valid */
3529 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != socket->read_io_timeout_task_id);
3530 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task_id);
3531 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
3532 /* reading task may be present; if so we have to stop it */
3533 if (GNUNET_SCHEDULER_NO_TASK != socket->read_task_id)
3535 GNUNET_SCHEDULER_cancel (socket->read_task_id);
3536 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
3539 socket->read_handle = NULL;
3542 /* end of stream_api.c */