2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * Checks for matching the sender and socket->other_peer in server
26 * Add code for write io timeout
28 * Include retransmission for control messages
32 * @file stream/stream_api.c
33 * @brief Implementation of the stream library
34 * @author Sree Harsha Totakura
39 #include "gnunet_common.h"
40 #include "gnunet_crypto_lib.h"
41 #include "gnunet_lockmanager_service.h"
42 #include "gnunet_stream_lib.h"
43 #include "stream_protocol.h"
46 * Generic logging shorthand
48 #define LOG(kind,...) \
49 GNUNET_log_from (kind, "stream-api", __VA_ARGS__)
52 * Debug logging shorthand
54 #define LOG_DEBUG(...) \
55 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
58 * Time in relative seconds shorthand
60 #define TIME_REL_SECS(sec) \
61 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
64 * The maximum packet size of a stream packet
66 #define DEFAULT_MAX_PAYLOAD_SIZE 64000
71 #define RECEIVE_BUFFER_SIZE 4096000
74 * states in the Protocol
79 * Client initialization state
84 * Listener initialization state
89 * Pre-connection establishment state
94 * State where a connection has been established
99 * State where the socket is closed on our side and waiting to be ACK'ed
101 STATE_RECEIVE_CLOSE_WAIT,
104 * State where the socket is closed for reading
106 STATE_RECEIVE_CLOSED,
109 * State where the socket is closed on our side and waiting to be ACK'ed
111 STATE_TRANSMIT_CLOSE_WAIT,
114 * State where the socket is closed for writing
116 STATE_TRANSMIT_CLOSED,
119 * State where the socket is closed on our side and waiting to be ACK'ed
124 * State where the socket is closed
131 * Functions of this type are called when a message is written
133 * @param cls the closure from queue_message
134 * @param socket the socket the written message was bound to
136 typedef void (*SendFinishCallback) (void *cls,
137 struct GNUNET_STREAM_Socket *socket);
141 * The send message queue
148 struct GNUNET_STREAM_MessageHeader *message;
151 * Callback to be called when the message is sent
153 SendFinishCallback finish_cb;
156 * The closure for finish_cb
161 * The next message in queue. Should be NULL in the last message
163 struct MessageQueue *next;
166 * The next message in queue. Should be NULL in the first message
168 struct MessageQueue *prev;
173 * The STREAM Socket Handler
175 struct GNUNET_STREAM_Socket
178 * Retransmission timeout
180 struct GNUNET_TIME_Relative retransmit_timeout;
183 * The Acknowledgement Bitmap
185 GNUNET_STREAM_AckBitmap ack_bitmap;
188 * Time when the Acknowledgement was queued
190 struct GNUNET_TIME_Absolute ack_time_registered;
193 * Queued Acknowledgement deadline
195 struct GNUNET_TIME_Relative ack_time_deadline;
200 struct GNUNET_MESH_Handle *mesh;
203 * The mesh tunnel handle
205 struct GNUNET_MESH_Tunnel *tunnel;
208 * Stream open closure
213 * Stream open callback
215 GNUNET_STREAM_OpenCallback open_cb;
218 * The current transmit handle (if a pending transmit request exists)
220 struct GNUNET_MESH_TransmitHandle *transmit_handle;
223 * The current message associated with the transmit handle
225 struct MessageQueue *queue_head;
228 * The queue tail, should always point to the last message in queue
230 struct MessageQueue *queue_tail;
233 * The write IO_handle associated with this socket
235 struct GNUNET_STREAM_IOWriteHandle *write_handle;
238 * The read IO_handle associated with this socket
240 struct GNUNET_STREAM_IOReadHandle *read_handle;
243 * The shutdown handle associated with this socket
245 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
248 * Buffer for storing received messages
250 void *receive_buffer;
253 * The listen socket from which this socket is derived. Should be NULL if it
254 * is not a derived socket
256 struct GNUNET_STREAM_ListenSocket *lsocket;
259 * The peer identity of the peer at the other end of the stream
261 struct GNUNET_PeerIdentity other_peer;
264 * Task identifier for retransmission task after timeout
266 GNUNET_SCHEDULER_TaskIdentifier data_retransmission_task_id;
269 * Task identifier for retransmission of control messages
271 GNUNET_SCHEDULER_TaskIdentifier control_retransmission_task_id;
274 * The task for sending timely Acks
276 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
279 * The state of the protocol associated with this socket
284 * The status of the socket
286 enum GNUNET_STREAM_Status status;
289 * The number of previous timeouts; FIXME: currently not used
291 unsigned int retries;
294 * The application port number (type: uint32_t)
296 GNUNET_MESH_ApplicationType app_port;
299 * Whether testing mode is active or not
304 * The write sequence number to be set incase of testing
306 uint32_t testing_set_write_sequence_number_value;
309 * Write sequence number. Set to random when sending HELLO(client) and
312 uint32_t write_sequence_number;
315 * Read sequence number. This number's value is determined during handshake
317 uint32_t read_sequence_number;
320 * The receiver buffer size
322 uint32_t receive_buffer_size;
325 * The receiver buffer boundaries
327 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
330 * receiver's available buffer after the last acknowledged packet
332 uint32_t receiver_window_available;
335 * The offset pointer used during write operation
337 uint32_t write_offset;
340 * The offset after which we are expecting data
342 uint32_t read_offset;
345 * The offset upto which user has read from the received buffer
347 uint32_t copy_offset;
350 * The maximum size of the data message payload this stream handle can send
352 uint16_t max_payload_size;
357 * A socket for listening
359 struct GNUNET_STREAM_ListenSocket
364 struct GNUNET_MESH_Handle *mesh;
369 struct GNUNET_CONFIGURATION_Handle *cfg;
372 * Handle to the lock manager service
374 struct GNUNET_LOCKMANAGER_Handle *lockmanager;
377 * The active LockingRequest from lockmanager
379 struct GNUNET_LOCKMANAGER_LockingRequest *locking_request;
382 * Callback to call after acquring a lock and listening
384 GNUNET_STREAM_ListenSuccessCallback listen_ok_cb;
387 * The callback function which is called after successful opening socket
389 GNUNET_STREAM_ListenCallback listen_cb;
392 * The call back closure
399 GNUNET_MESH_ApplicationType port;
402 * The id of the lockmanager timeout task
404 GNUNET_SCHEDULER_TaskIdentifier lockmanager_acquire_timeout_task;
407 * The retransmit timeout
409 struct GNUNET_TIME_Relative retransmit_timeout;
417 * Whether testing mode is active or not
422 * The write sequence number to be set incase of testing
424 uint32_t testing_set_write_sequence_number_value;
427 * The maximum size of the data message payload this stream handle can send
429 uint16_t max_payload_size;
435 * The IO Write Handle
437 struct GNUNET_STREAM_IOWriteHandle
440 * The socket to which this write handle is associated
442 struct GNUNET_STREAM_Socket *socket;
445 * The packet_buffers associated with this Handle
447 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
450 * The write continuation callback
452 GNUNET_STREAM_CompletionContinuation write_cont;
455 * Write continuation closure
457 void *write_cont_cls;
460 * The bitmap of this IOHandle; Corresponding bit for a message is set when
461 * it has been acknowledged by the receiver
463 GNUNET_STREAM_AckBitmap ack_bitmap;
466 * Number of bytes in this write handle
471 * Number of packets already transmitted from this IO handle. Retransmitted
472 * packets are not taken into account here. This is used to determine which
473 * packets account for retransmission and which packets occupy buffer space at
476 unsigned int packets_sent;
483 struct GNUNET_STREAM_IOReadHandle
486 * The socket to which this read handle is associated
488 struct GNUNET_STREAM_Socket *socket;
491 * Callback for the read processor
493 GNUNET_STREAM_DataProcessor proc;
496 * The closure pointer for the read processor callback
501 * Task identifier for the read io timeout task
503 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
506 * Task scheduled to continue a read operation.
508 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
513 * Handle for Shutdown
515 struct GNUNET_STREAM_ShutdownHandle
518 * The socket associated with this shutdown handle
520 struct GNUNET_STREAM_Socket *socket;
523 * Shutdown completion callback
525 GNUNET_STREAM_ShutdownCompletion completion_cb;
528 * Closure for completion callback
530 void *completion_cls;
533 * Close message retransmission task id
535 GNUNET_SCHEDULER_TaskIdentifier close_msg_retransmission_task_id;
538 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
545 * Default value in seconds for various timeouts
547 static const unsigned int default_timeout = 10;
550 * The domain name for locks we use here
552 static const char *locking_domain = "GNUNET_STREAM_APPLOCK";
556 * Callback function for sending queued message
558 * @param cls closure the socket
559 * @param size number of bytes available in buf
560 * @param buf where the callee should write the message
561 * @return number of bytes written to buf
564 send_message_notify (void *cls, size_t size, void *buf)
566 struct GNUNET_STREAM_Socket *socket = cls;
567 struct MessageQueue *head;
570 socket->transmit_handle = NULL; /* Remove the transmit handle */
571 head = socket->queue_head;
573 return 0; /* just to be safe */
574 if (0 == size) /* request timed out */
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "%s: Message sending timed out. Retry %d \n",
579 GNUNET_i2s (&socket->other_peer),
581 socket->transmit_handle =
582 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
583 GNUNET_NO, /* Corking */
584 /* FIXME: exponential backoff */
585 socket->retransmit_timeout,
587 ntohs (head->message->header.size),
588 &send_message_notify,
592 ret = ntohs (head->message->header.size);
593 GNUNET_assert (size >= ret);
594 memcpy (buf, head->message, ret);
595 if (NULL != head->finish_cb)
597 head->finish_cb (head->finish_cb_cls, socket);
599 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
602 GNUNET_free (head->message);
604 head = socket->queue_head;
605 if (NULL != head) /* more pending messages to send */
608 socket->transmit_handle =
609 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
610 GNUNET_NO, /* Corking */
611 /* FIXME: exponential backoff */
612 socket->retransmit_timeout,
614 ntohs (head->message->header.size),
615 &send_message_notify,
623 * Queues a message for sending using the mesh connection of a socket
625 * @param socket the socket whose mesh connection is used
626 * @param message the message to be sent
627 * @param finish_cb the callback to be called when the message is sent
628 * @param finish_cb_cls the closure for the callback
629 * @param urgent set to GNUNET_YES to add the message to the beginning of the
630 * queue; GNUNET_NO to add at the tail
633 queue_message (struct GNUNET_STREAM_Socket *socket,
634 struct GNUNET_STREAM_MessageHeader *message,
635 SendFinishCallback finish_cb,
639 struct MessageQueue *queue_entity;
642 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
643 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
644 LOG (GNUNET_ERROR_TYPE_DEBUG,
645 "%s: Queueing message of type %d and size %d\n",
646 GNUNET_i2s (&socket->other_peer),
647 ntohs (message->header.type),
648 ntohs (message->header.size));
649 GNUNET_assert (NULL != message);
650 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
651 queue_entity->message = message;
652 queue_entity->finish_cb = finish_cb;
653 queue_entity->finish_cb_cls = finish_cb_cls;
654 if (GNUNET_YES == urgent)
656 GNUNET_CONTAINER_DLL_insert (socket->queue_head, socket->queue_tail,
658 if (NULL != socket->transmit_handle)
660 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
661 socket->transmit_handle = NULL;
665 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
668 if (NULL == socket->transmit_handle)
671 socket->transmit_handle =
672 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
673 GNUNET_NO, /* Corking */
674 socket->retransmit_timeout,
676 ntohs (message->header.size),
677 &send_message_notify,
684 * Copies a message and queues it for sending using the mesh connection of
687 * @param socket the socket whose mesh connection is used
688 * @param message the message to be sent
689 * @param finish_cb the callback to be called when the message is sent
690 * @param finish_cb_cls the closure for the callback
693 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
694 const struct GNUNET_STREAM_MessageHeader *message,
695 SendFinishCallback finish_cb,
698 struct GNUNET_STREAM_MessageHeader *msg_copy;
701 size = ntohs (message->header.size);
702 msg_copy = GNUNET_malloc (size);
703 memcpy (msg_copy, message, size);
704 queue_message (socket, msg_copy, finish_cb, finish_cb_cls, GNUNET_NO);
709 * Writes data using the given socket. The amount of data written is limited by
710 * the receiver_window_size
712 * @param socket the socket to use
715 write_data (struct GNUNET_STREAM_Socket *socket);
719 * Task for retransmitting data messages if they aren't ACK before their ack
722 * @param cls the socket
723 * @param tc the Task context
726 data_retransmission_task (void *cls,
727 const struct GNUNET_SCHEDULER_TaskContext *tc)
729 struct GNUNET_STREAM_Socket *socket = cls;
731 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
732 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
734 LOG (GNUNET_ERROR_TYPE_DEBUG,
735 "%s: Retransmitting DATA...\n", GNUNET_i2s (&socket->other_peer));
741 * Task for sending ACK message
743 * @param cls the socket
744 * @param tc the Task context
748 const struct GNUNET_SCHEDULER_TaskContext *tc)
750 struct GNUNET_STREAM_Socket *socket = cls;
751 struct GNUNET_STREAM_AckMessage *ack_msg;
753 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
754 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
756 /* Create the ACK Message */
757 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
758 ack_msg->header.header.size = htons (sizeof (struct
759 GNUNET_STREAM_AckMessage));
760 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
761 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
762 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
763 ack_msg->receive_window_remaining =
764 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
765 /* Queue up ACK for immediate sending */
766 queue_message (socket, &ack_msg->header, NULL, NULL, GNUNET_YES);
771 * Retransmission task for shutdown messages
773 * @param cls the shutdown handle
774 * @param tc the Task Context
777 close_msg_retransmission_task (void *cls,
778 const struct GNUNET_SCHEDULER_TaskContext *tc)
780 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle = cls;
781 struct GNUNET_STREAM_MessageHeader *msg;
782 struct GNUNET_STREAM_Socket *socket;
784 shutdown_handle->close_msg_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
785 GNUNET_assert (NULL != shutdown_handle);
786 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
788 socket = shutdown_handle->socket;
789 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
790 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
791 switch (shutdown_handle->operation)
794 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
797 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
800 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
804 shutdown_handle->close_msg_retransmission_task_id =
805 GNUNET_SCHEDULER_NO_TASK;
808 queue_message (socket, msg, NULL, NULL, GNUNET_NO);
809 shutdown_handle->close_msg_retransmission_task_id =
810 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
811 &close_msg_retransmission_task,
817 * Function to modify a bit in GNUNET_STREAM_AckBitmap
819 * @param bitmap the bitmap to modify
820 * @param bit the bit number to modify
821 * @param value GNUNET_YES to on, GNUNET_NO to off
824 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
828 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
829 if (GNUNET_YES == value)
830 *bitmap |= (1LL << bit);
832 *bitmap &= ~(1LL << bit);
837 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
839 * @param bitmap address of the bitmap that has to be checked
840 * @param bit the bit number to check
841 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
844 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
847 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
848 return 0 != (*bitmap & (1LL << bit));
853 * Writes data using the given socket. The amount of data written is limited by
854 * the receiver_window_size
856 * @param socket the socket to use
859 write_data (struct GNUNET_STREAM_Socket *socket)
861 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
864 for (packet=0; packet < io_handle->packets_sent; packet++)
866 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
869 LOG (GNUNET_ERROR_TYPE_DEBUG,
870 "%s: Retransmitting DATA message with sequence %u\n",
871 GNUNET_i2s (&socket->other_peer),
872 ntohl (io_handle->messages[packet]->sequence_number));
873 copy_and_queue_message (socket,
874 &io_handle->messages[packet]->header,
879 /* Now send new packets if there is enough buffer space */
880 while ((packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH) &&
881 (NULL != io_handle->messages[packet]) &&
882 (socket->receiver_window_available
883 >= ntohs (io_handle->messages[packet]->header.header.size)))
885 socket->receiver_window_available -=
886 ntohs (io_handle->messages[packet]->header.header.size);
887 LOG (GNUNET_ERROR_TYPE_DEBUG,
888 "%s: Placing DATA message with sequence %u in send queue\n",
889 GNUNET_i2s (&socket->other_peer),
890 ntohl (io_handle->messages[packet]->sequence_number));
891 copy_and_queue_message (socket,
892 &io_handle->messages[packet]->header,
897 io_handle->packets_sent = packet;
898 // FIXME: 8s is not good, should use GNUNET_TIME_STD_BACKOFF...
899 if (GNUNET_SCHEDULER_NO_TASK == socket->data_retransmission_task_id)
900 socket->data_retransmission_task_id =
901 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
902 (GNUNET_TIME_UNIT_SECONDS, 8),
903 &data_retransmission_task,
909 * Task for calling the read processor
911 * @param cls the socket
912 * @param tc the task context
915 call_read_processor (void *cls,
916 const struct GNUNET_SCHEDULER_TaskContext *tc)
918 struct GNUNET_STREAM_Socket *socket = cls;
919 struct GNUNET_STREAM_IOReadHandle *read_handle;
921 size_t valid_read_size;
923 uint32_t sequence_increase;
924 uint32_t offset_increase;
926 read_handle = socket->read_handle;
927 GNUNET_assert (NULL != read_handle);
928 read_handle->read_task_id = GNUNET_SCHEDULER_NO_TASK;
929 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
931 if (NULL == socket->receive_buffer)
933 GNUNET_assert (NULL != socket->read_handle);
934 GNUNET_assert (NULL != socket->read_handle->proc);
935 /* Check the bitmap for any holes */
936 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
938 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
942 /* We only call read processor if we have the first packet */
943 GNUNET_assert (0 < packet);
945 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
946 GNUNET_assert (0 != valid_read_size);
947 /* Cancel the read_io_timeout_task */
948 GNUNET_SCHEDULER_cancel (read_handle->read_io_timeout_task_id);
949 read_handle->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
950 /* Call the data processor */
951 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Calling read processor\n",
952 GNUNET_i2s (&socket->other_peer));
954 socket->read_handle->proc (socket->read_handle->proc_cls,
956 socket->receive_buffer + socket->copy_offset,
958 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Read processor read %d bytes\n",
959 GNUNET_i2s (&socket->other_peer), read_size);
960 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Read processor completed successfully\n",
961 GNUNET_i2s (&socket->other_peer));
962 /* Free the read handle */
963 GNUNET_free (socket->read_handle);
964 socket->read_handle = NULL;
965 GNUNET_assert (read_size <= valid_read_size);
966 socket->copy_offset += read_size;
967 /* Determine upto which packet we can remove from the buffer */
968 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
970 if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
975 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
978 /* If no packets can be removed we can't move the buffer */
981 sequence_increase = packet;
982 LOG (GNUNET_ERROR_TYPE_DEBUG,
983 "%s: Sequence increase after read processor completion: %u\n",
984 GNUNET_i2s (&socket->other_peer), sequence_increase);
985 /* Shift the data in the receive buffer */
986 socket->receive_buffer =
987 memmove (socket->receive_buffer,
988 socket->receive_buffer
989 + socket->receive_buffer_boundaries[sequence_increase-1],
990 socket->receive_buffer_size
991 - socket->receive_buffer_boundaries[sequence_increase-1]);
992 /* Shift the bitmap */
993 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
994 /* Set read_sequence_number */
995 socket->read_sequence_number += sequence_increase;
996 /* Set read_offset */
997 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
998 socket->read_offset += offset_increase;
999 /* Fix copy_offset */
1000 GNUNET_assert (offset_increase <= socket->copy_offset);
1001 socket->copy_offset -= offset_increase;
1002 /* Fix relative boundaries */
1003 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1005 if (packet < (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase))
1007 uint32_t ahead_buffer_boundary;
1009 ahead_buffer_boundary =
1010 socket->receive_buffer_boundaries[packet + sequence_increase];
1011 if (0 == ahead_buffer_boundary)
1012 socket->receive_buffer_boundaries[packet] = 0;
1015 GNUNET_assert (offset_increase < ahead_buffer_boundary);
1016 socket->receive_buffer_boundaries[packet] =
1017 ahead_buffer_boundary - offset_increase;
1021 socket->receive_buffer_boundaries[packet] = 0;
1027 * Cancels the existing read io handle
1029 * @param cls the closure from the SCHEDULER call
1030 * @param tc the task context
1033 read_io_timeout (void *cls,
1034 const struct GNUNET_SCHEDULER_TaskContext *tc)
1036 struct GNUNET_STREAM_Socket *socket = cls;
1037 struct GNUNET_STREAM_IOReadHandle *read_handle;
1038 GNUNET_STREAM_DataProcessor proc;
1041 read_handle = socket->read_handle;
1042 GNUNET_assert (NULL != read_handle);
1043 read_handle->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
1044 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1046 if (read_handle->read_task_id != GNUNET_SCHEDULER_NO_TASK)
1048 LOG (GNUNET_ERROR_TYPE_DEBUG,
1049 "%s: Read task timedout - Cancelling it\n",
1050 GNUNET_i2s (&socket->other_peer));
1051 GNUNET_SCHEDULER_cancel (read_handle->read_task_id);
1052 read_handle->read_task_id = GNUNET_SCHEDULER_NO_TASK;
1054 proc = read_handle->proc;
1055 proc_cls = read_handle->proc_cls;
1056 GNUNET_free (read_handle);
1057 socket->read_handle = NULL;
1058 /* Call the read processor to signal timeout */
1060 GNUNET_STREAM_TIMEOUT,
1067 * Handler for DATA messages; Same for both client and server
1069 * @param socket the socket through which the ack was received
1070 * @param tunnel connection to the other end
1071 * @param sender who sent the message
1072 * @param msg the data message
1073 * @param atsi performance data for the connection
1074 * @return GNUNET_OK to keep the connection open,
1075 * GNUNET_SYSERR to close it (signal serious error)
1078 handle_data (struct GNUNET_STREAM_Socket *socket,
1079 struct GNUNET_MESH_Tunnel *tunnel,
1080 const struct GNUNET_PeerIdentity *sender,
1081 const struct GNUNET_STREAM_DataMessage *msg,
1082 const struct GNUNET_ATS_Information*atsi)
1084 const void *payload;
1085 struct GNUNET_TIME_Relative ack_deadline_rel;
1086 uint32_t bytes_needed;
1087 uint32_t relative_offset;
1088 uint32_t relative_sequence_number;
1091 size = htons (msg->header.header.size);
1092 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
1094 GNUNET_break_op (0);
1095 return GNUNET_SYSERR;
1097 if (0 != memcmp (sender, &socket->other_peer,
1098 sizeof (struct GNUNET_PeerIdentity)))
1100 LOG (GNUNET_ERROR_TYPE_DEBUG,
1101 "%s: Received DATA from non-confirming peer\n",
1102 GNUNET_i2s (&socket->other_peer));
1105 switch (socket->state)
1107 case STATE_ESTABLISHED:
1108 case STATE_TRANSMIT_CLOSED:
1109 case STATE_TRANSMIT_CLOSE_WAIT:
1110 /* check if the message's sequence number is in the range we are
1112 relative_sequence_number =
1113 ntohl (msg->sequence_number) - socket->read_sequence_number;
1114 if ( relative_sequence_number >= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1116 LOG (GNUNET_ERROR_TYPE_DEBUG,
1117 "%s: Ignoring received message with sequence number %u\n",
1118 GNUNET_i2s (&socket->other_peer),
1119 ntohl (msg->sequence_number));
1120 /* Start ACK sending task if one is not already present */
1121 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1123 socket->ack_task_id =
1124 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1125 (msg->ack_deadline),
1131 /* Check if we have already seen this message */
1132 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1133 relative_sequence_number))
1135 LOG (GNUNET_ERROR_TYPE_DEBUG,
1136 "%s: Ignoring already received message with sequence number %u\n",
1137 GNUNET_i2s (&socket->other_peer),
1138 ntohl (msg->sequence_number));
1139 /* Start ACK sending task if one is not already present */
1140 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1142 socket->ack_task_id =
1143 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1144 (msg->ack_deadline), &ack_task, socket);
1148 LOG (GNUNET_ERROR_TYPE_DEBUG,
1149 "%s: Receiving DATA with sequence number: %u and size: %d from %s\n",
1150 GNUNET_i2s (&socket->other_peer), ntohl (msg->sequence_number),
1151 ntohs (msg->header.header.size), GNUNET_i2s (&socket->other_peer));
1152 /* Check if we have to allocate the buffer */
1153 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1154 relative_offset = ntohl (msg->offset) - socket->read_offset;
1155 bytes_needed = relative_offset + size;
1156 if (bytes_needed > socket->receive_buffer_size)
1158 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1160 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1162 socket->receive_buffer_size = bytes_needed;
1166 LOG (GNUNET_ERROR_TYPE_DEBUG,
1167 "%s: Cannot accommodate packet %d as buffer is full\n",
1168 GNUNET_i2s (&socket->other_peer), ntohl (msg->sequence_number));
1172 /* Copy Data to buffer */
1174 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1175 memcpy (socket->receive_buffer + relative_offset, payload, size);
1176 socket->receive_buffer_boundaries[relative_sequence_number] =
1177 relative_offset + size;
1178 /* Modify the ACK bitmap */
1179 ackbitmap_modify_bit (&socket->ack_bitmap, relative_sequence_number,
1181 /* Start ACK sending task if one is not already present */
1182 ack_deadline_rel = GNUNET_TIME_relative_ntoh (msg->ack_deadline);
1183 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1186 GNUNET_TIME_relative_min (ack_deadline_rel,
1187 GNUNET_TIME_relative_multiply
1188 (GNUNET_TIME_UNIT_SECONDS, 300));
1189 socket->ack_task_id =
1190 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1191 (msg->ack_deadline), &ack_task, socket);
1192 socket->ack_time_registered = GNUNET_TIME_absolute_get ();
1193 socket->ack_time_deadline = ack_deadline_rel;
1197 struct GNUNET_TIME_Relative ack_time_past;
1198 struct GNUNET_TIME_Relative ack_time_remaining;
1199 struct GNUNET_TIME_Relative ack_time_min;
1201 GNUNET_TIME_absolute_get_duration (socket->ack_time_registered);
1202 ack_time_remaining = GNUNET_TIME_relative_subtract
1203 (socket->ack_time_deadline, ack_time_past);
1204 ack_time_min = GNUNET_TIME_relative_min (ack_time_remaining,
1206 if (0 == memcmp(&ack_deadline_rel, &ack_time_min,
1207 sizeof (struct GNUNET_TIME_Relative)))
1209 ack_deadline_rel = ack_time_min;
1210 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
1211 socket->ack_task_id = GNUNET_SCHEDULER_add_delayed (ack_deadline_rel,
1213 socket->ack_time_registered = GNUNET_TIME_absolute_get ();
1214 socket->ack_time_deadline = ack_deadline_rel;
1217 if ((NULL != socket->read_handle) /* A read handle is waiting */
1218 /* There is no current read task */
1219 && (GNUNET_SCHEDULER_NO_TASK == socket->read_handle->read_task_id)
1220 /* We have the first packet */
1221 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap, 0)))
1223 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Scheduling read processor\n",
1224 GNUNET_i2s (&socket->other_peer));
1225 socket->read_handle->read_task_id =
1226 GNUNET_SCHEDULER_add_now (&call_read_processor, socket);
1230 LOG (GNUNET_ERROR_TYPE_DEBUG,
1231 "%s: Received data message when it cannot be handled\n",
1232 GNUNET_i2s (&socket->other_peer));
1240 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1242 * @param cls the socket (set from GNUNET_MESH_connect)
1243 * @param tunnel connection to the other end
1244 * @param tunnel_ctx place to store local state associated with the tunnel
1245 * @param sender who sent the message
1246 * @param message the actual message
1247 * @param atsi performance data for the connection
1248 * @return GNUNET_OK to keep the connection open,
1249 * GNUNET_SYSERR to close it (signal serious error)
1252 client_handle_data (void *cls,
1253 struct GNUNET_MESH_Tunnel *tunnel,
1255 const struct GNUNET_PeerIdentity *sender,
1256 const struct GNUNET_MessageHeader *message,
1257 const struct GNUNET_ATS_Information*atsi)
1259 struct GNUNET_STREAM_Socket *socket = cls;
1261 return handle_data (socket, tunnel, sender,
1262 (const struct GNUNET_STREAM_DataMessage *) message, atsi);
1267 * Callback to set state to ESTABLISHED
1269 * @param cls the closure NULL;
1270 * @param socket the socket to requiring state change
1273 set_state_established (void *cls,
1274 struct GNUNET_STREAM_Socket *socket)
1276 LOG (GNUNET_ERROR_TYPE_DEBUG,
1277 "%s: Attaining ESTABLISHED state\n",
1278 GNUNET_i2s (&socket->other_peer));
1279 socket->write_offset = 0;
1280 socket->read_offset = 0;
1281 socket->state = STATE_ESTABLISHED;
1282 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
1283 socket->control_retransmission_task_id);
1284 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
1285 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
1286 if (NULL != socket->lsocket)
1288 LOG (GNUNET_ERROR_TYPE_DEBUG,
1289 "%s: Calling listen callback\n",
1290 GNUNET_i2s (&socket->other_peer));
1291 if (GNUNET_SYSERR ==
1292 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1294 &socket->other_peer))
1296 socket->state = STATE_CLOSED;
1297 /* FIXME: We should close in a decent way (send RST) */
1298 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1299 GNUNET_free (socket);
1303 socket->open_cb (socket->open_cls, socket);
1308 * Callback to set state to HELLO_WAIT
1310 * @param cls the closure from queue_message
1311 * @param socket the socket to requiring state change
1314 set_state_hello_wait (void *cls,
1315 struct GNUNET_STREAM_Socket *socket)
1317 GNUNET_assert (STATE_INIT == socket->state);
1318 LOG (GNUNET_ERROR_TYPE_DEBUG,
1319 "%s: Attaining HELLO_WAIT state\n",
1320 GNUNET_i2s (&socket->other_peer));
1321 socket->state = STATE_HELLO_WAIT;
1326 * Callback to set state to CLOSE_WAIT
1328 * @param cls the closure from queue_message
1329 * @param socket the socket requiring state change
1332 set_state_close_wait (void *cls,
1333 struct GNUNET_STREAM_Socket *socket)
1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1336 "%s: Attaing CLOSE_WAIT state\n",
1337 GNUNET_i2s (&socket->other_peer));
1338 socket->state = STATE_CLOSE_WAIT;
1339 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1340 socket->receive_buffer = NULL;
1341 socket->receive_buffer_size = 0;
1346 * Callback to set state to RECEIVE_CLOSE_WAIT
1348 * @param cls the closure from queue_message
1349 * @param socket the socket requiring state change
1352 set_state_receive_close_wait (void *cls,
1353 struct GNUNET_STREAM_Socket *socket)
1355 LOG (GNUNET_ERROR_TYPE_DEBUG,
1356 "%s: Attaing RECEIVE_CLOSE_WAIT state\n",
1357 GNUNET_i2s (&socket->other_peer));
1358 socket->state = STATE_RECEIVE_CLOSE_WAIT;
1359 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1360 socket->receive_buffer = NULL;
1361 socket->receive_buffer_size = 0;
1366 * Callback to set state to TRANSMIT_CLOSE_WAIT
1368 * @param cls the closure from queue_message
1369 * @param socket the socket requiring state change
1372 set_state_transmit_close_wait (void *cls,
1373 struct GNUNET_STREAM_Socket *socket)
1375 LOG (GNUNET_ERROR_TYPE_DEBUG,
1376 "%s: Attaing TRANSMIT_CLOSE_WAIT state\n",
1377 GNUNET_i2s (&socket->other_peer));
1378 socket->state = STATE_TRANSMIT_CLOSE_WAIT;
1383 * Callback to set state to CLOSED
1385 * @param cls the closure from queue_message
1386 * @param socket the socket requiring state change
1389 set_state_closed (void *cls,
1390 struct GNUNET_STREAM_Socket *socket)
1392 socket->state = STATE_CLOSED;
1397 * Returns GNUNET_MESSAGE_TYPE_STREAM_HELLO
1399 * @return the generate hello message
1401 static struct GNUNET_STREAM_MessageHeader *
1402 generate_hello (void)
1404 struct GNUNET_STREAM_MessageHeader *msg;
1406 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1407 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
1408 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1414 * Returns a new HelloAckMessage. Also sets the write sequence number for the
1417 * @param socket the socket for which this HelloAckMessage has to be generated
1418 * @param generate_seq GNUNET_YES to generate the write sequence number,
1419 * GNUNET_NO to use the existing sequence number
1420 * @return the HelloAckMessage
1422 static struct GNUNET_STREAM_HelloAckMessage *
1423 generate_hello_ack (struct GNUNET_STREAM_Socket *socket,
1426 struct GNUNET_STREAM_HelloAckMessage *msg;
1428 if (GNUNET_YES == generate_seq)
1430 if (GNUNET_YES == socket->testing_active)
1431 socket->write_sequence_number =
1432 socket->testing_set_write_sequence_number_value;
1434 socket->write_sequence_number =
1435 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1436 LOG_DEBUG ("%s: write sequence number %u\n",
1437 GNUNET_i2s (&socket->other_peer),
1438 (unsigned int) socket->write_sequence_number);
1440 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1441 msg->header.header.size =
1442 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1443 msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1444 msg->sequence_number = htonl (socket->write_sequence_number);
1445 msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1451 * Task for retransmitting control messages if they aren't ACK'ed before a
1454 * @param cls the socket
1455 * @param tc the Task context
1458 control_retransmission_task (void *cls,
1459 const struct GNUNET_SCHEDULER_TaskContext *tc)
1461 struct GNUNET_STREAM_Socket *socket = cls;
1463 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
1464 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
1466 LOG_DEBUG ("%s: Retransmitting a control message\n",
1467 GNUNET_i2s (&socket->other_peer));
1468 switch (socket->state)
1476 case STATE_HELLO_WAIT:
1477 if (NULL == socket->lsocket) /* We are client */
1478 queue_message (socket, generate_hello (), NULL, NULL, GNUNET_NO);
1480 queue_message (socket,
1481 (struct GNUNET_STREAM_MessageHeader *)
1482 generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
1484 socket->control_retransmission_task_id =
1485 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
1486 &control_retransmission_task, socket);
1488 case STATE_ESTABLISHED:
1489 if (NULL == socket->lsocket)
1490 queue_message (socket,
1491 (struct GNUNET_STREAM_MessageHeader *)
1492 generate_hello_ack (socket, GNUNET_NO), NULL, NULL,
1504 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1506 * @param cls the socket (set from GNUNET_MESH_connect)
1507 * @param tunnel connection to the other end
1508 * @param tunnel_ctx this is NULL
1509 * @param sender who sent the message
1510 * @param message the actual message
1511 * @param atsi performance data for the connection
1512 * @return GNUNET_OK to keep the connection open,
1513 * GNUNET_SYSERR to close it (signal serious error)
1516 client_handle_hello_ack (void *cls,
1517 struct GNUNET_MESH_Tunnel *tunnel,
1519 const struct GNUNET_PeerIdentity *sender,
1520 const struct GNUNET_MessageHeader *message,
1521 const struct GNUNET_ATS_Information*atsi)
1523 struct GNUNET_STREAM_Socket *socket = cls;
1524 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1525 struct GNUNET_STREAM_HelloAckMessage *reply;
1527 if (0 != memcmp (sender, &socket->other_peer,
1528 sizeof (struct GNUNET_PeerIdentity)))
1530 LOG (GNUNET_ERROR_TYPE_DEBUG,
1531 "%s: Received HELLO_ACK from non-confirming peer\n",
1532 GNUNET_i2s (&socket->other_peer));
1535 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1536 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received HELLO_ACK from %s\n",
1537 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1538 GNUNET_assert (socket->tunnel == tunnel);
1539 switch (socket->state)
1541 case STATE_HELLO_WAIT:
1542 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1543 LOG (GNUNET_ERROR_TYPE_DEBUG,
1544 "%s: Read sequence number %u\n",
1545 GNUNET_i2s (&socket->other_peer),
1546 (unsigned int) socket->read_sequence_number);
1547 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1548 reply = generate_hello_ack (socket, GNUNET_YES);
1549 queue_message (socket, &reply->header, &set_state_established,
1552 case STATE_ESTABLISHED:
1553 // call statistics (# ACKs ignored++)
1554 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
1555 socket->control_retransmission_task_id);
1556 socket->control_retransmission_task_id =
1557 GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
1560 LOG_DEBUG ("%s: Server %s sent HELLO_ACK when in state %d\n",
1561 GNUNET_i2s (&socket->other_peer),
1562 GNUNET_i2s (&socket->other_peer), socket->state);
1563 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1564 return GNUNET_SYSERR;
1570 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1572 * @param cls the socket (set from GNUNET_MESH_connect)
1573 * @param tunnel connection to the other end
1574 * @param tunnel_ctx this is NULL
1575 * @param sender who sent the message
1576 * @param message the actual message
1577 * @param atsi performance data for the connection
1578 * @return GNUNET_OK to keep the connection open,
1579 * GNUNET_SYSERR to close it (signal serious error)
1582 client_handle_reset (void *cls,
1583 struct GNUNET_MESH_Tunnel *tunnel,
1585 const struct GNUNET_PeerIdentity *sender,
1586 const struct GNUNET_MessageHeader *message,
1587 const struct GNUNET_ATS_Information*atsi)
1589 // struct GNUNET_STREAM_Socket *socket = cls;
1596 * Common message handler for handling TRANSMIT_CLOSE messages
1598 * @param socket the socket through which the ack was received
1599 * @param tunnel connection to the other end
1600 * @param sender who sent the message
1601 * @param msg the transmit close message
1602 * @param atsi performance data for the connection
1603 * @return GNUNET_OK to keep the connection open,
1604 * GNUNET_SYSERR to close it (signal serious error)
1607 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1608 struct GNUNET_MESH_Tunnel *tunnel,
1609 const struct GNUNET_PeerIdentity *sender,
1610 const struct GNUNET_STREAM_MessageHeader *msg,
1611 const struct GNUNET_ATS_Information*atsi)
1613 struct GNUNET_STREAM_MessageHeader *reply;
1615 switch (socket->state)
1617 case STATE_ESTABLISHED:
1618 socket->state = STATE_RECEIVE_CLOSED;
1619 /* Send TRANSMIT_CLOSE_ACK */
1620 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1621 reply->header.type =
1622 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1623 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1624 queue_message (socket, reply, NULL, NULL, GNUNET_NO);
1627 /* FIXME: Call statistics? */
1635 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1637 * @param cls the socket (set from GNUNET_MESH_connect)
1638 * @param tunnel connection to the other end
1639 * @param tunnel_ctx this is NULL
1640 * @param sender who sent the message
1641 * @param message the actual message
1642 * @param atsi performance data for the connection
1643 * @return GNUNET_OK to keep the connection open,
1644 * GNUNET_SYSERR to close it (signal serious error)
1647 client_handle_transmit_close (void *cls,
1648 struct GNUNET_MESH_Tunnel *tunnel,
1650 const struct GNUNET_PeerIdentity *sender,
1651 const struct GNUNET_MessageHeader *message,
1652 const struct GNUNET_ATS_Information*atsi)
1654 struct GNUNET_STREAM_Socket *socket = cls;
1656 return handle_transmit_close (socket,
1659 (struct GNUNET_STREAM_MessageHeader *)message,
1665 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_*_CLOSE_ACK messages
1667 * @param socket the socket
1668 * @param tunnel connection to the other end
1669 * @param sender who sent the message
1670 * @param message the actual message
1671 * @param atsi performance data for the connection
1672 * @param operation the close operation which is being ACK'ed
1673 * @return GNUNET_OK to keep the connection open,
1674 * GNUNET_SYSERR to close it (signal serious error)
1677 handle_generic_close_ack (struct GNUNET_STREAM_Socket *socket,
1678 struct GNUNET_MESH_Tunnel *tunnel,
1679 const struct GNUNET_PeerIdentity *sender,
1680 const struct GNUNET_STREAM_MessageHeader *message,
1681 const struct GNUNET_ATS_Information *atsi,
1684 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
1686 shutdown_handle = socket->shutdown_handle;
1687 if (NULL == shutdown_handle)
1689 LOG (GNUNET_ERROR_TYPE_DEBUG,
1690 "%s: Received CLOSE_ACK when shutdown handle is NULL\n",
1691 GNUNET_i2s (&socket->other_peer));
1697 switch (socket->state)
1699 case STATE_CLOSE_WAIT:
1700 if (SHUT_RDWR != shutdown_handle->operation)
1702 LOG (GNUNET_ERROR_TYPE_DEBUG,
1703 "%s: Received CLOSE_ACK when shutdown handle is not for "
1704 "SHUT_RDWR\n", GNUNET_i2s (&socket->other_peer));
1707 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received CLOSE_ACK from %s\n",
1708 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1709 socket->state = STATE_CLOSED;
1712 LOG (GNUNET_ERROR_TYPE_DEBUG,
1713 "%s: Received CLOSE_ACK when in it not expected\n",
1714 GNUNET_i2s (&socket->other_peer));
1719 switch (socket->state)
1721 case STATE_RECEIVE_CLOSE_WAIT:
1722 if (SHUT_RD != shutdown_handle->operation)
1724 LOG (GNUNET_ERROR_TYPE_DEBUG,
1725 "%s: Received RECEIVE_CLOSE_ACK when shutdown handle "
1726 "is not for SHUT_RD\n", GNUNET_i2s (&socket->other_peer));
1729 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received RECEIVE_CLOSE_ACK from %s\n",
1730 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1731 socket->state = STATE_RECEIVE_CLOSED;
1734 LOG (GNUNET_ERROR_TYPE_DEBUG,
1735 "%s: Received RECEIVE_CLOSE_ACK when in it not expected\n",
1736 GNUNET_i2s (&socket->other_peer));
1741 switch (socket->state)
1743 case STATE_TRANSMIT_CLOSE_WAIT:
1744 if (SHUT_WR != shutdown_handle->operation)
1746 LOG (GNUNET_ERROR_TYPE_DEBUG,
1747 "%s: Received TRANSMIT_CLOSE_ACK when shutdown handle "
1748 "is not for SHUT_WR\n",
1749 GNUNET_i2s (&socket->other_peer));
1752 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received TRANSMIT_CLOSE_ACK from %s\n",
1753 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
1754 socket->state = STATE_TRANSMIT_CLOSED;
1757 LOG (GNUNET_ERROR_TYPE_DEBUG,
1758 "%s: Received TRANSMIT_CLOSE_ACK when in it not expected\n",
1759 GNUNET_i2s (&socket->other_peer));
1766 if (NULL != shutdown_handle->completion_cb) /* Shutdown completion */
1767 shutdown_handle->completion_cb(shutdown_handle->completion_cls,
1769 if (GNUNET_SCHEDULER_NO_TASK
1770 != shutdown_handle->close_msg_retransmission_task_id)
1772 GNUNET_SCHEDULER_cancel
1773 (shutdown_handle->close_msg_retransmission_task_id);
1774 shutdown_handle->close_msg_retransmission_task_id =
1775 GNUNET_SCHEDULER_NO_TASK;
1777 GNUNET_free (shutdown_handle); /* Free shutdown handle */
1778 socket->shutdown_handle = NULL;
1784 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1786 * @param cls the socket (set from GNUNET_MESH_connect)
1787 * @param tunnel connection to the other end
1788 * @param tunnel_ctx this is NULL
1789 * @param sender who sent the message
1790 * @param message the actual message
1791 * @param atsi performance data for the connection
1792 * @return GNUNET_OK to keep the connection open,
1793 * GNUNET_SYSERR to close it (signal serious error)
1796 client_handle_transmit_close_ack (void *cls,
1797 struct GNUNET_MESH_Tunnel *tunnel,
1799 const struct GNUNET_PeerIdentity *sender,
1800 const struct GNUNET_MessageHeader *message,
1801 const struct GNUNET_ATS_Information*atsi)
1803 struct GNUNET_STREAM_Socket *socket = cls;
1805 return handle_generic_close_ack (socket,
1808 (const struct GNUNET_STREAM_MessageHeader *)
1816 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1818 * @param socket the socket
1819 * @param tunnel connection to the other end
1820 * @param sender who sent the message
1821 * @param message the actual message
1822 * @param atsi performance data for the connection
1823 * @return GNUNET_OK to keep the connection open,
1824 * GNUNET_SYSERR to close it (signal serious error)
1827 handle_receive_close (struct GNUNET_STREAM_Socket *socket,
1828 struct GNUNET_MESH_Tunnel *tunnel,
1829 const struct GNUNET_PeerIdentity *sender,
1830 const struct GNUNET_STREAM_MessageHeader *message,
1831 const struct GNUNET_ATS_Information *atsi)
1833 struct GNUNET_STREAM_MessageHeader *receive_close_ack;
1835 switch (socket->state)
1839 case STATE_HELLO_WAIT:
1840 LOG (GNUNET_ERROR_TYPE_DEBUG,
1841 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1842 GNUNET_i2s (&socket->other_peer));
1848 LOG (GNUNET_ERROR_TYPE_DEBUG,
1849 "%s: Received RECEIVE_CLOSE from %s\n",
1850 GNUNET_i2s (&socket->other_peer),
1851 GNUNET_i2s (&socket->other_peer));
1853 GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1854 receive_close_ack->header.size =
1855 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1856 receive_close_ack->header.type =
1857 htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK);
1858 queue_message (socket, receive_close_ack, &set_state_closed,
1860 /* FIXME: Handle the case where write handle is present; the write operation
1861 should be deemed as finised and the write continuation callback
1862 has to be called with the stream status GNUNET_STREAM_SHUTDOWN */
1868 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
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 (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;
1890 handle_receive_close (socket,
1893 (const struct GNUNET_STREAM_MessageHeader *) message,
1899 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1901 * @param cls the socket (set from GNUNET_MESH_connect)
1902 * @param tunnel connection to the other end
1903 * @param tunnel_ctx this is NULL
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 client_handle_receive_close_ack (void *cls,
1912 struct GNUNET_MESH_Tunnel *tunnel,
1914 const struct GNUNET_PeerIdentity *sender,
1915 const struct GNUNET_MessageHeader *message,
1916 const struct GNUNET_ATS_Information*atsi)
1918 struct GNUNET_STREAM_Socket *socket = cls;
1920 return handle_generic_close_ack (socket,
1923 (const struct GNUNET_STREAM_MessageHeader *)
1931 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1933 * @param socket the socket
1934 * @param tunnel connection to the other end
1935 * @param sender who sent the message
1936 * @param message the actual message
1937 * @param atsi performance data for the connection
1938 * @return GNUNET_OK to keep the connection open,
1939 * GNUNET_SYSERR to close it (signal serious error)
1942 handle_close (struct GNUNET_STREAM_Socket *socket,
1943 struct GNUNET_MESH_Tunnel *tunnel,
1944 const struct GNUNET_PeerIdentity *sender,
1945 const struct GNUNET_STREAM_MessageHeader *message,
1946 const struct GNUNET_ATS_Information*atsi)
1948 struct GNUNET_STREAM_MessageHeader *close_ack;
1950 switch (socket->state)
1954 case STATE_HELLO_WAIT:
1955 LOG (GNUNET_ERROR_TYPE_DEBUG,
1956 "%s: Ignoring RECEIVE_CLOSE as it cannot be handled now\n",
1957 GNUNET_i2s (&socket->other_peer));
1963 LOG (GNUNET_ERROR_TYPE_DEBUG,
1964 "%s: Received CLOSE from %s\n",
1965 GNUNET_i2s (&socket->other_peer),
1966 GNUNET_i2s (&socket->other_peer));
1967 close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1968 close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1969 close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK);
1970 queue_message (socket, close_ack, &set_state_closed, NULL, GNUNET_NO);
1971 if (socket->state == STATE_CLOSED)
1974 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1975 socket->receive_buffer = NULL;
1976 socket->receive_buffer_size = 0;
1982 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1984 * @param cls the socket (set from GNUNET_MESH_connect)
1985 * @param tunnel connection to the other end
1986 * @param tunnel_ctx this is NULL
1987 * @param sender who sent the message
1988 * @param message the actual message
1989 * @param atsi performance data for the connection
1990 * @return GNUNET_OK to keep the connection open,
1991 * GNUNET_SYSERR to close it (signal serious error)
1994 client_handle_close (void *cls,
1995 struct GNUNET_MESH_Tunnel *tunnel,
1997 const struct GNUNET_PeerIdentity *sender,
1998 const struct GNUNET_MessageHeader *message,
1999 const struct GNUNET_ATS_Information*atsi)
2001 struct GNUNET_STREAM_Socket *socket = cls;
2003 return handle_close (socket,
2006 (const struct GNUNET_STREAM_MessageHeader *) message,
2012 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2014 * @param cls the socket (set from GNUNET_MESH_connect)
2015 * @param tunnel connection to the other end
2016 * @param tunnel_ctx this is NULL
2017 * @param sender who sent the message
2018 * @param message the actual message
2019 * @param atsi performance data for the connection
2020 * @return GNUNET_OK to keep the connection open,
2021 * GNUNET_SYSERR to close it (signal serious error)
2024 client_handle_close_ack (void *cls,
2025 struct GNUNET_MESH_Tunnel *tunnel,
2027 const struct GNUNET_PeerIdentity *sender,
2028 const struct GNUNET_MessageHeader *message,
2029 const struct GNUNET_ATS_Information *atsi)
2031 struct GNUNET_STREAM_Socket *socket = cls;
2033 return handle_generic_close_ack (socket,
2036 (const struct GNUNET_STREAM_MessageHeader *)
2042 /*****************************/
2043 /* Server's Message Handlers */
2044 /*****************************/
2047 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
2049 * @param cls the closure
2050 * @param tunnel connection to the other end
2051 * @param tunnel_ctx the socket
2052 * @param sender who sent the message
2053 * @param message the actual message
2054 * @param atsi performance data for the connection
2055 * @return GNUNET_OK to keep the connection open,
2056 * GNUNET_SYSERR to close it (signal serious error)
2059 server_handle_data (void *cls,
2060 struct GNUNET_MESH_Tunnel *tunnel,
2062 const struct GNUNET_PeerIdentity *sender,
2063 const struct GNUNET_MessageHeader *message,
2064 const struct GNUNET_ATS_Information*atsi)
2066 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2068 return handle_data (socket,
2071 (const struct GNUNET_STREAM_DataMessage *)message,
2077 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
2079 * @param cls the closure
2080 * @param tunnel connection to the other end
2081 * @param tunnel_ctx the socket
2082 * @param sender who sent the message
2083 * @param message the actual message
2084 * @param atsi performance data for the connection
2085 * @return GNUNET_OK to keep the connection open,
2086 * GNUNET_SYSERR to close it (signal serious error)
2089 server_handle_hello (void *cls,
2090 struct GNUNET_MESH_Tunnel *tunnel,
2092 const struct GNUNET_PeerIdentity *sender,
2093 const struct GNUNET_MessageHeader *message,
2094 const struct GNUNET_ATS_Information*atsi)
2096 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2097 struct GNUNET_STREAM_HelloAckMessage *reply;
2099 if (0 != memcmp (sender,
2100 &socket->other_peer,
2101 sizeof (struct GNUNET_PeerIdentity)))
2103 LOG_DEBUG ("%s: Received HELLO from non-confirming peer\n",
2104 GNUNET_i2s (&socket->other_peer));
2107 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO == ntohs (message->type));
2108 GNUNET_assert (socket->tunnel == tunnel);
2109 LOG_DEBUG ("%s: Received HELLO from %s\n", GNUNET_i2s (&socket->other_peer),
2110 GNUNET_i2s (&socket->other_peer));
2111 switch (socket->state)
2114 reply = generate_hello_ack (socket, GNUNET_YES);
2115 queue_message (socket, &reply->header, &set_state_hello_wait, NULL,
2117 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
2118 socket->control_retransmission_task_id);
2119 socket->control_retransmission_task_id =
2120 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2121 &control_retransmission_task, socket);
2123 case STATE_HELLO_WAIT:
2124 /* Perhaps our HELLO_ACK was lost */
2125 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK !=
2126 socket->control_retransmission_task_id);
2127 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
2128 socket->control_retransmission_task_id =
2129 GNUNET_SCHEDULER_add_now (&control_retransmission_task, socket);
2132 LOG_DEBUG( "%s: Client sent HELLO when in state %d\n",
2133 GNUNET_i2s (&socket->other_peer), socket->state);
2134 /* FIXME: Send RESET? */
2141 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
2143 * @param cls the closure
2144 * @param tunnel connection to the other end
2145 * @param tunnel_ctx the socket
2146 * @param sender who sent the message
2147 * @param message the actual message
2148 * @param atsi performance data for the connection
2149 * @return GNUNET_OK to keep the connection open,
2150 * GNUNET_SYSERR to close it (signal serious error)
2153 server_handle_hello_ack (void *cls,
2154 struct GNUNET_MESH_Tunnel *tunnel,
2156 const struct GNUNET_PeerIdentity *sender,
2157 const struct GNUNET_MessageHeader *message,
2158 const struct GNUNET_ATS_Information*atsi)
2160 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2161 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
2163 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
2164 ntohs (message->type));
2165 GNUNET_assert (socket->tunnel == tunnel);
2166 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
2167 switch (socket->state)
2169 case STATE_HELLO_WAIT:
2170 LOG (GNUNET_ERROR_TYPE_DEBUG,
2171 "%s: Received HELLO_ACK from %s\n",
2172 GNUNET_i2s (&socket->other_peer),
2173 GNUNET_i2s (&socket->other_peer));
2174 socket->read_sequence_number = ntohl (ack_message->sequence_number);
2175 LOG (GNUNET_ERROR_TYPE_DEBUG,
2176 "%s: Read sequence number %u\n",
2177 GNUNET_i2s (&socket->other_peer),
2178 (unsigned int) socket->read_sequence_number);
2179 socket->receiver_window_available =
2180 ntohl (ack_message->receiver_window_size);
2181 set_state_established (NULL, socket);
2184 LOG (GNUNET_ERROR_TYPE_DEBUG,
2185 "Client sent HELLO_ACK when in state %d\n", socket->state);
2192 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
2194 * @param cls the closure
2195 * @param tunnel connection to the other end
2196 * @param tunnel_ctx the socket
2197 * @param sender who sent the message
2198 * @param message the actual message
2199 * @param atsi performance data for the connection
2200 * @return GNUNET_OK to keep the connection open,
2201 * GNUNET_SYSERR to close it (signal serious error)
2204 server_handle_reset (void *cls,
2205 struct GNUNET_MESH_Tunnel *tunnel,
2207 const struct GNUNET_PeerIdentity *sender,
2208 const struct GNUNET_MessageHeader *message,
2209 const struct GNUNET_ATS_Information*atsi)
2211 // struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2218 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
2220 * @param cls the closure
2221 * @param tunnel connection to the other end
2222 * @param tunnel_ctx the socket
2223 * @param sender who sent the message
2224 * @param message the actual message
2225 * @param atsi performance data for the connection
2226 * @return GNUNET_OK to keep the connection open,
2227 * GNUNET_SYSERR to close it (signal serious error)
2230 server_handle_transmit_close (void *cls,
2231 struct GNUNET_MESH_Tunnel *tunnel,
2233 const struct GNUNET_PeerIdentity *sender,
2234 const struct GNUNET_MessageHeader *message,
2235 const struct GNUNET_ATS_Information*atsi)
2237 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2239 return handle_transmit_close (socket,
2242 (struct GNUNET_STREAM_MessageHeader *)message,
2248 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
2250 * @param cls the closure
2251 * @param tunnel connection to the other end
2252 * @param tunnel_ctx the socket
2253 * @param sender who sent the message
2254 * @param message the actual message
2255 * @param atsi performance data for the connection
2256 * @return GNUNET_OK to keep the connection open,
2257 * GNUNET_SYSERR to close it (signal serious error)
2260 server_handle_transmit_close_ack (void *cls,
2261 struct GNUNET_MESH_Tunnel *tunnel,
2263 const struct GNUNET_PeerIdentity *sender,
2264 const struct GNUNET_MessageHeader *message,
2265 const struct GNUNET_ATS_Information*atsi)
2267 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2269 return handle_generic_close_ack (socket,
2272 (const struct GNUNET_STREAM_MessageHeader *)
2280 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
2282 * @param cls the closure
2283 * @param tunnel connection to the other end
2284 * @param tunnel_ctx the socket
2285 * @param sender who sent the message
2286 * @param message the actual message
2287 * @param atsi performance data for the connection
2288 * @return GNUNET_OK to keep the connection open,
2289 * GNUNET_SYSERR to close it (signal serious error)
2292 server_handle_receive_close (void *cls,
2293 struct GNUNET_MESH_Tunnel *tunnel,
2295 const struct GNUNET_PeerIdentity *sender,
2296 const struct GNUNET_MessageHeader *message,
2297 const struct GNUNET_ATS_Information*atsi)
2299 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2302 handle_receive_close (socket,
2305 (const struct GNUNET_STREAM_MessageHeader *) message,
2311 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
2313 * @param cls the closure
2314 * @param tunnel connection to the other end
2315 * @param tunnel_ctx the socket
2316 * @param sender who sent the message
2317 * @param message the actual message
2318 * @param atsi performance data for the connection
2319 * @return GNUNET_OK to keep the connection open,
2320 * GNUNET_SYSERR to close it (signal serious error)
2323 server_handle_receive_close_ack (void *cls,
2324 struct GNUNET_MESH_Tunnel *tunnel,
2326 const struct GNUNET_PeerIdentity *sender,
2327 const struct GNUNET_MessageHeader *message,
2328 const struct GNUNET_ATS_Information*atsi)
2330 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2332 return handle_generic_close_ack (socket,
2335 (const struct GNUNET_STREAM_MessageHeader *)
2343 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2345 * @param cls the listen socket (from GNUNET_MESH_connect in
2346 * GNUNET_STREAM_listen)
2347 * @param tunnel connection to the other end
2348 * @param tunnel_ctx the socket
2349 * @param sender who sent the message
2350 * @param message the actual message
2351 * @param atsi performance data for the connection
2352 * @return GNUNET_OK to keep the connection open,
2353 * GNUNET_SYSERR to close it (signal serious error)
2356 server_handle_close (void *cls,
2357 struct GNUNET_MESH_Tunnel *tunnel,
2359 const struct GNUNET_PeerIdentity *sender,
2360 const struct GNUNET_MessageHeader *message,
2361 const struct GNUNET_ATS_Information*atsi)
2363 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2365 return handle_close (socket,
2368 (const struct GNUNET_STREAM_MessageHeader *) message,
2374 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2376 * @param cls the closure
2377 * @param tunnel connection to the other end
2378 * @param tunnel_ctx the socket
2379 * @param sender who sent the message
2380 * @param message the actual message
2381 * @param atsi performance data for the connection
2382 * @return GNUNET_OK to keep the connection open,
2383 * GNUNET_SYSERR to close it (signal serious error)
2386 server_handle_close_ack (void *cls,
2387 struct GNUNET_MESH_Tunnel *tunnel,
2389 const struct GNUNET_PeerIdentity *sender,
2390 const struct GNUNET_MessageHeader *message,
2391 const struct GNUNET_ATS_Information*atsi)
2393 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2395 return handle_generic_close_ack (socket,
2398 (const struct GNUNET_STREAM_MessageHeader *)
2406 * Handler for DATA_ACK messages
2408 * @param socket the socket through which the ack was received
2409 * @param tunnel connection to the other end
2410 * @param sender who sent the message
2411 * @param ack the acknowledgment message
2412 * @param atsi performance data for the connection
2413 * @return GNUNET_OK to keep the connection open,
2414 * GNUNET_SYSERR to close it (signal serious error)
2417 handle_ack (struct GNUNET_STREAM_Socket *socket,
2418 struct GNUNET_MESH_Tunnel *tunnel,
2419 const struct GNUNET_PeerIdentity *sender,
2420 const struct GNUNET_STREAM_AckMessage *ack,
2421 const struct GNUNET_ATS_Information*atsi)
2423 unsigned int packet;
2424 int need_retransmission;
2425 uint32_t sequence_difference;
2427 if (0 != memcmp (sender,
2428 &socket->other_peer,
2429 sizeof (struct GNUNET_PeerIdentity)))
2431 LOG (GNUNET_ERROR_TYPE_DEBUG,
2432 "%s: Received ACK from non-confirming peer\n",
2433 GNUNET_i2s (&socket->other_peer));
2436 switch (socket->state)
2438 case (STATE_ESTABLISHED):
2439 case (STATE_RECEIVE_CLOSED):
2440 case (STATE_RECEIVE_CLOSE_WAIT):
2441 if (NULL == socket->write_handle)
2443 LOG (GNUNET_ERROR_TYPE_DEBUG,
2444 "%s: Received DATA_ACK when write_handle is NULL\n",
2445 GNUNET_i2s (&socket->other_peer));
2448 sequence_difference =
2449 socket->write_sequence_number - ntohl (ack->base_sequence_number);
2450 if (!(sequence_difference <= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
2452 LOG (GNUNET_ERROR_TYPE_DEBUG,
2453 "%s: Received DATA_ACK with unexpected base sequence number\n",
2454 GNUNET_i2s (&socket->other_peer));
2455 LOG (GNUNET_ERROR_TYPE_DEBUG,
2456 "%s: Current write sequence: %u; Ack's base sequence: %u\n",
2457 GNUNET_i2s (&socket->other_peer),
2458 socket->write_sequence_number,
2459 ntohl (ack->base_sequence_number));
2462 /* FIXME: include the case when write_handle is cancelled - ignore the
2464 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: Received DATA_ACK from %s\n",
2465 GNUNET_i2s (&socket->other_peer), GNUNET_i2s (&socket->other_peer));
2466 /* Cancel the retransmission task */
2467 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
2469 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
2470 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
2472 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2474 if (NULL == socket->write_handle->messages[packet]) break;
2475 /* BS: Base sequence from ack; PS: sequence num of current packet */
2476 sequence_difference = ntohl (ack->base_sequence_number)
2477 - ntohl (socket->write_handle->messages[packet]->sequence_number);
2478 if ((0 == sequence_difference) ||
2479 (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH < sequence_difference))
2480 continue; /* The message in our handle is not yet received */
2481 /* case where BS = PS + GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
2482 /* sequence_difference <= GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH */
2483 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
2484 packet, GNUNET_YES);
2486 /* Update the receive window remaining
2487 FIXME : Should update with the value from a data ack with greater
2489 socket->receiver_window_available =
2490 ntohl (ack->receive_window_remaining);
2491 /* Check if we have received all acknowledgements */
2492 need_retransmission = GNUNET_NO;
2493 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2495 if (NULL == socket->write_handle->messages[packet]) break;
2496 if (GNUNET_YES != ackbitmap_is_bit_set
2497 (&socket->write_handle->ack_bitmap,packet))
2499 need_retransmission = GNUNET_YES;
2503 if (GNUNET_YES == need_retransmission)
2505 write_data (socket);
2507 else /* We have to call the write continuation callback now */
2509 struct GNUNET_STREAM_IOWriteHandle *write_handle;
2511 /* Free the packets */
2512 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2514 GNUNET_free_non_null (socket->write_handle->messages[packet]);
2516 write_handle = socket->write_handle;
2517 socket->write_handle = NULL;
2518 if (NULL != write_handle->write_cont)
2519 write_handle->write_cont (write_handle->write_cont_cls,
2521 write_handle->size);
2522 /* We are done with the write handle - Freeing it */
2523 GNUNET_free (write_handle);
2524 LOG (GNUNET_ERROR_TYPE_DEBUG,
2525 "%s: Write completion callback completed\n",
2526 GNUNET_i2s (&socket->other_peer));
2537 * Handler for DATA_ACK messages
2539 * @param cls the 'struct GNUNET_STREAM_Socket'
2540 * @param tunnel connection to the other end
2541 * @param tunnel_ctx unused
2542 * @param sender who sent the message
2543 * @param message the actual message
2544 * @param atsi performance data for the connection
2545 * @return GNUNET_OK to keep the connection open,
2546 * GNUNET_SYSERR to close it (signal serious error)
2549 client_handle_ack (void *cls,
2550 struct GNUNET_MESH_Tunnel *tunnel,
2552 const struct GNUNET_PeerIdentity *sender,
2553 const struct GNUNET_MessageHeader *message,
2554 const struct GNUNET_ATS_Information*atsi)
2556 struct GNUNET_STREAM_Socket *socket = cls;
2557 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2559 return handle_ack (socket, tunnel, sender, ack, atsi);
2564 * Handler for DATA_ACK messages
2566 * @param cls the server's listen socket
2567 * @param tunnel connection to the other end
2568 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2569 * @param sender who sent the message
2570 * @param message the actual message
2571 * @param atsi performance data for the connection
2572 * @return GNUNET_OK to keep the connection open,
2573 * GNUNET_SYSERR to close it (signal serious error)
2576 server_handle_ack (void *cls,
2577 struct GNUNET_MESH_Tunnel *tunnel,
2579 const struct GNUNET_PeerIdentity *sender,
2580 const struct GNUNET_MessageHeader *message,
2581 const struct GNUNET_ATS_Information*atsi)
2583 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2584 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2586 return handle_ack (socket, tunnel, sender, ack, atsi);
2591 * For client message handlers, the stream socket is in the
2594 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2595 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2596 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2597 sizeof (struct GNUNET_STREAM_AckMessage) },
2598 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2599 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2600 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2601 sizeof (struct GNUNET_STREAM_MessageHeader)},
2602 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2603 sizeof (struct GNUNET_STREAM_MessageHeader)},
2604 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2605 sizeof (struct GNUNET_STREAM_MessageHeader)},
2606 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2607 sizeof (struct GNUNET_STREAM_MessageHeader)},
2608 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2609 sizeof (struct GNUNET_STREAM_MessageHeader)},
2610 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2611 sizeof (struct GNUNET_STREAM_MessageHeader)},
2612 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2613 sizeof (struct GNUNET_STREAM_MessageHeader)},
2619 * For server message handlers, the stream socket is in the
2620 * tunnel context, and the listen socket in the closure argument.
2622 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2623 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2624 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2625 sizeof (struct GNUNET_STREAM_AckMessage) },
2626 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2627 sizeof (struct GNUNET_STREAM_MessageHeader)},
2628 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2629 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2630 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2631 sizeof (struct GNUNET_STREAM_MessageHeader)},
2632 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2633 sizeof (struct GNUNET_STREAM_MessageHeader)},
2634 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2635 sizeof (struct GNUNET_STREAM_MessageHeader)},
2636 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2637 sizeof (struct GNUNET_STREAM_MessageHeader)},
2638 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2639 sizeof (struct GNUNET_STREAM_MessageHeader)},
2640 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2641 sizeof (struct GNUNET_STREAM_MessageHeader)},
2642 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2643 sizeof (struct GNUNET_STREAM_MessageHeader)},
2649 * Function called when our target peer is connected to our tunnel
2651 * @param cls the socket for which this tunnel is created
2652 * @param peer the peer identity of the target
2653 * @param atsi performance data for the connection
2656 mesh_peer_connect_callback (void *cls,
2657 const struct GNUNET_PeerIdentity *peer,
2658 const struct GNUNET_ATS_Information * atsi)
2660 struct GNUNET_STREAM_Socket *socket = cls;
2661 struct GNUNET_STREAM_MessageHeader *message;
2663 if (0 != memcmp (peer,
2664 &socket->other_peer,
2665 sizeof (struct GNUNET_PeerIdentity)))
2667 LOG (GNUNET_ERROR_TYPE_DEBUG,
2668 "%s: A peer which is not our target has connected to our tunnel\n",
2672 LOG (GNUNET_ERROR_TYPE_DEBUG,
2673 "%s: Target peer %s connected\n",
2674 GNUNET_i2s (&socket->other_peer),
2675 GNUNET_i2s (&socket->other_peer));
2676 /* Set state to INIT */
2677 socket->state = STATE_INIT;
2678 /* Send HELLO message */
2679 message = generate_hello ();
2680 queue_message (socket, message, &set_state_hello_wait, NULL, GNUNET_NO);
2681 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK ==
2682 socket->control_retransmission_task_id);
2683 socket->control_retransmission_task_id =
2684 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2685 &control_retransmission_task, socket);
2690 * Function called when our target peer is disconnected from our tunnel
2692 * @param cls the socket associated which this tunnel
2693 * @param peer the peer identity of the target
2696 mesh_peer_disconnect_callback (void *cls,
2697 const struct GNUNET_PeerIdentity *peer)
2699 struct GNUNET_STREAM_Socket *socket=cls;
2701 /* If the state is SHUTDOWN its ok; else set the state of the socket to SYSERR */
2702 LOG (GNUNET_ERROR_TYPE_DEBUG,
2703 "%s: Other peer %s disconnected \n",
2704 GNUNET_i2s (&socket->other_peer),
2705 GNUNET_i2s (&socket->other_peer));
2710 * Method called whenever a peer creates a tunnel to us
2712 * @param cls closure
2713 * @param tunnel new handle to the tunnel
2714 * @param initiator peer that started the tunnel
2715 * @param atsi performance information for the tunnel
2716 * @return initial tunnel context for the tunnel
2717 * (can be NULL -- that's not an error)
2720 new_tunnel_notify (void *cls,
2721 struct GNUNET_MESH_Tunnel *tunnel,
2722 const struct GNUNET_PeerIdentity *initiator,
2723 const struct GNUNET_ATS_Information *atsi)
2725 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2726 struct GNUNET_STREAM_Socket *socket;
2728 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2729 from the same peer again until the socket is closed */
2731 if (GNUNET_NO == lsocket->listening)
2733 GNUNET_MESH_tunnel_destroy (tunnel);
2736 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2737 socket->other_peer = *initiator;
2738 socket->tunnel = tunnel;
2739 socket->state = STATE_INIT;
2740 socket->lsocket = lsocket;
2741 socket->retransmit_timeout = lsocket->retransmit_timeout;
2742 socket->testing_active = lsocket->testing_active;
2743 socket->testing_set_write_sequence_number_value =
2744 lsocket->testing_set_write_sequence_number_value;
2745 socket->max_payload_size = lsocket->max_payload_size;
2746 LOG (GNUNET_ERROR_TYPE_DEBUG,
2747 "%s: Peer %s initiated tunnel to us\n",
2748 GNUNET_i2s (&socket->other_peer),
2749 GNUNET_i2s (&socket->other_peer));
2755 * Function called whenever an inbound tunnel is destroyed. Should clean up
2756 * any associated state. This function is NOT called if the client has
2757 * explicitly asked for the tunnel to be destroyed using
2758 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2761 * @param cls closure (set from GNUNET_MESH_connect)
2762 * @param tunnel connection to the other end (henceforth invalid)
2763 * @param tunnel_ctx place where local state associated
2764 * with the tunnel is stored
2767 tunnel_cleaner (void *cls,
2768 const struct GNUNET_MESH_Tunnel *tunnel,
2771 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2772 struct MessageQueue *head;
2774 GNUNET_assert (tunnel == socket->tunnel);
2776 LOG (GNUNET_ERROR_TYPE_DEBUG,
2777 "%s: Peer %s has terminated connection abruptly\n",
2778 GNUNET_i2s (&socket->other_peer),
2779 GNUNET_i2s (&socket->other_peer));
2780 socket->status = GNUNET_STREAM_SHUTDOWN;
2781 /* Clear Transmit handles */
2782 if (NULL != socket->transmit_handle)
2784 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2785 socket->transmit_handle = NULL;
2787 /* Stop Tasks using socket->tunnel */
2788 if (GNUNET_SCHEDULER_NO_TASK != socket->ack_task_id)
2790 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2791 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2793 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
2795 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
2796 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
2798 /* Terminate the control retransmission tasks */
2799 if (GNUNET_SCHEDULER_NO_TASK != socket->control_retransmission_task_id)
2801 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
2802 socket->control_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
2804 /* Clear Transmit handles */
2805 if (NULL != socket->transmit_handle)
2807 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2808 socket->transmit_handle = NULL;
2810 /* Clear existing message queue */
2811 while (NULL != (head = socket->queue_head)) {
2812 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2815 GNUNET_free (head->message);
2818 socket->tunnel = NULL;
2823 * Callback to signal timeout on lockmanager lock acquire
2825 * @param cls the ListenSocket
2826 * @param tc the scheduler task context
2829 lockmanager_acquire_timeout (void *cls,
2830 const struct GNUNET_SCHEDULER_TaskContext *tc)
2832 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2833 GNUNET_STREAM_ListenCallback listen_cb;
2834 void *listen_cb_cls;
2836 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2837 listen_cb = lsocket->listen_cb;
2838 listen_cb_cls = lsocket->listen_cb_cls;
2839 if (NULL != listen_cb)
2840 listen_cb (listen_cb_cls, NULL, NULL);
2845 * Callback to notify us on the status changes on app_port lock
2847 * @param cls the ListenSocket
2848 * @param domain the domain name of the lock
2849 * @param lock the app_port
2850 * @param status the current status of the lock
2853 lock_status_change_cb (void *cls, const char *domain, uint32_t lock,
2854 enum GNUNET_LOCKMANAGER_Status status)
2856 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2858 GNUNET_assert (lock == (uint32_t) lsocket->port);
2859 if (GNUNET_LOCKMANAGER_SUCCESS == status)
2861 lsocket->listening = GNUNET_YES;
2862 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
2864 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
2865 lsocket->lockmanager_acquire_timeout_task = GNUNET_SCHEDULER_NO_TASK;
2867 if (NULL == lsocket->mesh)
2869 GNUNET_MESH_ApplicationType ports[] = {lsocket->port, 0};
2871 lsocket->mesh = GNUNET_MESH_connect (lsocket->cfg,
2872 lsocket, /* Closure */
2875 server_message_handlers,
2877 GNUNET_assert (NULL != lsocket->mesh);
2878 if (NULL != lsocket->listen_ok_cb)
2880 (void) lsocket->listen_ok_cb ();
2884 if (GNUNET_LOCKMANAGER_RELEASE == status)
2885 lsocket->listening = GNUNET_NO;
2895 * Tries to open a stream to the target peer
2897 * @param cfg configuration to use
2898 * @param target the target peer to which the stream has to be opened
2899 * @param app_port the application port number which uniquely identifies this
2901 * @param open_cb this function will be called after stream has be established;
2903 * @param open_cb_cls the closure for open_cb
2904 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2905 * @return if successful it returns the stream socket; NULL if stream cannot be
2908 struct GNUNET_STREAM_Socket *
2909 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2910 const struct GNUNET_PeerIdentity *target,
2911 GNUNET_MESH_ApplicationType app_port,
2912 GNUNET_STREAM_OpenCallback open_cb,
2916 struct GNUNET_STREAM_Socket *socket;
2917 enum GNUNET_STREAM_Option option;
2918 GNUNET_MESH_ApplicationType ports[] = {app_port, 0};
2920 uint16_t payload_size;
2922 LOG (GNUNET_ERROR_TYPE_DEBUG,
2924 GNUNET_assert (NULL != open_cb);
2925 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2926 socket->other_peer = *target;
2927 socket->open_cb = open_cb;
2928 socket->open_cls = open_cb_cls;
2930 socket->retransmit_timeout = TIME_REL_SECS (default_timeout);
2931 socket->testing_active = GNUNET_NO;
2932 socket->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
2933 va_start (vargs, open_cb_cls); /* Parse variable args */
2935 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2938 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2939 /* Expect struct GNUNET_TIME_Relative */
2940 socket->retransmit_timeout = va_arg (vargs,
2941 struct GNUNET_TIME_Relative);
2943 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
2944 socket->testing_active = GNUNET_YES;
2945 socket->testing_set_write_sequence_number_value = va_arg (vargs,
2948 case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
2949 GNUNET_break (0); /* Option irrelevant in STREAM_open */
2951 case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
2952 GNUNET_break (0); /* Option irrelevant in STREAM_open */
2954 case GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE:
2955 payload_size = (uint16_t) va_arg (vargs, unsigned int);
2956 GNUNET_assert (0 != payload_size);
2957 if (payload_size < socket->max_payload_size)
2958 socket->max_payload_size = payload_size;
2960 case GNUNET_STREAM_OPTION_END:
2963 } while (GNUNET_STREAM_OPTION_END != option);
2964 va_end (vargs); /* End of variable args parsing */
2965 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2967 NULL, /* No inbound tunnel handler */
2968 NULL, /* No in-tunnel cleaner */
2969 client_message_handlers,
2970 ports); /* We don't get inbound tunnels */
2971 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2973 GNUNET_free (socket);
2976 /* Now create the mesh tunnel to target */
2977 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating MESH Tunnel\n");
2978 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2979 NULL, /* Tunnel context */
2980 &mesh_peer_connect_callback,
2981 &mesh_peer_disconnect_callback,
2983 GNUNET_assert (NULL != socket->tunnel);
2984 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2985 &socket->other_peer);
2986 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
2992 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
2994 * @param socket the stream socket
2995 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
2996 * @param completion_cb the callback that will be called upon successful
2997 * shutdown of given operation
2998 * @param completion_cls the closure for the completion callback
2999 * @return the shutdown handle
3001 struct GNUNET_STREAM_ShutdownHandle *
3002 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
3004 GNUNET_STREAM_ShutdownCompletion completion_cb,
3005 void *completion_cls)
3007 struct GNUNET_STREAM_ShutdownHandle *handle;
3008 struct GNUNET_STREAM_MessageHeader *msg;
3010 GNUNET_assert (NULL == socket->shutdown_handle);
3012 handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle));
3013 handle->socket = socket;
3014 handle->completion_cb = completion_cb;
3015 handle->completion_cls = completion_cls;
3016 socket->shutdown_handle = handle;
3018 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
3019 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
3023 handle->operation = SHUT_RD;
3024 if (NULL != socket->read_handle)
3025 LOG (GNUNET_ERROR_TYPE_WARNING,
3026 "Existing read handle should be cancelled before shutting"
3028 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
3029 queue_message (socket, msg, &set_state_receive_close_wait, NULL,
3033 handle->operation = SHUT_WR;
3034 if (NULL != socket->write_handle)
3035 LOG (GNUNET_ERROR_TYPE_WARNING,
3036 "Existing write handle should be cancelled before shutting"
3038 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
3039 queue_message (socket, msg, &set_state_transmit_close_wait, NULL,
3043 handle->operation = SHUT_RDWR;
3044 if (NULL != socket->write_handle)
3045 LOG (GNUNET_ERROR_TYPE_WARNING,
3046 "Existing write handle should be cancelled before shutting"
3048 if (NULL != socket->read_handle)
3049 LOG (GNUNET_ERROR_TYPE_WARNING,
3050 "Existing read handle should be cancelled before shutting"
3052 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
3053 queue_message (socket, msg, &set_state_close_wait, NULL, GNUNET_NO);
3056 LOG (GNUNET_ERROR_TYPE_WARNING,
3057 "GNUNET_STREAM_shutdown called with invalid value for "
3058 "parameter operation -- Ignoring\n");
3060 GNUNET_free (handle);
3063 handle->close_msg_retransmission_task_id =
3064 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
3065 &close_msg_retransmission_task,
3072 * Cancels a pending shutdown
3074 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
3077 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle)
3079 if (GNUNET_SCHEDULER_NO_TASK != handle->close_msg_retransmission_task_id)
3080 GNUNET_SCHEDULER_cancel (handle->close_msg_retransmission_task_id);
3081 handle->socket->shutdown_handle = NULL;
3082 GNUNET_free (handle);
3089 * @param socket the stream socket
3092 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
3094 struct MessageQueue *head;
3096 if (NULL != socket->read_handle)
3098 LOG (GNUNET_ERROR_TYPE_WARNING,
3099 "Closing STREAM socket when a read handle is pending\n");
3100 GNUNET_STREAM_io_read_cancel (socket->read_handle);
3102 if (NULL != socket->write_handle)
3104 LOG (GNUNET_ERROR_TYPE_WARNING,
3105 "Closing STREAM socket when a write handle is pending\n");
3106 GNUNET_STREAM_io_write_cancel (socket->write_handle);
3107 //socket->write_handle = NULL;
3109 /* Terminate the ack'ing task if they are still present */
3110 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
3112 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
3113 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
3115 /* Terminate the control retransmission tasks */
3116 if (GNUNET_SCHEDULER_NO_TASK != socket->control_retransmission_task_id)
3118 GNUNET_SCHEDULER_cancel (socket->control_retransmission_task_id);
3120 /* Clear Transmit handles */
3121 if (NULL != socket->transmit_handle)
3123 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
3124 socket->transmit_handle = NULL;
3126 /* Clear existing message queue */
3127 while (NULL != (head = socket->queue_head)) {
3128 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
3131 GNUNET_free (head->message);
3134 /* Close associated tunnel */
3135 if (NULL != socket->tunnel)
3137 GNUNET_MESH_tunnel_destroy (socket->tunnel);
3138 socket->tunnel = NULL;
3140 /* Close mesh connection */
3141 if (NULL != socket->mesh && NULL == socket->lsocket)
3143 GNUNET_MESH_disconnect (socket->mesh);
3144 socket->mesh = NULL;
3146 /* Release receive buffer */
3147 if (NULL != socket->receive_buffer)
3149 GNUNET_free (socket->receive_buffer);
3151 GNUNET_free (socket);
3156 * Listens for stream connections for a specific application ports
3158 * @param cfg the configuration to use
3159 * @param app_port the application port for which new streams will be accepted
3160 * @param listen_cb this function will be called when a peer tries to establish
3162 * @param listen_cb_cls closure for listen_cb
3163 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
3164 * @return listen socket, NULL for any error
3166 struct GNUNET_STREAM_ListenSocket *
3167 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
3168 GNUNET_MESH_ApplicationType app_port,
3169 GNUNET_STREAM_ListenCallback listen_cb,
3170 void *listen_cb_cls,
3173 /* FIXME: Add variable args for passing configration options? */
3174 struct GNUNET_STREAM_ListenSocket *lsocket;
3175 struct GNUNET_TIME_Relative listen_timeout;
3176 enum GNUNET_STREAM_Option option;
3178 uint16_t payload_size;
3180 GNUNET_assert (NULL != listen_cb);
3181 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
3182 lsocket->cfg = GNUNET_CONFIGURATION_dup (cfg);
3183 lsocket->lockmanager = GNUNET_LOCKMANAGER_connect (lsocket->cfg);
3184 if (NULL == lsocket->lockmanager)
3186 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3187 GNUNET_free (lsocket);
3190 lsocket->listening = GNUNET_NO;/* We listen when we get a lock on app_port */
3192 lsocket->retransmit_timeout = TIME_REL_SECS (default_timeout);
3193 lsocket->testing_active = GNUNET_NO;
3194 lsocket->listen_ok_cb = NULL;
3195 lsocket->max_payload_size = DEFAULT_MAX_PAYLOAD_SIZE;
3196 listen_timeout = TIME_REL_SECS (60); /* A minute for listen timeout */
3197 va_start (vargs, listen_cb_cls);
3199 option = va_arg (vargs, enum GNUNET_STREAM_Option);
3202 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
3203 lsocket->retransmit_timeout = va_arg (vargs,
3204 struct GNUNET_TIME_Relative);
3206 case GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER:
3207 lsocket->testing_active = GNUNET_YES;
3208 lsocket->testing_set_write_sequence_number_value = va_arg (vargs,
3211 case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT:
3212 listen_timeout = GNUNET_TIME_relative_multiply
3213 (GNUNET_TIME_UNIT_MILLISECONDS, va_arg (vargs, uint32_t));
3215 case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS:
3216 lsocket->listen_ok_cb = va_arg (vargs,
3217 GNUNET_STREAM_ListenSuccessCallback);
3219 case GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE:
3220 payload_size = (uint16_t) va_arg (vargs, unsigned int);
3221 GNUNET_assert (0 != payload_size);
3222 if (payload_size < lsocket->max_payload_size)
3223 lsocket->max_payload_size = payload_size;
3225 case GNUNET_STREAM_OPTION_END:
3228 } while (GNUNET_STREAM_OPTION_END != option);
3230 lsocket->port = app_port;
3231 lsocket->listen_cb = listen_cb;
3232 lsocket->listen_cb_cls = listen_cb_cls;
3233 lsocket->locking_request =
3234 GNUNET_LOCKMANAGER_acquire_lock (lsocket->lockmanager, locking_domain,
3235 (uint32_t) lsocket->port,
3236 &lock_status_change_cb, lsocket);
3237 lsocket->lockmanager_acquire_timeout_task =
3238 GNUNET_SCHEDULER_add_delayed (listen_timeout,
3239 &lockmanager_acquire_timeout, lsocket);
3245 * Closes the listen socket
3247 * @param lsocket the listen socket
3250 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
3252 /* Close MESH connection */
3253 if (NULL != lsocket->mesh)
3254 GNUNET_MESH_disconnect (lsocket->mesh);
3255 GNUNET_CONFIGURATION_destroy (lsocket->cfg);
3256 if (GNUNET_SCHEDULER_NO_TASK != lsocket->lockmanager_acquire_timeout_task)
3257 GNUNET_SCHEDULER_cancel (lsocket->lockmanager_acquire_timeout_task);
3258 if (NULL != lsocket->locking_request)
3259 GNUNET_LOCKMANAGER_cancel_request (lsocket->locking_request);
3260 if (NULL != lsocket->lockmanager)
3261 GNUNET_LOCKMANAGER_disconnect (lsocket->lockmanager);
3262 GNUNET_free (lsocket);
3267 * Tries to write the given data to the stream. The maximum size of data that
3268 * can be written as part of a write operation is (64 * (64000 - sizeof (struct
3269 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
3270 * violation, however only the said number of maximum bytes will be written.
3272 * @param socket the socket representing a stream
3273 * @param data the data buffer from where the data is written into the stream
3274 * @param size the number of bytes to be written from the data buffer
3275 * @param timeout the timeout period
3276 * @param write_cont the function to call upon writing some bytes into the
3278 * @param write_cont_cls the closure
3280 * @return handle to cancel the operation; if a previous write is pending or
3281 * the stream has been shutdown for this operation then write_cont is
3282 * immediately called and NULL is returned.
3284 struct GNUNET_STREAM_IOWriteHandle *
3285 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
3288 struct GNUNET_TIME_Relative timeout,
3289 GNUNET_STREAM_CompletionContinuation write_cont,
3290 void *write_cont_cls)
3292 struct GNUNET_STREAM_IOWriteHandle *io_handle;
3293 struct GNUNET_STREAM_DataMessage *data_msg;
3295 struct GNUNET_TIME_Relative ack_deadline;
3296 unsigned int num_needed_packets;
3297 unsigned int packet;
3298 uint32_t packet_size;
3299 uint32_t payload_size;
3300 uint16_t max_data_packet_size;
3302 LOG (GNUNET_ERROR_TYPE_DEBUG,
3304 if (NULL != socket->write_handle)
3309 switch (socket->state)
3311 case STATE_TRANSMIT_CLOSED:
3312 case STATE_TRANSMIT_CLOSE_WAIT:
3314 case STATE_CLOSE_WAIT:
3315 if (NULL != write_cont)
3316 write_cont (write_cont_cls, GNUNET_STREAM_SHUTDOWN, 0);
3317 LOG (GNUNET_ERROR_TYPE_DEBUG,
3318 "%s() END\n", __func__);
3322 case STATE_HELLO_WAIT:
3323 if (NULL != write_cont)
3324 /* FIXME: GNUNET_STREAM_SYSERR?? */
3325 write_cont (write_cont_cls, GNUNET_STREAM_SYSERR, 0);
3326 LOG (GNUNET_ERROR_TYPE_DEBUG,
3327 "%s() END\n", __func__);
3329 case STATE_ESTABLISHED:
3330 case STATE_RECEIVE_CLOSED:
3331 case STATE_RECEIVE_CLOSE_WAIT:
3334 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * socket->max_payload_size < size)
3335 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * socket->max_payload_size;
3336 num_needed_packets =
3337 (size + (socket->max_payload_size - 1)) / socket->max_payload_size;
3338 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
3339 io_handle->socket = socket;
3340 io_handle->write_cont = write_cont;
3341 io_handle->write_cont_cls = write_cont_cls;
3342 io_handle->size = size;
3343 io_handle->packets_sent = 0;
3345 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3346 determined from RTT */
3347 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
3348 /* Divide the given buffer into packets for sending */
3349 max_data_packet_size =
3350 socket->max_payload_size + sizeof (struct GNUNET_STREAM_DataMessage);
3351 for (packet=0; packet < num_needed_packets; packet++)
3353 if ((packet + 1) * socket->max_payload_size < size)
3355 payload_size = socket->max_payload_size;
3356 packet_size = max_data_packet_size;
3360 payload_size = size - packet * socket->max_payload_size;
3362 payload_size + sizeof (struct GNUNET_STREAM_DataMessage);
3364 io_handle->messages[packet] = GNUNET_malloc (packet_size);
3365 io_handle->messages[packet]->header.header.size = htons (packet_size);
3366 io_handle->messages[packet]->header.header.type =
3367 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
3368 io_handle->messages[packet]->sequence_number =
3369 htonl (socket->write_sequence_number++);
3370 io_handle->messages[packet]->offset = htonl (socket->write_offset);
3371 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
3372 determined from RTT */
3373 io_handle->messages[packet]->ack_deadline =
3374 GNUNET_TIME_relative_hton (ack_deadline);
3375 data_msg = io_handle->messages[packet];
3376 /* Copy data from given buffer to the packet */
3377 memcpy (&data_msg[1], sweep, payload_size);
3378 sweep += payload_size;
3379 socket->write_offset += payload_size;
3381 /* ack the last data message. FIXME: remove when we figure out how to do this
3383 io_handle->messages[num_needed_packets - 1]->ack_deadline =
3384 GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_ZERO);
3385 socket->write_handle = io_handle;
3386 write_data (socket);
3387 LOG (GNUNET_ERROR_TYPE_DEBUG,
3388 "%s() END\n", __func__);
3394 * Tries to read data from the stream.
3396 * @param socket the socket representing a stream
3397 * @param timeout the timeout period
3398 * @param proc function to call with data (once only)
3399 * @param proc_cls the closure for proc
3401 * @return handle to cancel the operation; NULL is returned if: the stream has
3402 * been shutdown for this type of opeartion (the DataProcessor is
3403 * immediately called with GNUNET_STREAM_SHUTDOWN as status) OR another
3404 * read handle is present (only one read handle per socket is present
3407 struct GNUNET_STREAM_IOReadHandle *
3408 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
3409 struct GNUNET_TIME_Relative timeout,
3410 GNUNET_STREAM_DataProcessor proc,
3413 struct GNUNET_STREAM_IOReadHandle *read_handle;
3415 LOG (GNUNET_ERROR_TYPE_DEBUG,
3417 GNUNET_i2s (&socket->other_peer),
3419 /* Return NULL if there is already a read handle; the user has to cancel that
3420 first before continuing or has to wait until it is completed */
3421 if (NULL != socket->read_handle)
3423 GNUNET_assert (NULL != proc);
3424 switch (socket->state)
3426 case STATE_RECEIVE_CLOSED:
3427 case STATE_RECEIVE_CLOSE_WAIT:
3429 case STATE_CLOSE_WAIT:
3430 proc (proc_cls, GNUNET_STREAM_SHUTDOWN, NULL, 0);
3431 LOG (GNUNET_ERROR_TYPE_DEBUG,
3433 GNUNET_i2s (&socket->other_peer),
3439 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
3440 read_handle->proc = proc;
3441 read_handle->proc_cls = proc_cls;
3442 read_handle->socket = socket;
3443 socket->read_handle = read_handle;
3444 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
3446 read_handle->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
3448 read_handle->read_io_timeout_task_id =
3449 GNUNET_SCHEDULER_add_delayed (timeout, &read_io_timeout, socket);
3450 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s: %s() END\n",
3451 GNUNET_i2s (&socket->other_peer), __func__);
3457 * Cancel pending write operation.
3459 * @param ioh handle to operation to cancel
3462 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
3464 struct GNUNET_STREAM_Socket *socket = ioh->socket;
3465 unsigned int packet;
3467 GNUNET_assert (NULL != socket->write_handle);
3468 GNUNET_assert (socket->write_handle == ioh);
3470 if (GNUNET_SCHEDULER_NO_TASK != socket->data_retransmission_task_id)
3472 GNUNET_SCHEDULER_cancel (socket->data_retransmission_task_id);
3473 socket->data_retransmission_task_id = GNUNET_SCHEDULER_NO_TASK;
3476 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
3478 if (NULL == ioh->messages[packet]) break;
3479 GNUNET_free (ioh->messages[packet]);
3482 GNUNET_free (socket->write_handle);
3483 socket->write_handle = NULL;
3488 * Cancel pending read operation.
3490 * @param ioh handle to operation to cancel
3493 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)
3495 struct GNUNET_STREAM_Socket *socket;
3497 socket = ioh->socket;
3498 GNUNET_assert (NULL != socket->read_handle);
3499 GNUNET_assert (ioh == socket->read_handle);
3500 /* Read io time task should be there; if it is already executed then this
3501 read handle is not valid; However upon scheduler shutdown the read io task
3502 may be executed before */
3503 if (GNUNET_SCHEDULER_NO_TASK != ioh->read_io_timeout_task_id)
3504 GNUNET_SCHEDULER_cancel (ioh->read_io_timeout_task_id);
3505 /* reading task may be present; if so we have to stop it */
3506 if (GNUNET_SCHEDULER_NO_TASK != ioh->read_task_id)
3507 GNUNET_SCHEDULER_cancel (ioh->read_task_id);
3509 socket->read_handle = NULL;
3512 /* end of stream_api.c */