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
33 * @file stream/stream_api.c
34 * @brief Implementation of the stream library
35 * @author Sree Harsha Totakura
40 #include "gnunet_common.h"
41 #include "gnunet_crypto_lib.h"
42 #include "gnunet_stream_lib.h"
43 #include "gnunet_testing_lib.h"
44 #include "stream_protocol.h"
48 * The maximum packet size of a stream packet
50 #define MAX_PACKET_SIZE 64000
55 #define RECEIVE_BUFFER_SIZE 4096000
58 * The maximum payload a data message packet can carry
60 static size_t max_payload_size =
61 MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
64 * states in the Protocol
69 * Client initialization state
74 * Listener initialization state
79 * Pre-connection establishment state
84 * State where a connection has been established
89 * State where the socket is closed on our side and waiting to be ACK'ed
91 STATE_RECEIVE_CLOSE_WAIT,
94 * State where the socket is closed for reading
99 * State where the socket is closed on our side and waiting to be ACK'ed
101 STATE_TRANSMIT_CLOSE_WAIT,
104 * State where the socket is closed for writing
106 STATE_TRANSMIT_CLOSED,
109 * State where the socket is closed on our side and waiting to be ACK'ed
114 * State where the socket is closed
121 * Functions of this type are called when a message is written
123 * @param cls the closure from queue_message
124 * @param socket the socket the written message was bound to
126 typedef void (*SendFinishCallback) (void *cls,
127 struct GNUNET_STREAM_Socket *socket);
131 * The send message queue
138 struct GNUNET_STREAM_MessageHeader *message;
141 * Callback to be called when the message is sent
143 SendFinishCallback finish_cb;
146 * The closure for finish_cb
151 * The next message in queue. Should be NULL in the last message
153 struct MessageQueue *next;
156 * The next message in queue. Should be NULL in the first message
158 struct MessageQueue *prev;
163 * The STREAM Socket Handler
165 struct GNUNET_STREAM_Socket
168 * Retransmission timeout
170 struct GNUNET_TIME_Relative retransmit_timeout;
173 * The Acknowledgement Bitmap
175 GNUNET_STREAM_AckBitmap ack_bitmap;
178 * Time when the Acknowledgement was queued
180 struct GNUNET_TIME_Absolute ack_time_registered;
183 * Queued Acknowledgement deadline
185 struct GNUNET_TIME_Relative ack_time_deadline;
190 struct GNUNET_MESH_Handle *mesh;
193 * The mesh tunnel handle
195 struct GNUNET_MESH_Tunnel *tunnel;
198 * Stream open closure
203 * Stream open callback
205 GNUNET_STREAM_OpenCallback open_cb;
208 * The current transmit handle (if a pending transmit request exists)
210 struct GNUNET_MESH_TransmitHandle *transmit_handle;
213 * The current message associated with the transmit handle
215 struct MessageQueue *queue_head;
218 * The queue tail, should always point to the last message in queue
220 struct MessageQueue *queue_tail;
223 * The write IO_handle associated with this socket
225 struct GNUNET_STREAM_IOWriteHandle *write_handle;
228 * The read IO_handle associated with this socket
230 struct GNUNET_STREAM_IOReadHandle *read_handle;
233 * Buffer for storing received messages
235 void *receive_buffer;
238 * The listen socket from which this socket is derived. Should be NULL if it
239 * is not a derived socket
241 struct GNUNET_STREAM_ListenSocket *lsocket;
244 * Task identifier for the read io timeout task
246 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task_id;
249 * Task identifier for retransmission task after timeout
251 GNUNET_SCHEDULER_TaskIdentifier retransmission_timeout_task_id;
254 * The task for sending timely Acks
256 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
259 * Task scheduled to continue a read operation.
261 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
264 * The state of the protocol associated with this socket
269 * The status of the socket
271 enum GNUNET_STREAM_Status status;
274 * The number of previous timeouts; FIXME: currently not used
276 unsigned int retries;
279 * The peer identity of the peer at the other end of the stream
281 GNUNET_PEER_Id other_peer;
284 * Our Peer Identity (for debugging)
286 GNUNET_PEER_Id our_id;
289 * The application port number (type: uint32_t)
291 GNUNET_MESH_ApplicationType app_port;
294 * The session id associated with this stream connection
295 * FIXME: Not used currently, may be removed
300 * Write sequence number. Set to random when sending HELLO(client) and
303 uint32_t write_sequence_number;
306 * Read sequence number. This number's value is determined during handshake
308 uint32_t read_sequence_number;
311 * The receiver buffer size
313 uint32_t receive_buffer_size;
316 * The receiver buffer boundaries
318 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
321 * receiver's available buffer after the last acknowledged packet
323 uint32_t receiver_window_available;
326 * The offset pointer used during write operation
328 uint32_t write_offset;
331 * The offset after which we are expecting data
333 uint32_t read_offset;
336 * The offset upto which user has read from the received buffer
338 uint32_t copy_offset;
343 * A socket for listening
345 struct GNUNET_STREAM_ListenSocket
350 struct GNUNET_MESH_Handle *mesh;
353 * The callback function which is called after successful opening socket
355 GNUNET_STREAM_ListenCallback listen_cb;
358 * The call back closure
363 * Our interned Peer's identity
365 GNUNET_PEER_Id our_id;
369 * FIXME: Remove if not required!
371 GNUNET_MESH_ApplicationType port;
376 * The IO Write Handle
378 struct GNUNET_STREAM_IOWriteHandle
381 * The socket to which this write handle is associated
383 struct GNUNET_STREAM_Socket *socket;
386 * The packet_buffers associated with this Handle
388 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
391 * The write continuation callback
393 GNUNET_STREAM_CompletionContinuation write_cont;
396 * Write continuation closure
398 void *write_cont_cls;
401 * The bitmap of this IOHandle; Corresponding bit for a message is set when
402 * it has been acknowledged by the receiver
404 GNUNET_STREAM_AckBitmap ack_bitmap;
407 * Number of bytes in this write handle
416 struct GNUNET_STREAM_IOReadHandle
419 * Callback for the read processor
421 GNUNET_STREAM_DataProcessor proc;
424 * The closure pointer for the read processor callback
431 * Handle for Shutdown
433 struct GNUNET_STREAM_ShutdownHandle
436 * The socket associated with this shutdown handle
438 struct GNUNET_STREAM_Socket *socket;
441 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
448 * Default value in seconds for various timeouts
450 static unsigned int default_timeout = 10;
454 * Callback function for sending queued message
456 * @param cls closure the socket
457 * @param size number of bytes available in buf
458 * @param buf where the callee should write the message
459 * @return number of bytes written to buf
462 send_message_notify (void *cls, size_t size, void *buf)
464 struct GNUNET_STREAM_Socket *socket = cls;
465 struct GNUNET_PeerIdentity target;
466 struct MessageQueue *head;
469 socket->transmit_handle = NULL; /* Remove the transmit handle */
470 head = socket->queue_head;
472 return 0; /* just to be safe */
473 GNUNET_PEER_resolve (socket->other_peer, &target);
474 if (0 == size) /* request timed out */
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478 "Message sending timed out. Retry %d \n",
480 socket->transmit_handle =
481 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
484 /* FIXME: exponential backoff */
485 socket->retransmit_timeout,
487 ntohs (head->message->header.size),
488 &send_message_notify,
493 ret = ntohs (head->message->header.size);
494 GNUNET_assert (size >= ret);
495 memcpy (buf, head->message, ret);
496 if (NULL != head->finish_cb)
498 head->finish_cb (head->finish_cb_cls, socket);
500 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
503 GNUNET_free (head->message);
505 head = socket->queue_head;
506 if (NULL != head) /* more pending messages to send */
509 socket->transmit_handle =
510 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
513 /* FIXME: exponential backoff */
514 socket->retransmit_timeout,
516 ntohs (head->message->header.size),
517 &send_message_notify,
525 * Queues a message for sending using the mesh connection of a socket
527 * @param socket the socket whose mesh connection is used
528 * @param message the message to be sent
529 * @param finish_cb the callback to be called when the message is sent
530 * @param finish_cb_cls the closure for the callback
533 queue_message (struct GNUNET_STREAM_Socket *socket,
534 struct GNUNET_STREAM_MessageHeader *message,
535 SendFinishCallback finish_cb,
538 struct MessageQueue *queue_entity;
539 struct GNUNET_PeerIdentity target;
542 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
543 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "%x: Queueing message of type %d and size %d\n",
548 ntohs (message->header.type),
549 ntohs (message->header.size));
550 GNUNET_assert (NULL != message);
551 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
552 queue_entity->message = message;
553 queue_entity->finish_cb = finish_cb;
554 queue_entity->finish_cb_cls = finish_cb_cls;
555 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
558 if (NULL == socket->transmit_handle)
561 GNUNET_PEER_resolve (socket->other_peer, &target);
562 socket->transmit_handle =
563 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
566 socket->retransmit_timeout,
568 ntohs (message->header.size),
569 &send_message_notify,
576 * Copies a message and queues it for sending using the mesh connection of
579 * @param socket the socket whose mesh connection is used
580 * @param message the message to be sent
581 * @param finish_cb the callback to be called when the message is sent
582 * @param finish_cb_cls the closure for the callback
585 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
586 const struct GNUNET_STREAM_MessageHeader *message,
587 SendFinishCallback finish_cb,
590 struct GNUNET_STREAM_MessageHeader *msg_copy;
593 size = ntohs (message->header.size);
594 msg_copy = GNUNET_malloc (size);
595 memcpy (msg_copy, message, size);
596 queue_message (socket, msg_copy, finish_cb, finish_cb_cls);
601 * Callback function for sending ack message
603 * @param cls closure the ACK message created in ack_task
604 * @param size number of bytes available in buffer
605 * @param buf where the callee should write the message
606 * @return number of bytes written to buf
609 send_ack_notify (void *cls, size_t size, void *buf)
611 struct GNUNET_STREAM_AckMessage *ack_msg = cls;
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "%s called with size 0\n", __func__);
619 GNUNET_assert (ntohs (ack_msg->header.header.size) <= size);
621 size = ntohs (ack_msg->header.header.size);
622 memcpy (buf, ack_msg, size);
627 * Writes data using the given socket. The amount of data written is limited by
628 * the receiver_window_size
630 * @param socket the socket to use
633 write_data (struct GNUNET_STREAM_Socket *socket);
636 * Task for retransmitting data messages if they aren't ACK before their ack
639 * @param cls the socket
640 * @param tc the Task context
643 retransmission_timeout_task (void *cls,
644 const struct GNUNET_SCHEDULER_TaskContext *tc)
646 struct GNUNET_STREAM_Socket *socket = cls;
648 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
652 "%x: Retransmitting DATA...\n", socket->our_id);
653 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
659 * Task for sending ACK message
661 * @param cls the socket
662 * @param tc the Task context
666 const struct GNUNET_SCHEDULER_TaskContext *tc)
668 struct GNUNET_STREAM_Socket *socket = cls;
669 struct GNUNET_STREAM_AckMessage *ack_msg;
670 struct GNUNET_PeerIdentity target;
672 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
677 socket->ack_task_id = 0;
679 /* Create the ACK Message */
680 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
681 ack_msg->header.header.size = htons (sizeof (struct
682 GNUNET_STREAM_AckMessage));
683 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
684 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
685 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
686 ack_msg->receive_window_remaining =
687 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
689 GNUNET_PEER_resolve (socket->other_peer, &target);
690 /* Request MESH for sending ACK */
691 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
694 socket->retransmit_timeout,
696 ntohs (ack_msg->header.header.size),
705 * Function to modify a bit in GNUNET_STREAM_AckBitmap
707 * @param bitmap the bitmap to modify
708 * @param bit the bit number to modify
709 * @param value GNUNET_YES to on, GNUNET_NO to off
712 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
716 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
717 if (GNUNET_YES == value)
718 *bitmap |= (1LL << bit);
720 *bitmap &= ~(1LL << bit);
725 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
727 * @param bitmap address of the bitmap that has to be checked
728 * @param bit the bit number to check
729 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
732 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
735 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
736 return 0 != (*bitmap & (1LL << bit));
741 * Writes data using the given socket. The amount of data written is limited by
742 * the receiver_window_size
744 * @param socket the socket to use
747 write_data (struct GNUNET_STREAM_Socket *socket)
749 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
750 int packet; /* Although an int, should never be negative */
754 /* Find the last acknowledged packet */
755 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
757 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
760 else if (NULL == io_handle->messages[packet])
763 /* Resend packets which weren't ack'ed */
764 for (packet=0; packet < ack_packet; packet++)
766 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
770 "%x: Placing DATA message with sequence %u in send queue\n",
772 ntohl (io_handle->messages[packet]->sequence_number));
774 copy_and_queue_message (socket,
775 &io_handle->messages[packet]->header,
780 packet = ack_packet + 1;
781 /* Now send new packets if there is enough buffer space */
782 while ( (NULL != io_handle->messages[packet]) &&
783 (socket->receiver_window_available
784 >= ntohs (io_handle->messages[packet]->header.header.size)) )
786 socket->receiver_window_available -=
787 ntohs (io_handle->messages[packet]->header.header.size);
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "%x: Placing DATA message with sequence %u in send queue\n",
791 ntohl (io_handle->messages[packet]->sequence_number));
792 copy_and_queue_message (socket,
793 &io_handle->messages[packet]->header,
799 if (GNUNET_SCHEDULER_NO_TASK == socket->retransmission_timeout_task_id)
800 socket->retransmission_timeout_task_id =
801 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
802 (GNUNET_TIME_UNIT_SECONDS, 8),
803 &retransmission_timeout_task,
809 * Task for calling the read processor
811 * @param cls the socket
812 * @param tc the task context
815 call_read_processor (void *cls,
816 const struct GNUNET_SCHEDULER_TaskContext *tc)
818 struct GNUNET_STREAM_Socket *socket = cls;
820 size_t valid_read_size;
822 uint32_t sequence_increase;
823 uint32_t offset_increase;
825 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
826 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
829 GNUNET_assert (NULL != socket->read_handle);
830 GNUNET_assert (NULL != socket->read_handle->proc);
832 /* Check the bitmap for any holes */
833 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
835 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
839 /* We only call read processor if we have the first packet */
840 GNUNET_assert (0 < packet);
843 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
845 GNUNET_assert (0 != valid_read_size);
847 /* Cancel the read_io_timeout_task */
848 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task_id);
849 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
851 /* Call the data processor */
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "%x: Calling read processor\n",
856 socket->read_handle->proc (socket->read_handle->proc_cls,
858 socket->receive_buffer + socket->copy_offset,
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "%x: Read processor read %d bytes\n",
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865 "%x: Read processor completed successfully\n",
868 /* Free the read handle */
869 GNUNET_free (socket->read_handle);
870 socket->read_handle = NULL;
872 GNUNET_assert (read_size <= valid_read_size);
873 socket->copy_offset += read_size;
875 /* Determine upto which packet we can remove from the buffer */
876 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
878 if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
880 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
884 /* If no packets can be removed we can't move the buffer */
885 if (0 == packet) return;
887 sequence_increase = packet;
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "%x: Sequence increase after read processor completion: %u\n",
893 /* Shift the data in the receive buffer */
894 memmove (socket->receive_buffer,
895 socket->receive_buffer
896 + socket->receive_buffer_boundaries[sequence_increase-1],
897 socket->receive_buffer_size
898 - socket->receive_buffer_boundaries[sequence_increase-1]);
900 /* Shift the bitmap */
901 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
903 /* Set read_sequence_number */
904 socket->read_sequence_number += sequence_increase;
906 /* Set read_offset */
907 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
908 socket->read_offset += offset_increase;
910 /* Fix copy_offset */
911 GNUNET_assert (offset_increase <= socket->copy_offset);
912 socket->copy_offset -= offset_increase;
914 /* Fix relative boundaries */
915 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
917 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
919 socket->receive_buffer_boundaries[packet] =
920 socket->receive_buffer_boundaries[packet + sequence_increase]
924 socket->receive_buffer_boundaries[packet] = 0;
930 * Cancels the existing read io handle
932 * @param cls the closure from the SCHEDULER call
933 * @param tc the task context
936 read_io_timeout (void *cls,
937 const struct GNUNET_SCHEDULER_TaskContext *tc)
939 struct GNUNET_STREAM_Socket *socket = cls;
940 GNUNET_STREAM_DataProcessor proc;
943 socket->read_io_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
944 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "%x: Read task timedout - Cancelling it\n",
949 GNUNET_SCHEDULER_cancel (socket->read_task_id);
950 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
952 GNUNET_assert (NULL != socket->read_handle);
953 proc = socket->read_handle->proc;
954 proc_cls = socket->read_handle->proc_cls;
956 GNUNET_free (socket->read_handle);
957 socket->read_handle = NULL;
958 /* Call the read processor to signal timeout */
960 GNUNET_STREAM_TIMEOUT,
967 * Handler for DATA messages; Same for both client and server
969 * @param socket the socket through which the ack was received
970 * @param tunnel connection to the other end
971 * @param sender who sent the message
972 * @param msg the data message
973 * @param atsi performance data for the connection
974 * @return GNUNET_OK to keep the connection open,
975 * GNUNET_SYSERR to close it (signal serious error)
978 handle_data (struct GNUNET_STREAM_Socket *socket,
979 struct GNUNET_MESH_Tunnel *tunnel,
980 const struct GNUNET_PeerIdentity *sender,
981 const struct GNUNET_STREAM_DataMessage *msg,
982 const struct GNUNET_ATS_Information*atsi)
985 uint32_t bytes_needed;
986 uint32_t relative_offset;
987 uint32_t relative_sequence_number;
990 size = htons (msg->header.header.size);
991 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
994 return GNUNET_SYSERR;
997 if (GNUNET_PEER_search (sender) != socket->other_peer)
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 "%x: Received DATA from non-confirming peer\n",
1005 switch (socket->state)
1007 case STATE_ESTABLISHED:
1008 case STATE_TRANSMIT_CLOSED:
1009 case STATE_TRANSMIT_CLOSE_WAIT:
1011 /* check if the message's sequence number is in the range we are
1013 relative_sequence_number =
1014 ntohl (msg->sequence_number) - socket->read_sequence_number;
1015 if ( relative_sequence_number > GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "%x: Ignoring received message with sequence number %u\n",
1020 ntohl (msg->sequence_number));
1021 /* Start ACK sending task if one is not already present */
1022 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1024 socket->ack_task_id =
1025 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1026 (msg->ack_deadline),
1033 /* Check if we have already seen this message */
1034 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1035 relative_sequence_number))
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "%x: Ignoring already received message with sequence "
1041 ntohl (msg->sequence_number));
1042 /* Start ACK sending task if one is not already present */
1043 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1045 socket->ack_task_id =
1046 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1047 (msg->ack_deadline),
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "%x: Receiving DATA with sequence number: %u and size: %d "
1058 ntohl (msg->sequence_number),
1059 ntohs (msg->header.header.size),
1060 socket->other_peer);
1062 /* Check if we have to allocate the buffer */
1063 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1064 relative_offset = ntohl (msg->offset) - socket->read_offset;
1065 bytes_needed = relative_offset + size;
1066 if (bytes_needed > socket->receive_buffer_size)
1068 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1070 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1072 socket->receive_buffer_size = bytes_needed;
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1077 "%x: Cannot accommodate packet %d as buffer is",
1080 ntohl (msg->sequence_number));
1085 /* Copy Data to buffer */
1087 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1088 memcpy (socket->receive_buffer + relative_offset,
1091 socket->receive_buffer_boundaries[relative_sequence_number] =
1092 relative_offset + size;
1094 /* Modify the ACK bitmap */
1095 ackbitmap_modify_bit (&socket->ack_bitmap,
1096 relative_sequence_number,
1099 /* Start ACK sending task if one is not already present */
1100 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1102 socket->ack_task_id =
1103 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1104 (msg->ack_deadline),
1109 if ((NULL != socket->read_handle) /* A read handle is waiting */
1110 /* There is no current read task */
1111 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
1112 /* We have the first packet */
1113 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "%x: Scheduling read processor\n",
1120 socket->read_task_id =
1121 GNUNET_SCHEDULER_add_now (&call_read_processor,
1128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129 "%x: Received data message when it cannot be handled\n",
1138 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1140 * @param cls the socket (set from GNUNET_MESH_connect)
1141 * @param tunnel connection to the other end
1142 * @param tunnel_ctx place to store local state associated with the tunnel
1143 * @param sender who sent the message
1144 * @param message the actual message
1145 * @param atsi performance data for the connection
1146 * @return GNUNET_OK to keep the connection open,
1147 * GNUNET_SYSERR to close it (signal serious error)
1150 client_handle_data (void *cls,
1151 struct GNUNET_MESH_Tunnel *tunnel,
1153 const struct GNUNET_PeerIdentity *sender,
1154 const struct GNUNET_MessageHeader *message,
1155 const struct GNUNET_ATS_Information*atsi)
1157 struct GNUNET_STREAM_Socket *socket = cls;
1159 return handle_data (socket,
1162 (const struct GNUNET_STREAM_DataMessage *) message,
1168 * Callback to set state to ESTABLISHED
1170 * @param cls the closure from queue_message FIXME: document
1171 * @param socket the socket to requiring state change
1174 set_state_established (void *cls,
1175 struct GNUNET_STREAM_Socket *socket)
1177 struct GNUNET_PeerIdentity initiator_pid;
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "%x: Attaining ESTABLISHED state\n",
1182 socket->write_offset = 0;
1183 socket->read_offset = 0;
1184 socket->state = STATE_ESTABLISHED;
1185 /* FIXME: What if listen_cb is NULL */
1186 if (NULL != socket->lsocket)
1188 GNUNET_PEER_resolve (socket->other_peer, &initiator_pid);
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "%x: Calling listen callback\n",
1192 if (GNUNET_SYSERR ==
1193 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1197 socket->state = STATE_CLOSED;
1198 /* FIXME: We should close in a decent way */
1199 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1200 GNUNET_free (socket);
1203 else if (socket->open_cb)
1204 socket->open_cb (socket->open_cls, socket);
1209 * Callback to set state to HELLO_WAIT
1211 * @param cls the closure from queue_message
1212 * @param socket the socket to requiring state change
1215 set_state_hello_wait (void *cls,
1216 struct GNUNET_STREAM_Socket *socket)
1218 GNUNET_assert (STATE_INIT == socket->state);
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 "%x: Attaining HELLO_WAIT state\n",
1222 socket->state = STATE_HELLO_WAIT;
1227 * Returns a new HelloAckMessage. Also sets the write sequence number for the
1230 * @param socket the socket for which this HelloAckMessage has to be generated
1231 * @return the HelloAckMessage
1233 static struct GNUNET_STREAM_HelloAckMessage *
1234 generate_hello_ack_msg (struct GNUNET_STREAM_Socket *socket)
1236 struct GNUNET_STREAM_HelloAckMessage *msg;
1238 /* Get the random sequence number */
1239 socket->write_sequence_number =
1240 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 "%x: Generated write sequence number %u\n",
1244 (unsigned int) socket->write_sequence_number);
1246 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1247 msg->header.header.size =
1248 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1249 msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1250 msg->sequence_number = htonl (socket->write_sequence_number);
1251 msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1258 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1260 * @param cls the socket (set from GNUNET_MESH_connect)
1261 * @param tunnel connection to the other end
1262 * @param tunnel_ctx this is NULL
1263 * @param sender who sent the message
1264 * @param message the actual message
1265 * @param atsi performance data for the connection
1266 * @return GNUNET_OK to keep the connection open,
1267 * GNUNET_SYSERR to close it (signal serious error)
1270 client_handle_hello_ack (void *cls,
1271 struct GNUNET_MESH_Tunnel *tunnel,
1273 const struct GNUNET_PeerIdentity *sender,
1274 const struct GNUNET_MessageHeader *message,
1275 const struct GNUNET_ATS_Information*atsi)
1277 struct GNUNET_STREAM_Socket *socket = cls;
1278 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1279 struct GNUNET_STREAM_HelloAckMessage *reply;
1281 if (GNUNET_PEER_search (sender) != socket->other_peer)
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284 "%x: Received HELLO_ACK from non-confirming peer\n",
1288 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "%x: Received HELLO_ACK from %x\n",
1292 socket->other_peer);
1294 GNUNET_assert (socket->tunnel == tunnel);
1295 switch (socket->state)
1297 case STATE_HELLO_WAIT:
1298 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 "%x: Read sequence number %u\n",
1302 (unsigned int) socket->read_sequence_number);
1303 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1304 reply = generate_hello_ack_msg (socket);
1305 queue_message (socket,
1307 &set_state_established,
1310 case STATE_ESTABLISHED:
1311 case STATE_RECEIVE_CLOSE_WAIT:
1312 // call statistics (# ACKs ignored++)
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 "%x: Server %x sent HELLO_ACK when in state %d\n",
1321 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1322 return GNUNET_SYSERR;
1329 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1331 * @param cls the socket (set from GNUNET_MESH_connect)
1332 * @param tunnel connection to the other end
1333 * @param tunnel_ctx this is NULL
1334 * @param sender who sent the message
1335 * @param message the actual message
1336 * @param atsi performance data for the connection
1337 * @return GNUNET_OK to keep the connection open,
1338 * GNUNET_SYSERR to close it (signal serious error)
1341 client_handle_reset (void *cls,
1342 struct GNUNET_MESH_Tunnel *tunnel,
1344 const struct GNUNET_PeerIdentity *sender,
1345 const struct GNUNET_MessageHeader *message,
1346 const struct GNUNET_ATS_Information*atsi)
1348 struct GNUNET_STREAM_Socket *socket = cls;
1355 * Common message handler for handling TRANSMIT_CLOSE messages
1357 * @param socket the socket through which the ack was received
1358 * @param tunnel connection to the other end
1359 * @param sender who sent the message
1360 * @param msg the transmit close message
1361 * @param atsi performance data for the connection
1362 * @return GNUNET_OK to keep the connection open,
1363 * GNUNET_SYSERR to close it (signal serious error)
1366 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1367 struct GNUNET_MESH_Tunnel *tunnel,
1368 const struct GNUNET_PeerIdentity *sender,
1369 const struct GNUNET_STREAM_MessageHeader *msg,
1370 const struct GNUNET_ATS_Information*atsi)
1372 struct GNUNET_STREAM_MessageHeader *reply;
1374 switch (socket->state)
1376 case STATE_ESTABLISHED:
1377 socket->state = STATE_RECEIVE_CLOSED;
1379 /* Send TRANSMIT_CLOSE_ACK */
1380 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1381 reply->header.type =
1382 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1383 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1384 queue_message (socket, reply, NULL, NULL);
1388 /* FIXME: Call statistics? */
1396 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1398 * @param cls the socket (set from GNUNET_MESH_connect)
1399 * @param tunnel connection to the other end
1400 * @param tunnel_ctx this is NULL
1401 * @param sender who sent the message
1402 * @param message the actual message
1403 * @param atsi performance data for the connection
1404 * @return GNUNET_OK to keep the connection open,
1405 * GNUNET_SYSERR to close it (signal serious error)
1408 client_handle_transmit_close (void *cls,
1409 struct GNUNET_MESH_Tunnel *tunnel,
1411 const struct GNUNET_PeerIdentity *sender,
1412 const struct GNUNET_MessageHeader *message,
1413 const struct GNUNET_ATS_Information*atsi)
1415 struct GNUNET_STREAM_Socket *socket = cls;
1417 return handle_transmit_close (socket,
1420 (struct GNUNET_STREAM_MessageHeader *)message,
1426 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1428 * @param cls the socket (set from GNUNET_MESH_connect)
1429 * @param tunnel connection to the other end
1430 * @param tunnel_ctx this is NULL
1431 * @param sender who sent the message
1432 * @param message the actual message
1433 * @param atsi performance data for the connection
1434 * @return GNUNET_OK to keep the connection open,
1435 * GNUNET_SYSERR to close it (signal serious error)
1438 client_handle_transmit_close_ack (void *cls,
1439 struct GNUNET_MESH_Tunnel *tunnel,
1441 const struct GNUNET_PeerIdentity *sender,
1442 const struct GNUNET_MessageHeader *message,
1443 const struct GNUNET_ATS_Information*atsi)
1445 struct GNUNET_STREAM_Socket *socket = cls;
1452 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1454 * @param cls the socket (set from GNUNET_MESH_connect)
1455 * @param tunnel connection to the other end
1456 * @param tunnel_ctx this is NULL
1457 * @param sender who sent the message
1458 * @param message the actual message
1459 * @param atsi performance data for the connection
1460 * @return GNUNET_OK to keep the connection open,
1461 * GNUNET_SYSERR to close it (signal serious error)
1464 client_handle_receive_close (void *cls,
1465 struct GNUNET_MESH_Tunnel *tunnel,
1467 const struct GNUNET_PeerIdentity *sender,
1468 const struct GNUNET_MessageHeader *message,
1469 const struct GNUNET_ATS_Information*atsi)
1471 struct GNUNET_STREAM_Socket *socket = cls;
1478 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1480 * @param cls the socket (set from GNUNET_MESH_connect)
1481 * @param tunnel connection to the other end
1482 * @param tunnel_ctx this is NULL
1483 * @param sender who sent the message
1484 * @param message the actual message
1485 * @param atsi performance data for the connection
1486 * @return GNUNET_OK to keep the connection open,
1487 * GNUNET_SYSERR to close it (signal serious error)
1490 client_handle_receive_close_ack (void *cls,
1491 struct GNUNET_MESH_Tunnel *tunnel,
1493 const struct GNUNET_PeerIdentity *sender,
1494 const struct GNUNET_MessageHeader *message,
1495 const struct GNUNET_ATS_Information*atsi)
1497 struct GNUNET_STREAM_Socket *socket = cls;
1504 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
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_close (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;
1530 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1532 * @param cls the socket (set from GNUNET_MESH_connect)
1533 * @param tunnel connection to the other end
1534 * @param tunnel_ctx this is NULL
1535 * @param sender who sent the message
1536 * @param message the actual message
1537 * @param atsi performance data for the connection
1538 * @return GNUNET_OK to keep the connection open,
1539 * GNUNET_SYSERR to close it (signal serious error)
1542 client_handle_close_ack (void *cls,
1543 struct GNUNET_MESH_Tunnel *tunnel,
1545 const struct GNUNET_PeerIdentity *sender,
1546 const struct GNUNET_MessageHeader *message,
1547 const struct GNUNET_ATS_Information*atsi)
1549 struct GNUNET_STREAM_Socket *socket = cls;
1554 /*****************************/
1555 /* Server's Message Handlers */
1556 /*****************************/
1559 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1561 * @param cls the closure
1562 * @param tunnel connection to the other end
1563 * @param tunnel_ctx the socket
1564 * @param sender who sent the message
1565 * @param message the actual message
1566 * @param atsi performance data for the connection
1567 * @return GNUNET_OK to keep the connection open,
1568 * GNUNET_SYSERR to close it (signal serious error)
1571 server_handle_data (void *cls,
1572 struct GNUNET_MESH_Tunnel *tunnel,
1574 const struct GNUNET_PeerIdentity *sender,
1575 const struct GNUNET_MessageHeader *message,
1576 const struct GNUNET_ATS_Information*atsi)
1578 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1580 return handle_data (socket,
1583 (const struct GNUNET_STREAM_DataMessage *)message,
1589 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1591 * @param cls the closure
1592 * @param tunnel connection to the other end
1593 * @param tunnel_ctx the socket
1594 * @param sender who sent the message
1595 * @param message the actual message
1596 * @param atsi performance data for the connection
1597 * @return GNUNET_OK to keep the connection open,
1598 * GNUNET_SYSERR to close it (signal serious error)
1601 server_handle_hello (void *cls,
1602 struct GNUNET_MESH_Tunnel *tunnel,
1604 const struct GNUNET_PeerIdentity *sender,
1605 const struct GNUNET_MessageHeader *message,
1606 const struct GNUNET_ATS_Information*atsi)
1608 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1609 struct GNUNET_STREAM_HelloAckMessage *reply;
1611 if (GNUNET_PEER_search (sender) != socket->other_peer)
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1614 "%x: Received HELLO from non-confirming peer\n",
1619 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO ==
1620 ntohs (message->type));
1621 GNUNET_assert (socket->tunnel == tunnel);
1622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1623 "%x: Received HELLO from %x\n",
1625 socket->other_peer);
1627 if (STATE_INIT == socket->state)
1629 reply = generate_hello_ack_msg (socket);
1630 queue_message (socket,
1632 &set_state_hello_wait,
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1638 "Client sent HELLO when in state %d\n", socket->state);
1639 /* FIXME: Send RESET? */
1647 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1649 * @param cls the closure
1650 * @param tunnel connection to the other end
1651 * @param tunnel_ctx the socket
1652 * @param sender who sent the message
1653 * @param message the actual message
1654 * @param atsi performance data for the connection
1655 * @return GNUNET_OK to keep the connection open,
1656 * GNUNET_SYSERR to close it (signal serious error)
1659 server_handle_hello_ack (void *cls,
1660 struct GNUNET_MESH_Tunnel *tunnel,
1662 const struct GNUNET_PeerIdentity *sender,
1663 const struct GNUNET_MessageHeader *message,
1664 const struct GNUNET_ATS_Information*atsi)
1666 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1667 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1669 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
1670 ntohs (message->type));
1671 GNUNET_assert (socket->tunnel == tunnel);
1672 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1673 if (STATE_HELLO_WAIT == socket->state)
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "%x: Received HELLO_ACK from %x\n",
1678 socket->other_peer);
1679 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1681 "%x: Read sequence number %u\n",
1683 (unsigned int) socket->read_sequence_number);
1684 socket->receiver_window_available =
1685 ntohl (ack_message->receiver_window_size);
1686 /* Attain ESTABLISHED state */
1687 set_state_established (NULL, socket);
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Client sent HELLO_ACK when in state %d\n", socket->state);
1693 /* FIXME: Send RESET? */
1701 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1703 * @param cls the closure
1704 * @param tunnel connection to the other end
1705 * @param tunnel_ctx the socket
1706 * @param sender who sent the message
1707 * @param message the actual message
1708 * @param atsi performance data for the connection
1709 * @return GNUNET_OK to keep the connection open,
1710 * GNUNET_SYSERR to close it (signal serious error)
1713 server_handle_reset (void *cls,
1714 struct GNUNET_MESH_Tunnel *tunnel,
1716 const struct GNUNET_PeerIdentity *sender,
1717 const struct GNUNET_MessageHeader *message,
1718 const struct GNUNET_ATS_Information*atsi)
1720 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1727 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1729 * @param cls the closure
1730 * @param tunnel connection to the other end
1731 * @param tunnel_ctx the socket
1732 * @param sender who sent the message
1733 * @param message the actual message
1734 * @param atsi performance data for the connection
1735 * @return GNUNET_OK to keep the connection open,
1736 * GNUNET_SYSERR to close it (signal serious error)
1739 server_handle_transmit_close (void *cls,
1740 struct GNUNET_MESH_Tunnel *tunnel,
1742 const struct GNUNET_PeerIdentity *sender,
1743 const struct GNUNET_MessageHeader *message,
1744 const struct GNUNET_ATS_Information*atsi)
1746 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1748 return handle_transmit_close (socket,
1751 (struct GNUNET_STREAM_MessageHeader *)message,
1757 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1759 * @param cls the closure
1760 * @param tunnel connection to the other end
1761 * @param tunnel_ctx the socket
1762 * @param sender who sent the message
1763 * @param message the actual message
1764 * @param atsi performance data for the connection
1765 * @return GNUNET_OK to keep the connection open,
1766 * GNUNET_SYSERR to close it (signal serious error)
1769 server_handle_transmit_close_ack (void *cls,
1770 struct GNUNET_MESH_Tunnel *tunnel,
1772 const struct GNUNET_PeerIdentity *sender,
1773 const struct GNUNET_MessageHeader *message,
1774 const struct GNUNET_ATS_Information*atsi)
1776 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1783 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1785 * @param cls the closure
1786 * @param tunnel connection to the other end
1787 * @param tunnel_ctx the socket
1788 * @param sender who sent the message
1789 * @param message the actual message
1790 * @param atsi performance data for the connection
1791 * @return GNUNET_OK to keep the connection open,
1792 * GNUNET_SYSERR to close it (signal serious error)
1795 server_handle_receive_close (void *cls,
1796 struct GNUNET_MESH_Tunnel *tunnel,
1798 const struct GNUNET_PeerIdentity *sender,
1799 const struct GNUNET_MessageHeader *message,
1800 const struct GNUNET_ATS_Information*atsi)
1802 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1809 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1811 * @param cls the closure
1812 * @param tunnel connection to the other end
1813 * @param tunnel_ctx the socket
1814 * @param sender who sent the message
1815 * @param message the actual message
1816 * @param atsi performance data for the connection
1817 * @return GNUNET_OK to keep the connection open,
1818 * GNUNET_SYSERR to close it (signal serious error)
1821 server_handle_receive_close_ack (void *cls,
1822 struct GNUNET_MESH_Tunnel *tunnel,
1824 const struct GNUNET_PeerIdentity *sender,
1825 const struct GNUNET_MessageHeader *message,
1826 const struct GNUNET_ATS_Information*atsi)
1828 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1835 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1837 * @param cls the closure
1838 * @param tunnel connection to the other end
1839 * @param tunnel_ctx the socket
1840 * @param sender who sent the message
1841 * @param message the actual message
1842 * @param atsi performance data for the connection
1843 * @return GNUNET_OK to keep the connection open,
1844 * GNUNET_SYSERR to close it (signal serious error)
1847 server_handle_close (void *cls,
1848 struct GNUNET_MESH_Tunnel *tunnel,
1850 const struct GNUNET_PeerIdentity *sender,
1851 const struct GNUNET_MessageHeader *message,
1852 const struct GNUNET_ATS_Information*atsi)
1854 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1861 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1863 * @param cls the closure
1864 * @param tunnel connection to the other end
1865 * @param tunnel_ctx the socket
1866 * @param sender who sent the message
1867 * @param message the actual message
1868 * @param atsi performance data for the connection
1869 * @return GNUNET_OK to keep the connection open,
1870 * GNUNET_SYSERR to close it (signal serious error)
1873 server_handle_close_ack (void *cls,
1874 struct GNUNET_MESH_Tunnel *tunnel,
1876 const struct GNUNET_PeerIdentity *sender,
1877 const struct GNUNET_MessageHeader *message,
1878 const struct GNUNET_ATS_Information*atsi)
1880 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1887 * Message Handler for mesh
1889 * @param socket the socket through which the ack was received
1890 * @param tunnel connection to the other end
1891 * @param sender who sent the message
1892 * @param ack the acknowledgment message
1893 * @param atsi performance data for the connection
1894 * @return GNUNET_OK to keep the connection open,
1895 * GNUNET_SYSERR to close it (signal serious error)
1898 handle_ack (struct GNUNET_STREAM_Socket *socket,
1899 struct GNUNET_MESH_Tunnel *tunnel,
1900 const struct GNUNET_PeerIdentity *sender,
1901 const struct GNUNET_STREAM_AckMessage *ack,
1902 const struct GNUNET_ATS_Information*atsi)
1904 unsigned int packet;
1905 int need_retransmission;
1908 if (GNUNET_PEER_search (sender) != socket->other_peer)
1910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1911 "%x: Received ACK from non-confirming peer\n",
1916 switch (socket->state)
1918 case (STATE_ESTABLISHED):
1919 if (NULL == socket->write_handle)
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1922 "%x: Received DATA_ACK when write_handle is NULL\n",
1926 /* FIXME: increment in the base sequence number is breaking current flow
1928 if (!((socket->write_sequence_number
1929 - ntohl (ack->base_sequence_number)) < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1932 "%x: Received DATA_ACK with unexpected base sequence "
1935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1936 "%x: Current write sequence: %u; Ack's base sequence: %u\n",
1938 socket->write_sequence_number,
1939 ntohl (ack->base_sequence_number));
1942 /* FIXME: include the case when write_handle is cancelled - ignore the
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1946 "%x: Received DATA_ACK from %x\n",
1948 socket->other_peer);
1950 /* Cancel the retransmission task */
1951 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
1953 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
1954 socket->retransmission_timeout_task_id =
1955 GNUNET_SCHEDULER_NO_TASK;
1958 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1960 if (NULL == socket->write_handle->messages[packet]) break;
1961 if (ntohl (ack->base_sequence_number)
1962 >= ntohl (socket->write_handle->messages[packet]->sequence_number))
1963 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
1968 ackbitmap_is_bit_set (&socket->write_handle->ack_bitmap,
1969 ntohl (socket->write_handle->messages[packet]->sequence_number)
1970 - ntohl (ack->base_sequence_number)))
1971 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
1976 /* Update the receive window remaining
1977 FIXME : Should update with the value from a data ack with greater
1979 socket->receiver_window_available =
1980 ntohl (ack->receive_window_remaining);
1982 /* Check if we have received all acknowledgements */
1983 need_retransmission = GNUNET_NO;
1984 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1986 if (NULL == socket->write_handle->messages[packet]) break;
1987 if (GNUNET_YES != ackbitmap_is_bit_set
1988 (&socket->write_handle->ack_bitmap,packet))
1990 need_retransmission = GNUNET_YES;
1994 if (GNUNET_YES == need_retransmission)
1996 write_data (socket);
1998 else /* We have to call the write continuation callback now */
2000 /* Free the packets */
2001 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2003 GNUNET_free_non_null (socket->write_handle->messages[packet]);
2005 if (NULL != socket->write_handle->write_cont)
2006 socket->write_handle->write_cont
2007 (socket->write_handle->write_cont_cls,
2009 socket->write_handle->size);
2010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2011 "%x: Write completion callback completed\n",
2013 /* We are done with the write handle - Freeing it */
2014 GNUNET_free (socket->write_handle);
2015 socket->write_handle = NULL;
2026 * Message Handler for mesh
2028 * @param cls the 'struct GNUNET_STREAM_Socket'
2029 * @param tunnel connection to the other end
2030 * @param tunnel_ctx unused
2031 * @param sender who sent the message
2032 * @param message the actual message
2033 * @param atsi performance data for the connection
2034 * @return GNUNET_OK to keep the connection open,
2035 * GNUNET_SYSERR to close it (signal serious error)
2038 client_handle_ack (void *cls,
2039 struct GNUNET_MESH_Tunnel *tunnel,
2041 const struct GNUNET_PeerIdentity *sender,
2042 const struct GNUNET_MessageHeader *message,
2043 const struct GNUNET_ATS_Information*atsi)
2045 struct GNUNET_STREAM_Socket *socket = cls;
2046 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2048 return handle_ack (socket, tunnel, sender, ack, atsi);
2053 * Message Handler for mesh
2055 * @param cls the server's listen socket
2056 * @param tunnel connection to the other end
2057 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2058 * @param sender who sent the message
2059 * @param message the actual message
2060 * @param atsi performance data for the connection
2061 * @return GNUNET_OK to keep the connection open,
2062 * GNUNET_SYSERR to close it (signal serious error)
2065 server_handle_ack (void *cls,
2066 struct GNUNET_MESH_Tunnel *tunnel,
2068 const struct GNUNET_PeerIdentity *sender,
2069 const struct GNUNET_MessageHeader *message,
2070 const struct GNUNET_ATS_Information*atsi)
2072 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2073 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2075 return handle_ack (socket, tunnel, sender, ack, atsi);
2080 * For client message handlers, the stream socket is in the
2083 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2084 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2085 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2086 sizeof (struct GNUNET_STREAM_AckMessage) },
2087 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2088 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2089 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2090 sizeof (struct GNUNET_STREAM_MessageHeader)},
2091 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2092 sizeof (struct GNUNET_STREAM_MessageHeader)},
2093 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2094 sizeof (struct GNUNET_STREAM_MessageHeader)},
2095 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2096 sizeof (struct GNUNET_STREAM_MessageHeader)},
2097 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2098 sizeof (struct GNUNET_STREAM_MessageHeader)},
2099 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2100 sizeof (struct GNUNET_STREAM_MessageHeader)},
2101 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2102 sizeof (struct GNUNET_STREAM_MessageHeader)},
2108 * For server message handlers, the stream socket is in the
2109 * tunnel context, and the listen socket in the closure argument.
2111 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2112 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2113 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2114 sizeof (struct GNUNET_STREAM_AckMessage) },
2115 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2116 sizeof (struct GNUNET_STREAM_MessageHeader)},
2117 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2118 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2119 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2120 sizeof (struct GNUNET_STREAM_MessageHeader)},
2121 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2122 sizeof (struct GNUNET_STREAM_MessageHeader)},
2123 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2124 sizeof (struct GNUNET_STREAM_MessageHeader)},
2125 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2126 sizeof (struct GNUNET_STREAM_MessageHeader)},
2127 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2128 sizeof (struct GNUNET_STREAM_MessageHeader)},
2129 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2130 sizeof (struct GNUNET_STREAM_MessageHeader)},
2131 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2132 sizeof (struct GNUNET_STREAM_MessageHeader)},
2138 * Function called when our target peer is connected to our tunnel
2140 * @param cls the socket for which this tunnel is created
2141 * @param peer the peer identity of the target
2142 * @param atsi performance data for the connection
2145 mesh_peer_connect_callback (void *cls,
2146 const struct GNUNET_PeerIdentity *peer,
2147 const struct GNUNET_ATS_Information * atsi)
2149 struct GNUNET_STREAM_Socket *socket = cls;
2150 struct GNUNET_STREAM_MessageHeader *message;
2151 GNUNET_PEER_Id connected_peer;
2153 connected_peer = GNUNET_PEER_search (peer);
2155 if (connected_peer != socket->other_peer)
2157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2158 "%x: A peer which is not our target has connected",
2164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2165 "%x: Target peer %x connected\n",
2169 /* Set state to INIT */
2170 socket->state = STATE_INIT;
2172 /* Send HELLO message */
2173 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2174 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
2175 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2176 queue_message (socket,
2178 &set_state_hello_wait,
2181 /* Call open callback */
2182 if (NULL == socket->open_cb)
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2185 "STREAM_open callback is NULL\n");
2191 * Function called when our target peer is disconnected from our tunnel
2193 * @param cls the socket associated which this tunnel
2194 * @param peer the peer identity of the target
2197 mesh_peer_disconnect_callback (void *cls,
2198 const struct GNUNET_PeerIdentity *peer)
2205 * Method called whenever a peer creates a tunnel to us
2207 * @param cls closure
2208 * @param tunnel new handle to the tunnel
2209 * @param initiator peer that started the tunnel
2210 * @param atsi performance information for the tunnel
2211 * @return initial tunnel context for the tunnel
2212 * (can be NULL -- that's not an error)
2215 new_tunnel_notify (void *cls,
2216 struct GNUNET_MESH_Tunnel *tunnel,
2217 const struct GNUNET_PeerIdentity *initiator,
2218 const struct GNUNET_ATS_Information *atsi)
2220 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2221 struct GNUNET_STREAM_Socket *socket;
2223 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2224 from the same peer again until the socket is closed */
2226 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2227 socket->other_peer = GNUNET_PEER_intern (initiator);
2228 socket->tunnel = tunnel;
2229 socket->session_id = 0; /* FIXME */
2230 socket->state = STATE_INIT;
2231 socket->lsocket = lsocket;
2232 socket->our_id = lsocket->our_id;
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2235 "%x: Peer %x initiated tunnel to us\n",
2237 socket->other_peer);
2239 /* FIXME: Copy MESH handle from lsocket to socket */
2246 * Function called whenever an inbound tunnel is destroyed. Should clean up
2247 * any associated state. This function is NOT called if the client has
2248 * explicitly asked for the tunnel to be destroyed using
2249 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2252 * @param cls closure (set from GNUNET_MESH_connect)
2253 * @param tunnel connection to the other end (henceforth invalid)
2254 * @param tunnel_ctx place where local state associated
2255 * with the tunnel is stored
2258 tunnel_cleaner (void *cls,
2259 const struct GNUNET_MESH_Tunnel *tunnel,
2262 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2265 "%x: Peer %x has terminated connection abruptly\n",
2267 socket->other_peer);
2269 socket->status = GNUNET_STREAM_SHUTDOWN;
2271 /* Clear Transmit handles */
2272 if (NULL != socket->transmit_handle)
2274 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2275 socket->transmit_handle = NULL;
2277 socket->tunnel = NULL;
2287 * Tries to open a stream to the target peer
2289 * @param cfg configuration to use
2290 * @param target the target peer to which the stream has to be opened
2291 * @param app_port the application port number which uniquely identifies this
2293 * @param open_cb this function will be called after stream has be established
2294 * @param open_cb_cls the closure for open_cb
2295 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2296 * @return if successful it returns the stream socket; NULL if stream cannot be
2299 struct GNUNET_STREAM_Socket *
2300 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2301 const struct GNUNET_PeerIdentity *target,
2302 GNUNET_MESH_ApplicationType app_port,
2303 GNUNET_STREAM_OpenCallback open_cb,
2307 struct GNUNET_STREAM_Socket *socket;
2308 struct GNUNET_PeerIdentity own_peer_id;
2309 enum GNUNET_STREAM_Option option;
2310 va_list vargs; /* Variable arguments */
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2315 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2316 socket->other_peer = GNUNET_PEER_intern (target);
2317 socket->open_cb = open_cb;
2318 socket->open_cls = open_cb_cls;
2319 GNUNET_TESTING_get_peer_identity (cfg, &own_peer_id);
2320 socket->our_id = GNUNET_PEER_intern (&own_peer_id);
2323 socket->retransmit_timeout =
2324 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
2326 va_start (vargs, open_cb_cls); /* Parse variable args */
2328 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2331 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2332 /* Expect struct GNUNET_TIME_Relative */
2333 socket->retransmit_timeout = va_arg (vargs,
2334 struct GNUNET_TIME_Relative);
2336 case GNUNET_STREAM_OPTION_END:
2339 } while (GNUNET_STREAM_OPTION_END != option);
2340 va_end (vargs); /* End of variable args parsing */
2341 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2342 10, /* QUEUE size as parameter? */
2344 NULL, /* No inbound tunnel handler */
2345 &tunnel_cleaner, /* FIXME: not required? */
2346 client_message_handlers,
2347 &app_port); /* We don't get inbound tunnels */
2348 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2350 GNUNET_free (socket);
2354 /* Now create the mesh tunnel to target */
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2356 "Creating MESH Tunnel\n");
2357 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2358 NULL, /* Tunnel context */
2359 &mesh_peer_connect_callback,
2360 &mesh_peer_disconnect_callback,
2362 GNUNET_assert (NULL != socket->tunnel);
2363 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2367 "%s() END\n", __func__);
2373 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
2375 * @param socket the stream socket
2376 * @param opertion SHUT_RD, SHUT_WR or SHUT_RDWR
2377 * @param completion_cb the callback that will be called upon successful
2378 * shutdown of given operation
2379 * @param completion_cls the closure for the completion callback
2380 * @return the shutdown handle
2382 struct GNUNET_STREAM_ShutdownHandle *
2383 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2385 GNUNET_STREAM_ShutdownCompletion completion_cb,
2386 void *completion_cls)
2393 * Cancels a pending shutdown
2395 * @param the shutdown handle returned from GNUNET_STREAM_shutdown
2398 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle)
2407 * @param socket the stream socket
2410 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
2412 struct MessageQueue *head;
2414 GNUNET_break (NULL == socket->read_handle);
2415 GNUNET_break (NULL == socket->write_handle);
2417 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
2419 /* socket closed with read task pending!? */
2421 GNUNET_SCHEDULER_cancel (socket->read_task_id);
2422 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
2425 /* Terminate the ack'ing tasks if they are still present */
2426 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
2428 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2429 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2432 /* Clear Transmit handles */
2433 if (NULL != socket->transmit_handle)
2435 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2436 socket->transmit_handle = NULL;
2439 /* Clear existing message queue */
2440 while (NULL != (head = socket->queue_head)) {
2441 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2444 GNUNET_free (head->message);
2448 /* Close associated tunnel */
2449 if (NULL != socket->tunnel)
2451 GNUNET_MESH_tunnel_destroy (socket->tunnel);
2452 socket->tunnel = NULL;
2455 /* Close mesh connection */
2456 if (NULL != socket->mesh && NULL == socket->lsocket)
2458 GNUNET_MESH_disconnect (socket->mesh);
2459 socket->mesh = NULL;
2462 /* Release receive buffer */
2463 if (NULL != socket->receive_buffer)
2465 GNUNET_free (socket->receive_buffer);
2468 GNUNET_free (socket);
2473 * Listens for stream connections for a specific application ports
2475 * @param cfg the configuration to use
2476 * @param app_port the application port for which new streams will be accepted
2477 * @param listen_cb this function will be called when a peer tries to establish
2479 * @param listen_cb_cls closure for listen_cb
2480 * @return listen socket, NULL for any error
2482 struct GNUNET_STREAM_ListenSocket *
2483 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
2484 GNUNET_MESH_ApplicationType app_port,
2485 GNUNET_STREAM_ListenCallback listen_cb,
2486 void *listen_cb_cls)
2488 /* FIXME: Add variable args for passing configration options? */
2489 struct GNUNET_STREAM_ListenSocket *lsocket;
2490 struct GNUNET_PeerIdentity our_peer_id;
2492 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
2493 lsocket->port = app_port;
2494 lsocket->listen_cb = listen_cb;
2495 lsocket->listen_cb_cls = listen_cb_cls;
2496 GNUNET_TESTING_get_peer_identity (cfg, &our_peer_id);
2497 lsocket->our_id = GNUNET_PEER_intern (&our_peer_id);
2498 lsocket->mesh = GNUNET_MESH_connect (cfg,
2499 10, /* FIXME: QUEUE size as parameter? */
2500 lsocket, /* Closure */
2503 server_message_handlers,
2505 GNUNET_assert (NULL != lsocket->mesh);
2511 * Closes the listen socket
2513 * @param lsocket the listen socket
2516 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
2518 /* Close MESH connection */
2519 GNUNET_assert (NULL != lsocket->mesh);
2520 GNUNET_MESH_disconnect (lsocket->mesh);
2522 GNUNET_free (lsocket);
2527 * Tries to write the given data to the stream
2529 * @param socket the socket representing a stream
2530 * @param data the data buffer from where the data is written into the stream
2531 * @param size the number of bytes to be written from the data buffer
2532 * @param timeout the timeout period
2533 * @param write_cont the function to call upon writing some bytes into the stream
2534 * @param write_cont_cls the closure
2535 * @return handle to cancel the operation
2537 struct GNUNET_STREAM_IOWriteHandle *
2538 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
2541 struct GNUNET_TIME_Relative timeout,
2542 GNUNET_STREAM_CompletionContinuation write_cont,
2543 void *write_cont_cls)
2545 unsigned int num_needed_packets;
2546 unsigned int packet;
2547 struct GNUNET_STREAM_IOWriteHandle *io_handle;
2548 uint32_t packet_size;
2549 uint32_t payload_size;
2550 struct GNUNET_STREAM_DataMessage *data_msg;
2552 struct GNUNET_TIME_Relative ack_deadline;
2554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2557 /* Return NULL if there is already a write request pending */
2558 if (NULL != socket->write_handle)
2563 if (!((STATE_ESTABLISHED == socket->state)
2564 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
2565 || (STATE_RECEIVE_CLOSED == socket->state)))
2567 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2568 "%x: Attempting to write on a closed (OR) not-yet-established"
2573 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
2574 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
2575 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
2576 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
2577 io_handle->socket = socket;
2578 io_handle->write_cont = write_cont;
2579 io_handle->write_cont_cls = write_cont_cls;
2580 io_handle->size = size;
2582 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2583 determined from RTT */
2584 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
2585 /* Divide the given buffer into packets for sending */
2586 for (packet=0; packet < num_needed_packets; packet++)
2588 if ((packet + 1) * max_payload_size < size)
2590 payload_size = max_payload_size;
2591 packet_size = MAX_PACKET_SIZE;
2595 payload_size = size - packet * max_payload_size;
2596 packet_size = payload_size + sizeof (struct
2597 GNUNET_STREAM_DataMessage);
2599 io_handle->messages[packet] = GNUNET_malloc (packet_size);
2600 io_handle->messages[packet]->header.header.size = htons (packet_size);
2601 io_handle->messages[packet]->header.header.type =
2602 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
2603 io_handle->messages[packet]->sequence_number =
2604 htonl (socket->write_sequence_number++);
2605 io_handle->messages[packet]->offset = htonl (socket->write_offset);
2607 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2608 determined from RTT */
2609 io_handle->messages[packet]->ack_deadline =
2610 GNUNET_TIME_relative_hton (ack_deadline);
2611 data_msg = io_handle->messages[packet];
2612 /* Copy data from given buffer to the packet */
2613 memcpy (&data_msg[1],
2616 sweep += payload_size;
2617 socket->write_offset += payload_size;
2619 socket->write_handle = io_handle;
2620 write_data (socket);
2622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2623 "%s() END\n", __func__);
2630 * Tries to read data from the stream
2632 * @param socket the socket representing a stream
2633 * @param timeout the timeout period
2634 * @param proc function to call with data (once only)
2635 * @param proc_cls the closure for proc
2636 * @return handle to cancel the operation
2638 struct GNUNET_STREAM_IOReadHandle *
2639 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
2640 struct GNUNET_TIME_Relative timeout,
2641 GNUNET_STREAM_DataProcessor proc,
2644 struct GNUNET_STREAM_IOReadHandle *read_handle;
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2651 /* Return NULL if there is already a read handle; the user has to cancel that
2652 first before continuing or has to wait until it is completed */
2653 if (NULL != socket->read_handle) return NULL;
2655 GNUNET_assert (NULL != proc);
2657 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
2658 read_handle->proc = proc;
2659 read_handle->proc_cls = proc_cls;
2660 socket->read_handle = read_handle;
2662 /* Check if we have a packet at bitmap 0 */
2663 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
2666 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
2671 /* Setup the read timeout task */
2672 socket->read_io_timeout_task_id =
2673 GNUNET_SCHEDULER_add_delayed (timeout,
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 * Cancel pending write operation.
2687 * @param ioh handle to operation to cancel
2690 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
2692 struct GNUNET_STREAM_Socket *socket = ioh->socket;
2693 unsigned int packet;
2695 GNUNET_assert (NULL != socket->write_handle);
2696 GNUNET_assert (socket->write_handle == ioh);
2698 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
2700 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
2701 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
2704 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
2706 if (NULL == ioh->messages[packet]) break;
2707 GNUNET_free (ioh->messages[packet]);
2710 GNUNET_free (socket->write_handle);
2711 socket->write_handle = NULL;
2717 * Cancel pending read operation.
2719 * @param ioh handle to operation to cancel
2722 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)