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"
45 #define LOG(kind,...) \
46 GNUNET_log_from (kind, "stream-api", __VA_ARGS__)
48 #define TIME_REL_SECS(sec) \
49 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
52 * The maximum packet size of a stream packet
54 #define MAX_PACKET_SIZE 512//64000
59 #define RECEIVE_BUFFER_SIZE 4096000
62 * The maximum payload a data message packet can carry
64 static const size_t max_payload_size =
65 MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
68 * states in the Protocol
73 * Client initialization state
78 * Listener initialization state
83 * Pre-connection establishment state
88 * State where a connection has been established
93 * State where the socket is closed on our side and waiting to be ACK'ed
95 STATE_RECEIVE_CLOSE_WAIT,
98 * State where the socket is closed for reading
100 STATE_RECEIVE_CLOSED,
103 * State where the socket is closed on our side and waiting to be ACK'ed
105 STATE_TRANSMIT_CLOSE_WAIT,
108 * State where the socket is closed for writing
110 STATE_TRANSMIT_CLOSED,
113 * State where the socket is closed on our side and waiting to be ACK'ed
118 * State where the socket is closed
125 * Functions of this type are called when a message is written
127 * @param cls the closure from queue_message
128 * @param socket the socket the written message was bound to
130 typedef void (*SendFinishCallback) (void *cls,
131 struct GNUNET_STREAM_Socket *socket);
135 * The send message queue
142 struct GNUNET_STREAM_MessageHeader *message;
145 * Callback to be called when the message is sent
147 SendFinishCallback finish_cb;
150 * The closure for finish_cb
155 * The next message in queue. Should be NULL in the last message
157 struct MessageQueue *next;
160 * The next message in queue. Should be NULL in the first message
162 struct MessageQueue *prev;
167 * The STREAM Socket Handler
169 struct GNUNET_STREAM_Socket
172 * Retransmission timeout
174 struct GNUNET_TIME_Relative retransmit_timeout;
177 * The Acknowledgement Bitmap
179 GNUNET_STREAM_AckBitmap ack_bitmap;
182 * Time when the Acknowledgement was queued
184 struct GNUNET_TIME_Absolute ack_time_registered;
187 * Queued Acknowledgement deadline
189 struct GNUNET_TIME_Relative ack_time_deadline;
194 struct GNUNET_MESH_Handle *mesh;
197 * The mesh tunnel handle
199 struct GNUNET_MESH_Tunnel *tunnel;
202 * Stream open closure
207 * Stream open callback
209 GNUNET_STREAM_OpenCallback open_cb;
212 * The current transmit handle (if a pending transmit request exists)
214 struct GNUNET_MESH_TransmitHandle *transmit_handle;
217 * The current act transmit handle (if a pending ack transmit request exists)
219 struct GNUNET_MESH_TransmitHandle *ack_transmit_handle;
222 * Pointer to the current ack message using in ack_task
224 struct GNUNET_STREAM_AckMessage *ack_msg;
227 * The current message associated with the transmit handle
229 struct MessageQueue *queue_head;
232 * The queue tail, should always point to the last message in queue
234 struct MessageQueue *queue_tail;
237 * The write IO_handle associated with this socket
239 struct GNUNET_STREAM_IOWriteHandle *write_handle;
242 * The read IO_handle associated with this socket
244 struct GNUNET_STREAM_IOReadHandle *read_handle;
247 * The shutdown handle associated with this socket
249 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
252 * Buffer for storing received messages
254 void *receive_buffer;
257 * The listen socket from which this socket is derived. Should be NULL if it
258 * is not a derived socket
260 struct GNUNET_STREAM_ListenSocket *lsocket;
263 * The peer identity of the peer at the other end of the stream
265 struct GNUNET_PeerIdentity other_peer;
268 * Task identifier for the read io timeout task
270 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
273 * Task identifier for retransmission task after timeout
275 GNUNET_SCHEDULER_TaskIdentifier retransmission_timeout_task_id;
278 * The task for sending timely Acks
280 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
283 * Task scheduled to continue a read operation.
285 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
288 * The state of the protocol associated with this socket
293 * The status of the socket
295 enum GNUNET_STREAM_Status status;
298 * The number of previous timeouts; FIXME: currently not used
300 unsigned int retries;
303 * The application port number (type: uint32_t)
305 GNUNET_MESH_ApplicationType app_port;
308 * Whether testing mode is active or not
313 * The write sequence number to be set incase of testing
315 uint32_t testing_set_write_sequence_number_value;
318 * The session id associated with this stream connection
319 * FIXME: Not used currently, may be removed
324 * Write sequence number. Set to random when sending HELLO(client) and
327 uint32_t write_sequence_number;
330 * Read sequence number. This number's value is determined during handshake
332 uint32_t read_sequence_number;
335 * The receiver buffer size
337 uint32_t receive_buffer_size;
340 * The receiver buffer boundaries
342 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
345 * receiver's available buffer after the last acknowledged packet
347 uint32_t receiver_window_available;
350 * The offset pointer used during write operation
352 uint32_t write_offset;
355 * The offset after which we are expecting data
357 uint32_t read_offset;
360 * The offset upto which user has read from the received buffer
362 uint32_t copy_offset;
367 * A socket for listening
369 struct GNUNET_STREAM_ListenSocket
374 struct GNUNET_MESH_Handle *mesh;
379 struct GNUNET_CONFIGURATION_Handle *cfg;
382 * Handle to the lock manager service
384 struct GNUNET_LOCKMANAGER_Handle *lockmanager;
387 * The active LockingRequest from lockmanager
389 struct GNUNET_LOCKMANAGER_LockingRequest *locking_request;
392 * The callback function which is called after successful opening socket
394 GNUNET_STREAM_ListenCallback listen_cb;
397 * The call back closure
404 GNUNET_MESH_ApplicationType port;
407 * The id of the lockmanager timeout task
409 GNUNET_SCHEDULER_TaskIdentifier lockmanager_acquire_timeout_task;
412 * The retransmit timeout
414 struct GNUNET_TIME_Relative retransmit_timeout;
422 * Whether testing mode is active or not
427 * The write sequence number to be set incase of testing
429 uint32_t testing_set_write_sequence_number_value;
434 * The IO Write Handle
436 struct GNUNET_STREAM_IOWriteHandle
439 * The socket to which this write handle is associated
441 struct GNUNET_STREAM_Socket *socket;
444 * The packet_buffers associated with this Handle
446 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
449 * The write continuation callback
451 GNUNET_STREAM_CompletionContinuation write_cont;
454 * Write continuation closure
456 void *write_cont_cls;
459 * The bitmap of this IOHandle; Corresponding bit for a message is set when
460 * it has been acknowledged by the receiver
462 GNUNET_STREAM_AckBitmap ack_bitmap;
465 * Number of bytes in this write handle
474 struct GNUNET_STREAM_IOReadHandle
477 * Callback for the read processor
479 GNUNET_STREAM_DataProcessor proc;
482 * The closure pointer for the read processor callback
489 * Handle for Shutdown
491 struct GNUNET_STREAM_ShutdownHandle
494 * The socket associated with this shutdown handle
496 struct GNUNET_STREAM_Socket *socket;
499 * Shutdown completion callback
501 GNUNET_STREAM_ShutdownCompletion completion_cb;
504 * Closure for completion callback
506 void *completion_cls;
509 * Close message retransmission task id
511 GNUNET_SCHEDULER_TaskIdentifier close_msg_retransmission_task_id;
514 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
521 * Default value in seconds for various timeouts
523 static const unsigned int default_timeout = 10;
526 * The domain name for locks we use here
528 static const char *locking_domain = "GNUNET_STREAM_APPLOCK";
532 * Callback function for sending queued message
534 * @param cls closure the socket
535 * @param size number of bytes available in buf
536 * @param buf where the callee should write the message
537 * @return number of bytes written to buf
540 send_message_notify (void *cls, size_t size, void *buf)
542 struct GNUNET_STREAM_Socket *socket = cls;
543 struct MessageQueue *head;
546 socket->transmit_handle = NULL; /* Remove the transmit handle */
547 head = socket->queue_head;
549 return 0; /* just to be safe */
550 if (0 == size) /* request timed out */
553 LOG (GNUNET_ERROR_TYPE_DEBUG,
554 "%s: Message sending timed out. Retry %d \n",
555 GNUNET_i2s (&socket->other_peer),
557 socket->transmit_handle =
558 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
561 /* FIXME: exponential backoff */
562 socket->retransmit_timeout,
564 ntohs (head->message->header.size),
565 &send_message_notify,
570 ret = ntohs (head->message->header.size);
571 GNUNET_assert (size >= ret);
572 memcpy (buf, head->message, ret);
573 if (NULL != head->finish_cb)
575 head->finish_cb (head->finish_cb_cls, socket);
577 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
580 GNUNET_free (head->message);
582 head = socket->queue_head;
583 if (NULL != head) /* more pending messages to send */
586 socket->transmit_handle =
587 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
590 /* FIXME: exponential backoff */
591 socket->retransmit_timeout,
593 ntohs (head->message->header.size),
594 &send_message_notify,
602 * Queues a message for sending using the mesh connection of a socket
604 * @param socket the socket whose mesh connection is used
605 * @param message the message to be sent
606 * @param finish_cb the callback to be called when the message is sent
607 * @param finish_cb_cls the closure for the callback
610 queue_message (struct GNUNET_STREAM_Socket *socket,
611 struct GNUNET_STREAM_MessageHeader *message,
612 SendFinishCallback finish_cb,
615 struct MessageQueue *queue_entity;
618 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
619 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
621 LOG (GNUNET_ERROR_TYPE_DEBUG,
622 "%s: Queueing message of type %d and size %d\n",
623 GNUNET_i2s (&socket->other_peer),
624 ntohs (message->header.type),
625 ntohs (message->header.size));
626 GNUNET_assert (NULL != message);
627 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
628 queue_entity->message = message;
629 queue_entity->finish_cb = finish_cb;
630 queue_entity->finish_cb_cls = finish_cb_cls;
631 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
634 if (NULL == socket->transmit_handle)
637 socket->transmit_handle =
638 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
641 socket->retransmit_timeout,
643 ntohs (message->header.size),
644 &send_message_notify,
651 * Copies a message and queues it for sending using the mesh connection of
654 * @param socket the socket whose mesh connection is used
655 * @param message the message to be sent
656 * @param finish_cb the callback to be called when the message is sent
657 * @param finish_cb_cls the closure for the callback
660 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
661 const struct GNUNET_STREAM_MessageHeader *message,
662 SendFinishCallback finish_cb,
665 struct GNUNET_STREAM_MessageHeader *msg_copy;
668 size = ntohs (message->header.size);
669 msg_copy = GNUNET_malloc (size);
670 memcpy (msg_copy, message, size);
671 queue_message (socket, msg_copy, finish_cb, finish_cb_cls);
676 * Callback function for sending ack message
678 * @param cls closure the ACK message created in ack_task
679 * @param size number of bytes available in buffer
680 * @param buf where the callee should write the message
681 * @return number of bytes written to buf
684 send_ack_notify (void *cls, size_t size, void *buf)
686 struct GNUNET_STREAM_Socket *socket = cls;
690 LOG (GNUNET_ERROR_TYPE_DEBUG,
691 "%s called with size 0\n", __func__);
694 GNUNET_assert (ntohs (socket->ack_msg->header.header.size) <= size);
696 size = ntohs (socket->ack_msg->header.header.size);
697 memcpy (buf, socket->ack_msg, size);
699 GNUNET_free (socket->ack_msg);
700 socket->ack_msg = NULL;
701 socket->ack_transmit_handle = NULL;
707 * Writes data using the given socket. The amount of data written is limited by
708 * the receiver_window_size
710 * @param socket the socket to use
713 write_data (struct GNUNET_STREAM_Socket *socket);
717 * Task for retransmitting data messages if they aren't ACK before their ack
720 * @param cls the socket
721 * @param tc the Task context
724 retransmission_timeout_task (void *cls,
725 const struct GNUNET_SCHEDULER_TaskContext *tc)
727 struct GNUNET_STREAM_Socket *socket = cls;
729 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
732 LOG (GNUNET_ERROR_TYPE_DEBUG,
733 "%s: Retransmitting DATA...\n", GNUNET_i2s (&socket->other_peer));
734 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
740 * Task for sending ACK message
742 * @param cls the socket
743 * @param tc the Task context
747 const struct GNUNET_SCHEDULER_TaskContext *tc)
749 struct GNUNET_STREAM_Socket *socket = cls;
750 struct GNUNET_STREAM_AckMessage *ack_msg;
752 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
756 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
757 /* Create the ACK Message */
758 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
759 ack_msg->header.header.size = htons (sizeof (struct
760 GNUNET_STREAM_AckMessage));
761 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
762 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
763 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
764 ack_msg->receive_window_remaining =
765 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
766 socket->ack_msg = ack_msg;
767 /* Request MESH for sending ACK */
768 socket->ack_transmit_handle =
769 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
772 socket->retransmit_timeout,
774 ntohs (ack_msg->header.header.size),
781 * Retransmission task for shutdown messages
783 * @param cls the shutdown handle
784 * @param tc the Task Context
787 close_msg_retransmission_task (void *cls,
788 const struct GNUNET_SCHEDULER_TaskContext *tc)
790 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle = cls;
791 struct GNUNET_STREAM_MessageHeader *msg;
792 struct GNUNET_STREAM_Socket *socket;
794 GNUNET_assert (NULL != shutdown_handle);
795 socket = shutdown_handle->socket;
797 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
798 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
799 switch (shutdown_handle->operation)
802 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
805 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
808 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
812 shutdown_handle->close_msg_retransmission_task_id =
813 GNUNET_SCHEDULER_NO_TASK;
816 queue_message (socket, msg, NULL, NULL);
817 shutdown_handle->close_msg_retransmission_task_id =
818 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
819 &close_msg_retransmission_task,
825 * Function to modify a bit in GNUNET_STREAM_AckBitmap
827 * @param bitmap the bitmap to modify
828 * @param bit the bit number to modify
829 * @param value GNUNET_YES to on, GNUNET_NO to off
832 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
836 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
837 if (GNUNET_YES == value)
838 *bitmap |= (1LL << bit);
840 *bitmap &= ~(1LL << bit);
845 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
847 * @param bitmap address of the bitmap that has to be checked
848 * @param bit the bit number to check
849 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
852 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
855 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
856 return 0 != (*bitmap & (1LL << bit));
861 * Writes data using the given socket. The amount of data written is limited by
862 * the receiver_window_size
864 * @param socket the socket to use
867 write_data (struct GNUNET_STREAM_Socket *socket)
869 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
870 int packet; /* Although an int, should never be negative */
874 /* Find the last acknowledged packet */
875 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
877 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
880 else if (NULL == io_handle->messages[packet])
883 /* Resend packets which weren't ack'ed */
884 for (packet=0; packet < ack_packet; packet++)
886 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
889 LOG (GNUNET_ERROR_TYPE_DEBUG,
890 "%s: Placing DATA message with sequence %u in send queue\n",
891 GNUNET_i2s (&socket->other_peer),
892 ntohl (io_handle->messages[packet]->sequence_number));
893 copy_and_queue_message (socket,
894 &io_handle->messages[packet]->header,
899 packet = ack_packet + 1;
900 /* Now send new packets if there is enough buffer space */
901 while ( (NULL != io_handle->messages[packet]) &&
902 (socket->receiver_window_available
903 >= ntohs (io_handle->messages[packet]->header.header.size)) &&
904 (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
906 socket->receiver_window_available -=
907 ntohs (io_handle->messages[packet]->header.header.size);
908 LOG (GNUNET_ERROR_TYPE_DEBUG,
909 "%s: Placing DATA message with sequence %u in send queue\n",
910 GNUNET_i2s (&socket->other_peer),
911 ntohl (io_handle->messages[packet]->sequence_number));
912 copy_and_queue_message (socket,
913 &io_handle->messages[packet]->header,
918 if (GNUNET_SCHEDULER_NO_TASK == socket->retransmission_timeout_task_id)
919 socket->retransmission_timeout_task_id =
920 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
921 (GNUNET_TIME_UNIT_SECONDS, 8),
922 &retransmission_timeout_task,
928 * Task for calling the read processor
930 * @param cls the socket
931 * @param tc the task context
934 call_read_processor (void *cls,
935 const struct GNUNET_SCHEDULER_TaskContext *tc)
937 struct GNUNET_STREAM_Socket *socket = cls;
939 size_t valid_read_size;
941 uint32_t sequence_increase;
942 uint32_t offset_increase;
944 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
945 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
948 if (NULL == socket->receive_buffer)
951 GNUNET_assert (NULL != socket->read_handle);
952 GNUNET_assert (NULL != socket->read_handle->proc);
954 /* Check the bitmap for any holes */
955 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
957 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
961 /* We only call read processor if we have the first packet */
962 GNUNET_assert (0 < packet);
964 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
965 GNUNET_assert (0 != valid_read_size);
966 /* Cancel the read_io_timeout_task */
967 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task_id);
968 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
969 /* Call the data processor */
970 LOG (GNUNET_ERROR_TYPE_DEBUG,
971 "%s: Calling read processor\n",
972 GNUNET_i2s (&socket->other_peer));
974 socket->read_handle->proc (socket->read_handle->proc_cls,
976 socket->receive_buffer + socket->copy_offset,
978 LOG (GNUNET_ERROR_TYPE_DEBUG,
979 "%s: Read processor read %d bytes\n",
980 GNUNET_i2s (&socket->other_peer), read_size);
981 LOG (GNUNET_ERROR_TYPE_DEBUG,
982 "%s: Read processor completed successfully\n",
983 GNUNET_i2s (&socket->other_peer));
984 /* Free the read handle */
985 GNUNET_free (socket->read_handle);
986 socket->read_handle = NULL;
987 GNUNET_assert (read_size <= valid_read_size);
988 socket->copy_offset += read_size;
989 /* Determine upto which packet we can remove from the buffer */
990 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
992 if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
994 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
998 /* If no packets can be removed we can't move the buffer */
999 if (0 == packet) return;
1000 sequence_increase = packet;
1001 LOG (GNUNET_ERROR_TYPE_DEBUG,
1002 "%s: Sequence increase after read processor completion: %u\n",
1003 GNUNET_i2s (&socket->other_peer), sequence_increase);
1005 /* Shift the data in the receive buffer */
1006 socket->receive_buffer =
1007 memmove (socket->receive_buffer,
1008 socket->receive_buffer
1009 + socket->receive_buffer_boundaries[sequence_increase-1],
1010 socket->receive_buffer_size
1011 - socket->receive_buffer_boundaries[sequence_increase-1]);
1012 /* Shift the bitmap */
1013 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
1014 /* Set read_sequence_number */
1015 socket->read_sequence_number += sequence_increase;
1016 /* Set read_offset */
1017 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
1018 socket->read_offset += offset_increase;
1019 /* Fix copy_offset */
1020 GNUNET_assert (offset_increase <= socket->copy_offset);
1021 socket->copy_offset -= offset_increase;
1022 /* Fix relative boundaries */
1023 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1025 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
1027 uint32_t ahead_buffer_boundary;
1029 ahead_buffer_boundary =
1030 socket->receive_buffer_boundaries[packet + sequence_increase];
1031 if (0 == ahead_buffer_boundary)
1032 socket->receive_buffer_boundaries[packet] = 0;
1035 GNUNET_assert (offset_increase < ahead_buffer_boundary);
1036 socket->receive_buffer_boundaries[packet] =
1037 ahead_buffer_boundary - offset_increase;
1041 socket->receive_buffer_boundaries[packet] = 0;
1047 * Cancels the existing read io handle
1049 * @param cls the closure from the SCHEDULER call
1050 * @param tc the task context
1053 read_io_timeout (void *cls,
1054 const struct GNUNET_SCHEDULER_TaskContext *tc)
1056 struct GNUNET_STREAM_Socket *socket = cls;
1057 GNUNET_STREAM_DataProcessor proc;
1060 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
1061 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
1063 LOG (GNUNET_ERROR_TYPE_DEBUG,
1064 "%s: Read task timedout - Cancelling it\n",
1065 GNUNET_i2s (&socket->other_peer));
1066 GNUNET_SCHEDULER_cancel (socket->read_task_id);
1067 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
1069 GNUNET_assert (NULL != socket->read_handle);
1070 proc = socket->read_handle->proc;
1071 proc_cls = socket->read_handle->proc_cls;
1073 GNUNET_free (socket->read_handle);
1074 socket->read_handle = NULL;
1075 /* Call the read processor to signal timeout */
1077 GNUNET_STREAM_TIMEOUT,
1084 * Handler for DATA messages; Same for both client and server
1086 * @param socket the socket through which the ack was received
1087 * @param tunnel connection to the other end
1088 * @param sender who sent the message
1089 * @param msg the data message
1090 * @param atsi performance data for the connection
1091 * @return GNUNET_OK to keep the connection open,
1092 * GNUNET_SYSERR to close it (signal serious error)
1095 handle_data (struct GNUNET_STREAM_Socket *socket,
1096 struct GNUNET_MESH_Tunnel *tunnel,
1097 const struct GNUNET_PeerIdentity *sender,
1098 const struct GNUNET_STREAM_DataMessage *msg,
1099 const struct GNUNET_ATS_Information*atsi)
1101 const void *payload;
1102 uint32_t bytes_needed;
1103 uint32_t relative_offset;
1104 uint32_t relative_sequence_number;
1107 size = htons (msg->header.header.size);
1108 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
1110 GNUNET_break_op (0);
1111 return GNUNET_SYSERR;
1114 if (0 != memcmp (sender,
1115 &socket->other_peer,
1116 sizeof (struct GNUNET_PeerIdentity)))
1118 LOG (GNUNET_ERROR_TYPE_DEBUG,
1119 "%s: Received DATA from non-confirming peer\n",
1120 GNUNET_i2s (&socket->other_peer));
1124 switch (socket->state)
1126 case STATE_ESTABLISHED:
1127 case STATE_TRANSMIT_CLOSED:
1128 case STATE_TRANSMIT_CLOSE_WAIT:
1130 /* check if the message's sequence number is in the range we are
1132 relative_sequence_number =
1133 ntohl (msg->sequence_number) - socket->read_sequence_number;
1134 if ( relative_sequence_number > GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1136 LOG (GNUNET_ERROR_TYPE_DEBUG,
1137 "%s: Ignoring received message with sequence number %u\n",
1138 GNUNET_i2s (&socket->other_peer),
1139 ntohl (msg->sequence_number));
1140 /* Start ACK sending task if one is not already present */
1141 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1143 socket->ack_task_id =
1144 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1145 (msg->ack_deadline),
1152 /* Check if we have already seen this message */
1153 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1154 relative_sequence_number))
1156 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 "%s: Ignoring already received message with sequence number %u\n",
1158 GNUNET_i2s (&socket->other_peer),
1159 ntohl (msg->sequence_number));
1160 /* Start ACK sending task if one is not already present */
1161 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1163 socket->ack_task_id =
1164 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1165 (msg->ack_deadline),
1172 LOG (GNUNET_ERROR_TYPE_DEBUG,
1173 "%s: Receiving DATA with sequence number: %u and size: %d from %s\n",
1174 GNUNET_i2s (&socket->other_peer),
1175 ntohl (msg->sequence_number),
1176 ntohs (msg->header.header.size),
1177 GNUNET_i2s (&socket->other_peer));
1179 /* Check if we have to allocate the buffer */
1180 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1181 relative_offset = ntohl (msg->offset) - socket->read_offset;
1182 bytes_needed = relative_offset + size;
1183 if (bytes_needed > socket->receive_buffer_size)
1185 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1187 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1189 socket->receive_buffer_size = bytes_needed;
1193 LOG (GNUNET_ERROR_TYPE_DEBUG,
1194 "%s: Cannot accommodate packet %d as buffer is full\n",
1195 GNUNET_i2s (&socket->other_peer),
1196 ntohl (msg->sequence_number));
1201 /* Copy Data to buffer */
1203 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1204 memcpy (socket->receive_buffer + relative_offset,
1207 socket->receive_buffer_boundaries[relative_sequence_number] =
1208 relative_offset + size;
1210 /* Modify the ACK bitmap */
1211 ackbitmap_modify_bit (&socket->ack_bitmap,
1212 relative_sequence_number,
1215 /* Start ACK sending task if one is not already present */
1216 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1218 socket->ack_task_id =
1219 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1220 (msg->ack_deadline),
1225 if ((NULL != socket->read_handle) /* A read handle is waiting */
1226 /* There is no current read task */
1227 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
1228 /* We have the first packet */
1229 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
1232 LOG (GNUNET_ERROR_TYPE_DEBUG,
1233 "%s: Scheduling read processor\n",
1234 GNUNET_i2s (&socket->other_peer));
1236 socket->read_task_id =
1237 GNUNET_SCHEDULER_add_now (&call_read_processor,
1244 LOG (GNUNET_ERROR_TYPE_DEBUG,
1245 "%s: Received data message when it cannot be handled\n",
1246 GNUNET_i2s (&socket->other_peer));
1254 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1256 * @param cls the socket (set from GNUNET_MESH_connect)
1257 * @param tunnel connection to the other end
1258 * @param tunnel_ctx place to store local state associated with the tunnel
1259 * @param sender who sent the message
1260 * @param message the actual message
1261 * @param atsi performance data for the connection
1262 * @return GNUNET_OK to keep the connection open,
1263 * GNUNET_SYSERR to close it (signal serious error)
1266 client_handle_data (void *cls,
1267 struct GNUNET_MESH_Tunnel *tunnel,
1269 const struct GNUNET_PeerIdentity *sender,
1270 const struct GNUNET_MessageHeader *message,
1271 const struct GNUNET_ATS_Information*atsi)
1273 struct GNUNET_STREAM_Socket *socket = cls;
1275 return handle_data (socket,
1278 (const struct GNUNET_STREAM_DataMessage *) message,
1284 * Callback to set state to ESTABLISHED
1286 * @param cls the closure from queue_message FIXME: document
1287 * @param socket the socket to requiring state change
1290 set_state_established (void *cls,
1291 struct GNUNET_STREAM_Socket *socket)
1293 LOG (GNUNET_ERROR_TYPE_DEBUG,
1294 "%s: Attaining ESTABLISHED state\n",
1295 GNUNET_i2s (&socket->other_peer));
1296 socket->write_offset = 0;
1297 socket->read_offset = 0;
1298 socket->state = STATE_ESTABLISHED;
1299 if (NULL != socket->lsocket)
1301 LOG (GNUNET_ERROR_TYPE_DEBUG,
1302 "%s: Calling listen callback\n",
1303 GNUNET_i2s (&socket->other_peer));
1304 if (GNUNET_SYSERR ==
1305 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1307 &socket->other_peer))
1309 socket->state = STATE_CLOSED;
1310 /* FIXME: We should close in a decent way (send RST) */
1311 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1312 GNUNET_free (socket);
1315 else if (NULL != socket->open_cb)
1316 socket->open_cb (socket->open_cls, socket);
1321 * Callback to set state to HELLO_WAIT
1323 * @param cls the closure from queue_message
1324 * @param socket the socket to requiring state change
1327 set_state_hello_wait (void *cls,
1328 struct GNUNET_STREAM_Socket *socket)
1330 GNUNET_assert (STATE_INIT == socket->state);
1331 LOG (GNUNET_ERROR_TYPE_DEBUG,
1332 "%s: Attaining HELLO_WAIT state\n",
1333 GNUNET_i2s (&socket->other_peer));
1334 socket->state = STATE_HELLO_WAIT;
1339 * Callback to set state to CLOSE_WAIT
1341 * @param cls the closure from queue_message
1342 * @param socket the socket requiring state change
1345 set_state_close_wait (void *cls,
1346 struct GNUNET_STREAM_Socket *socket)
1348 LOG (GNUNET_ERROR_TYPE_DEBUG,
1349 "%s: Attaing CLOSE_WAIT state\n",
1350 GNUNET_i2s (&socket->other_peer));
1351 socket->state = STATE_CLOSE_WAIT;
1352 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1353 socket->receive_buffer = NULL;
1354 socket->receive_buffer_size = 0;
1359 * Callback to set state to RECEIVE_CLOSE_WAIT
1361 * @param cls the closure from queue_message
1362 * @param socket the socket requiring state change
1365 set_state_receive_close_wait (void *cls,
1366 struct GNUNET_STREAM_Socket *socket)
1368 LOG (GNUNET_ERROR_TYPE_DEBUG,
1369 "%s: Attaing RECEIVE_CLOSE_WAIT state\n",
1370 GNUNET_i2s (&socket->other_peer));
1371 socket->state = STATE_RECEIVE_CLOSE_WAIT;
1372 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1373 socket->receive_buffer = NULL;
1374 socket->receive_buffer_size = 0;
1379 * Callback to set state to TRANSMIT_CLOSE_WAIT
1381 * @param cls the closure from queue_message
1382 * @param socket the socket requiring state change
1385 set_state_transmit_close_wait (void *cls,
1386 struct GNUNET_STREAM_Socket *socket)
1388 LOG (GNUNET_ERROR_TYPE_DEBUG,
1389 "%s: Attaing TRANSMIT_CLOSE_WAIT state\n",
1390 GNUNET_i2s (&socket->other_peer));
1391 socket->state = STATE_TRANSMIT_CLOSE_WAIT;
1396 * Callback to set state to CLOSED
1398 * @param cls the closure from queue_message
1399 * @param socket the socket requiring state change
1402 set_state_closed (void *cls,
1403 struct GNUNET_STREAM_Socket *socket)
1405 socket->state = STATE_CLOSED;
1410 * Returns a new HelloAckMessage. Also sets the write sequence number for the
1413 * @param socket the socket for which this HelloAckMessage has to be generated
1414 * @return the HelloAckMessage
1416 static struct GNUNET_STREAM_HelloAckMessage *
1417 generate_hello_ack_msg (struct GNUNET_STREAM_Socket *socket)
1419 struct GNUNET_STREAM_HelloAckMessage *msg;
1421 /* Get the random sequence number */
1422 if (GNUNET_YES == socket->testing_active)
1423 socket->write_sequence_number =
1424 socket->testing_set_write_sequence_number_value;
1426 socket->write_sequence_number =
1427 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1428 LOG (GNUNET_ERROR_TYPE_DEBUG,
1429 "%s: write sequence number %u\n",
1430 GNUNET_i2s (&socket->other_peer),
1431 (unsigned int) socket->write_sequence_number);
1433 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1434 msg->header.header.size =
1435 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1436 msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1437 msg->sequence_number = htonl (socket->write_sequence_number);
1438 msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1445 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1447 * @param cls the socket (set from GNUNET_MESH_connect)
1448 * @param tunnel connection to the other end
1449 * @param tunnel_ctx this is NULL
1450 * @param sender who sent the message
1451 * @param message the actual message
1452 * @param atsi performance data for the connection
1453 * @return GNUNET_OK to keep the connection open,
1454 * GNUNET_SYSERR to close it (signal serious error)
1457 client_handle_hello_ack (void *cls,
1458 struct GNUNET_MESH_Tunnel *tunnel,
1460 const struct GNUNET_PeerIdentity *sender,
1461 const struct GNUNET_MessageHeader *message,
1462 const struct GNUNET_ATS_Information*atsi)
1464 struct GNUNET_STREAM_Socket *socket = cls;
1465 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1466 struct GNUNET_STREAM_HelloAckMessage *reply;
1468 if (0 != memcmp (sender,
1469 &socket->other_peer,
1470 sizeof (struct GNUNET_PeerIdentity)))
1472 LOG (GNUNET_ERROR_TYPE_DEBUG,
1473 "%s: Received HELLO_ACK from non-confirming peer\n",
1474 GNUNET_i2s (&socket->other_peer));
1477 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1478 LOG (GNUNET_ERROR_TYPE_DEBUG,
1479 "%s: Received HELLO_ACK from %s\n",
1480 GNUNET_i2s (&socket->other_peer),
1481 GNUNET_i2s (&socket->other_peer));
1483 GNUNET_assert (socket->tunnel == tunnel);
1484 switch (socket->state)
1486 case STATE_HELLO_WAIT:
1487 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1488 LOG (GNUNET_ERROR_TYPE_DEBUG,
1489 "%s: Read sequence number %u\n",
1490 GNUNET_i2s (&socket->other_peer),
1491 (unsigned int) socket->read_sequence_number);
1492 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1493 reply = generate_hello_ack_msg (socket);
1494 queue_message (socket,
1496 &set_state_established,
1499 case STATE_ESTABLISHED:
1500 case STATE_RECEIVE_CLOSE_WAIT:
1501 // call statistics (# ACKs ignored++)
1505 LOG (GNUNET_ERROR_TYPE_DEBUG,
1506 "%s: Server %s sent HELLO_ACK when in state %d\n",
1507 GNUNET_i2s (&socket->other_peer),
1508 GNUNET_i2s (&socket->other_peer),
1510 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1511 return GNUNET_SYSERR;
1518 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1520 * @param cls the socket (set from GNUNET_MESH_connect)
1521 * @param tunnel connection to the other end
1522 * @param tunnel_ctx this is NULL
1523 * @param sender who sent the message
1524 * @param message the actual message
1525 * @param atsi performance data for the connection
1526 * @return GNUNET_OK to keep the connection open,
1527 * GNUNET_SYSERR to close it (signal serious error)
1530 client_handle_reset (void *cls,
1531 struct GNUNET_MESH_Tunnel *tunnel,
1533 const struct GNUNET_PeerIdentity *sender,
1534 const struct GNUNET_MessageHeader *message,
1535 const struct GNUNET_ATS_Information*atsi)
1537 // struct GNUNET_STREAM_Socket *socket = cls;
1544 * Common message handler for handling TRANSMIT_CLOSE messages
1546 * @param socket the socket through which the ack was received
1547 * @param tunnel connection to the other end
1548 * @param sender who sent the message
1549 * @param msg the transmit close message
1550 * @param atsi performance data for the connection
1551 * @return GNUNET_OK to keep the connection open,
1552 * GNUNET_SYSERR to close it (signal serious error)
1555 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1556 struct GNUNET_MESH_Tunnel *tunnel,
1557 const struct GNUNET_PeerIdentity *sender,
1558 const struct GNUNET_STREAM_MessageHeader *msg,
1559 const struct GNUNET_ATS_Information*atsi)
1561 struct GNUNET_STREAM_MessageHeader *reply;
1563 switch (socket->state)
1565 case STATE_ESTABLISHED:
1566 socket->state = STATE_RECEIVE_CLOSED;
1568 /* Send TRANSMIT_CLOSE_ACK */
1569 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1570 reply->header.type =
1571 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1572 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1573 queue_message (socket, reply, NULL, NULL);
1577 /* FIXME: Call statistics? */
1585 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1587 * @param cls the socket (set from GNUNET_MESH_connect)
1588 * @param tunnel connection to the other end
1589 * @param tunnel_ctx this is NULL
1590 * @param sender who sent the message
1591 * @param message the actual message
1592 * @param atsi performance data for the connection
1593 * @return GNUNET_OK to keep the connection open,
1594 * GNUNET_SYSERR to close it (signal serious error)
1597 client_handle_transmit_close (void *cls,
1598 struct GNUNET_MESH_Tunnel *tunnel,
1600 const struct GNUNET_PeerIdentity *sender,
1601 const struct GNUNET_MessageHeader *message,
1602 const struct GNUNET_ATS_Information*atsi)
1604 struct GNUNET_STREAM_Socket *socket = cls;
1606 return handle_transmit_close (socket,
1609 (struct GNUNET_STREAM_MessageHeader *)message,
1615 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_*_CLOSE_ACK messages
1617 * @param socket the socket
1618 * @param tunnel connection to the other end
1619 * @param sender who sent the message
1620 * @param message the actual message
1621 * @param atsi performance data for the connection
1622 * @param operation the close operation which is being ACK'ed
1623 * @return GNUNET_OK to keep the connection open,
1624 * GNUNET_SYSERR to close it (signal serious error)
1627 handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
1628 struct GNUNET_MESH_Tunnel *tunnel,
1629 const struct GNUNET_PeerIdentity *sender,
1630 const struct GNUNET_STREAM_MessageHeader *message,
1631 const struct GNUNET_ATS_Information *atsi,
1634 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
1636 shutdown_handle = socket->shutdown_handle;
1637 if (NULL == shutdown_handle)
1639 LOG (GNUNET_ERROR_TYPE_DEBUG,
1640 "%s: Received CLOSE_ACK when shutdown handle is NULL\n",
1641 GNUNET_i2s (&socket->other_peer));
1648 switch (socket->state)
1650 case STATE_CLOSE_WAIT:
1651 if (SHUT_RDWR != shutdown_handle->operation)
1653 LOG (GNUNET_ERROR_TYPE_DEBUG,
1654 "%s: Received CLOSE_ACK when shutdown handle is not for "
1656 GNUNET_i2s (&socket->other_peer));
1660 LOG (GNUNET_ERROR_TYPE_DEBUG,
1661 "%s: Received CLOSE_ACK from %s\n",
1662 GNUNET_i2s (&socket->other_peer),
1663 GNUNET_i2s (&socket->other_peer));
1664 socket->state = STATE_CLOSED;
1667 LOG (GNUNET_ERROR_TYPE_DEBUG,
1668 "%s: Received CLOSE_ACK when in it not expected\n",
1669 GNUNET_i2s (&socket->other_peer));
1675 switch (socket->state)
1677 case STATE_RECEIVE_CLOSE_WAIT:
1678 if (SHUT_RD != shutdown_handle->operation)
1680 LOG (GNUNET_ERROR_TYPE_DEBUG,
1681 "%s: Received RECEIVE_CLOSE_ACK when shutdown handle "
1682 "is not for SHUT_RD\n",
1683 GNUNET_i2s (&socket->other_peer));
1687 LOG (GNUNET_ERROR_TYPE_DEBUG,
1688 "%s: Received RECEIVE_CLOSE_ACK from %s\n",
1689 GNUNET_i2s (&socket->other_peer),
1690 GNUNET_i2s (&socket->other_peer));
1691 socket->state = STATE_RECEIVE_CLOSED;
1694 LOG (GNUNET_ERROR_TYPE_DEBUG,
1695 "%s: Received RECEIVE_CLOSE_ACK when in it not expected\n",
1696 GNUNET_i2s (&socket->other_peer));
1702 switch (socket->state)
1704 case STATE_TRANSMIT_CLOSE_WAIT:
1705 if (SHUT_WR != shutdown_handle->operation)
1707 LOG (GNUNET_ERROR_TYPE_DEBUG,
1708 "%s: Received TRANSMIT_CLOSE_ACK when shutdown handle "
1709 "is not for SHUT_WR\n",
1710 GNUNET_i2s (&socket->other_peer));
1714 LOG (GNUNET_ERROR_TYPE_DEBUG,
1715 "%s: Received TRANSMIT_CLOSE_ACK from %s\n",
1716 GNUNET_i2s (&socket->other_peer),
1717 GNUNET_i2s (&socket->other_peer));
1718 socket->state = STATE_TRANSMIT_CLOSED;
1721 LOG (GNUNET_ERROR_TYPE_DEBUG,
1722 "%s: Received TRANSMIT_CLOSE_ACK when in it not expected\n",
1723 GNUNET_i2s (&socket->other_peer));
1732 if (NULL != shutdown_handle->completion_cb) /* Shutdown completion */
1733 shutdown_handle->completion_cb(shutdown_handle->completion_cls,
1735 GNUNET_free (shutdown_handle); /* Free shutdown handle */
1736 socket->shutdown_handle = NULL;
1737 if (GNUNET_SCHEDULER_NO_TASK
1738 != shutdown_handle->close_msg_retransmission_task_id)
1740 GNUNET_SCHEDULER_cancel
1741 (shutdown_handle->close_msg_retransmission_task_id);
1742 shutdown_handle->close_msg_retransmission_task_id =
1743 GNUNET_SCHEDULER_NO_TASK;
1750 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1752 * @param cls the socket (set from GNUNET_MESH_connect)
1753 * @param tunnel connection to the other end
1754 * @param tunnel_ctx this is NULL
1755 * @param sender who sent the message
1756 * @param message the actual message
1757 * @param atsi performance data for the connection
1758 * @return GNUNET_OK to keep the connection open,
1759 * GNUNET_SYSERR to close it (signal serious error)
1762 client_handle_transmit_close_ack (void *cls,
1763 struct GNUNET_MESH_Tunnel *tunnel,
1765 const struct GNUNET_PeerIdentity *sender,
1766 const struct GNUNET_MessageHeader *message,
1767 const struct GNUNET_ATS_Information*atsi)
1769 struct GNUNET_STREAM_Socket *socket = cls;
1771 return handle_generic_close_ack (socket,
1774 (const struct GNUNET_STREAM_MessageHeader *)
1782 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1784 * @param socket the socket
1785 * @param tunnel connection to the other end
1786 * @param sender who sent the message
1787 * @param message the actual message
1788 * @param atsi performance data for the connection
1789 * @return GNUNET_OK to keep the connection open,
1790 * GNUNET_SYSERR to close it (signal serious error)
1793 handle_receive_close (struct GNUNET_STREAM_Socket *socket,
1794 struct GNUNET_MESH_Tunnel *tunnel,
1795 const struct GNUNET_PeerIdentity *sender,
1796 const struct GNUNET_STREAM_MessageHeader *message,
1797 const struct GNUNET_ATS_Information *atsi)
1799 struct GNUNET_STREAM_MessageHeader *receive_close_ack;
1801 switch (socket->state)
1805 case STATE_HELLO_WAIT:
1806 LOG (GNUNET_ERROR_TYPE_DEBUG,
1807 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1808 GNUNET_i2s (&socket->other_peer));
1814 LOG (GNUNET_ERROR_TYPE_DEBUG,
1815 "%s: Received RECEIVE_CLOSE from %s\n",
1816 GNUNET_i2s (&socket->other_peer),
1817 GNUNET_i2s (&socket->other_peer));
1819 GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1820 receive_close_ack->header.size =
1821 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1822 receive_close_ack->header.type =
1823 htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK);
1824 queue_message (socket,
1829 /* FIXME: Handle the case where write handle is present; the write operation
1830 should be deemed as finised and the write continuation callback
1831 has to be called with the stream status GNUNET_STREAM_SHUTDOWN */
1837 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1839 * @param cls the socket (set from GNUNET_MESH_connect)
1840 * @param tunnel connection to the other end
1841 * @param tunnel_ctx this is NULL
1842 * @param sender who sent the message
1843 * @param message the actual message
1844 * @param atsi performance data for the connection
1845 * @return GNUNET_OK to keep the connection open,
1846 * GNUNET_SYSERR to close it (signal serious error)
1849 client_handle_receive_close (void *cls,
1850 struct GNUNET_MESH_Tunnel *tunnel,
1852 const struct GNUNET_PeerIdentity *sender,
1853 const struct GNUNET_MessageHeader *message,
1854 const struct GNUNET_ATS_Information*atsi)
1856 struct GNUNET_STREAM_Socket *socket = cls;
1859 handle_receive_close (socket,
1862 (const struct GNUNET_STREAM_MessageHeader *) message,
1868 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1870 * @param cls the socket (set from GNUNET_MESH_connect)
1871 * @param tunnel connection to the other end
1872 * @param tunnel_ctx this is NULL
1873 * @param sender who sent the message
1874 * @param message the actual message
1875 * @param atsi performance data for the connection
1876 * @return GNUNET_OK to keep the connection open,
1877 * GNUNET_SYSERR to close it (signal serious error)
1880 client_handle_receive_close_ack (void *cls,
1881 struct GNUNET_MESH_Tunnel *tunnel,
1883 const struct GNUNET_PeerIdentity *sender,
1884 const struct GNUNET_MessageHeader *message,
1885 const struct GNUNET_ATS_Information*atsi)
1887 struct GNUNET_STREAM_Socket *socket = cls;
1889 return handle_generic_close_ack (socket,
1892 (const struct GNUNET_STREAM_MessageHeader *)
1900 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1902 * @param socket the socket
1903 * @param tunnel connection to the other end
1904 * @param sender who sent the message
1905 * @param message the actual message
1906 * @param atsi performance data for the connection
1907 * @return GNUNET_OK to keep the connection open,
1908 * GNUNET_SYSERR to close it (signal serious error)
1911 handle_close (struct GNUNET_STREAM_Socket *socket,
1912 struct GNUNET_MESH_Tunnel *tunnel,
1913 const struct GNUNET_PeerIdentity *sender,
1914 const struct GNUNET_STREAM_MessageHeader *message,
1915 const struct GNUNET_ATS_Information*atsi)
1917 struct GNUNET_STREAM_MessageHeader *close_ack;
1919 switch (socket->state)
1923 case STATE_HELLO_WAIT:
1924 LOG (GNUNET_ERROR_TYPE_DEBUG,
1925 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1926 GNUNET_i2s (&socket->other_peer));
1932 LOG (GNUNET_ERROR_TYPE_DEBUG,
1933 "%s: Received CLOSE from %s\n",
1934 GNUNET_i2s (&socket->other_peer),
1935 GNUNET_i2s (&socket->other_peer));
1936 close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1937 close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1938 close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK);
1939 queue_message (socket,
1943 if (socket->state == STATE_CLOSED)
1946 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1947 socket->receive_buffer = NULL;
1948 socket->receive_buffer_size = 0;
1954 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1956 * @param cls the socket (set from GNUNET_MESH_connect)
1957 * @param tunnel connection to the other end
1958 * @param tunnel_ctx this is NULL
1959 * @param sender who sent the message
1960 * @param message the actual message
1961 * @param atsi performance data for the connection
1962 * @return GNUNET_OK to keep the connection open,
1963 * GNUNET_SYSERR to close it (signal serious error)
1966 client_handle_close (void *cls,
1967 struct GNUNET_MESH_Tunnel *tunnel,
1969 const struct GNUNET_PeerIdentity *sender,
1970 const struct GNUNET_MessageHeader *message,
1971 const struct GNUNET_ATS_Information*atsi)
1973 struct GNUNET_STREAM_Socket *socket = cls;
1975 return handle_close (socket,
1978 (const struct GNUNET_STREAM_MessageHeader *) message,
1984 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1986 * @param cls the socket (set from GNUNET_MESH_connect)
1987 * @param tunnel connection to the other end
1988 * @param tunnel_ctx this is NULL
1989 * @param sender who sent the message
1990 * @param message the actual message
1991 * @param atsi performance data for the connection
1992 * @return GNUNET_OK to keep the connection open,
1993 * GNUNET_SYSERR to close it (signal serious error)
1996 client_handle_close_ack (void *cls,
1997 struct GNUNET_MESH_Tunnel *tunnel,
1999 const struct GNUNET_PeerIdentity *sender,
2000 const struct GNUNET_MessageHeader *message,
2001 const struct GNUNET_ATS_Information *atsi)
2003 struct GNUNET_STREAM_Socket *socket = cls;
2005 return handle_generic_close_ack (socket,
2008 (const struct GNUNET_STREAM_MessageHeader *)
2014 /*****************************/
2015 /* Server's Message Handlers */
2016 /*****************************/
2019 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
2021 * @param cls the closure
2022 * @param tunnel connection to the other end
2023 * @param tunnel_ctx the socket
2024 * @param sender who sent the message
2025 * @param message the actual message
2026 * @param atsi performance data for the connection
2027 * @return GNUNET_OK to keep the connection open,
2028 * GNUNET_SYSERR to close it (signal serious error)
2031 server_handle_data (void *cls,
2032 struct GNUNET_MESH_Tunnel *tunnel,
2034 const struct GNUNET_PeerIdentity *sender,
2035 const struct GNUNET_MessageHeader *message,
2036 const struct GNUNET_ATS_Information*atsi)
2038 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2040 return handle_data (socket,
2043 (const struct GNUNET_STREAM_DataMessage *)message,
2049 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
2051 * @param cls the closure
2052 * @param tunnel connection to the other end
2053 * @param tunnel_ctx the socket
2054 * @param sender who sent the message
2055 * @param message the actual message
2056 * @param atsi performance data for the connection
2057 * @return GNUNET_OK to keep the connection open,
2058 * GNUNET_SYSERR to close it (signal serious error)
2061 server_handle_hello (void *cls,
2062 struct GNUNET_MESH_Tunnel *tunnel,
2064 const struct GNUNET_PeerIdentity *sender,
2065 const struct GNUNET_MessageHeader *message,
2066 const struct GNUNET_ATS_Information*atsi)
2068 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2069 struct GNUNET_STREAM_HelloAckMessage *reply;
2071 if (0 != memcmp (sender,
2072 &socket->other_peer,
2073 sizeof (struct GNUNET_PeerIdentity)))
2075 LOG (GNUNET_ERROR_TYPE_DEBUG,
2076 "%s: Received HELLO from non-confirming peer\n",
2077 GNUNET_i2s (&socket->other_peer));
2081 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO ==
2082 ntohs (message->type));
2083 GNUNET_assert (socket->tunnel == tunnel);
2084 LOG (GNUNET_ERROR_TYPE_DEBUG,
2085 "%s: Received HELLO from %s\n",
2086 GNUNET_i2s (&socket->other_peer),
2087 GNUNET_i2s (&socket->other_peer));
2089 if (STATE_INIT == socket->state)
2091 reply = generate_hello_ack_msg (socket);
2092 queue_message (socket,
2094 &set_state_hello_wait,
2099 LOG (GNUNET_ERROR_TYPE_DEBUG,
2100 "%s: Client sent HELLO when in state %d\n",
2101 GNUNET_i2s (&socket->other_peer),
2103 /* FIXME: Send RESET? */
2111 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
2113 * @param cls the closure
2114 * @param tunnel connection to the other end
2115 * @param tunnel_ctx the socket
2116 * @param sender who sent the message
2117 * @param message the actual message
2118 * @param atsi performance data for the connection
2119 * @return GNUNET_OK to keep the connection open,
2120 * GNUNET_SYSERR to close it (signal serious error)
2123 server_handle_hello_ack (void *cls,
2124 struct GNUNET_MESH_Tunnel *tunnel,
2126 const struct GNUNET_PeerIdentity *sender,
2127 const struct GNUNET_MessageHeader *message,
2128 const struct GNUNET_ATS_Information*atsi)
2130 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2131 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
2133 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
2134 ntohs (message->type));
2135 GNUNET_assert (socket->tunnel == tunnel);
2136 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
2137 if (STATE_HELLO_WAIT == socket->state)
2139 LOG (GNUNET_ERROR_TYPE_DEBUG,
2140 "%s: Received HELLO_ACK from %s\n",
2141 GNUNET_i2s (&socket->other_peer),
2142 GNUNET_i2s (&socket->other_peer));
2143 socket->read_sequence_number = ntohl (ack_message->sequence_number);
2144 LOG (GNUNET_ERROR_TYPE_DEBUG,
2145 "%s: Read sequence number %u\n",
2146 GNUNET_i2s (&socket->other_peer),
2147 (unsigned int) socket->read_sequence_number);
2148 socket->receiver_window_available =
2149 ntohl (ack_message->receiver_window_size);
2150 /* Attain ESTABLISHED state */
2151 set_state_established (NULL, socket);
2155 LOG (GNUNET_ERROR_TYPE_DEBUG,
2156 "Client sent HELLO_ACK when in state %d\n", socket->state);
2157 /* FIXME: Send RESET? */
2165 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
2167 * @param cls the closure
2168 * @param tunnel connection to the other end
2169 * @param tunnel_ctx the socket
2170 * @param sender who sent the message
2171 * @param message the actual message
2172 * @param atsi performance data for the connection
2173 * @return GNUNET_OK to keep the connection open,
2174 * GNUNET_SYSERR to close it (signal serious error)
2177 server_handle_reset (void *cls,
2178 struct GNUNET_MESH_Tunnel *tunnel,
2180 const struct GNUNET_PeerIdentity *sender,
2181 const struct GNUNET_MessageHeader *message,
2182 const struct GNUNET_ATS_Information*atsi)
2184 // struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2191 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
2193 * @param cls the closure
2194 * @param tunnel connection to the other end
2195 * @param tunnel_ctx the socket
2196 * @param sender who sent the message
2197 * @param message the actual message
2198 * @param atsi performance data for the connection
2199 * @return GNUNET_OK to keep the connection open,
2200 * GNUNET_SYSERR to close it (signal serious error)
2203 server_handle_transmit_close (void *cls,
2204 struct GNUNET_MESH_Tunnel *tunnel,
2206 const struct GNUNET_PeerIdentity *sender,
2207 const struct GNUNET_MessageHeader *message,
2208 const struct GNUNET_ATS_Information*atsi)
2210 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2212 return handle_transmit_close (socket,
2215 (struct GNUNET_STREAM_MessageHeader *)message,
2221 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
2223 * @param cls the closure
2224 * @param tunnel connection to the other end
2225 * @param tunnel_ctx the socket
2226 * @param sender who sent the message
2227 * @param message the actual message
2228 * @param atsi performance data for the connection
2229 * @return GNUNET_OK to keep the connection open,
2230 * GNUNET_SYSERR to close it (signal serious error)
2233 server_handle_transmit_close_ack (void *cls,
2234 struct GNUNET_MESH_Tunnel *tunnel,
2236 const struct GNUNET_PeerIdentity *sender,
2237 const struct GNUNET_MessageHeader *message,
2238 const struct GNUNET_ATS_Information*atsi)
2240 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2242 return handle_generic_close_ack (socket,
2245 (const struct GNUNET_STREAM_MessageHeader *)
2253 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
2255 * @param cls the closure
2256 * @param tunnel connection to the other end
2257 * @param tunnel_ctx the socket
2258 * @param sender who sent the message
2259 * @param message the actual message
2260 * @param atsi performance data for the connection
2261 * @return GNUNET_OK to keep the connection open,
2262 * GNUNET_SYSERR to close it (signal serious error)
2265 server_handle_receive_close (void *cls,
2266 struct GNUNET_MESH_Tunnel *tunnel,
2268 const struct GNUNET_PeerIdentity *sender,
2269 const struct GNUNET_MessageHeader *message,
2270 const struct GNUNET_ATS_Information*atsi)
2272 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2275 handle_receive_close (socket,
2278 (const struct GNUNET_STREAM_MessageHeader *) message,
2284 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
2286 * @param cls the closure
2287 * @param tunnel connection to the other end
2288 * @param tunnel_ctx the socket
2289 * @param sender who sent the message
2290 * @param message the actual message
2291 * @param atsi performance data for the connection
2292 * @return GNUNET_OK to keep the connection open,
2293 * GNUNET_SYSERR to close it (signal serious error)
2296 server_handle_receive_close_ack (void *cls,
2297 struct GNUNET_MESH_Tunnel *tunnel,
2299 const struct GNUNET_PeerIdentity *sender,
2300 const struct GNUNET_MessageHeader *message,
2301 const struct GNUNET_ATS_Information*atsi)
2303 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2305 return handle_generic_close_ack (socket,
2308 (const struct GNUNET_STREAM_MessageHeader *)
2316 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2318 * @param cls the listen socket (from GNUNET_MESH_connect in
2319 * GNUNET_STREAM_listen)
2320 * @param tunnel connection to the other end
2321 * @param tunnel_ctx the socket
2322 * @param sender who sent the message
2323 * @param message the actual message
2324 * @param atsi performance data for the connection
2325 * @return GNUNET_OK to keep the connection open,
2326 * GNUNET_SYSERR to close it (signal serious error)
2329 server_handle_close (void *cls,
2330 struct GNUNET_MESH_Tunnel *tunnel,
2332 const struct GNUNET_PeerIdentity *sender,
2333 const struct GNUNET_MessageHeader *message,
2334 const struct GNUNET_ATS_Information*atsi)
2336 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2338 return handle_close (socket,
2341 (const struct GNUNET_STREAM_MessageHeader *) message,
2347 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2349 * @param cls the closure
2350 * @param tunnel connection to the other end
2351 * @param tunnel_ctx the socket
2352 * @param sender who sent the message
2353 * @param message the actual message
2354 * @param atsi performance data for the connection
2355 * @return GNUNET_OK to keep the connection open,
2356 * GNUNET_SYSERR to close it (signal serious error)
2359 server_handle_close_ack (void *cls,
2360 struct GNUNET_MESH_Tunnel *tunnel,
2362 const struct GNUNET_PeerIdentity *sender,
2363 const struct GNUNET_MessageHeader *message,
2364 const struct GNUNET_ATS_Information*atsi)
2366 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2368 return handle_generic_close_ack (socket,
2371 (const struct GNUNET_STREAM_MessageHeader *)
2379 * Handler for DATA_ACK messages
2381 * @param socket the socket through which the ack was received
2382 * @param tunnel connection to the other end
2383 * @param sender who sent the message
2384 * @param ack the acknowledgment message
2385 * @param atsi performance data for the connection
2386 * @return GNUNET_OK to keep the connection open,
2387 * GNUNET_SYSERR to close it (signal serious error)
2390 handle_ack (struct GNUNET_STREAM_Socket *socket,
2391 struct GNUNET_MESH_Tunnel *tunnel,
2392 const struct GNUNET_PeerIdentity *sender,
2393 const struct GNUNET_STREAM_AckMessage *ack,
2394 const struct GNUNET_ATS_Information*atsi)
2396 unsigned int packet;
2397 int need_retransmission;
2398 uint32_t sequence_difference;
2400 if (0 != memcmp (sender,
2401 &socket->other_peer,
2402 sizeof (struct GNUNET_PeerIdentity)))
2404 LOG (GNUNET_ERROR_TYPE_DEBUG,
2405 "%s: Received ACK from non-confirming peer\n",
2406 GNUNET_i2s (&socket->other_peer));
2409 switch (socket->state)
2411 case (STATE_ESTABLISHED):
2412 case (STATE_RECEIVE_CLOSED):
2413 case (STATE_RECEIVE_CLOSE_WAIT):
2414 if (NULL == socket->write_handle)
2416 LOG (GNUNET_ERROR_TYPE_DEBUG,
2417 "%s: Received DATA_ACK when write_handle is NULL\n",
2418 GNUNET_i2s (&socket->other_peer));
2421 /* FIXME: increment in the base sequence number is breaking current flow
2423 if (!((socket->write_sequence_number
2424 - ntohl (ack->base_sequence_number)) < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
2426 LOG (GNUNET_ERROR_TYPE_DEBUG,
2427 "%s: Received DATA_ACK with unexpected base sequence number\n",
2428 GNUNET_i2s (&socket->other_peer));
2429 LOG (GNUNET_ERROR_TYPE_DEBUG,
2430 "%s: Current write sequence: %u; Ack's base sequence: %u\n",
2431 GNUNET_i2s (&socket->other_peer),
2432 socket->write_sequence_number,
2433 ntohl (ack->base_sequence_number));
2436 /* FIXME: include the case when write_handle is cancelled - ignore the
2438 LOG (GNUNET_ERROR_TYPE_DEBUG,
2439 "%s: Received DATA_ACK from %s\n",
2440 GNUNET_i2s (&socket->other_peer),
2441 GNUNET_i2s (&socket->other_peer));
2443 /* Cancel the retransmission task */
2444 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
2446 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
2447 socket->retransmission_timeout_task_id =
2448 GNUNET_SCHEDULER_NO_TASK;
2450 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2452 if (NULL == socket->write_handle->messages[packet]) break;
2453 /* BS: Base sequence from ack; PS: sequence num of current packet */
2454 sequence_difference = ntohl (ack->base_sequence_number)
2455 - ntohl (socket->write_handle->messages[packet]->sequence_number);
2456 /* case where BS = PS + GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
2457 if ((sequence_difference == GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
2458 || ((sequence_difference < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
2459 && (0 != sequence_difference))) /* case: BS > PS and BS != PS*/
2461 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap, packet,
2466 ackbitmap_is_bit_set (&socket->write_handle->ack_bitmap,
2467 -sequence_difference))/*inversion as PS >= BS */
2469 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap, packet,
2473 /* Update the receive window remaining
2474 FIXME : Should update with the value from a data ack with greater
2476 socket->receiver_window_available =
2477 ntohl (ack->receive_window_remaining);
2478 /* Check if we have received all acknowledgements */
2479 need_retransmission = GNUNET_NO;
2480 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2482 if (NULL == socket->write_handle->messages[packet]) break;
2483 if (GNUNET_YES != ackbitmap_is_bit_set
2484 (&socket->write_handle->ack_bitmap,packet))
2486 need_retransmission = GNUNET_YES;
2490 if (GNUNET_YES == need_retransmission)
2492 write_data (socket);
2494 else /* We have to call the write continuation callback now */
2496 struct GNUNET_STREAM_IOWriteHandle *write_handle;
2498 /* Free the packets */
2499 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2501 GNUNET_free_non_null (socket->write_handle->messages[packet]);
2503 write_handle = socket->write_handle;
2504 socket->write_handle = NULL;
2505 if (NULL != write_handle->write_cont)
2506 write_handle->write_cont (write_handle->write_cont_cls,
2508 write_handle->size);
2509 /* We are done with the write handle - Freeing it */
2510 GNUNET_free (write_handle);
2511 LOG (GNUNET_ERROR_TYPE_DEBUG,
2512 "%s: Write completion callback completed\n",
2513 GNUNET_i2s (&socket->other_peer));
2524 * Handler for DATA_ACK messages
2526 * @param cls the 'struct GNUNET_STREAM_Socket'
2527 * @param tunnel connection to the other end
2528 * @param tunnel_ctx unused
2529 * @param sender who sent the message
2530 * @param message the actual message
2531 * @param atsi performance data for the connection
2532 * @return GNUNET_OK to keep the connection open,
2533 * GNUNET_SYSERR to close it (signal serious error)
2536 client_handle_ack (void *cls,
2537 struct GNUNET_MESH_Tunnel *tunnel,
2539 const struct GNUNET_PeerIdentity *sender,
2540 const struct GNUNET_MessageHeader *message,
2541 const struct GNUNET_ATS_Information*atsi)
2543 struct GNUNET_STREAM_Socket *socket = cls;
2544 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2546 return handle_ack (socket, tunnel, sender, ack, atsi);
2551 * Handler for DATA_ACK messages
2553 * @param cls the server's listen socket
2554 * @param tunnel connection to the other end
2555 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2556 * @param sender who sent the message
2557 * @param message the actual message
2558 * @param atsi performance data for the connection
2559 * @return GNUNET_OK to keep the connection open,
2560 * GNUNET_SYSERR to close it (signal serious error)
2563 server_handle_ack (void *cls,
2564 struct GNUNET_MESH_Tunnel *tunnel,
2566 const struct GNUNET_PeerIdentity *sender,
2567 const struct GNUNET_MessageHeader *message,
2568 const struct GNUNET_ATS_Information*atsi)
2570 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2571 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2573 return handle_ack (socket, tunnel, sender, ack, atsi);
2578 * For client message handlers, the stream socket is in the
2581 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2582 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2583 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2584 sizeof (struct GNUNET_STREAM_AckMessage) },
2585 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2586 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2587 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2588 sizeof (struct GNUNET_STREAM_MessageHeader)},
2589 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2590 sizeof (struct GNUNET_STREAM_MessageHeader)},
2591 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2592 sizeof (struct GNUNET_STREAM_MessageHeader)},
2593 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2594 sizeof (struct GNUNET_STREAM_MessageHeader)},
2595 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2596 sizeof (struct GNUNET_STREAM_MessageHeader)},
2597 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2598 sizeof (struct GNUNET_STREAM_MessageHeader)},
2599 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2600 sizeof (struct GNUNET_STREAM_MessageHeader)},
2606 * For server message handlers, the stream socket is in the
2607 * tunnel context, and the listen socket in the closure argument.
2609 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2610 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2611 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2612 sizeof (struct GNUNET_STREAM_AckMessage) },
2613 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2614 sizeof (struct GNUNET_STREAM_MessageHeader)},
2615 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2616 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2617 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2618 sizeof (struct GNUNET_STREAM_MessageHeader)},
2619 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2620 sizeof (struct GNUNET_STREAM_MessageHeader)},
2621 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2622 sizeof (struct GNUNET_STREAM_MessageHeader)},
2623 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2624 sizeof (struct GNUNET_STREAM_MessageHeader)},
2625 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2626 sizeof (struct GNUNET_STREAM_MessageHeader)},
2627 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2628 sizeof (struct GNUNET_STREAM_MessageHeader)},
2629 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2630 sizeof (struct GNUNET_STREAM_MessageHeader)},
2636 * Function called when our target peer is connected to our tunnel
2638 * @param cls the socket for which this tunnel is created
2639 * @param peer the peer identity of the target
2640 * @param atsi performance data for the connection
2643 mesh_peer_connect_callback (void *cls,
2644 const struct GNUNET_PeerIdentity *peer,
2645 const struct GNUNET_ATS_Information * atsi)
2647 struct GNUNET_STREAM_Socket *socket = cls;
2648 struct GNUNET_STREAM_MessageHeader *message;
2650 if (0 != memcmp (peer,
2651 &socket->other_peer,
2652 sizeof (struct GNUNET_PeerIdentity)))
2654 LOG (GNUNET_ERROR_TYPE_DEBUG,
2655 "%s: A peer which is not our target has connected to our tunnel\n",
2660 LOG (GNUNET_ERROR_TYPE_DEBUG,
2661 "%s: Target peer %s connected\n",
2662 GNUNET_i2s (&socket->other_peer),
2663 GNUNET_i2s (&socket->other_peer));
2665 /* Set state to INIT */
2666 socket->state = STATE_INIT;
2668 /* Send HELLO message */
2669 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2670 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
2671 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2672 queue_message (socket,
2674 &set_state_hello_wait,
2677 /* Call open callback */
2678 if (NULL == socket->open_cb)
2680 LOG (GNUNET_ERROR_TYPE_DEBUG,
2681 "STREAM_open callback is NULL\n");
2687 * Function called when our target peer is disconnected from our tunnel
2689 * @param cls the socket associated which this tunnel
2690 * @param peer the peer identity of the target
2693 mesh_peer_disconnect_callback (void *cls,
2694 const struct GNUNET_PeerIdentity *peer)
2696 struct GNUNET_STREAM_Socket *socket=cls;
2698 /* If the state is SHUTDOWN its ok; else set the state of the socket to SYSERR */
2699 LOG (GNUNET_ERROR_TYPE_DEBUG,
2700 "%s: Other peer %s disconnected \n",
2701 GNUNET_i2s (&socket->other_peer),
2702 GNUNET_i2s (&socket->other_peer));
2707 * Method called whenever a peer creates a tunnel to us
2709 * @param cls closure
2710 * @param tunnel new handle to the tunnel
2711 * @param initiator peer that started the tunnel
2712 * @param atsi performance information for the tunnel
2713 * @return initial tunnel context for the tunnel
2714 * (can be NULL -- that's not an error)
2717 new_tunnel_notify (void *cls,
2718 struct GNUNET_MESH_Tunnel *tunnel,
2719 const struct GNUNET_PeerIdentity *initiator,
2720 const struct GNUNET_ATS_Information *atsi)
2722 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2723 struct GNUNET_STREAM_Socket *socket;
2725 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2726 from the same peer again until the socket is closed */
2728 if (GNUNET_NO == lsocket->listening)
2730 LOG (GNUNET_ERROR_TYPE_DEBUG,
2731 "%s: Destroying tunnel from peer %s as we don't have the lock\n",
2732 GNUNET_i2s (&socket->other_peer),
2733 GNUNET_i2s (&socket->other_peer));
2734 GNUNET_MESH_tunnel_destroy (tunnel);
2737 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2738 socket->other_peer = *initiator;
2739 socket->tunnel = tunnel;
2740 socket->session_id = 0; /* FIXME */
2741 socket->state = STATE_INIT;
2742 socket->lsocket = lsocket;
2743 socket->retransmit_timeout = lsocket->retransmit_timeout;
2744 socket->testing_active = lsocket->testing_active;
2745 socket->testing_set_write_sequence_number_value =
2746 lsocket->testing_set_write_sequence_number_value;
2748 LOG (GNUNET_ERROR_TYPE_DEBUG,
2749 "%s: Peer %s initiated tunnel to us\n",
2750 GNUNET_i2s (&socket->other_peer),
2751 GNUNET_i2s (&socket->other_peer));
2753 /* FIXME: Copy MESH handle from lsocket to socket */
2760 * Function called whenever an inbound tunnel is destroyed. Should clean up
2761 * any associated state. This function is NOT called if the client has
2762 * explicitly asked for the tunnel to be destroyed using
2763 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2766 * @param cls closure (set from GNUNET_MESH_connect)
2767 * @param tunnel connection to the other end (henceforth invalid)
2768 * @param tunnel_ctx place where local state associated
2769 * with the tunnel is stored
2772 tunnel_cleaner (void *cls,
2773 const struct GNUNET_MESH_Tunnel *tunnel,
2776 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2778 if (tunnel != socket->tunnel)
2782 LOG (GNUNET_ERROR_TYPE_DEBUG,
2783 "%s: Peer %s has terminated connection abruptly\n",
2784 GNUNET_i2s (&socket->other_peer),
2785 GNUNET_i2s (&socket->other_peer));
2787 socket->status = GNUNET_STREAM_SHUTDOWN;
2789 /* Clear Transmit handles */
2790 if (NULL != socket->transmit_handle)
2792 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2793 socket->transmit_handle = NULL;
2795 if (NULL != socket->ack_transmit_handle)
2797 GNUNET_MESH_notify_transmit_ready_cancel (socket->ack_transmit_handle);
2798 GNUNET_free (socket->ack_msg);
2799 socket->ack_msg = NULL;
2800 socket->ack_transmit_handle = NULL;
2802 /* Stop Tasks using socket->tunnel */
2803 if (GNUNET_SCHEDULER_NO_TASK != socket->ack_task_id)
2805 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2806 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2808 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
2810 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
2811 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
2813 /* FIXME: Cancel all other tasks using socket->tunnel */
2814 socket->tunnel = NULL;
2819 * Callback to signal timeout on lockmanager lock acquire
2821 * @param cls the ListenSocket
2822 * @param tc the scheduler task context
2825 lockmanager_acquire_timeout (void *cls,
2826 const struct GNUNET_SCHEDULER_TaskContext *tc)
2828 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2829 GNUNET_STREAM_ListenCallback listen_cb;
2830 void *listen_cb_cls;
2832 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2833 listen_cb = lsocket->listen_cb;
2834 listen_cb_cls = lsocket->listen_cb_cls;
2835 GNUNET_STREAM_listen_close (lsocket);
2836 if (NULL != listen_cb)
2837 listen_cb (listen_cb_cls, NULL, NULL);
2842 * Callback to notify us on the status changes on app_port lock
2844 * @param cls the ListenSocket
2845 * @param domain the domain name of the lock
2846 * @param lock the app_port
2847 * @param status the current status of the lock
2850 lock_status_change_cb (void *cls, const char *domain, uint32_t lock,
2851 enum GNUNET_LOCKMANAGER_Status status)
2853 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2855 GNUNET_assert (lock == (uint32_t) lsocket->port);
2856 if (GNUNET_LOCKMANAGER_SUCCESS == status)
2858 lsocket->listening = GNUNET_YES;
2859 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
2861 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
2862 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2864 if (NULL == lsocket->mesh)
2866 GNUNET_MESH_ApplicationType ports[] = {lsocket->port, 0};
2868 lsocket->mesh = GNUNET_MESH_connect (lsocket->cfg,
2869 RECEIVE_BUFFER_SIZE, /* FIXME: QUEUE size as parameter? */
2870 lsocket, /* Closure */
2873 server_message_handlers,
2875 GNUNET_assert (NULL != lsocket->mesh);
2878 if (GNUNET_LOCKMANAGER_RELEASE == status)
2879 lsocket->listening = GNUNET_NO;
2889 * Tries to open a stream to the target peer
2891 * @param cfg configuration to use
2892 * @param target the target peer to which the stream has to be opened
2893 * @param app_port the application port number which uniquely identifies this
2895 * @param open_cb this function will be called after stream has be established
2896 * @param open_cb_cls the closure for open_cb
2897 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2898 * @return if successful it returns the stream socket; NULL if stream cannot be
2901 struct GNUNET_STREAM_Socket *
2902 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2903 const struct GNUNET_PeerIdentity *target,
2904 GNUNET_MESH_ApplicationType app_port,
2905 GNUNET_STREAM_OpenCallback open_cb,
2909 struct GNUNET_STREAM_Socket *socket;
2910 enum GNUNET_STREAM_Option option;
2911 GNUNET_MESH_ApplicationType ports[] = {app_port, 0};
2912 va_list vargs; /* Variable arguments */
2914 LOG (GNUNET_ERROR_TYPE_DEBUG,
2916 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2917 socket->other_peer = *target;
2918 socket->open_cb = open_cb;
2919 socket->open_cls = open_cb_cls;
2921 socket->retransmit_timeout =
2922 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
2923 socket->testing_active = GNUNET_NO;
2924 va_start (vargs, open_cb_cls); /* Parse variable args */
2926 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2929 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2930 /* Expect struct GNUNET_TIME_Relative */
2931 socket->retransmit_timeout = va_arg (vargs,
2932 struct GNUNET_TIME_Relative);
2934 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
2935 socket->testing_active = GNUNET_YES;
2936 socket->testing_set_write_sequence_number_value = va_arg (vargs,
2939 case GNUNET_STREAM_OPTION_END:
2942 } while (GNUNET_STREAM_OPTION_END != option);
2943 va_end (vargs); /* End of variable args parsing */
2944 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2945 RECEIVE_BUFFER_SIZE, /* QUEUE size as parameter? */
2947 NULL, /* No inbound tunnel handler */
2948 NULL, /* No in-tunnel cleaner */
2949 client_message_handlers,
2950 ports); /* We don't get inbound tunnels */
2951 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2953 GNUNET_free (socket);
2957 /* Now create the mesh tunnel to target */
2958 LOG (GNUNET_ERROR_TYPE_DEBUG,
2959 "Creating MESH Tunnel\n");
2960 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2961 NULL, /* Tunnel context */
2962 &mesh_peer_connect_callback,
2963 &mesh_peer_disconnect_callback,
2965 GNUNET_assert (NULL != socket->tunnel);
2966 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2967 &socket->other_peer);
2969 LOG (GNUNET_ERROR_TYPE_DEBUG,
2970 "%s() END\n", __func__);
2976 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
2978 * @param socket the stream socket
2979 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
2980 * @param completion_cb the callback that will be called upon successful
2981 * shutdown of given operation
2982 * @param completion_cls the closure for the completion callback
2983 * @return the shutdown handle
2985 struct GNUNET_STREAM_ShutdownHandle *
2986 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2988 GNUNET_STREAM_ShutdownCompletion completion_cb,
2989 void *completion_cls)
2991 struct GNUNET_STREAM_ShutdownHandle *handle;
2992 struct GNUNET_STREAM_MessageHeader *msg;
2994 GNUNET_assert (NULL == socket->shutdown_handle);
2996 handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle));
2997 handle->socket = socket;
2998 handle->completion_cb = completion_cb;
2999 handle->completion_cls = completion_cls;
3000 socket->shutdown_handle = handle;
3002 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
3003 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
3007 handle->operation = SHUT_RD;
3008 if (NULL != socket->read_handle)
3009 LOG (GNUNET_ERROR_TYPE_WARNING,
3010 "Existing read handle should be cancelled before shutting"
3012 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
3013 queue_message (socket,
3015 &set_state_receive_close_wait,
3019 handle->operation = SHUT_WR;
3020 if (NULL != socket->write_handle)
3021 LOG (GNUNET_ERROR_TYPE_WARNING,
3022 "Existing write handle should be cancelled before shutting"
3024 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
3025 queue_message (socket,
3027 &set_state_transmit_close_wait,
3031 handle->operation = SHUT_RDWR;
3032 if (NULL != socket->write_handle)
3033 LOG (GNUNET_ERROR_TYPE_WARNING,
3034 "Existing write handle should be cancelled before shutting"
3036 if (NULL != socket->read_handle)
3037 LOG (GNUNET_ERROR_TYPE_WARNING,
3038 "Existing read handle should be cancelled before shutting"
3040 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
3041 queue_message (socket,
3043 &set_state_close_wait,
3047 LOG (GNUNET_ERROR_TYPE_WARNING,
3048 "GNUNET_STREAM_shutdown called with invalid value for "
3049 "parameter operation -- Ignoring\n");
3051 GNUNET_free (handle);
3054 handle->close_msg_retransmission_task_id =
3055 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
3056 &close_msg_retransmission_task,
3063 * Cancels a pending shutdown
3065 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
3068 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle)
3070 if (GNUNET_SCHEDULER_NO_TASK != handle->close_msg_retransmission_task_id)
3071 GNUNET_SCHEDULER_cancel (handle->close_msg_retransmission_task_id);
3072 GNUNET_free (handle);
3079 * @param socket the stream socket
3082 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
3084 struct MessageQueue *head;
3086 if (NULL != socket->read_handle)
3088 LOG (GNUNET_ERROR_TYPE_WARNING,
3089 "Closing STREAM socket when a read handle is pending\n");
3091 if (NULL != socket->write_handle)
3093 LOG (GNUNET_ERROR_TYPE_WARNING,
3094 "Closing STREAM socket when a write handle is pending\n");
3095 GNUNET_STREAM_io_write_cancel (socket->write_handle);
3096 //socket->write_handle = NULL;
3099 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
3101 /* socket closed with read task pending!? */
3103 GNUNET_SCHEDULER_cancel (socket->read_task_id);
3104 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
3107 /* Terminate the ack'ing tasks if they are still present */
3108 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
3110 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
3111 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
3114 /* Clear Transmit handles */
3115 if (NULL != socket->transmit_handle)
3117 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
3118 socket->transmit_handle = NULL;
3120 if (NULL != socket->ack_transmit_handle)
3122 GNUNET_MESH_notify_transmit_ready_cancel (socket->ack_transmit_handle);
3123 GNUNET_free (socket->ack_msg);
3124 socket->ack_msg = NULL;
3125 socket->ack_transmit_handle = NULL;
3128 /* Clear existing message queue */
3129 while (NULL != (head = socket->queue_head)) {
3130 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
3133 GNUNET_free (head->message);
3137 /* Close associated tunnel */
3138 if (NULL != socket->tunnel)
3140 GNUNET_MESH_tunnel_destroy (socket->tunnel);
3141 socket->tunnel = NULL;
3144 /* Close mesh connection */
3145 if (NULL != socket->mesh && NULL == socket->lsocket)
3147 GNUNET_MESH_disconnect (socket->mesh);
3148 socket->mesh = NULL;
3151 /* Release receive buffer */
3152 if (NULL != socket->receive_buffer)
3154 GNUNET_free (socket->receive_buffer);
3157 GNUNET_free (socket);
3162 * Listens for stream connections for a specific application ports
3164 * @param cfg the configuration to use
3165 * @param app_port the application port for which new streams will be accepted
3166 * @param listen_cb this function will be called when a peer tries to establish
3168 * @param listen_cb_cls closure for listen_cb
3169 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
3170 * @return listen socket, NULL for any error
3172 struct GNUNET_STREAM_ListenSocket *
3173 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
3174 GNUNET_MESH_ApplicationType app_port,
3175 GNUNET_STREAM_ListenCallback listen_cb,
3176 void *listen_cb_cls,
3179 /* FIXME: Add variable args for passing configration options? */
3180 struct GNUNET_STREAM_ListenSocket *lsocket;
3181 enum GNUNET_STREAM_Option option;
3184 GNUNET_assert (NULL != listen_cb);
3185 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
3186 lsocket->cfg = GNUNET_CONFIGURATION_dup (cfg);
3187 lsocket->lockmanager = GNUNET_LOCKMANAGER_connect (lsocket->cfg);
3188 if (NULL == lsocket->lockmanager)
3190 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3191 GNUNET_free (lsocket);
3194 lsocket->listening = GNUNET_NO;/* We listen when we get a lock on app_port */
3196 lsocket->retransmit_timeout =
3197 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
3198 lsocket->testing_active = GNUNET_NO;
3199 va_start (vargs, listen_cb_cls);
3201 option = va_arg (vargs, enum GNUNET_STREAM_Option);
3204 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
3205 lsocket->retransmit_timeout = va_arg (vargs,
3206 struct GNUNET_TIME_Relative);
3208 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
3209 lsocket->testing_active = GNUNET_YES;
3210 lsocket->testing_set_write_sequence_number_value = va_arg (vargs,
3213 case GNUNET_STREAM_OPTION_END:
3216 } while (GNUNET_STREAM_OPTION_END != option);
3218 lsocket->port = app_port;
3219 lsocket->listen_cb = listen_cb;
3220 lsocket->listen_cb_cls = listen_cb_cls;
3221 lsocket->locking_request =
3222 GNUNET_LOCKMANAGER_acquire_lock (lsocket->lockmanager, locking_domain,
3223 (uint32_t) lsocket->port,
3224 &lock_status_change_cb, lsocket);
3225 lsocket->lockmanager_acquire_timeout_task =
3226 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (20),
3227 &lockmanager_acquire_timeout, lsocket);
3233 * Closes the listen socket
3235 * @param lsocket the listen socket
3238 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
3240 /* Close MESH connection */
3241 if (NULL != lsocket->mesh)
3242 GNUNET_MESH_disconnect (lsocket->mesh);
3243 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3244 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
3245 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
3246 if (NULL != lsocket->locking_request)
3247 GNUNET_LOCKMANAGER_cancel_request (lsocket->locking_request);
3248 if (NULL != lsocket->lockmanager)
3249 GNUNET_LOCKMANAGER_disconnect (lsocket->lockmanager);
3250 GNUNET_free (lsocket);
3255 * Tries to write the given data to the stream. The maximum size of data that
3256 * can be written as part of a write operation is (64 * (64000 - sizeof (struct
3257 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
3258 * violation, however only the said number of maximum bytes will be written.
3260 * @param socket the socket representing a stream
3261 * @param data the data buffer from where the data is written into the stream
3262 * @param size the number of bytes to be written from the data buffer
3263 * @param timeout the timeout period
3264 * @param write_cont the function to call upon writing some bytes into the
3266 * @param write_cont_cls the closure
3268 * @return handle to cancel the operation; if a previous write is pending or
3269 * the stream has been shutdown for this operation then write_cont is
3270 * immediately called and NULL is returned.
3272 struct GNUNET_STREAM_IOWriteHandle *
3273 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
3276 struct GNUNET_TIME_Relative timeout,
3277 GNUNET_STREAM_CompletionContinuation write_cont,
3278 void *write_cont_cls)
3280 unsigned int num_needed_packets;
3281 unsigned int packet;
3282 struct GNUNET_STREAM_IOWriteHandle *io_handle;
3283 uint32_t packet_size;
3284 uint32_t payload_size;
3285 struct GNUNET_STREAM_DataMessage *data_msg;
3287 struct GNUNET_TIME_Relative ack_deadline;
3289 LOG (GNUNET_ERROR_TYPE_DEBUG,
3292 /* Return NULL if there is already a write request pending */
3293 if (NULL != socket->write_handle)
3299 switch (socket->state)
3301 case STATE_TRANSMIT_CLOSED:
3302 case STATE_TRANSMIT_CLOSE_WAIT:
3304 case STATE_CLOSE_WAIT:
3305 if (NULL != write_cont)
3306 write_cont (write_cont_cls, GNUNET_STREAM_SHUTDOWN, 0);
3307 LOG (GNUNET_ERROR_TYPE_DEBUG,
3308 "%s() END\n", __func__);
3312 case STATE_HELLO_WAIT:
3313 if (NULL != write_cont)
3314 /* FIXME: GNUNET_STREAM_SYSERR?? */
3315 write_cont (write_cont_cls, GNUNET_STREAM_SYSERR, 0);
3316 LOG (GNUNET_ERROR_TYPE_DEBUG,
3317 "%s() END\n", __func__);
3319 case STATE_ESTABLISHED:
3320 case STATE_RECEIVE_CLOSED:
3321 case STATE_RECEIVE_CLOSE_WAIT:
3325 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
3326 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
3327 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
3328 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
3329 io_handle->socket = socket;
3330 io_handle->write_cont = write_cont;
3331 io_handle->write_cont_cls = write_cont_cls;
3332 io_handle->size = size;
3334 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3335 determined from RTT */
3336 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
3337 /* Divide the given buffer into packets for sending */
3338 for (packet=0; packet < num_needed_packets; packet++)
3340 if ((packet + 1) * max_payload_size < size)
3342 payload_size = max_payload_size;
3343 packet_size = MAX_PACKET_SIZE;
3347 payload_size = size - packet * max_payload_size;
3348 packet_size = payload_size + sizeof (struct
3349 GNUNET_STREAM_DataMessage);
3351 io_handle->messages[packet] = GNUNET_malloc (packet_size);
3352 io_handle->messages[packet]->header.header.size = htons (packet_size);
3353 io_handle->messages[packet]->header.header.type =
3354 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
3355 io_handle->messages[packet]->sequence_number =
3356 htonl (socket->write_sequence_number++);
3357 io_handle->messages[packet]->offset = htonl (socket->write_offset);
3359 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3360 determined from RTT */
3361 io_handle->messages[packet]->ack_deadline =
3362 GNUNET_TIME_relative_hton (ack_deadline);
3363 data_msg = io_handle->messages[packet];
3364 /* Copy data from given buffer to the packet */
3365 memcpy (&data_msg[1],
3368 sweep += payload_size;
3369 socket->write_offset += payload_size;
3371 socket->write_handle = io_handle;
3372 write_data (socket);
3374 LOG (GNUNET_ERROR_TYPE_DEBUG,
3375 "%s() END\n", __func__);
3382 * Tries to read data from the stream.
3384 * @param socket the socket representing a stream
3385 * @param timeout the timeout period
3386 * @param proc function to call with data (once only)
3387 * @param proc_cls the closure for proc
3389 * @return handle to cancel the operation; if the stream has been shutdown for
3390 * this type of opeartion then the DataProcessor is immediately
3391 * called with GNUNET_STREAM_SHUTDOWN as status and NULL if returned
3393 struct GNUNET_STREAM_IOReadHandle *
3394 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
3395 struct GNUNET_TIME_Relative timeout,
3396 GNUNET_STREAM_DataProcessor proc,
3399 struct GNUNET_STREAM_IOReadHandle *read_handle;
3401 LOG (GNUNET_ERROR_TYPE_DEBUG,
3403 GNUNET_i2s (&socket->other_peer),
3406 /* Return NULL if there is already a read handle; the user has to cancel that
3407 first before continuing or has to wait until it is completed */
3408 if (NULL != socket->read_handle) return NULL;
3410 GNUNET_assert (NULL != proc);
3412 switch (socket->state)
3414 case STATE_RECEIVE_CLOSED:
3415 case STATE_RECEIVE_CLOSE_WAIT:
3417 case STATE_CLOSE_WAIT:
3418 proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
3419 LOG (GNUNET_ERROR_TYPE_DEBUG,
3421 GNUNET_i2s (&socket->other_peer),
3428 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
3429 read_handle->proc = proc;
3430 read_handle->proc_cls = proc_cls;
3431 socket->read_handle = read_handle;
3433 /* Check if we have a packet at bitmap 0 */
3434 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
3437 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
3442 /* Setup the read timeout task */
3443 socket->read_io_timeout_task_id =
3444 GNUNET_SCHEDULER_add_delayed (timeout,
3447 LOG (GNUNET_ERROR_TYPE_DEBUG,
3449 GNUNET_i2s (&socket->other_peer),
3456 * Cancel pending write operation.
3458 * @param ioh handle to operation to cancel
3461 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
3463 struct GNUNET_STREAM_Socket *socket = ioh->socket;
3464 unsigned int packet;
3466 GNUNET_assert (NULL != socket->write_handle);
3467 GNUNET_assert (socket->write_handle == ioh);
3469 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
3471 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
3472 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
3475 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
3477 if (NULL == ioh->messages[packet]) break;
3478 GNUNET_free (ioh->messages[packet]);
3481 GNUNET_free (socket->write_handle);
3482 socket->write_handle = NULL;
3487 * Cancel pending read operation.
3489 * @param ioh handle to operation to cancel
3492 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)
3497 /* end of stream_api.c */