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 * Decrement PEER intern count during socket close and listen close to free the
27 * memory used for PEER interning
29 * Add code for write io timeout
31 * Include retransmission for control messages
35 * @file stream/stream_api.c
36 * @brief Implementation of the stream library
37 * @author Sree Harsha Totakura
42 #include "gnunet_common.h"
43 #include "gnunet_crypto_lib.h"
44 #include "gnunet_stream_lib.h"
45 #include "gnunet_testing_lib.h"
46 #include "stream_protocol.h"
50 * The maximum packet size of a stream packet
52 #define MAX_PACKET_SIZE 64000
57 #define RECEIVE_BUFFER_SIZE 4096000
60 * The maximum payload a data message packet can carry
62 static size_t max_payload_size =
63 MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
66 * states in the Protocol
71 * Client initialization state
76 * Listener initialization state
81 * Pre-connection establishment state
86 * State where a connection has been established
91 * State where the socket is closed on our side and waiting to be ACK'ed
93 STATE_RECEIVE_CLOSE_WAIT,
96 * State where the socket is closed for reading
101 * State where the socket is closed on our side and waiting to be ACK'ed
103 STATE_TRANSMIT_CLOSE_WAIT,
106 * State where the socket is closed for writing
108 STATE_TRANSMIT_CLOSED,
111 * State where the socket is closed on our side and waiting to be ACK'ed
116 * State where the socket is closed
123 * Functions of this type are called when a message is written
125 * @param cls the closure from queue_message
126 * @param socket the socket the written message was bound to
128 typedef void (*SendFinishCallback) (void *cls,
129 struct GNUNET_STREAM_Socket *socket);
133 * The send message queue
140 struct GNUNET_STREAM_MessageHeader *message;
143 * Callback to be called when the message is sent
145 SendFinishCallback finish_cb;
148 * The closure for finish_cb
153 * The next message in queue. Should be NULL in the last message
155 struct MessageQueue *next;
158 * The next message in queue. Should be NULL in the first message
160 struct MessageQueue *prev;
165 * The STREAM Socket Handler
167 struct GNUNET_STREAM_Socket
170 * Retransmission timeout
172 struct GNUNET_TIME_Relative retransmit_timeout;
175 * The Acknowledgement Bitmap
177 GNUNET_STREAM_AckBitmap ack_bitmap;
180 * Time when the Acknowledgement was queued
182 struct GNUNET_TIME_Absolute ack_time_registered;
185 * Queued Acknowledgement deadline
187 struct GNUNET_TIME_Relative ack_time_deadline;
192 struct GNUNET_MESH_Handle *mesh;
195 * The mesh tunnel handle
197 struct GNUNET_MESH_Tunnel *tunnel;
200 * Stream open closure
205 * Stream open callback
207 GNUNET_STREAM_OpenCallback open_cb;
210 * The current transmit handle (if a pending transmit request exists)
212 struct GNUNET_MESH_TransmitHandle *transmit_handle;
215 * The current message associated with the transmit handle
217 struct MessageQueue *queue_head;
220 * The queue tail, should always point to the last message in queue
222 struct MessageQueue *queue_tail;
225 * The write IO_handle associated with this socket
227 struct GNUNET_STREAM_IOWriteHandle *write_handle;
230 * The read IO_handle associated with this socket
232 struct GNUNET_STREAM_IOReadHandle *read_handle;
235 * The shutdown handle associated with this socket
237 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
240 * Buffer for storing received messages
242 void *receive_buffer;
245 * The listen socket from which this socket is derived. Should be NULL if it
246 * is not a derived socket
248 struct GNUNET_STREAM_ListenSocket *lsocket;
251 * Task identifier for the read io timeout task
253 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
256 * Task identifier for retransmission task after timeout
258 GNUNET_SCHEDULER_TaskIdentifier retransmission_timeout_task_id;
261 * The task for sending timely Acks
263 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
266 * Task scheduled to continue a read operation.
268 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
271 * The state of the protocol associated with this socket
276 * The status of the socket
278 enum GNUNET_STREAM_Status status;
281 * The number of previous timeouts; FIXME: currently not used
283 unsigned int retries;
286 * The peer identity of the peer at the other end of the stream
288 GNUNET_PEER_Id other_peer;
291 * Our Peer Identity (for debugging)
293 GNUNET_PEER_Id our_id;
296 * The application port number (type: uint32_t)
298 GNUNET_MESH_ApplicationType app_port;
301 * The session id associated with this stream connection
302 * FIXME: Not used currently, may be removed
307 * Write sequence number. Set to random when sending HELLO(client) and
310 uint32_t write_sequence_number;
313 * Read sequence number. This number's value is determined during handshake
315 uint32_t read_sequence_number;
318 * The receiver buffer size
320 uint32_t receive_buffer_size;
323 * The receiver buffer boundaries
325 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
328 * receiver's available buffer after the last acknowledged packet
330 uint32_t receiver_window_available;
333 * The offset pointer used during write operation
335 uint32_t write_offset;
338 * The offset after which we are expecting data
340 uint32_t read_offset;
343 * The offset upto which user has read from the received buffer
345 uint32_t copy_offset;
350 * A socket for listening
352 struct GNUNET_STREAM_ListenSocket
357 struct GNUNET_MESH_Handle *mesh;
360 * The callback function which is called after successful opening socket
362 GNUNET_STREAM_ListenCallback listen_cb;
365 * The call back closure
370 * Our interned Peer's identity
372 GNUNET_PEER_Id our_id;
376 * FIXME: Remove if not required!
378 GNUNET_MESH_ApplicationType port;
383 * The IO Write Handle
385 struct GNUNET_STREAM_IOWriteHandle
388 * The socket to which this write handle is associated
390 struct GNUNET_STREAM_Socket *socket;
393 * The packet_buffers associated with this Handle
395 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
398 * The write continuation callback
400 GNUNET_STREAM_CompletionContinuation write_cont;
403 * Write continuation closure
405 void *write_cont_cls;
408 * The bitmap of this IOHandle; Corresponding bit for a message is set when
409 * it has been acknowledged by the receiver
411 GNUNET_STREAM_AckBitmap ack_bitmap;
414 * Number of bytes in this write handle
423 struct GNUNET_STREAM_IOReadHandle
426 * Callback for the read processor
428 GNUNET_STREAM_DataProcessor proc;
431 * The closure pointer for the read processor callback
438 * Handle for Shutdown
440 struct GNUNET_STREAM_ShutdownHandle
443 * The socket associated with this shutdown handle
445 struct GNUNET_STREAM_Socket *socket;
448 * Shutdown completion callback
450 GNUNET_STREAM_ShutdownCompletion completion_cb;
453 * Closure for completion callback
455 void *completion_cls;
458 * Close message retransmission task id
460 GNUNET_SCHEDULER_TaskIdentifier close_msg_retransmission_task_id;
463 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
470 * Default value in seconds for various timeouts
472 static unsigned int default_timeout = 10;
476 * Callback function for sending queued message
478 * @param cls closure the socket
479 * @param size number of bytes available in buf
480 * @param buf where the callee should write the message
481 * @return number of bytes written to buf
484 send_message_notify (void *cls, size_t size, void *buf)
486 struct GNUNET_STREAM_Socket *socket = cls;
487 struct GNUNET_PeerIdentity target;
488 struct MessageQueue *head;
491 socket->transmit_handle = NULL; /* Remove the transmit handle */
492 head = socket->queue_head;
494 return 0; /* just to be safe */
495 GNUNET_PEER_resolve (socket->other_peer, &target);
496 if (0 == size) /* request timed out */
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
500 "Message sending timed out. Retry %d \n",
502 socket->transmit_handle =
503 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
506 /* FIXME: exponential backoff */
507 socket->retransmit_timeout,
509 ntohs (head->message->header.size),
510 &send_message_notify,
515 ret = ntohs (head->message->header.size);
516 GNUNET_assert (size >= ret);
517 memcpy (buf, head->message, ret);
518 if (NULL != head->finish_cb)
520 head->finish_cb (head->finish_cb_cls, socket);
522 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
525 GNUNET_free (head->message);
527 head = socket->queue_head;
528 if (NULL != head) /* more pending messages to send */
531 socket->transmit_handle =
532 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
535 /* FIXME: exponential backoff */
536 socket->retransmit_timeout,
538 ntohs (head->message->header.size),
539 &send_message_notify,
547 * Queues a message for sending using the mesh connection of a socket
549 * @param socket the socket whose mesh connection is used
550 * @param message the message to be sent
551 * @param finish_cb the callback to be called when the message is sent
552 * @param finish_cb_cls the closure for the callback
555 queue_message (struct GNUNET_STREAM_Socket *socket,
556 struct GNUNET_STREAM_MessageHeader *message,
557 SendFinishCallback finish_cb,
560 struct MessageQueue *queue_entity;
561 struct GNUNET_PeerIdentity target;
564 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
565 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 "%x: Queueing message of type %d and size %d\n",
570 ntohs (message->header.type),
571 ntohs (message->header.size));
572 GNUNET_assert (NULL != message);
573 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
574 queue_entity->message = message;
575 queue_entity->finish_cb = finish_cb;
576 queue_entity->finish_cb_cls = finish_cb_cls;
577 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
580 if (NULL == socket->transmit_handle)
583 GNUNET_PEER_resolve (socket->other_peer, &target);
584 socket->transmit_handle =
585 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
588 socket->retransmit_timeout,
590 ntohs (message->header.size),
591 &send_message_notify,
598 * Copies a message and queues it for sending using the mesh connection of
601 * @param socket the socket whose mesh connection is used
602 * @param message the message to be sent
603 * @param finish_cb the callback to be called when the message is sent
604 * @param finish_cb_cls the closure for the callback
607 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
608 const struct GNUNET_STREAM_MessageHeader *message,
609 SendFinishCallback finish_cb,
612 struct GNUNET_STREAM_MessageHeader *msg_copy;
615 size = ntohs (message->header.size);
616 msg_copy = GNUNET_malloc (size);
617 memcpy (msg_copy, message, size);
618 queue_message (socket, msg_copy, finish_cb, finish_cb_cls);
623 * Callback function for sending ack message
625 * @param cls closure the ACK message created in ack_task
626 * @param size number of bytes available in buffer
627 * @param buf where the callee should write the message
628 * @return number of bytes written to buf
631 send_ack_notify (void *cls, size_t size, void *buf)
633 struct GNUNET_STREAM_AckMessage *ack_msg = cls;
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "%s called with size 0\n", __func__);
641 GNUNET_assert (ntohs (ack_msg->header.header.size) <= size);
643 size = ntohs (ack_msg->header.header.size);
644 memcpy (buf, ack_msg, size);
649 * Writes data using the given socket. The amount of data written is limited by
650 * the receiver_window_size
652 * @param socket the socket to use
655 write_data (struct GNUNET_STREAM_Socket *socket);
658 * Task for retransmitting data messages if they aren't ACK before their ack
661 * @param cls the socket
662 * @param tc the Task context
665 retransmission_timeout_task (void *cls,
666 const struct GNUNET_SCHEDULER_TaskContext *tc)
668 struct GNUNET_STREAM_Socket *socket = cls;
670 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 "%x: Retransmitting DATA...\n", socket->our_id);
675 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
681 * Task for sending ACK message
683 * @param cls the socket
684 * @param tc the Task context
688 const struct GNUNET_SCHEDULER_TaskContext *tc)
690 struct GNUNET_STREAM_Socket *socket = cls;
691 struct GNUNET_STREAM_AckMessage *ack_msg;
692 struct GNUNET_PeerIdentity target;
694 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
699 socket->ack_task_id = 0;
701 /* Create the ACK Message */
702 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
703 ack_msg->header.header.size = htons (sizeof (struct
704 GNUNET_STREAM_AckMessage));
705 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
706 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
707 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
708 ack_msg->receive_window_remaining =
709 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
711 GNUNET_PEER_resolve (socket->other_peer, &target);
712 /* Request MESH for sending ACK */
713 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
716 socket->retransmit_timeout,
718 ntohs (ack_msg->header.header.size),
727 * Retransmission task for shutdown messages
729 * @param cls the shutdown handle
730 * @param tc the Task Context
733 close_msg_retransmission_task (void *cls,
734 const struct GNUNET_SCHEDULER_TaskContext *tc)
736 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle = cls;
737 struct GNUNET_STREAM_MessageHeader *msg;
738 struct GNUNET_STREAM_Socket *socket;
740 GNUNET_assert (NULL != shutdown_handle);
741 socket = shutdown_handle->socket;
743 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
744 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
745 switch (shutdown_handle->operation)
748 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
751 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
754 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
758 shutdown_handle->close_msg_retransmission_task_id =
759 GNUNET_SCHEDULER_NO_TASK;
762 queue_message (socket, msg, NULL, NULL);
763 shutdown_handle->close_msg_retransmission_task_id =
764 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
765 &close_msg_retransmission_task,
771 * Function to modify a bit in GNUNET_STREAM_AckBitmap
773 * @param bitmap the bitmap to modify
774 * @param bit the bit number to modify
775 * @param value GNUNET_YES to on, GNUNET_NO to off
778 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
782 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
783 if (GNUNET_YES == value)
784 *bitmap |= (1LL << bit);
786 *bitmap &= ~(1LL << bit);
791 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
793 * @param bitmap address of the bitmap that has to be checked
794 * @param bit the bit number to check
795 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
798 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
801 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
802 return 0 != (*bitmap & (1LL << bit));
807 * Writes data using the given socket. The amount of data written is limited by
808 * the receiver_window_size
810 * @param socket the socket to use
813 write_data (struct GNUNET_STREAM_Socket *socket)
815 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
816 int packet; /* Although an int, should never be negative */
820 /* Find the last acknowledged packet */
821 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
823 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
826 else if (NULL == io_handle->messages[packet])
829 /* Resend packets which weren't ack'ed */
830 for (packet=0; packet < ack_packet; packet++)
832 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "%x: Placing DATA message with sequence %u in send queue\n",
838 ntohl (io_handle->messages[packet]->sequence_number));
840 copy_and_queue_message (socket,
841 &io_handle->messages[packet]->header,
846 packet = ack_packet + 1;
847 /* Now send new packets if there is enough buffer space */
848 while ( (NULL != io_handle->messages[packet]) &&
849 (socket->receiver_window_available
850 >= ntohs (io_handle->messages[packet]->header.header.size)) )
852 socket->receiver_window_available -=
853 ntohs (io_handle->messages[packet]->header.header.size);
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "%x: Placing DATA message with sequence %u in send queue\n",
857 ntohl (io_handle->messages[packet]->sequence_number));
858 copy_and_queue_message (socket,
859 &io_handle->messages[packet]->header,
865 if (GNUNET_SCHEDULER_NO_TASK == socket->retransmission_timeout_task_id)
866 socket->retransmission_timeout_task_id =
867 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
868 (GNUNET_TIME_UNIT_SECONDS, 8),
869 &retransmission_timeout_task,
875 * Task for calling the read processor
877 * @param cls the socket
878 * @param tc the task context
881 call_read_processor (void *cls,
882 const struct GNUNET_SCHEDULER_TaskContext *tc)
884 struct GNUNET_STREAM_Socket *socket = cls;
886 size_t valid_read_size;
888 uint32_t sequence_increase;
889 uint32_t offset_increase;
891 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
892 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
895 if (NULL == socket->receive_buffer)
898 GNUNET_assert (NULL != socket->read_handle);
899 GNUNET_assert (NULL != socket->read_handle->proc);
901 /* Check the bitmap for any holes */
902 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
904 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
908 /* We only call read processor if we have the first packet */
909 GNUNET_assert (0 < packet);
912 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
914 GNUNET_assert (0 != valid_read_size);
916 /* Cancel the read_io_timeout_task */
917 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task_id);
918 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
920 /* Call the data processor */
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "%x: Calling read processor\n",
925 socket->read_handle->proc (socket->read_handle->proc_cls,
927 socket->receive_buffer + socket->copy_offset,
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 "%x: Read processor read %d bytes\n",
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "%x: Read processor completed successfully\n",
937 /* Free the read handle */
938 GNUNET_free (socket->read_handle);
939 socket->read_handle = NULL;
941 GNUNET_assert (read_size <= valid_read_size);
942 socket->copy_offset += read_size;
944 /* Determine upto which packet we can remove from the buffer */
945 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
947 if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
949 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
953 /* If no packets can be removed we can't move the buffer */
954 if (0 == packet) return;
956 sequence_increase = packet;
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "%x: Sequence increase after read processor completion: %u\n",
962 /* Shift the data in the receive buffer */
963 memmove (socket->receive_buffer,
964 socket->receive_buffer
965 + socket->receive_buffer_boundaries[sequence_increase-1],
966 socket->receive_buffer_size
967 - socket->receive_buffer_boundaries[sequence_increase-1]);
969 /* Shift the bitmap */
970 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
972 /* Set read_sequence_number */
973 socket->read_sequence_number += sequence_increase;
975 /* Set read_offset */
976 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
977 socket->read_offset += offset_increase;
979 /* Fix copy_offset */
980 GNUNET_assert (offset_increase <= socket->copy_offset);
981 socket->copy_offset -= offset_increase;
983 /* Fix relative boundaries */
984 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
986 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
988 socket->receive_buffer_boundaries[packet] =
989 socket->receive_buffer_boundaries[packet + sequence_increase]
993 socket->receive_buffer_boundaries[packet] = 0;
999 * Cancels the existing read io handle
1001 * @param cls the closure from the SCHEDULER call
1002 * @param tc the task context
1005 read_io_timeout (void *cls,
1006 const struct GNUNET_SCHEDULER_TaskContext *tc)
1008 struct GNUNET_STREAM_Socket *socket = cls;
1009 GNUNET_STREAM_DataProcessor proc;
1012 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
1013 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016 "%x: Read task timedout - Cancelling it\n",
1018 GNUNET_SCHEDULER_cancel (socket->read_task_id);
1019 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
1021 GNUNET_assert (NULL != socket->read_handle);
1022 proc = socket->read_handle->proc;
1023 proc_cls = socket->read_handle->proc_cls;
1025 GNUNET_free (socket->read_handle);
1026 socket->read_handle = NULL;
1027 /* Call the read processor to signal timeout */
1029 GNUNET_STREAM_TIMEOUT,
1036 * Handler for DATA messages; Same for both client and server
1038 * @param socket the socket through which the ack was received
1039 * @param tunnel connection to the other end
1040 * @param sender who sent the message
1041 * @param msg the data message
1042 * @param atsi performance data for the connection
1043 * @return GNUNET_OK to keep the connection open,
1044 * GNUNET_SYSERR to close it (signal serious error)
1047 handle_data (struct GNUNET_STREAM_Socket *socket,
1048 struct GNUNET_MESH_Tunnel *tunnel,
1049 const struct GNUNET_PeerIdentity *sender,
1050 const struct GNUNET_STREAM_DataMessage *msg,
1051 const struct GNUNET_ATS_Information*atsi)
1053 const void *payload;
1054 uint32_t bytes_needed;
1055 uint32_t relative_offset;
1056 uint32_t relative_sequence_number;
1059 size = htons (msg->header.header.size);
1060 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
1062 GNUNET_break_op (0);
1063 return GNUNET_SYSERR;
1066 if (GNUNET_PEER_search (sender) != socket->other_peer)
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "%x: Received DATA from non-confirming peer\n",
1074 switch (socket->state)
1076 case STATE_ESTABLISHED:
1077 case STATE_TRANSMIT_CLOSED:
1078 case STATE_TRANSMIT_CLOSE_WAIT:
1080 /* check if the message's sequence number is in the range we are
1082 relative_sequence_number =
1083 ntohl (msg->sequence_number) - socket->read_sequence_number;
1084 if ( relative_sequence_number > GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1087 "%x: Ignoring received message with sequence number %u\n",
1089 ntohl (msg->sequence_number));
1090 /* Start ACK sending task if one is not already present */
1091 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1093 socket->ack_task_id =
1094 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1095 (msg->ack_deadline),
1102 /* Check if we have already seen this message */
1103 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1104 relative_sequence_number))
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1107 "%x: Ignoring already received message with sequence "
1110 ntohl (msg->sequence_number));
1111 /* Start ACK sending task if one is not already present */
1112 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1114 socket->ack_task_id =
1115 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1116 (msg->ack_deadline),
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "%x: Receiving DATA with sequence number: %u and size: %d "
1127 ntohl (msg->sequence_number),
1128 ntohs (msg->header.header.size),
1129 socket->other_peer);
1131 /* Check if we have to allocate the buffer */
1132 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1133 relative_offset = ntohl (msg->offset) - socket->read_offset;
1134 bytes_needed = relative_offset + size;
1135 if (bytes_needed > socket->receive_buffer_size)
1137 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1139 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1141 socket->receive_buffer_size = bytes_needed;
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 "%x: Cannot accommodate packet %d as buffer is",
1149 ntohl (msg->sequence_number));
1154 /* Copy Data to buffer */
1156 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1157 memcpy (socket->receive_buffer + relative_offset,
1160 socket->receive_buffer_boundaries[relative_sequence_number] =
1161 relative_offset + size;
1163 /* Modify the ACK bitmap */
1164 ackbitmap_modify_bit (&socket->ack_bitmap,
1165 relative_sequence_number,
1168 /* Start ACK sending task if one is not already present */
1169 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1171 socket->ack_task_id =
1172 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1173 (msg->ack_deadline),
1178 if ((NULL != socket->read_handle) /* A read handle is waiting */
1179 /* There is no current read task */
1180 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
1181 /* We have the first packet */
1182 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "%x: Scheduling read processor\n",
1189 socket->read_task_id =
1190 GNUNET_SCHEDULER_add_now (&call_read_processor,
1197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198 "%x: Received data message when it cannot be handled\n",
1207 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1209 * @param cls the socket (set from GNUNET_MESH_connect)
1210 * @param tunnel connection to the other end
1211 * @param tunnel_ctx place to store local state associated with the tunnel
1212 * @param sender who sent the message
1213 * @param message the actual message
1214 * @param atsi performance data for the connection
1215 * @return GNUNET_OK to keep the connection open,
1216 * GNUNET_SYSERR to close it (signal serious error)
1219 client_handle_data (void *cls,
1220 struct GNUNET_MESH_Tunnel *tunnel,
1222 const struct GNUNET_PeerIdentity *sender,
1223 const struct GNUNET_MessageHeader *message,
1224 const struct GNUNET_ATS_Information*atsi)
1226 struct GNUNET_STREAM_Socket *socket = cls;
1228 return handle_data (socket,
1231 (const struct GNUNET_STREAM_DataMessage *) message,
1237 * Callback to set state to ESTABLISHED
1239 * @param cls the closure from queue_message FIXME: document
1240 * @param socket the socket to requiring state change
1243 set_state_established (void *cls,
1244 struct GNUNET_STREAM_Socket *socket)
1246 struct GNUNET_PeerIdentity initiator_pid;
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "%x: Attaining ESTABLISHED state\n",
1251 socket->write_offset = 0;
1252 socket->read_offset = 0;
1253 socket->state = STATE_ESTABLISHED;
1254 /* FIXME: What if listen_cb is NULL */
1255 if (NULL != socket->lsocket)
1257 GNUNET_PEER_resolve (socket->other_peer, &initiator_pid);
1258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259 "%x: Calling listen callback\n",
1261 if (GNUNET_SYSERR ==
1262 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1266 socket->state = STATE_CLOSED;
1267 /* FIXME: We should close in a decent way */
1268 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1269 GNUNET_free (socket);
1272 else if (socket->open_cb)
1273 socket->open_cb (socket->open_cls, socket);
1278 * Callback to set state to HELLO_WAIT
1280 * @param cls the closure from queue_message
1281 * @param socket the socket to requiring state change
1284 set_state_hello_wait (void *cls,
1285 struct GNUNET_STREAM_Socket *socket)
1287 GNUNET_assert (STATE_INIT == socket->state);
1288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1289 "%x: Attaining HELLO_WAIT state\n",
1291 socket->state = STATE_HELLO_WAIT;
1296 * Callback to set state to CLOSE_WAIT
1298 * @param cls the closure from queue_message
1299 * @param socket the socket requiring state change
1302 set_state_close_wait (void *cls,
1303 struct GNUNET_STREAM_Socket *socket)
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306 "%x: Attaing CLOSE_WAIT state\n",
1308 socket->state = STATE_CLOSE_WAIT;
1309 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1310 socket->receive_buffer = NULL;
1311 socket->receive_buffer_size = 0;
1316 * Callback to set state to RECEIVE_CLOSE_WAIT
1318 * @param cls the closure from queue_message
1319 * @param socket the socket requiring state change
1322 set_state_receive_close_wait (void *cls,
1323 struct GNUNET_STREAM_Socket *socket)
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 "%x: Attaing RECEIVE_CLOSE_WAIT state\n",
1328 socket->state = STATE_RECEIVE_CLOSE_WAIT;
1329 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1330 socket->receive_buffer = NULL;
1331 socket->receive_buffer_size = 0;
1336 * Callback to set state to TRANSMIT_CLOSE_WAIT
1338 * @param cls the closure from queue_message
1339 * @param socket the socket requiring state change
1342 set_state_transmit_close_wait (void *cls,
1343 struct GNUNET_STREAM_Socket *socket)
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1346 "%x: Attaing TRANSMIT_CLOSE_WAIT state\n",
1348 socket->state = STATE_TRANSMIT_CLOSE_WAIT;
1353 * Callback to set state to CLOSED
1355 * @param cls the closure from queue_message
1356 * @param socket the socket requiring state change
1359 set_state_closed (void *cls,
1360 struct GNUNET_STREAM_Socket *socket)
1362 socket->state = STATE_CLOSED;
1366 * Returns a new HelloAckMessage. Also sets the write sequence number for the
1369 * @param socket the socket for which this HelloAckMessage has to be generated
1370 * @return the HelloAckMessage
1372 static struct GNUNET_STREAM_HelloAckMessage *
1373 generate_hello_ack_msg (struct GNUNET_STREAM_Socket *socket)
1375 struct GNUNET_STREAM_HelloAckMessage *msg;
1377 /* Get the random sequence number */
1378 socket->write_sequence_number =
1379 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "%x: Generated write sequence number %u\n",
1383 (unsigned int) socket->write_sequence_number);
1385 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1386 msg->header.header.size =
1387 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1388 msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1389 msg->sequence_number = htonl (socket->write_sequence_number);
1390 msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1397 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1399 * @param cls the socket (set from GNUNET_MESH_connect)
1400 * @param tunnel connection to the other end
1401 * @param tunnel_ctx this is NULL
1402 * @param sender who sent the message
1403 * @param message the actual message
1404 * @param atsi performance data for the connection
1405 * @return GNUNET_OK to keep the connection open,
1406 * GNUNET_SYSERR to close it (signal serious error)
1409 client_handle_hello_ack (void *cls,
1410 struct GNUNET_MESH_Tunnel *tunnel,
1412 const struct GNUNET_PeerIdentity *sender,
1413 const struct GNUNET_MessageHeader *message,
1414 const struct GNUNET_ATS_Information*atsi)
1416 struct GNUNET_STREAM_Socket *socket = cls;
1417 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1418 struct GNUNET_STREAM_HelloAckMessage *reply;
1420 if (GNUNET_PEER_search (sender) != socket->other_peer)
1422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1423 "%x: Received HELLO_ACK from non-confirming peer\n",
1427 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "%x: Received HELLO_ACK from %x\n",
1431 socket->other_peer);
1433 GNUNET_assert (socket->tunnel == tunnel);
1434 switch (socket->state)
1436 case STATE_HELLO_WAIT:
1437 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1439 "%x: Read sequence number %u\n",
1441 (unsigned int) socket->read_sequence_number);
1442 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1443 reply = generate_hello_ack_msg (socket);
1444 queue_message (socket,
1446 &set_state_established,
1449 case STATE_ESTABLISHED:
1450 case STATE_RECEIVE_CLOSE_WAIT:
1451 // call statistics (# ACKs ignored++)
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "%x: Server %x sent HELLO_ACK when in state %d\n",
1460 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1461 return GNUNET_SYSERR;
1468 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1470 * @param cls the socket (set from GNUNET_MESH_connect)
1471 * @param tunnel connection to the other end
1472 * @param tunnel_ctx this is NULL
1473 * @param sender who sent the message
1474 * @param message the actual message
1475 * @param atsi performance data for the connection
1476 * @return GNUNET_OK to keep the connection open,
1477 * GNUNET_SYSERR to close it (signal serious error)
1480 client_handle_reset (void *cls,
1481 struct GNUNET_MESH_Tunnel *tunnel,
1483 const struct GNUNET_PeerIdentity *sender,
1484 const struct GNUNET_MessageHeader *message,
1485 const struct GNUNET_ATS_Information*atsi)
1487 struct GNUNET_STREAM_Socket *socket = cls;
1494 * Common message handler for handling TRANSMIT_CLOSE messages
1496 * @param socket the socket through which the ack was received
1497 * @param tunnel connection to the other end
1498 * @param sender who sent the message
1499 * @param msg the transmit close message
1500 * @param atsi performance data for the connection
1501 * @return GNUNET_OK to keep the connection open,
1502 * GNUNET_SYSERR to close it (signal serious error)
1505 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1506 struct GNUNET_MESH_Tunnel *tunnel,
1507 const struct GNUNET_PeerIdentity *sender,
1508 const struct GNUNET_STREAM_MessageHeader *msg,
1509 const struct GNUNET_ATS_Information*atsi)
1511 struct GNUNET_STREAM_MessageHeader *reply;
1513 switch (socket->state)
1515 case STATE_ESTABLISHED:
1516 socket->state = STATE_RECEIVE_CLOSED;
1518 /* Send TRANSMIT_CLOSE_ACK */
1519 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1520 reply->header.type =
1521 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1522 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1523 queue_message (socket, reply, NULL, NULL);
1527 /* FIXME: Call statistics? */
1535 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1537 * @param cls the socket (set from GNUNET_MESH_connect)
1538 * @param tunnel connection to the other end
1539 * @param tunnel_ctx this is NULL
1540 * @param sender who sent the message
1541 * @param message the actual message
1542 * @param atsi performance data for the connection
1543 * @return GNUNET_OK to keep the connection open,
1544 * GNUNET_SYSERR to close it (signal serious error)
1547 client_handle_transmit_close (void *cls,
1548 struct GNUNET_MESH_Tunnel *tunnel,
1550 const struct GNUNET_PeerIdentity *sender,
1551 const struct GNUNET_MessageHeader *message,
1552 const struct GNUNET_ATS_Information*atsi)
1554 struct GNUNET_STREAM_Socket *socket = cls;
1556 return handle_transmit_close (socket,
1559 (struct GNUNET_STREAM_MessageHeader *)message,
1565 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1567 * @param cls the socket (set from GNUNET_MESH_connect)
1568 * @param tunnel connection to the other end
1569 * @param tunnel_ctx this is NULL
1570 * @param sender who sent the message
1571 * @param message the actual message
1572 * @param atsi performance data for the connection
1573 * @return GNUNET_OK to keep the connection open,
1574 * GNUNET_SYSERR to close it (signal serious error)
1577 client_handle_transmit_close_ack (void *cls,
1578 struct GNUNET_MESH_Tunnel *tunnel,
1580 const struct GNUNET_PeerIdentity *sender,
1581 const struct GNUNET_MessageHeader *message,
1582 const struct GNUNET_ATS_Information*atsi)
1584 struct GNUNET_STREAM_Socket *socket = cls;
1591 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1593 * @param cls the socket (set from GNUNET_MESH_connect)
1594 * @param tunnel connection to the other end
1595 * @param tunnel_ctx this is NULL
1596 * @param sender who sent the message
1597 * @param message the actual message
1598 * @param atsi performance data for the connection
1599 * @return GNUNET_OK to keep the connection open,
1600 * GNUNET_SYSERR to close it (signal serious error)
1603 client_handle_receive_close (void *cls,
1604 struct GNUNET_MESH_Tunnel *tunnel,
1606 const struct GNUNET_PeerIdentity *sender,
1607 const struct GNUNET_MessageHeader *message,
1608 const struct GNUNET_ATS_Information*atsi)
1610 struct GNUNET_STREAM_Socket *socket = cls;
1617 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1619 * @param cls the socket (set from GNUNET_MESH_connect)
1620 * @param tunnel connection to the other end
1621 * @param tunnel_ctx this is NULL
1622 * @param sender who sent the message
1623 * @param message the actual message
1624 * @param atsi performance data for the connection
1625 * @return GNUNET_OK to keep the connection open,
1626 * GNUNET_SYSERR to close it (signal serious error)
1629 client_handle_receive_close_ack (void *cls,
1630 struct GNUNET_MESH_Tunnel *tunnel,
1632 const struct GNUNET_PeerIdentity *sender,
1633 const struct GNUNET_MessageHeader *message,
1634 const struct GNUNET_ATS_Information*atsi)
1636 struct GNUNET_STREAM_Socket *socket = cls;
1643 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1645 * @param socket the socket
1646 * @param tunnel connection to the other end
1647 * @param sender who sent the message
1648 * @param message the actual message
1649 * @param atsi performance data for the connection
1650 * @return GNUNET_OK to keep the connection open,
1651 * GNUNET_SYSERR to close it (signal serious error)
1654 handle_close (struct GNUNET_STREAM_Socket *socket,
1655 struct GNUNET_MESH_Tunnel *tunnel,
1656 const struct GNUNET_PeerIdentity *sender,
1657 const struct GNUNET_STREAM_MessageHeader *message,
1658 const struct GNUNET_ATS_Information*atsi)
1660 struct GNUNET_STREAM_MessageHeader *close_ack;
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663 "%x: Received CLOSE from %x\n",
1665 socket->other_peer);
1666 close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1667 close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1668 close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK);
1669 queue_message (socket,
1673 if (socket->state == STATE_CLOSED)
1676 GNUNET_free_non_null (socket->receive_buffer); /* Free the receive buffer */
1677 socket->receive_buffer = NULL;
1678 socket->receive_buffer_size = 0;
1684 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1686 * @param cls the socket (set from GNUNET_MESH_connect)
1687 * @param tunnel connection to the other end
1688 * @param tunnel_ctx this is NULL
1689 * @param sender who sent the message
1690 * @param message the actual message
1691 * @param atsi performance data for the connection
1692 * @return GNUNET_OK to keep the connection open,
1693 * GNUNET_SYSERR to close it (signal serious error)
1696 client_handle_close (void *cls,
1697 struct GNUNET_MESH_Tunnel *tunnel,
1699 const struct GNUNET_PeerIdentity *sender,
1700 const struct GNUNET_MessageHeader *message,
1701 const struct GNUNET_ATS_Information*atsi)
1703 struct GNUNET_STREAM_Socket *socket = cls;
1705 return handle_close (socket,
1708 (const struct GNUNET_STREAM_MessageHeader *) message,
1714 * Generic handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1716 * @param socket the socket
1717 * @param tunnel connection to the other end
1718 * @param sender who sent the message
1719 * @param message the actual message
1720 * @param atsi performance data for the connection
1721 * @return GNUNET_OK to keep the connection open,
1722 * GNUNET_SYSERR to close it (signal serious error)
1725 handle_close_ack (struct GNUNET_STREAM_Socket *socket,
1726 struct GNUNET_MESH_Tunnel *tunnel,
1727 const struct GNUNET_PeerIdentity *sender,
1728 const struct GNUNET_STREAM_MessageHeader *message,
1729 const struct GNUNET_ATS_Information *atsi)
1731 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
1733 shutdown_handle = socket->shutdown_handle;
1734 switch (socket->state)
1736 case STATE_CLOSE_WAIT:
1737 if ( (NULL == shutdown_handle) ||
1738 (SHUT_RDWR != shutdown_handle->operation) )
1740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1741 "%x: Received CLOSE_ACK when shutdown handle is NULL or "
1742 "not for SHUT_RDWR\n",
1747 if (GNUNET_SCHEDULER_NO_TASK
1748 != shutdown_handle->close_msg_retransmission_task_id)
1750 GNUNET_SCHEDULER_cancel
1751 (shutdown_handle->close_msg_retransmission_task_id);
1752 shutdown_handle->close_msg_retransmission_task_id =
1753 GNUNET_SCHEDULER_NO_TASK;
1756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1757 "%x: Received CLOSE_ACK from %x\n",
1759 socket->other_peer);
1760 socket->state = STATE_CLOSED;
1761 if (NULL != shutdown_handle->completion_cb) /* Shutdown completion */
1762 shutdown_handle->completion_cb(shutdown_handle->completion_cls,
1764 GNUNET_free (shutdown_handle); /* Free shutdown handle */
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1768 "%x: Received CLOSE_ACK when in it not expected\n",
1777 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1779 * @param cls the socket (set from GNUNET_MESH_connect)
1780 * @param tunnel connection to the other end
1781 * @param tunnel_ctx this is NULL
1782 * @param sender who sent the message
1783 * @param message the actual message
1784 * @param atsi performance data for the connection
1785 * @return GNUNET_OK to keep the connection open,
1786 * GNUNET_SYSERR to close it (signal serious error)
1789 client_handle_close_ack (void *cls,
1790 struct GNUNET_MESH_Tunnel *tunnel,
1792 const struct GNUNET_PeerIdentity *sender,
1793 const struct GNUNET_MessageHeader *message,
1794 const struct GNUNET_ATS_Information *atsi)
1796 struct GNUNET_STREAM_Socket *socket = cls;
1798 return handle_close_ack (socket,
1801 (const struct GNUNET_STREAM_MessageHeader *)
1806 /*****************************/
1807 /* Server's Message Handlers */
1808 /*****************************/
1811 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1813 * @param cls the closure
1814 * @param tunnel connection to the other end
1815 * @param tunnel_ctx the socket
1816 * @param sender who sent the message
1817 * @param message the actual message
1818 * @param atsi performance data for the connection
1819 * @return GNUNET_OK to keep the connection open,
1820 * GNUNET_SYSERR to close it (signal serious error)
1823 server_handle_data (void *cls,
1824 struct GNUNET_MESH_Tunnel *tunnel,
1826 const struct GNUNET_PeerIdentity *sender,
1827 const struct GNUNET_MessageHeader *message,
1828 const struct GNUNET_ATS_Information*atsi)
1830 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1832 return handle_data (socket,
1835 (const struct GNUNET_STREAM_DataMessage *)message,
1841 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1843 * @param cls the closure
1844 * @param tunnel connection to the other end
1845 * @param tunnel_ctx the socket
1846 * @param sender who sent the message
1847 * @param message the actual message
1848 * @param atsi performance data for the connection
1849 * @return GNUNET_OK to keep the connection open,
1850 * GNUNET_SYSERR to close it (signal serious error)
1853 server_handle_hello (void *cls,
1854 struct GNUNET_MESH_Tunnel *tunnel,
1856 const struct GNUNET_PeerIdentity *sender,
1857 const struct GNUNET_MessageHeader *message,
1858 const struct GNUNET_ATS_Information*atsi)
1860 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1861 struct GNUNET_STREAM_HelloAckMessage *reply;
1863 if (GNUNET_PEER_search (sender) != socket->other_peer)
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1866 "%x: Received HELLO from non-confirming peer\n",
1871 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO ==
1872 ntohs (message->type));
1873 GNUNET_assert (socket->tunnel == tunnel);
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1875 "%x: Received HELLO from %x\n",
1877 socket->other_peer);
1879 if (STATE_INIT == socket->state)
1881 reply = generate_hello_ack_msg (socket);
1882 queue_message (socket,
1884 &set_state_hello_wait,
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 "Client sent HELLO when in state %d\n", socket->state);
1891 /* FIXME: Send RESET? */
1899 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1901 * @param cls the closure
1902 * @param tunnel connection to the other end
1903 * @param tunnel_ctx the socket
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 server_handle_hello_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 = *tunnel_ctx;
1919 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1921 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
1922 ntohs (message->type));
1923 GNUNET_assert (socket->tunnel == tunnel);
1924 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1925 if (STATE_HELLO_WAIT == socket->state)
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1928 "%x: Received HELLO_ACK from %x\n",
1930 socket->other_peer);
1931 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1933 "%x: Read sequence number %u\n",
1935 (unsigned int) socket->read_sequence_number);
1936 socket->receiver_window_available =
1937 ntohl (ack_message->receiver_window_size);
1938 /* Attain ESTABLISHED state */
1939 set_state_established (NULL, socket);
1943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1944 "Client sent HELLO_ACK when in state %d\n", socket->state);
1945 /* FIXME: Send RESET? */
1953 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1955 * @param cls the closure
1956 * @param tunnel connection to the other end
1957 * @param tunnel_ctx the socket
1958 * @param sender who sent the message
1959 * @param message the actual message
1960 * @param atsi performance data for the connection
1961 * @return GNUNET_OK to keep the connection open,
1962 * GNUNET_SYSERR to close it (signal serious error)
1965 server_handle_reset (void *cls,
1966 struct GNUNET_MESH_Tunnel *tunnel,
1968 const struct GNUNET_PeerIdentity *sender,
1969 const struct GNUNET_MessageHeader *message,
1970 const struct GNUNET_ATS_Information*atsi)
1972 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1979 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1981 * @param cls the closure
1982 * @param tunnel connection to the other end
1983 * @param tunnel_ctx the socket
1984 * @param sender who sent the message
1985 * @param message the actual message
1986 * @param atsi performance data for the connection
1987 * @return GNUNET_OK to keep the connection open,
1988 * GNUNET_SYSERR to close it (signal serious error)
1991 server_handle_transmit_close (void *cls,
1992 struct GNUNET_MESH_Tunnel *tunnel,
1994 const struct GNUNET_PeerIdentity *sender,
1995 const struct GNUNET_MessageHeader *message,
1996 const struct GNUNET_ATS_Information*atsi)
1998 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2000 return handle_transmit_close (socket,
2003 (struct GNUNET_STREAM_MessageHeader *)message,
2009 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
2011 * @param cls the closure
2012 * @param tunnel connection to the other end
2013 * @param tunnel_ctx the socket
2014 * @param sender who sent the message
2015 * @param message the actual message
2016 * @param atsi performance data for the connection
2017 * @return GNUNET_OK to keep the connection open,
2018 * GNUNET_SYSERR to close it (signal serious error)
2021 server_handle_transmit_close_ack (void *cls,
2022 struct GNUNET_MESH_Tunnel *tunnel,
2024 const struct GNUNET_PeerIdentity *sender,
2025 const struct GNUNET_MessageHeader *message,
2026 const struct GNUNET_ATS_Information*atsi)
2028 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2035 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
2037 * @param cls the closure
2038 * @param tunnel connection to the other end
2039 * @param tunnel_ctx the socket
2040 * @param sender who sent the message
2041 * @param message the actual message
2042 * @param atsi performance data for the connection
2043 * @return GNUNET_OK to keep the connection open,
2044 * GNUNET_SYSERR to close it (signal serious error)
2047 server_handle_receive_close (void *cls,
2048 struct GNUNET_MESH_Tunnel *tunnel,
2050 const struct GNUNET_PeerIdentity *sender,
2051 const struct GNUNET_MessageHeader *message,
2052 const struct GNUNET_ATS_Information*atsi)
2054 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2061 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
2063 * @param cls the closure
2064 * @param tunnel connection to the other end
2065 * @param tunnel_ctx the socket
2066 * @param sender who sent the message
2067 * @param message the actual message
2068 * @param atsi performance data for the connection
2069 * @return GNUNET_OK to keep the connection open,
2070 * GNUNET_SYSERR to close it (signal serious error)
2073 server_handle_receive_close_ack (void *cls,
2074 struct GNUNET_MESH_Tunnel *tunnel,
2076 const struct GNUNET_PeerIdentity *sender,
2077 const struct GNUNET_MessageHeader *message,
2078 const struct GNUNET_ATS_Information*atsi)
2080 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2087 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
2089 * @param cls the listen socket (from GNUNET_MESH_connect in
2090 * GNUNET_STREAM_listen)
2091 * @param tunnel connection to the other end
2092 * @param tunnel_ctx the socket
2093 * @param sender who sent the message
2094 * @param message the actual message
2095 * @param atsi performance data for the connection
2096 * @return GNUNET_OK to keep the connection open,
2097 * GNUNET_SYSERR to close it (signal serious error)
2100 server_handle_close (void *cls,
2101 struct GNUNET_MESH_Tunnel *tunnel,
2103 const struct GNUNET_PeerIdentity *sender,
2104 const struct GNUNET_MessageHeader *message,
2105 const struct GNUNET_ATS_Information*atsi)
2107 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2109 return handle_close (socket,
2112 (const struct GNUNET_STREAM_MessageHeader *) message,
2118 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
2120 * @param cls the closure
2121 * @param tunnel connection to the other end
2122 * @param tunnel_ctx the socket
2123 * @param sender who sent the message
2124 * @param message the actual message
2125 * @param atsi performance data for the connection
2126 * @return GNUNET_OK to keep the connection open,
2127 * GNUNET_SYSERR to close it (signal serious error)
2130 server_handle_close_ack (void *cls,
2131 struct GNUNET_MESH_Tunnel *tunnel,
2133 const struct GNUNET_PeerIdentity *sender,
2134 const struct GNUNET_MessageHeader *message,
2135 const struct GNUNET_ATS_Information*atsi)
2137 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2139 return handle_close_ack (socket,
2142 (const struct GNUNET_STREAM_MessageHeader *) message,
2148 * Message Handler for mesh
2150 * @param socket the socket through which the ack was received
2151 * @param tunnel connection to the other end
2152 * @param sender who sent the message
2153 * @param ack the acknowledgment message
2154 * @param atsi performance data for the connection
2155 * @return GNUNET_OK to keep the connection open,
2156 * GNUNET_SYSERR to close it (signal serious error)
2159 handle_ack (struct GNUNET_STREAM_Socket *socket,
2160 struct GNUNET_MESH_Tunnel *tunnel,
2161 const struct GNUNET_PeerIdentity *sender,
2162 const struct GNUNET_STREAM_AckMessage *ack,
2163 const struct GNUNET_ATS_Information*atsi)
2165 unsigned int packet;
2166 int need_retransmission;
2169 if (GNUNET_PEER_search (sender) != socket->other_peer)
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 "%x: Received ACK from non-confirming peer\n",
2177 switch (socket->state)
2179 case (STATE_ESTABLISHED):
2180 if (NULL == socket->write_handle)
2182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2183 "%x: Received DATA_ACK when write_handle is NULL\n",
2187 /* FIXME: increment in the base sequence number is breaking current flow
2189 if (!((socket->write_sequence_number
2190 - ntohl (ack->base_sequence_number)) < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 "%x: Received DATA_ACK with unexpected base sequence "
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2197 "%x: Current write sequence: %u; Ack's base sequence: %u\n",
2199 socket->write_sequence_number,
2200 ntohl (ack->base_sequence_number));
2203 /* FIXME: include the case when write_handle is cancelled - ignore the
2206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2207 "%x: Received DATA_ACK from %x\n",
2209 socket->other_peer);
2211 /* Cancel the retransmission task */
2212 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
2214 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
2215 socket->retransmission_timeout_task_id =
2216 GNUNET_SCHEDULER_NO_TASK;
2219 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2221 if (NULL == socket->write_handle->messages[packet]) break;
2222 if (ntohl (ack->base_sequence_number)
2223 >= ntohl (socket->write_handle->messages[packet]->sequence_number))
2224 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
2229 ackbitmap_is_bit_set (&socket->write_handle->ack_bitmap,
2230 ntohl (socket->write_handle->messages[packet]->sequence_number)
2231 - ntohl (ack->base_sequence_number)))
2232 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
2237 /* Update the receive window remaining
2238 FIXME : Should update with the value from a data ack with greater
2240 socket->receiver_window_available =
2241 ntohl (ack->receive_window_remaining);
2243 /* Check if we have received all acknowledgements */
2244 need_retransmission = GNUNET_NO;
2245 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2247 if (NULL == socket->write_handle->messages[packet]) break;
2248 if (GNUNET_YES != ackbitmap_is_bit_set
2249 (&socket->write_handle->ack_bitmap,packet))
2251 need_retransmission = GNUNET_YES;
2255 if (GNUNET_YES == need_retransmission)
2257 write_data (socket);
2259 else /* We have to call the write continuation callback now */
2261 /* Free the packets */
2262 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2264 GNUNET_free_non_null (socket->write_handle->messages[packet]);
2266 if (NULL != socket->write_handle->write_cont)
2267 socket->write_handle->write_cont
2268 (socket->write_handle->write_cont_cls,
2270 socket->write_handle->size);
2271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2272 "%x: Write completion callback completed\n",
2274 /* We are done with the write handle - Freeing it */
2275 GNUNET_free (socket->write_handle);
2276 socket->write_handle = NULL;
2287 * Message Handler for mesh
2289 * @param cls the 'struct GNUNET_STREAM_Socket'
2290 * @param tunnel connection to the other end
2291 * @param tunnel_ctx unused
2292 * @param sender who sent the message
2293 * @param message the actual message
2294 * @param atsi performance data for the connection
2295 * @return GNUNET_OK to keep the connection open,
2296 * GNUNET_SYSERR to close it (signal serious error)
2299 client_handle_ack (void *cls,
2300 struct GNUNET_MESH_Tunnel *tunnel,
2302 const struct GNUNET_PeerIdentity *sender,
2303 const struct GNUNET_MessageHeader *message,
2304 const struct GNUNET_ATS_Information*atsi)
2306 struct GNUNET_STREAM_Socket *socket = cls;
2307 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2309 return handle_ack (socket, tunnel, sender, ack, atsi);
2314 * Message Handler for mesh
2316 * @param cls the server's listen socket
2317 * @param tunnel connection to the other end
2318 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2319 * @param sender who sent the message
2320 * @param message the actual message
2321 * @param atsi performance data for the connection
2322 * @return GNUNET_OK to keep the connection open,
2323 * GNUNET_SYSERR to close it (signal serious error)
2326 server_handle_ack (void *cls,
2327 struct GNUNET_MESH_Tunnel *tunnel,
2329 const struct GNUNET_PeerIdentity *sender,
2330 const struct GNUNET_MessageHeader *message,
2331 const struct GNUNET_ATS_Information*atsi)
2333 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2334 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2336 return handle_ack (socket, tunnel, sender, ack, atsi);
2341 * For client message handlers, the stream socket is in the
2344 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2345 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2346 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2347 sizeof (struct GNUNET_STREAM_AckMessage) },
2348 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2349 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2350 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2351 sizeof (struct GNUNET_STREAM_MessageHeader)},
2352 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2353 sizeof (struct GNUNET_STREAM_MessageHeader)},
2354 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2355 sizeof (struct GNUNET_STREAM_MessageHeader)},
2356 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2357 sizeof (struct GNUNET_STREAM_MessageHeader)},
2358 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2359 sizeof (struct GNUNET_STREAM_MessageHeader)},
2360 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2361 sizeof (struct GNUNET_STREAM_MessageHeader)},
2362 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2363 sizeof (struct GNUNET_STREAM_MessageHeader)},
2369 * For server message handlers, the stream socket is in the
2370 * tunnel context, and the listen socket in the closure argument.
2372 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2373 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2374 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2375 sizeof (struct GNUNET_STREAM_AckMessage) },
2376 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2377 sizeof (struct GNUNET_STREAM_MessageHeader)},
2378 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2379 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2380 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2381 sizeof (struct GNUNET_STREAM_MessageHeader)},
2382 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2383 sizeof (struct GNUNET_STREAM_MessageHeader)},
2384 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2385 sizeof (struct GNUNET_STREAM_MessageHeader)},
2386 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2387 sizeof (struct GNUNET_STREAM_MessageHeader)},
2388 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2389 sizeof (struct GNUNET_STREAM_MessageHeader)},
2390 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2391 sizeof (struct GNUNET_STREAM_MessageHeader)},
2392 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2393 sizeof (struct GNUNET_STREAM_MessageHeader)},
2399 * Function called when our target peer is connected to our tunnel
2401 * @param cls the socket for which this tunnel is created
2402 * @param peer the peer identity of the target
2403 * @param atsi performance data for the connection
2406 mesh_peer_connect_callback (void *cls,
2407 const struct GNUNET_PeerIdentity *peer,
2408 const struct GNUNET_ATS_Information * atsi)
2410 struct GNUNET_STREAM_Socket *socket = cls;
2411 struct GNUNET_STREAM_MessageHeader *message;
2412 GNUNET_PEER_Id connected_peer;
2414 connected_peer = GNUNET_PEER_search (peer);
2416 if (connected_peer != socket->other_peer)
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2419 "%x: A peer which is not our target has connected",
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2426 "%x: Target peer %x connected\n",
2430 /* Set state to INIT */
2431 socket->state = STATE_INIT;
2433 /* Send HELLO message */
2434 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2435 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
2436 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2437 queue_message (socket,
2439 &set_state_hello_wait,
2442 /* Call open callback */
2443 if (NULL == socket->open_cb)
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 "STREAM_open callback is NULL\n");
2452 * Function called when our target peer is disconnected from our tunnel
2454 * @param cls the socket associated which this tunnel
2455 * @param peer the peer identity of the target
2458 mesh_peer_disconnect_callback (void *cls,
2459 const struct GNUNET_PeerIdentity *peer)
2466 * Method called whenever a peer creates a tunnel to us
2468 * @param cls closure
2469 * @param tunnel new handle to the tunnel
2470 * @param initiator peer that started the tunnel
2471 * @param atsi performance information for the tunnel
2472 * @return initial tunnel context for the tunnel
2473 * (can be NULL -- that's not an error)
2476 new_tunnel_notify (void *cls,
2477 struct GNUNET_MESH_Tunnel *tunnel,
2478 const struct GNUNET_PeerIdentity *initiator,
2479 const struct GNUNET_ATS_Information *atsi)
2481 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2482 struct GNUNET_STREAM_Socket *socket;
2484 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2485 from the same peer again until the socket is closed */
2487 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2488 socket->other_peer = GNUNET_PEER_intern (initiator);
2489 socket->tunnel = tunnel;
2490 socket->session_id = 0; /* FIXME */
2491 socket->state = STATE_INIT;
2492 socket->lsocket = lsocket;
2493 socket->our_id = lsocket->our_id;
2495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2496 "%x: Peer %x initiated tunnel to us\n",
2498 socket->other_peer);
2500 /* FIXME: Copy MESH handle from lsocket to socket */
2507 * Function called whenever an inbound tunnel is destroyed. Should clean up
2508 * any associated state. This function is NOT called if the client has
2509 * explicitly asked for the tunnel to be destroyed using
2510 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2513 * @param cls closure (set from GNUNET_MESH_connect)
2514 * @param tunnel connection to the other end (henceforth invalid)
2515 * @param tunnel_ctx place where local state associated
2516 * with the tunnel is stored
2519 tunnel_cleaner (void *cls,
2520 const struct GNUNET_MESH_Tunnel *tunnel,
2523 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2526 "%x: Peer %x has terminated connection abruptly\n",
2528 socket->other_peer);
2530 socket->status = GNUNET_STREAM_SHUTDOWN;
2532 /* Clear Transmit handles */
2533 if (NULL != socket->transmit_handle)
2535 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2536 socket->transmit_handle = NULL;
2538 socket->tunnel = NULL;
2548 * Tries to open a stream to the target peer
2550 * @param cfg configuration to use
2551 * @param target the target peer to which the stream has to be opened
2552 * @param app_port the application port number which uniquely identifies this
2554 * @param open_cb this function will be called after stream has be established
2555 * @param open_cb_cls the closure for open_cb
2556 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2557 * @return if successful it returns the stream socket; NULL if stream cannot be
2560 struct GNUNET_STREAM_Socket *
2561 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2562 const struct GNUNET_PeerIdentity *target,
2563 GNUNET_MESH_ApplicationType app_port,
2564 GNUNET_STREAM_OpenCallback open_cb,
2568 struct GNUNET_STREAM_Socket *socket;
2569 struct GNUNET_PeerIdentity own_peer_id;
2570 enum GNUNET_STREAM_Option option;
2571 va_list vargs; /* Variable arguments */
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2576 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2577 socket->other_peer = GNUNET_PEER_intern (target);
2578 socket->open_cb = open_cb;
2579 socket->open_cls = open_cb_cls;
2580 GNUNET_TESTING_get_peer_identity (cfg, &own_peer_id);
2581 socket->our_id = GNUNET_PEER_intern (&own_peer_id);
2584 socket->retransmit_timeout =
2585 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
2587 va_start (vargs, open_cb_cls); /* Parse variable args */
2589 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2592 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2593 /* Expect struct GNUNET_TIME_Relative */
2594 socket->retransmit_timeout = va_arg (vargs,
2595 struct GNUNET_TIME_Relative);
2597 case GNUNET_STREAM_OPTION_END:
2600 } while (GNUNET_STREAM_OPTION_END != option);
2601 va_end (vargs); /* End of variable args parsing */
2602 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2603 10, /* QUEUE size as parameter? */
2605 NULL, /* No inbound tunnel handler */
2606 &tunnel_cleaner, /* FIXME: not required? */
2607 client_message_handlers,
2608 &app_port); /* We don't get inbound tunnels */
2609 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2611 GNUNET_free (socket);
2615 /* Now create the mesh tunnel to target */
2616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2617 "Creating MESH Tunnel\n");
2618 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2619 NULL, /* Tunnel context */
2620 &mesh_peer_connect_callback,
2621 &mesh_peer_disconnect_callback,
2623 GNUNET_assert (NULL != socket->tunnel);
2624 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 "%s() END\n", __func__);
2634 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
2636 * @param socket the stream socket
2637 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
2638 * @param completion_cb the callback that will be called upon successful
2639 * shutdown of given operation
2640 * @param completion_cls the closure for the completion callback
2641 * @return the shutdown handle
2643 struct GNUNET_STREAM_ShutdownHandle *
2644 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2646 GNUNET_STREAM_ShutdownCompletion completion_cb,
2647 void *completion_cls)
2649 struct GNUNET_STREAM_ShutdownHandle *handle;
2650 struct GNUNET_STREAM_MessageHeader *msg;
2652 GNUNET_assert (NULL == socket->shutdown_handle);
2654 handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle));
2655 handle->socket = socket;
2656 handle->completion_cb = completion_cb;
2657 handle->completion_cls = completion_cls;
2658 socket->shutdown_handle = handle;
2660 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2661 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2665 handle->operation = SHUT_RD;
2666 if (NULL != socket->read_handle)
2667 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2668 "Existing read handle should be cancelled before shutting"
2670 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE);
2671 queue_message (socket,
2673 &set_state_receive_close_wait,
2677 handle->operation = SHUT_WR;
2678 if (NULL != socket->write_handle)
2679 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2680 "Existing write handle should be cancelled before shutting"
2682 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE);
2683 queue_message (socket,
2685 &set_state_transmit_close_wait,
2689 handle->operation = SHUT_RDWR;
2690 if (NULL != socket->write_handle)
2691 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2692 "Existing write handle should be cancelled before shutting"
2694 if (NULL != socket->read_handle)
2695 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2696 "Existing read handle should be cancelled before shutting"
2698 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE);
2699 queue_message (socket,
2701 &set_state_close_wait,
2705 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2706 "GNUNET_STREAM_shutdown called with invalid value for "
2707 "parameter operation -- Ignoring\n");
2709 GNUNET_free (handle);
2712 handle->close_msg_retransmission_task_id =
2713 GNUNET_SCHEDULER_add_delayed (socket->retransmit_timeout,
2714 &close_msg_retransmission_task,
2721 * Cancels a pending shutdown
2723 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
2726 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle)
2728 if (GNUNET_SCHEDULER_NO_TASK != handle->close_msg_retransmission_task_id)
2729 GNUNET_SCHEDULER_cancel (handle->close_msg_retransmission_task_id);
2730 GNUNET_free (handle);
2738 * @param socket the stream socket
2741 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
2743 struct MessageQueue *head;
2745 GNUNET_break (NULL == socket->read_handle);
2746 GNUNET_break (NULL == socket->write_handle);
2748 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
2750 /* socket closed with read task pending!? */
2752 GNUNET_SCHEDULER_cancel (socket->read_task_id);
2753 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
2756 /* Terminate the ack'ing tasks if they are still present */
2757 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
2759 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2760 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2763 /* Clear Transmit handles */
2764 if (NULL != socket->transmit_handle)
2766 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2767 socket->transmit_handle = NULL;
2770 /* Clear existing message queue */
2771 while (NULL != (head = socket->queue_head)) {
2772 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2775 GNUNET_free (head->message);
2779 /* Close associated tunnel */
2780 if (NULL != socket->tunnel)
2782 GNUNET_MESH_tunnel_destroy (socket->tunnel);
2783 socket->tunnel = NULL;
2786 /* Close mesh connection */
2787 if (NULL != socket->mesh && NULL == socket->lsocket)
2789 GNUNET_MESH_disconnect (socket->mesh);
2790 socket->mesh = NULL;
2793 /* Release receive buffer */
2794 if (NULL != socket->receive_buffer)
2796 GNUNET_free (socket->receive_buffer);
2799 GNUNET_free (socket);
2804 * Listens for stream connections for a specific application ports
2806 * @param cfg the configuration to use
2807 * @param app_port the application port for which new streams will be accepted
2808 * @param listen_cb this function will be called when a peer tries to establish
2810 * @param listen_cb_cls closure for listen_cb
2811 * @return listen socket, NULL for any error
2813 struct GNUNET_STREAM_ListenSocket *
2814 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
2815 GNUNET_MESH_ApplicationType app_port,
2816 GNUNET_STREAM_ListenCallback listen_cb,
2817 void *listen_cb_cls)
2819 /* FIXME: Add variable args for passing configration options? */
2820 struct GNUNET_STREAM_ListenSocket *lsocket;
2821 struct GNUNET_PeerIdentity our_peer_id;
2823 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
2824 lsocket->port = app_port;
2825 lsocket->listen_cb = listen_cb;
2826 lsocket->listen_cb_cls = listen_cb_cls;
2827 GNUNET_TESTING_get_peer_identity (cfg, &our_peer_id);
2828 lsocket->our_id = GNUNET_PEER_intern (&our_peer_id);
2829 lsocket->mesh = GNUNET_MESH_connect (cfg,
2830 10, /* FIXME: QUEUE size as parameter? */
2831 lsocket, /* Closure */
2834 server_message_handlers,
2836 GNUNET_assert (NULL != lsocket->mesh);
2842 * Closes the listen socket
2844 * @param lsocket the listen socket
2847 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
2849 /* Close MESH connection */
2850 GNUNET_assert (NULL != lsocket->mesh);
2851 GNUNET_MESH_disconnect (lsocket->mesh);
2853 GNUNET_free (lsocket);
2858 * Tries to write the given data to the stream
2860 * @param socket the socket representing a stream
2861 * @param data the data buffer from where the data is written into the stream
2862 * @param size the number of bytes to be written from the data buffer
2863 * @param timeout the timeout period
2864 * @param write_cont the function to call upon writing some bytes into the stream
2865 * @param write_cont_cls the closure
2866 * @return handle to cancel the operation
2868 struct GNUNET_STREAM_IOWriteHandle *
2869 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
2872 struct GNUNET_TIME_Relative timeout,
2873 GNUNET_STREAM_CompletionContinuation write_cont,
2874 void *write_cont_cls)
2876 unsigned int num_needed_packets;
2877 unsigned int packet;
2878 struct GNUNET_STREAM_IOWriteHandle *io_handle;
2879 uint32_t packet_size;
2880 uint32_t payload_size;
2881 struct GNUNET_STREAM_DataMessage *data_msg;
2883 struct GNUNET_TIME_Relative ack_deadline;
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2888 /* Return NULL if there is already a write request pending */
2889 if (NULL != socket->write_handle)
2894 if (!((STATE_ESTABLISHED == socket->state)
2895 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
2896 || (STATE_RECEIVE_CLOSED == socket->state)))
2898 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2899 "%x: Attempting to write on a closed (OR) not-yet-established"
2904 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
2905 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
2906 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
2907 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
2908 io_handle->socket = socket;
2909 io_handle->write_cont = write_cont;
2910 io_handle->write_cont_cls = write_cont_cls;
2911 io_handle->size = size;
2913 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2914 determined from RTT */
2915 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
2916 /* Divide the given buffer into packets for sending */
2917 for (packet=0; packet < num_needed_packets; packet++)
2919 if ((packet + 1) * max_payload_size < size)
2921 payload_size = max_payload_size;
2922 packet_size = MAX_PACKET_SIZE;
2926 payload_size = size - packet * max_payload_size;
2927 packet_size = payload_size + sizeof (struct
2928 GNUNET_STREAM_DataMessage);
2930 io_handle->messages[packet] = GNUNET_malloc (packet_size);
2931 io_handle->messages[packet]->header.header.size = htons (packet_size);
2932 io_handle->messages[packet]->header.header.type =
2933 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
2934 io_handle->messages[packet]->sequence_number =
2935 htonl (socket->write_sequence_number++);
2936 io_handle->messages[packet]->offset = htonl (socket->write_offset);
2938 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2939 determined from RTT */
2940 io_handle->messages[packet]->ack_deadline =
2941 GNUNET_TIME_relative_hton (ack_deadline);
2942 data_msg = io_handle->messages[packet];
2943 /* Copy data from given buffer to the packet */
2944 memcpy (&data_msg[1],
2947 sweep += payload_size;
2948 socket->write_offset += payload_size;
2950 socket->write_handle = io_handle;
2951 write_data (socket);
2953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2954 "%s() END\n", __func__);
2961 * Tries to read data from the stream
2963 * @param socket the socket representing a stream
2964 * @param timeout the timeout period
2965 * @param proc function to call with data (once only)
2966 * @param proc_cls the closure for proc
2967 * @return handle to cancel the operation
2969 struct GNUNET_STREAM_IOReadHandle *
2970 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
2971 struct GNUNET_TIME_Relative timeout,
2972 GNUNET_STREAM_DataProcessor proc,
2975 struct GNUNET_STREAM_IOReadHandle *read_handle;
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2982 /* Return NULL if there is already a read handle; the user has to cancel that
2983 first before continuing or has to wait until it is completed */
2984 if (NULL != socket->read_handle) return NULL;
2986 GNUNET_assert (NULL != proc);
2988 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
2989 read_handle->proc = proc;
2990 read_handle->proc_cls = proc_cls;
2991 socket->read_handle = read_handle;
2993 /* Check if we have a packet at bitmap 0 */
2994 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
2997 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
3002 /* Setup the read timeout task */
3003 socket->read_io_timeout_task_id =
3004 GNUNET_SCHEDULER_add_delayed (timeout,
3007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3016 * Cancel pending write operation.
3018 * @param ioh handle to operation to cancel
3021 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
3023 struct GNUNET_STREAM_Socket *socket = ioh->socket;
3024 unsigned int packet;
3026 GNUNET_assert (NULL != socket->write_handle);
3027 GNUNET_assert (socket->write_handle == ioh);
3029 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
3031 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
3032 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
3035 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
3037 if (NULL == ioh->messages[packet]) break;
3038 GNUNET_free (ioh->messages[packet]);
3041 GNUNET_free (socket->write_handle);
3042 socket->write_handle = NULL;
3048 * Cancel pending read operation.
3050 * @param ioh handle to operation to cancel
3053 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)