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
31 * @file stream/stream_api.c
32 * @brief Implementation of the stream library
33 * @author Sree Harsha Totakura
36 #include "gnunet_common.h"
37 #include "gnunet_crypto_lib.h"
38 #include "gnunet_stream_lib.h"
39 #include "gnunet_testing_lib.h"
40 #include "stream_protocol.h"
44 * The maximum packet size of a stream packet
46 #define MAX_PACKET_SIZE 64000
49 * The maximum payload a data message packet can carry
51 static size_t max_payload_size =
52 MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
57 #define RECEIVE_BUFFER_SIZE 4096000
60 * states in the Protocol
65 * Client initialization state
70 * Listener initialization state
75 * Pre-connection establishment state
80 * State where a connection has been established
85 * State where the socket is closed on our side and waiting to be ACK'ed
87 STATE_RECEIVE_CLOSE_WAIT,
90 * State where the socket is closed for reading
95 * State where the socket is closed on our side and waiting to be ACK'ed
97 STATE_TRANSMIT_CLOSE_WAIT,
100 * State where the socket is closed for writing
102 STATE_TRANSMIT_CLOSED,
105 * State where the socket is closed on our side and waiting to be ACK'ed
110 * State where the socket is closed
117 * Functions of this type are called when a message is written
119 * @param cls the closure from queue_message
120 * @param socket the socket the written message was bound to
122 typedef void (*SendFinishCallback) (void *cls,
123 struct GNUNET_STREAM_Socket *socket);
127 * The send message queue
134 struct GNUNET_STREAM_MessageHeader *message;
137 * Callback to be called when the message is sent
139 SendFinishCallback finish_cb;
142 * The closure for finish_cb
147 * The next message in queue. Should be NULL in the last message
149 struct MessageQueue *next;
152 * The next message in queue. Should be NULL in the first message
154 struct MessageQueue *prev;
159 * The STREAM Socket Handler
161 struct GNUNET_STREAM_Socket
164 * Retransmission timeout
166 struct GNUNET_TIME_Relative retransmit_timeout;
169 * The Acknowledgement Bitmap
171 GNUNET_STREAM_AckBitmap ack_bitmap;
174 * Time when the Acknowledgement was queued
176 struct GNUNET_TIME_Absolute ack_time_registered;
179 * Queued Acknowledgement deadline
181 struct GNUNET_TIME_Relative ack_time_deadline;
186 struct GNUNET_MESH_Handle *mesh;
189 * The mesh tunnel handle
191 struct GNUNET_MESH_Tunnel *tunnel;
194 * Stream open closure
199 * Stream open callback
201 GNUNET_STREAM_OpenCallback open_cb;
204 * The current transmit handle (if a pending transmit request exists)
206 struct GNUNET_MESH_TransmitHandle *transmit_handle;
209 * The current message associated with the transmit handle
211 struct MessageQueue *queue_head;
214 * The queue tail, should always point to the last message in queue
216 struct MessageQueue *queue_tail;
219 * The write IO_handle associated with this socket
221 struct GNUNET_STREAM_IOWriteHandle *write_handle;
224 * The read IO_handle associated with this socket
226 struct GNUNET_STREAM_IOReadHandle *read_handle;
229 * Buffer for storing received messages
231 void *receive_buffer;
234 * The listen socket from which this socket is derived. Should be NULL if it
235 * is not a derived socket
237 struct GNUNET_STREAM_ListenSocket *lsocket;
240 * Task identifier for the read io timeout task
242 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task;
245 * Task identifier for retransmission task after timeout
247 GNUNET_SCHEDULER_TaskIdentifier retransmission_timeout_task_id;
250 * The task for sending timely Acks
252 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
255 * Task scheduled to continue a read operation.
257 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
260 * The state of the protocol associated with this socket
265 * The status of the socket
267 enum GNUNET_STREAM_Status status;
270 * The number of previous timeouts; FIXME: currently not used
272 unsigned int retries;
275 * The peer identity of the peer at the other end of the stream
277 GNUNET_PEER_Id other_peer;
280 * Our Peer Identity (for debugging)
282 GNUNET_PEER_Id our_id;
285 * The application port number (type: uint32_t)
287 GNUNET_MESH_ApplicationType app_port;
290 * The session id associated with this stream connection
291 * FIXME: Not used currently, may be removed
296 * Write sequence number. Set to random when sending HELLO(client) and
299 uint32_t write_sequence_number;
302 * Read sequence number. This number's value is determined during handshake
304 uint32_t read_sequence_number;
307 * The receiver buffer size
309 uint32_t receive_buffer_size;
312 * The receiver buffer boundaries
314 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
317 * receiver's available buffer after the last acknowledged packet
319 uint32_t receiver_window_available;
322 * The offset pointer used during write operation
324 uint32_t write_offset;
327 * The offset after which we are expecting data
329 uint32_t read_offset;
332 * The offset upto which user has read from the received buffer
334 uint32_t copy_offset;
339 * A socket for listening
341 struct GNUNET_STREAM_ListenSocket
346 struct GNUNET_MESH_Handle *mesh;
349 * The callback function which is called after successful opening socket
351 GNUNET_STREAM_ListenCallback listen_cb;
354 * The call back closure
359 * Our interned Peer's identity
361 GNUNET_PEER_Id our_id;
365 * FIXME: Remove if not required!
367 GNUNET_MESH_ApplicationType port;
372 * The IO Write Handle
374 struct GNUNET_STREAM_IOWriteHandle
377 * The packet_buffers associated with this Handle
379 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
382 * The write continuation callback
384 GNUNET_STREAM_CompletionContinuation write_cont;
387 * Write continuation closure
389 void *write_cont_cls;
392 * The bitmap of this IOHandle; Corresponding bit for a message is set when
393 * it has been acknowledged by the receiver
395 GNUNET_STREAM_AckBitmap ack_bitmap;
398 * Number of bytes in this write handle
403 * Number of packets sent before waiting for an ack
405 * FIXME: Do we need this?
407 unsigned int sent_packets;
414 struct GNUNET_STREAM_IOReadHandle
417 * Callback for the read processor
419 GNUNET_STREAM_DataProcessor proc;
422 * The closure pointer for the read processor callback
429 * Default value in seconds for various timeouts
431 static unsigned int default_timeout = 10;
434 * Callback function for sending hello message
436 * @param cls closure the socket
437 * @param size number of bytes available in buf
438 * @param buf where the callee should write the message
439 * @return number of bytes written to buf
442 send_message_notify (void *cls, size_t size, void *buf)
444 struct GNUNET_STREAM_Socket *socket = cls;
445 struct GNUNET_PeerIdentity target;
446 struct MessageQueue *head;
449 socket->transmit_handle = NULL; /* Remove the transmit handle */
450 head = socket->queue_head;
452 return 0; /* just to be safe */
453 GNUNET_PEER_resolve (socket->other_peer, &target);
454 if (0 == size) /* request timed out */
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Message sending timed out. Retry %d \n",
460 socket->transmit_handle =
461 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
464 /* FIXME: exponential backoff */
465 socket->retransmit_timeout,
467 ntohs (head->message->header.size),
468 &send_message_notify,
473 ret = ntohs (head->message->header.size);
474 GNUNET_assert (size >= ret);
475 memcpy (buf, head->message, ret);
476 if (NULL != head->finish_cb)
478 head->finish_cb (head->finish_cb_cls, socket);
480 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
483 GNUNET_free (head->message);
485 head = socket->queue_head;
486 if (NULL != head) /* more pending messages to send */
489 socket->transmit_handle =
490 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
493 /* FIXME: exponential backoff */
494 socket->retransmit_timeout,
496 ntohs (head->message->header.size),
497 &send_message_notify,
505 * Queues a message for sending using the mesh connection of a socket
507 * @param socket the socket whose mesh connection is used
508 * @param message the message to be sent
509 * @param finish_cb the callback to be called when the message is sent
510 * @param finish_cb_cls the closure for the callback
513 queue_message (struct GNUNET_STREAM_Socket *socket,
514 struct GNUNET_STREAM_MessageHeader *message,
515 SendFinishCallback finish_cb,
518 struct MessageQueue *queue_entity;
519 struct GNUNET_PeerIdentity target;
522 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
523 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "%x: Queueing message of type %d and size %d\n",
528 ntohs (message->header.type),
529 ntohs (message->header.size));
530 GNUNET_assert (NULL != message);
531 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
532 queue_entity->message = message;
533 queue_entity->finish_cb = finish_cb;
534 queue_entity->finish_cb_cls = finish_cb_cls;
535 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
538 if (NULL == socket->transmit_handle)
541 GNUNET_PEER_resolve (socket->other_peer, &target);
542 socket->transmit_handle =
543 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
546 socket->retransmit_timeout,
548 ntohs (message->header.size),
549 &send_message_notify,
556 * Copies a message and queues it for sending using the mesh connection of
559 * @param socket the socket whose mesh connection is used
560 * @param message the message to be sent
561 * @param finish_cb the callback to be called when the message is sent
562 * @param finish_cb_cls the closure for the callback
565 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
566 const struct GNUNET_STREAM_MessageHeader *message,
567 SendFinishCallback finish_cb,
570 struct GNUNET_STREAM_MessageHeader *msg_copy;
573 size = ntohs (message->header.size);
574 msg_copy = GNUNET_malloc (size);
575 memcpy (msg_copy, message, size);
576 queue_message (socket, msg_copy, finish_cb, finish_cb_cls);
581 * Callback function for sending ack message
583 * @param cls closure the ACK message created in ack_task
584 * @param size number of bytes available in buffer
585 * @param buf where the callee should write the message
586 * @return number of bytes written to buf
589 send_ack_notify (void *cls, size_t size, void *buf)
591 struct GNUNET_STREAM_AckMessage *ack_msg = cls;
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
596 "%s called with size 0\n", __func__);
599 GNUNET_assert (ntohs (ack_msg->header.header.size) <= size);
601 size = ntohs (ack_msg->header.header.size);
602 memcpy (buf, ack_msg, size);
607 * Writes data using the given socket. The amount of data written is limited by
608 * the receiver_window_size
610 * @param socket the socket to use
613 write_data (struct GNUNET_STREAM_Socket *socket);
616 * Task for retransmitting data messages if they aren't ACK before their ack
619 * @param cls the socket
620 * @param tc the Task context
623 retransmission_timeout_task (void *cls,
624 const struct GNUNET_SCHEDULER_TaskContext *tc)
626 struct GNUNET_STREAM_Socket *socket = cls;
628 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "%x: Retransmitting DATA...\n", socket->our_id);
633 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
639 * Task for sending ACK message
641 * @param cls the socket
642 * @param tc the Task context
646 const struct GNUNET_SCHEDULER_TaskContext *tc)
648 struct GNUNET_STREAM_Socket *socket = cls;
649 struct GNUNET_STREAM_AckMessage *ack_msg;
650 struct GNUNET_PeerIdentity target;
652 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
657 socket->ack_task_id = 0;
659 /* Create the ACK Message */
660 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
661 ack_msg->header.header.size = htons (sizeof (struct
662 GNUNET_STREAM_AckMessage));
663 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
664 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
665 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
666 ack_msg->receive_window_remaining =
667 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
669 GNUNET_PEER_resolve (socket->other_peer, &target);
670 /* Request MESH for sending ACK */
671 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
674 socket->retransmit_timeout,
676 ntohs (ack_msg->header.header.size),
685 * Function to modify a bit in GNUNET_STREAM_AckBitmap
687 * @param bitmap the bitmap to modify
688 * @param bit the bit number to modify
689 * @param value GNUNET_YES to on, GNUNET_NO to off
692 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
696 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
697 if (GNUNET_YES == value)
698 *bitmap |= (1LL << bit);
700 *bitmap &= ~(1LL << bit);
705 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
707 * @param bitmap address of the bitmap that has to be checked
708 * @param bit the bit number to check
709 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
712 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
715 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
716 return 0 != (*bitmap & (1LL << bit));
722 * Function called when Data Message is sent
724 * @param cls the io_handle corresponding to the Data Message
725 * @param socket the socket which was used
728 write_data_finish_cb (void *cls,
729 struct GNUNET_STREAM_Socket *socket)
731 struct GNUNET_STREAM_IOWriteHandle *io_handle = cls;
733 io_handle->sent_packets++;
738 * Writes data using the given socket. The amount of data written is limited by
739 * the receiver_window_size
741 * @param socket the socket to use
744 write_data (struct GNUNET_STREAM_Socket *socket)
746 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
747 int packet; /* Although an int, should never be negative */
751 /* Find the last acknowledged packet */
752 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
754 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
757 else if (NULL == io_handle->messages[packet])
760 /* Resend packets which weren't ack'ed */
761 for (packet=0; packet < ack_packet; packet++)
763 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "%x: Placing DATA message with sequence %u in send queue\n",
769 ntohl (io_handle->messages[packet]->sequence_number));
771 copy_and_queue_message (socket,
772 &io_handle->messages[packet]->header,
777 packet = ack_packet + 1;
778 /* Now send new packets if there is enough buffer space */
779 while ( (NULL != io_handle->messages[packet]) &&
780 (socket->receiver_window_available >= ntohs (io_handle->messages[packet]->header.header.size)) )
782 socket->receiver_window_available -=
783 ntohs (io_handle->messages[packet]->header.header.size);
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "%x: Placing DATA message with sequence %u in send queue\n",
787 ntohl (io_handle->messages[packet]->sequence_number));
788 copy_and_queue_message (socket,
789 &io_handle->messages[packet]->header,
790 &write_data_finish_cb,
795 if (GNUNET_SCHEDULER_NO_TASK == socket->retransmission_timeout_task_id)
796 socket->retransmission_timeout_task_id =
797 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
798 (GNUNET_TIME_UNIT_SECONDS, 5),
799 &retransmission_timeout_task,
805 * Task for calling the read processor
807 * @param cls the socket
808 * @param tc the task context
811 call_read_processor (void *cls,
812 const struct GNUNET_SCHEDULER_TaskContext *tc)
814 struct GNUNET_STREAM_Socket *socket = cls;
816 size_t valid_read_size;
818 uint32_t sequence_increase;
819 uint32_t offset_increase;
821 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
822 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
825 GNUNET_assert (NULL != socket->read_handle);
826 GNUNET_assert (NULL != socket->read_handle->proc);
828 /* Check the bitmap for any holes */
829 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
831 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
835 /* We only call read processor if we have the first packet */
836 GNUNET_assert (0 < packet);
839 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
841 GNUNET_assert (0 != valid_read_size);
843 /* Cancel the read_io_timeout_task */
844 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task);
845 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
847 /* Call the data processor */
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "%x: Calling read processor\n",
852 socket->read_handle->proc (socket->read_handle->proc_cls,
854 socket->receive_buffer + socket->copy_offset,
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "%x: Read processor read %d bytes\n",
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "%x: Read processor completed successfully\n",
864 /* Free the read handle */
865 GNUNET_free (socket->read_handle);
866 socket->read_handle = NULL;
868 GNUNET_assert (read_size <= valid_read_size);
869 socket->copy_offset += read_size;
871 /* Determine upto which packet we can remove from the buffer */
872 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
874 if (socket->copy_offset = socket->receive_buffer_boundaries[packet])
876 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
880 /* If no packets can be removed we can't move the buffer */
881 if (0 == packet) return;
883 sequence_increase = packet;
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 "%x: Sequence increase after read processor completion: %u\n",
889 /* Shift the data in the receive buffer */
890 memmove (socket->receive_buffer,
891 socket->receive_buffer
892 + socket->receive_buffer_boundaries[sequence_increase-1],
893 socket->receive_buffer_size - socket->receive_buffer_boundaries[sequence_increase-1]);
895 /* Shift the bitmap */
896 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
898 /* Set read_sequence_number */
899 socket->read_sequence_number += sequence_increase;
901 /* Set read_offset */
902 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
903 socket->read_offset += offset_increase;
905 /* Fix copy_offset */
906 GNUNET_assert (offset_increase <= socket->copy_offset);
907 socket->copy_offset -= offset_increase;
909 /* Fix relative boundaries */
910 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
912 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
914 socket->receive_buffer_boundaries[packet] =
915 socket->receive_buffer_boundaries[packet + sequence_increase]
919 socket->receive_buffer_boundaries[packet] = 0;
925 * Cancels the existing read io handle
927 * @param cls the closure from the SCHEDULER call
928 * @param tc the task context
931 read_io_timeout (void *cls,
932 const struct GNUNET_SCHEDULER_TaskContext *tc)
934 struct GNUNET_STREAM_Socket *socket = cls;
935 GNUNET_STREAM_DataProcessor proc;
938 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
939 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "%x: Read task timedout - Cancelling it\n",
944 GNUNET_SCHEDULER_cancel (socket->read_task_id);
945 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
947 GNUNET_assert (NULL != socket->read_handle);
948 proc = socket->read_handle->proc;
949 proc_cls = socket->read_handle->proc_cls;
951 GNUNET_free (socket->read_handle);
952 socket->read_handle = NULL;
953 /* Call the read processor to signal timeout */
955 GNUNET_STREAM_TIMEOUT,
962 * Handler for DATA messages; Same for both client and server
964 * @param socket the socket through which the ack was received
965 * @param tunnel connection to the other end
966 * @param sender who sent the message
967 * @param msg the data message
968 * @param atsi performance data for the connection
969 * @return GNUNET_OK to keep the connection open,
970 * GNUNET_SYSERR to close it (signal serious error)
973 handle_data (struct GNUNET_STREAM_Socket *socket,
974 struct GNUNET_MESH_Tunnel *tunnel,
975 const struct GNUNET_PeerIdentity *sender,
976 const struct GNUNET_STREAM_DataMessage *msg,
977 const struct GNUNET_ATS_Information*atsi)
980 uint32_t bytes_needed;
981 uint32_t relative_offset;
982 uint32_t relative_sequence_number;
985 size = htons (msg->header.header.size);
986 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
989 return GNUNET_SYSERR;
992 if (GNUNET_PEER_search (sender) != socket->other_peer)
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "%x: Received DATA from non-confirming peer\n",
1000 switch (socket->state)
1002 case STATE_ESTABLISHED:
1003 case STATE_TRANSMIT_CLOSED:
1004 case STATE_TRANSMIT_CLOSE_WAIT:
1006 /* check if the message's sequence number is in the range we are
1008 relative_sequence_number =
1009 ntohl (msg->sequence_number) - socket->read_sequence_number;
1010 if ( relative_sequence_number > GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 "%x: Ignoring received message with sequence number %u\n",
1015 ntohl (msg->sequence_number));
1016 /* Start ACK sending task if one is not already present */
1017 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1019 socket->ack_task_id =
1020 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1021 (msg->ack_deadline),
1028 /* Check if we have already seen this message */
1029 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1030 relative_sequence_number))
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "%x: Ignoring already received message with sequence "
1036 ntohl (msg->sequence_number));
1037 /* Start ACK sending task if one is not already present */
1038 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1040 socket->ack_task_id =
1041 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1042 (msg->ack_deadline),
1049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1050 "%x: Receiving DATA with sequence number: %u and size: %d "
1053 ntohl (msg->sequence_number),
1054 ntohs (msg->header.header.size),
1055 socket->other_peer);
1057 /* Check if we have to allocate the buffer */
1058 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1059 relative_offset = ntohl (msg->offset) - socket->read_offset;
1060 bytes_needed = relative_offset + size;
1061 if (bytes_needed > socket->receive_buffer_size)
1063 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1065 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1067 socket->receive_buffer_size = bytes_needed;
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "%x: Cannot accommodate packet %d as buffer is",
1075 ntohl (msg->sequence_number));
1080 /* Copy Data to buffer */
1082 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1083 memcpy (socket->receive_buffer + relative_offset,
1086 socket->receive_buffer_boundaries[relative_sequence_number] =
1087 relative_offset + size;
1089 /* Modify the ACK bitmap */
1090 ackbitmap_modify_bit (&socket->ack_bitmap,
1091 relative_sequence_number,
1094 /* Start ACK sending task if one is not already present */
1095 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1097 socket->ack_task_id =
1098 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1099 (msg->ack_deadline),
1104 if ((NULL != socket->read_handle) /* A read handle is waiting */
1105 /* There is no current read task */
1106 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
1107 /* We have the first packet */
1108 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "%x: Scheduling read processor\n",
1115 socket->read_task_id =
1116 GNUNET_SCHEDULER_add_now (&call_read_processor,
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "%x: Received data message when it cannot be handled\n",
1132 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1134 * @param cls the socket (set from GNUNET_MESH_connect)
1135 * @param tunnel connection to the other end
1136 * @param tunnel_ctx place to store local state associated with the tunnel
1137 * @param sender who sent the message
1138 * @param message the actual message
1139 * @param atsi performance data for the connection
1140 * @return GNUNET_OK to keep the connection open,
1141 * GNUNET_SYSERR to close it (signal serious error)
1144 client_handle_data (void *cls,
1145 struct GNUNET_MESH_Tunnel *tunnel,
1147 const struct GNUNET_PeerIdentity *sender,
1148 const struct GNUNET_MessageHeader *message,
1149 const struct GNUNET_ATS_Information*atsi)
1151 struct GNUNET_STREAM_Socket *socket = cls;
1153 return handle_data (socket,
1156 (const struct GNUNET_STREAM_DataMessage *) message,
1162 * Callback to set state to ESTABLISHED
1164 * @param cls the closure from queue_message FIXME: document
1165 * @param socket the socket to requiring state change
1168 set_state_established (void *cls,
1169 struct GNUNET_STREAM_Socket *socket)
1171 struct GNUNET_PeerIdentity initiator_pid;
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "%x: Attaining ESTABLISHED state\n",
1176 socket->write_offset = 0;
1177 socket->read_offset = 0;
1178 socket->state = STATE_ESTABLISHED;
1179 /* FIXME: What if listen_cb is NULL */
1180 if (NULL != socket->lsocket)
1182 GNUNET_PEER_resolve (socket->other_peer, &initiator_pid);
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "%x: Calling listen callback\n",
1186 if (GNUNET_SYSERR ==
1187 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1191 socket->state = STATE_CLOSED;
1192 /* FIXME: We should close in a decent way */
1193 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1194 GNUNET_free (socket);
1197 else if (socket->open_cb)
1198 socket->open_cb (socket->open_cls, socket);
1203 * Callback to set state to HELLO_WAIT
1205 * @param cls the closure from queue_message
1206 * @param socket the socket to requiring state change
1209 set_state_hello_wait (void *cls,
1210 struct GNUNET_STREAM_Socket *socket)
1212 GNUNET_assert (STATE_INIT == socket->state);
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214 "%x: Attaining HELLO_WAIT state\n",
1216 socket->state = STATE_HELLO_WAIT;
1221 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1223 * @param cls the socket (set from GNUNET_MESH_connect)
1224 * @param tunnel connection to the other end
1225 * @param tunnel_ctx this is NULL
1226 * @param sender who sent the message
1227 * @param message the actual message
1228 * @param atsi performance data for the connection
1229 * @return GNUNET_OK to keep the connection open,
1230 * GNUNET_SYSERR to close it (signal serious error)
1233 client_handle_hello_ack (void *cls,
1234 struct GNUNET_MESH_Tunnel *tunnel,
1236 const struct GNUNET_PeerIdentity *sender,
1237 const struct GNUNET_MessageHeader *message,
1238 const struct GNUNET_ATS_Information*atsi)
1240 struct GNUNET_STREAM_Socket *socket = cls;
1241 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1242 struct GNUNET_STREAM_HelloAckMessage *reply;
1244 if (GNUNET_PEER_search (sender) != socket->other_peer)
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "%x: Received HELLO_ACK from non-confirming peer\n",
1251 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "%x: Received HELLO_ACK from %x\n",
1255 socket->other_peer);
1257 GNUNET_assert (socket->tunnel == tunnel);
1258 switch (socket->state)
1260 case STATE_HELLO_WAIT:
1261 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1263 "%x: Read sequence number %u\n",
1265 (unsigned int) socket->read_sequence_number);
1266 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1267 /* Get the random sequence number */
1268 socket->write_sequence_number =
1269 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 "%x: Generated write sequence number %u\n",
1273 (unsigned int) socket->write_sequence_number);
1275 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1276 reply->header.header.size =
1277 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1278 reply->header.header.type =
1279 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1280 reply->sequence_number = htonl (socket->write_sequence_number);
1281 reply->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1282 queue_message (socket,
1284 &set_state_established,
1287 case STATE_ESTABLISHED:
1288 case STATE_RECEIVE_CLOSE_WAIT:
1289 // call statistics (# ACKs ignored++)
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 "%x: Server %x sent HELLO_ACK when in state %d\n",
1298 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1299 return GNUNET_SYSERR;
1306 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1308 * @param cls the socket (set from GNUNET_MESH_connect)
1309 * @param tunnel connection to the other end
1310 * @param tunnel_ctx this is NULL
1311 * @param sender who sent the message
1312 * @param message the actual message
1313 * @param atsi performance data for the connection
1314 * @return GNUNET_OK to keep the connection open,
1315 * GNUNET_SYSERR to close it (signal serious error)
1318 client_handle_reset (void *cls,
1319 struct GNUNET_MESH_Tunnel *tunnel,
1321 const struct GNUNET_PeerIdentity *sender,
1322 const struct GNUNET_MessageHeader *message,
1323 const struct GNUNET_ATS_Information*atsi)
1325 struct GNUNET_STREAM_Socket *socket = cls;
1332 * Common message handler for handling TRANSMIT_CLOSE messages
1334 * @param socket the socket through which the ack was received
1335 * @param tunnel connection to the other end
1336 * @param sender who sent the message
1337 * @param msg the transmit close message
1338 * @param atsi performance data for the connection
1339 * @return GNUNET_OK to keep the connection open,
1340 * GNUNET_SYSERR to close it (signal serious error)
1343 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1344 struct GNUNET_MESH_Tunnel *tunnel,
1345 const struct GNUNET_PeerIdentity *sender,
1346 const struct GNUNET_STREAM_MessageHeader *msg,
1347 const struct GNUNET_ATS_Information*atsi)
1349 struct GNUNET_STREAM_MessageHeader *reply;
1351 switch (socket->state)
1353 case STATE_ESTABLISHED:
1354 socket->state = STATE_RECEIVE_CLOSED;
1356 /* Send TRANSMIT_CLOSE_ACK */
1357 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1358 reply->header.type =
1359 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1360 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1361 queue_message (socket, reply, NULL, NULL);
1365 /* FIXME: Call statistics? */
1373 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1375 * @param cls the socket (set from GNUNET_MESH_connect)
1376 * @param tunnel connection to the other end
1377 * @param tunnel_ctx this is NULL
1378 * @param sender who sent the message
1379 * @param message the actual message
1380 * @param atsi performance data for the connection
1381 * @return GNUNET_OK to keep the connection open,
1382 * GNUNET_SYSERR to close it (signal serious error)
1385 client_handle_transmit_close (void *cls,
1386 struct GNUNET_MESH_Tunnel *tunnel,
1388 const struct GNUNET_PeerIdentity *sender,
1389 const struct GNUNET_MessageHeader *message,
1390 const struct GNUNET_ATS_Information*atsi)
1392 struct GNUNET_STREAM_Socket *socket = cls;
1394 return handle_transmit_close (socket,
1397 (struct GNUNET_STREAM_MessageHeader *)message,
1403 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1405 * @param cls the socket (set from GNUNET_MESH_connect)
1406 * @param tunnel connection to the other end
1407 * @param tunnel_ctx this is NULL
1408 * @param sender who sent the message
1409 * @param message the actual message
1410 * @param atsi performance data for the connection
1411 * @return GNUNET_OK to keep the connection open,
1412 * GNUNET_SYSERR to close it (signal serious error)
1415 client_handle_transmit_close_ack (void *cls,
1416 struct GNUNET_MESH_Tunnel *tunnel,
1418 const struct GNUNET_PeerIdentity *sender,
1419 const struct GNUNET_MessageHeader *message,
1420 const struct GNUNET_ATS_Information*atsi)
1422 struct GNUNET_STREAM_Socket *socket = cls;
1429 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1431 * @param cls the socket (set from GNUNET_MESH_connect)
1432 * @param tunnel connection to the other end
1433 * @param tunnel_ctx this is NULL
1434 * @param sender who sent the message
1435 * @param message the actual message
1436 * @param atsi performance data for the connection
1437 * @return GNUNET_OK to keep the connection open,
1438 * GNUNET_SYSERR to close it (signal serious error)
1441 client_handle_receive_close (void *cls,
1442 struct GNUNET_MESH_Tunnel *tunnel,
1444 const struct GNUNET_PeerIdentity *sender,
1445 const struct GNUNET_MessageHeader *message,
1446 const struct GNUNET_ATS_Information*atsi)
1448 struct GNUNET_STREAM_Socket *socket = cls;
1455 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1457 * @param cls the socket (set from GNUNET_MESH_connect)
1458 * @param tunnel connection to the other end
1459 * @param tunnel_ctx this is NULL
1460 * @param sender who sent the message
1461 * @param message the actual message
1462 * @param atsi performance data for the connection
1463 * @return GNUNET_OK to keep the connection open,
1464 * GNUNET_SYSERR to close it (signal serious error)
1467 client_handle_receive_close_ack (void *cls,
1468 struct GNUNET_MESH_Tunnel *tunnel,
1470 const struct GNUNET_PeerIdentity *sender,
1471 const struct GNUNET_MessageHeader *message,
1472 const struct GNUNET_ATS_Information*atsi)
1474 struct GNUNET_STREAM_Socket *socket = cls;
1481 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1483 * @param cls the socket (set from GNUNET_MESH_connect)
1484 * @param tunnel connection to the other end
1485 * @param tunnel_ctx this is NULL
1486 * @param sender who sent the message
1487 * @param message the actual message
1488 * @param atsi performance data for the connection
1489 * @return GNUNET_OK to keep the connection open,
1490 * GNUNET_SYSERR to close it (signal serious error)
1493 client_handle_close (void *cls,
1494 struct GNUNET_MESH_Tunnel *tunnel,
1496 const struct GNUNET_PeerIdentity *sender,
1497 const struct GNUNET_MessageHeader *message,
1498 const struct GNUNET_ATS_Information*atsi)
1500 struct GNUNET_STREAM_Socket *socket = cls;
1507 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1509 * @param cls the socket (set from GNUNET_MESH_connect)
1510 * @param tunnel connection to the other end
1511 * @param tunnel_ctx this is NULL
1512 * @param sender who sent the message
1513 * @param message the actual message
1514 * @param atsi performance data for the connection
1515 * @return GNUNET_OK to keep the connection open,
1516 * GNUNET_SYSERR to close it (signal serious error)
1519 client_handle_close_ack (void *cls,
1520 struct GNUNET_MESH_Tunnel *tunnel,
1522 const struct GNUNET_PeerIdentity *sender,
1523 const struct GNUNET_MessageHeader *message,
1524 const struct GNUNET_ATS_Information*atsi)
1526 struct GNUNET_STREAM_Socket *socket = cls;
1531 /*****************************/
1532 /* Server's Message Handlers */
1533 /*****************************/
1536 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1538 * @param cls the closure
1539 * @param tunnel connection to the other end
1540 * @param tunnel_ctx the socket
1541 * @param sender who sent the message
1542 * @param message the actual message
1543 * @param atsi performance data for the connection
1544 * @return GNUNET_OK to keep the connection open,
1545 * GNUNET_SYSERR to close it (signal serious error)
1548 server_handle_data (void *cls,
1549 struct GNUNET_MESH_Tunnel *tunnel,
1551 const struct GNUNET_PeerIdentity *sender,
1552 const struct GNUNET_MessageHeader *message,
1553 const struct GNUNET_ATS_Information*atsi)
1555 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1557 return handle_data (socket,
1560 (const struct GNUNET_STREAM_DataMessage *)message,
1566 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1568 * @param cls the closure
1569 * @param tunnel connection to the other end
1570 * @param tunnel_ctx the socket
1571 * @param sender who sent the message
1572 * @param message the actual message
1573 * @param atsi performance data for the connection
1574 * @return GNUNET_OK to keep the connection open,
1575 * GNUNET_SYSERR to close it (signal serious error)
1578 server_handle_hello (void *cls,
1579 struct GNUNET_MESH_Tunnel *tunnel,
1581 const struct GNUNET_PeerIdentity *sender,
1582 const struct GNUNET_MessageHeader *message,
1583 const struct GNUNET_ATS_Information*atsi)
1585 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1586 struct GNUNET_STREAM_HelloAckMessage *reply;
1588 if (GNUNET_PEER_search (sender) != socket->other_peer)
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "%x: Received HELLO from non-confirming peer\n",
1596 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO ==
1597 ntohs (message->type));
1598 GNUNET_assert (socket->tunnel == tunnel);
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "%x: Received HELLO from %x\n",
1602 socket->other_peer);
1604 if (STATE_INIT == socket->state)
1606 /* Get the random sequence number */
1607 socket->write_sequence_number =
1608 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610 "%x: Generated write sequence number %u\n",
1612 (unsigned int) socket->write_sequence_number);
1614 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1615 reply->header.header.size =
1616 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1617 reply->header.header.type =
1618 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1619 reply->sequence_number = htonl (socket->write_sequence_number);
1620 reply->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1621 queue_message (socket,
1623 &set_state_hello_wait,
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1629 "Client sent HELLO when in state %d\n", socket->state);
1630 /* FIXME: Send RESET? */
1638 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1640 * @param cls the closure
1641 * @param tunnel connection to the other end
1642 * @param tunnel_ctx the socket
1643 * @param sender who sent the message
1644 * @param message the actual message
1645 * @param atsi performance data for the connection
1646 * @return GNUNET_OK to keep the connection open,
1647 * GNUNET_SYSERR to close it (signal serious error)
1650 server_handle_hello_ack (void *cls,
1651 struct GNUNET_MESH_Tunnel *tunnel,
1653 const struct GNUNET_PeerIdentity *sender,
1654 const struct GNUNET_MessageHeader *message,
1655 const struct GNUNET_ATS_Information*atsi)
1657 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1658 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1660 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
1661 ntohs (message->type));
1662 GNUNET_assert (socket->tunnel == tunnel);
1663 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1664 if (STATE_HELLO_WAIT == socket->state)
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1667 "%x: Received HELLO_ACK from %x\n",
1669 socket->other_peer);
1670 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1672 "%x: Read sequence number %u\n",
1674 (unsigned int) socket->read_sequence_number);
1675 socket->receiver_window_available =
1676 ntohl (ack_message->receiver_window_size);
1677 /* Attain ESTABLISHED state */
1678 set_state_established (NULL, socket);
1682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1683 "Client sent HELLO_ACK when in state %d\n", socket->state);
1684 /* FIXME: Send RESET? */
1692 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1694 * @param cls the closure
1695 * @param tunnel connection to the other end
1696 * @param tunnel_ctx the socket
1697 * @param sender who sent the message
1698 * @param message the actual message
1699 * @param atsi performance data for the connection
1700 * @return GNUNET_OK to keep the connection open,
1701 * GNUNET_SYSERR to close it (signal serious error)
1704 server_handle_reset (void *cls,
1705 struct GNUNET_MESH_Tunnel *tunnel,
1707 const struct GNUNET_PeerIdentity *sender,
1708 const struct GNUNET_MessageHeader *message,
1709 const struct GNUNET_ATS_Information*atsi)
1711 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1718 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1720 * @param cls the closure
1721 * @param tunnel connection to the other end
1722 * @param tunnel_ctx the socket
1723 * @param sender who sent the message
1724 * @param message the actual message
1725 * @param atsi performance data for the connection
1726 * @return GNUNET_OK to keep the connection open,
1727 * GNUNET_SYSERR to close it (signal serious error)
1730 server_handle_transmit_close (void *cls,
1731 struct GNUNET_MESH_Tunnel *tunnel,
1733 const struct GNUNET_PeerIdentity *sender,
1734 const struct GNUNET_MessageHeader *message,
1735 const struct GNUNET_ATS_Information*atsi)
1737 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1739 return handle_transmit_close (socket,
1742 (struct GNUNET_STREAM_MessageHeader *)message,
1748 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1750 * @param cls the closure
1751 * @param tunnel connection to the other end
1752 * @param tunnel_ctx the socket
1753 * @param sender who sent the message
1754 * @param message the actual message
1755 * @param atsi performance data for the connection
1756 * @return GNUNET_OK to keep the connection open,
1757 * GNUNET_SYSERR to close it (signal serious error)
1760 server_handle_transmit_close_ack (void *cls,
1761 struct GNUNET_MESH_Tunnel *tunnel,
1763 const struct GNUNET_PeerIdentity *sender,
1764 const struct GNUNET_MessageHeader *message,
1765 const struct GNUNET_ATS_Information*atsi)
1767 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1774 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1776 * @param cls the closure
1777 * @param tunnel connection to the other end
1778 * @param tunnel_ctx the socket
1779 * @param sender who sent the message
1780 * @param message the actual message
1781 * @param atsi performance data for the connection
1782 * @return GNUNET_OK to keep the connection open,
1783 * GNUNET_SYSERR to close it (signal serious error)
1786 server_handle_receive_close (void *cls,
1787 struct GNUNET_MESH_Tunnel *tunnel,
1789 const struct GNUNET_PeerIdentity *sender,
1790 const struct GNUNET_MessageHeader *message,
1791 const struct GNUNET_ATS_Information*atsi)
1793 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1800 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1802 * @param cls the closure
1803 * @param tunnel connection to the other end
1804 * @param tunnel_ctx the socket
1805 * @param sender who sent the message
1806 * @param message the actual message
1807 * @param atsi performance data for the connection
1808 * @return GNUNET_OK to keep the connection open,
1809 * GNUNET_SYSERR to close it (signal serious error)
1812 server_handle_receive_close_ack (void *cls,
1813 struct GNUNET_MESH_Tunnel *tunnel,
1815 const struct GNUNET_PeerIdentity *sender,
1816 const struct GNUNET_MessageHeader *message,
1817 const struct GNUNET_ATS_Information*atsi)
1819 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1826 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1828 * @param cls the closure
1829 * @param tunnel connection to the other end
1830 * @param tunnel_ctx the socket
1831 * @param sender who sent the message
1832 * @param message the actual message
1833 * @param atsi performance data for the connection
1834 * @return GNUNET_OK to keep the connection open,
1835 * GNUNET_SYSERR to close it (signal serious error)
1838 server_handle_close (void *cls,
1839 struct GNUNET_MESH_Tunnel *tunnel,
1841 const struct GNUNET_PeerIdentity *sender,
1842 const struct GNUNET_MessageHeader *message,
1843 const struct GNUNET_ATS_Information*atsi)
1845 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1852 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1854 * @param cls the closure
1855 * @param tunnel connection to the other end
1856 * @param tunnel_ctx the socket
1857 * @param sender who sent the message
1858 * @param message the actual message
1859 * @param atsi performance data for the connection
1860 * @return GNUNET_OK to keep the connection open,
1861 * GNUNET_SYSERR to close it (signal serious error)
1864 server_handle_close_ack (void *cls,
1865 struct GNUNET_MESH_Tunnel *tunnel,
1867 const struct GNUNET_PeerIdentity *sender,
1868 const struct GNUNET_MessageHeader *message,
1869 const struct GNUNET_ATS_Information*atsi)
1871 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1878 * Message Handler for mesh
1880 * @param socket the socket through which the ack was received
1881 * @param tunnel connection to the other end
1882 * @param sender who sent the message
1883 * @param ack the acknowledgment message
1884 * @param atsi performance data for the connection
1885 * @return GNUNET_OK to keep the connection open,
1886 * GNUNET_SYSERR to close it (signal serious error)
1889 handle_ack (struct GNUNET_STREAM_Socket *socket,
1890 struct GNUNET_MESH_Tunnel *tunnel,
1891 const struct GNUNET_PeerIdentity *sender,
1892 const struct GNUNET_STREAM_AckMessage *ack,
1893 const struct GNUNET_ATS_Information*atsi)
1895 unsigned int packet;
1896 int need_retransmission;
1899 if (GNUNET_PEER_search (sender) != socket->other_peer)
1901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1902 "%x: Received ACK from non-confirming peer\n",
1907 switch (socket->state)
1909 case (STATE_ESTABLISHED):
1910 if (NULL == socket->write_handle)
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1913 "%x: Received DATA_ACK when write_handle is NULL\n",
1917 /* FIXME: increment in the base sequence number is breaking current flow
1919 if (!((socket->write_sequence_number
1920 - ntohl (ack->base_sequence_number)) < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1923 "%x: Received DATA_ACK with unexpected base sequence "
1926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1927 "%x: Current write sequence: %u; Ack's base sequence: %u\n",
1929 socket->write_sequence_number,
1930 ntohl (ack->base_sequence_number));
1933 /* FIXME: include the case when write_handle is cancelled - ignore the
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1937 "%x: Received DATA_ACK from %x\n",
1939 socket->other_peer);
1941 /* Cancel the retransmission task */
1942 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
1944 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
1945 socket->retransmission_timeout_task_id =
1946 GNUNET_SCHEDULER_NO_TASK;
1949 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1951 if (NULL == socket->write_handle->messages[packet]) break;
1952 if (ntohl (ack->base_sequence_number)
1953 >= ntohl (socket->write_handle->messages[packet]->sequence_number))
1954 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
1959 ackbitmap_is_bit_set (&socket->write_handle->ack_bitmap,
1960 ntohl (socket->write_handle->messages[packet]->sequence_number)
1961 - ntohl (ack->base_sequence_number)))
1962 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
1967 /* Update the receive window remaining
1968 FIXME : Should update with the value from a data ack with greater
1970 socket->receiver_window_available =
1971 ntohl (ack->receive_window_remaining);
1973 /* Check if we have received all acknowledgements */
1974 need_retransmission = GNUNET_NO;
1975 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1977 if (NULL == socket->write_handle->messages[packet]) break;
1978 if (GNUNET_YES != ackbitmap_is_bit_set
1979 (&socket->write_handle->ack_bitmap,packet))
1981 need_retransmission = GNUNET_YES;
1985 if (GNUNET_YES == need_retransmission)
1987 write_data (socket);
1989 else /* We have to call the write continuation callback now */
1991 /* Free the packets */
1992 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1994 GNUNET_free_non_null (socket->write_handle->messages[packet]);
1996 if (NULL != socket->write_handle->write_cont)
1997 socket->write_handle->write_cont
1998 (socket->write_handle->write_cont_cls,
2000 socket->write_handle->size);
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2002 "%x: Write completion callback completed\n",
2004 /* We are done with the write handle - Freeing it */
2005 GNUNET_free (socket->write_handle);
2006 socket->write_handle = NULL;
2017 * Message Handler for mesh
2019 * @param cls the 'struct GNUNET_STREAM_Socket'
2020 * @param tunnel connection to the other end
2021 * @param tunnel_ctx unused
2022 * @param sender who sent the message
2023 * @param message the actual message
2024 * @param atsi performance data for the connection
2025 * @return GNUNET_OK to keep the connection open,
2026 * GNUNET_SYSERR to close it (signal serious error)
2029 client_handle_ack (void *cls,
2030 struct GNUNET_MESH_Tunnel *tunnel,
2032 const struct GNUNET_PeerIdentity *sender,
2033 const struct GNUNET_MessageHeader *message,
2034 const struct GNUNET_ATS_Information*atsi)
2036 struct GNUNET_STREAM_Socket *socket = cls;
2037 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2039 return handle_ack (socket, tunnel, sender, ack, atsi);
2044 * Message Handler for mesh
2046 * @param cls the server's listen socket
2047 * @param tunnel connection to the other end
2048 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2049 * @param sender who sent the message
2050 * @param message the actual message
2051 * @param atsi performance data for the connection
2052 * @return GNUNET_OK to keep the connection open,
2053 * GNUNET_SYSERR to close it (signal serious error)
2056 server_handle_ack (void *cls,
2057 struct GNUNET_MESH_Tunnel *tunnel,
2059 const struct GNUNET_PeerIdentity *sender,
2060 const struct GNUNET_MessageHeader *message,
2061 const struct GNUNET_ATS_Information*atsi)
2063 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2064 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2066 return handle_ack (socket, tunnel, sender, ack, atsi);
2071 * For client message handlers, the stream socket is in the
2074 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2075 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2076 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2077 sizeof (struct GNUNET_STREAM_AckMessage) },
2078 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2079 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2080 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2081 sizeof (struct GNUNET_STREAM_MessageHeader)},
2082 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2083 sizeof (struct GNUNET_STREAM_MessageHeader)},
2084 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2085 sizeof (struct GNUNET_STREAM_MessageHeader)},
2086 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2087 sizeof (struct GNUNET_STREAM_MessageHeader)},
2088 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2089 sizeof (struct GNUNET_STREAM_MessageHeader)},
2090 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2091 sizeof (struct GNUNET_STREAM_MessageHeader)},
2092 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2093 sizeof (struct GNUNET_STREAM_MessageHeader)},
2099 * For server message handlers, the stream socket is in the
2100 * tunnel context, and the listen socket in the closure argument.
2102 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2103 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2104 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2105 sizeof (struct GNUNET_STREAM_AckMessage) },
2106 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2107 sizeof (struct GNUNET_STREAM_MessageHeader)},
2108 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2109 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2110 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2111 sizeof (struct GNUNET_STREAM_MessageHeader)},
2112 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2113 sizeof (struct GNUNET_STREAM_MessageHeader)},
2114 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2115 sizeof (struct GNUNET_STREAM_MessageHeader)},
2116 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2117 sizeof (struct GNUNET_STREAM_MessageHeader)},
2118 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2119 sizeof (struct GNUNET_STREAM_MessageHeader)},
2120 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2121 sizeof (struct GNUNET_STREAM_MessageHeader)},
2122 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2123 sizeof (struct GNUNET_STREAM_MessageHeader)},
2129 * Function called when our target peer is connected to our tunnel
2131 * @param cls the socket for which this tunnel is created
2132 * @param peer the peer identity of the target
2133 * @param atsi performance data for the connection
2136 mesh_peer_connect_callback (void *cls,
2137 const struct GNUNET_PeerIdentity *peer,
2138 const struct GNUNET_ATS_Information * atsi)
2140 struct GNUNET_STREAM_Socket *socket = cls;
2141 struct GNUNET_STREAM_MessageHeader *message;
2142 GNUNET_PEER_Id connected_peer;
2144 connected_peer = GNUNET_PEER_search (peer);
2146 if (connected_peer != socket->other_peer)
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 "%x: A peer which is not our target has connected",
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2156 "%x: Target peer %x connected\n",
2160 /* Set state to INIT */
2161 socket->state = STATE_INIT;
2163 /* Send HELLO message */
2164 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2165 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
2166 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2167 queue_message (socket,
2169 &set_state_hello_wait,
2172 /* Call open callback */
2173 if (NULL == socket->open_cb)
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 "STREAM_open callback is NULL\n");
2182 * Function called when our target peer is disconnected from our tunnel
2184 * @param cls the socket associated which this tunnel
2185 * @param peer the peer identity of the target
2188 mesh_peer_disconnect_callback (void *cls,
2189 const struct GNUNET_PeerIdentity *peer)
2196 * Method called whenever a peer creates a tunnel to us
2198 * @param cls closure
2199 * @param tunnel new handle to the tunnel
2200 * @param initiator peer that started the tunnel
2201 * @param atsi performance information for the tunnel
2202 * @return initial tunnel context for the tunnel
2203 * (can be NULL -- that's not an error)
2206 new_tunnel_notify (void *cls,
2207 struct GNUNET_MESH_Tunnel *tunnel,
2208 const struct GNUNET_PeerIdentity *initiator,
2209 const struct GNUNET_ATS_Information *atsi)
2211 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2212 struct GNUNET_STREAM_Socket *socket;
2214 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2215 from the same peer again until the socket is closed */
2217 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2218 socket->other_peer = GNUNET_PEER_intern (initiator);
2219 socket->tunnel = tunnel;
2220 socket->session_id = 0; /* FIXME */
2221 socket->state = STATE_INIT;
2222 socket->lsocket = lsocket;
2223 socket->our_id = lsocket->our_id;
2225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2226 "%x: Peer %x initiated tunnel to us\n",
2228 socket->other_peer);
2230 /* FIXME: Copy MESH handle from lsocket to socket */
2237 * Function called whenever an inbound tunnel is destroyed. Should clean up
2238 * any associated state. This function is NOT called if the client has
2239 * explicitly asked for the tunnel to be destroyed using
2240 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2243 * @param cls closure (set from GNUNET_MESH_connect)
2244 * @param tunnel connection to the other end (henceforth invalid)
2245 * @param tunnel_ctx place where local state associated
2246 * with the tunnel is stored
2249 tunnel_cleaner (void *cls,
2250 const struct GNUNET_MESH_Tunnel *tunnel,
2253 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "%x: Peer %x has terminated connection abruptly\n",
2258 socket->other_peer);
2260 socket->status = GNUNET_STREAM_SHUTDOWN;
2262 /* Clear Transmit handles */
2263 if (NULL != socket->transmit_handle)
2265 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2266 socket->transmit_handle = NULL;
2268 socket->tunnel = NULL;
2278 * Tries to open a stream to the target peer
2280 * @param cfg configuration to use
2281 * @param target the target peer to which the stream has to be opened
2282 * @param app_port the application port number which uniquely identifies this
2284 * @param open_cb this function will be called after stream has be established
2285 * @param open_cb_cls the closure for open_cb
2286 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2287 * @return if successful it returns the stream socket; NULL if stream cannot be
2290 struct GNUNET_STREAM_Socket *
2291 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2292 const struct GNUNET_PeerIdentity *target,
2293 GNUNET_MESH_ApplicationType app_port,
2294 GNUNET_STREAM_OpenCallback open_cb,
2298 struct GNUNET_STREAM_Socket *socket;
2299 struct GNUNET_PeerIdentity own_peer_id;
2300 enum GNUNET_STREAM_Option option;
2301 va_list vargs; /* Variable arguments */
2303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2307 socket->other_peer = GNUNET_PEER_intern (target);
2308 socket->open_cb = open_cb;
2309 socket->open_cls = open_cb_cls;
2310 GNUNET_TESTING_get_peer_identity (cfg, &own_peer_id);
2311 socket->our_id = GNUNET_PEER_intern (&own_peer_id);
2314 socket->retransmit_timeout =
2315 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
2317 va_start (vargs, open_cb_cls); /* Parse variable args */
2319 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2322 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2323 /* Expect struct GNUNET_TIME_Relative */
2324 socket->retransmit_timeout = va_arg (vargs,
2325 struct GNUNET_TIME_Relative);
2327 case GNUNET_STREAM_OPTION_END:
2330 } while (GNUNET_STREAM_OPTION_END != option);
2331 va_end (vargs); /* End of variable args parsing */
2332 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2333 10, /* QUEUE size as parameter? */
2335 NULL, /* No inbound tunnel handler */
2336 &tunnel_cleaner, /* FIXME: not required? */
2337 client_message_handlers,
2338 &app_port); /* We don't get inbound tunnels */
2339 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2341 GNUNET_free (socket);
2345 /* Now create the mesh tunnel to target */
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2347 "Creating MESH Tunnel\n");
2348 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2349 NULL, /* Tunnel context */
2350 &mesh_peer_connect_callback,
2351 &mesh_peer_disconnect_callback,
2353 GNUNET_assert (NULL != socket->tunnel);
2354 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2358 "%s() END\n", __func__);
2364 * Shutdown the stream for reading or writing (man 2 shutdown).
2366 * @param socket the stream socket
2367 * @param how SHUT_RD, SHUT_WR or SHUT_RDWR
2370 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2380 * @param socket the stream socket
2383 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
2385 struct MessageQueue *head;
2387 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
2389 /* socket closed with read task pending!? */
2391 GNUNET_SCHEDULER_cancel (socket->read_task_id);
2392 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
2395 /* Terminate the ack'ing tasks if they are still present */
2396 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
2398 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2399 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2402 /* Clear Transmit handles */
2403 if (NULL != socket->transmit_handle)
2405 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2406 socket->transmit_handle = NULL;
2409 /* Clear existing message queue */
2410 while (NULL != (head = socket->queue_head)) {
2411 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2414 GNUNET_free (head->message);
2418 /* Close associated tunnel */
2419 if (NULL != socket->tunnel)
2421 GNUNET_MESH_tunnel_destroy (socket->tunnel);
2422 socket->tunnel = NULL;
2425 /* Close mesh connection */
2426 if (NULL != socket->mesh && NULL == socket->lsocket)
2428 GNUNET_MESH_disconnect (socket->mesh);
2429 socket->mesh = NULL;
2432 /* Release receive buffer */
2433 if (NULL != socket->receive_buffer)
2435 GNUNET_free (socket->receive_buffer);
2438 GNUNET_free (socket);
2443 * Listens for stream connections for a specific application ports
2445 * @param cfg the configuration to use
2446 * @param app_port the application port for which new streams will be accepted
2447 * @param listen_cb this function will be called when a peer tries to establish
2449 * @param listen_cb_cls closure for listen_cb
2450 * @return listen socket, NULL for any error
2452 struct GNUNET_STREAM_ListenSocket *
2453 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
2454 GNUNET_MESH_ApplicationType app_port,
2455 GNUNET_STREAM_ListenCallback listen_cb,
2456 void *listen_cb_cls)
2458 /* FIXME: Add variable args for passing configration options? */
2459 struct GNUNET_STREAM_ListenSocket *lsocket;
2460 struct GNUNET_PeerIdentity our_peer_id;
2462 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
2463 lsocket->port = app_port;
2464 lsocket->listen_cb = listen_cb;
2465 lsocket->listen_cb_cls = listen_cb_cls;
2466 GNUNET_TESTING_get_peer_identity (cfg, &our_peer_id);
2467 lsocket->our_id = GNUNET_PEER_intern (&our_peer_id);
2468 lsocket->mesh = GNUNET_MESH_connect (cfg,
2469 10, /* FIXME: QUEUE size as parameter? */
2470 lsocket, /* Closure */
2473 server_message_handlers,
2475 GNUNET_assert (NULL != lsocket->mesh);
2481 * Closes the listen socket
2483 * @param lsocket the listen socket
2486 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
2488 /* Close MESH connection */
2489 GNUNET_assert (NULL != lsocket->mesh);
2490 GNUNET_MESH_disconnect (lsocket->mesh);
2492 GNUNET_free (lsocket);
2497 * Tries to write the given data to the stream
2499 * @param socket the socket representing a stream
2500 * @param data the data buffer from where the data is written into the stream
2501 * @param size the number of bytes to be written from the data buffer
2502 * @param timeout the timeout period
2503 * @param write_cont the function to call upon writing some bytes into the stream
2504 * @param write_cont_cls the closure
2505 * @return handle to cancel the operation
2507 struct GNUNET_STREAM_IOWriteHandle *
2508 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
2511 struct GNUNET_TIME_Relative timeout,
2512 GNUNET_STREAM_CompletionContinuation write_cont,
2513 void *write_cont_cls)
2515 unsigned int num_needed_packets;
2516 unsigned int packet;
2517 struct GNUNET_STREAM_IOWriteHandle *io_handle;
2518 uint32_t packet_size;
2519 uint32_t payload_size;
2520 struct GNUNET_STREAM_DataMessage *data_msg;
2522 struct GNUNET_TIME_Relative ack_deadline;
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2527 /* Return NULL if there is already a write request pending */
2528 if (NULL != socket->write_handle)
2533 if (!((STATE_ESTABLISHED == socket->state)
2534 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
2535 || (STATE_RECEIVE_CLOSED == socket->state)))
2537 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2538 "%x: Attempting to write on a closed (OR) not-yet-established"
2543 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
2544 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
2545 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
2546 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
2547 io_handle->write_cont = write_cont;
2548 io_handle->write_cont_cls = write_cont_cls;
2549 io_handle->size = size;
2551 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2552 determined from RTT */
2553 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
2554 /* Divide the given buffer into packets for sending */
2555 for (packet=0; packet < num_needed_packets; packet++)
2557 if ((packet + 1) * max_payload_size < size)
2559 payload_size = max_payload_size;
2560 packet_size = MAX_PACKET_SIZE;
2564 payload_size = size - packet * max_payload_size;
2565 packet_size = payload_size + sizeof (struct
2566 GNUNET_STREAM_DataMessage);
2568 io_handle->messages[packet] = GNUNET_malloc (packet_size);
2569 io_handle->messages[packet]->header.header.size = htons (packet_size);
2570 io_handle->messages[packet]->header.header.type =
2571 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
2572 io_handle->messages[packet]->sequence_number =
2573 htonl (socket->write_sequence_number++);
2574 io_handle->messages[packet]->offset = htonl (socket->write_offset);
2576 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2577 determined from RTT */
2578 io_handle->messages[packet]->ack_deadline =
2579 GNUNET_TIME_relative_hton (ack_deadline);
2580 data_msg = io_handle->messages[packet];
2581 /* Copy data from given buffer to the packet */
2582 memcpy (&data_msg[1],
2585 sweep += payload_size;
2586 socket->write_offset += payload_size;
2588 socket->write_handle = io_handle;
2589 write_data (socket);
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2592 "%s() END\n", __func__);
2599 * Tries to read data from the stream
2601 * @param socket the socket representing a stream
2602 * @param timeout the timeout period
2603 * @param proc function to call with data (once only)
2604 * @param proc_cls the closure for proc
2605 * @return handle to cancel the operation
2607 struct GNUNET_STREAM_IOReadHandle *
2608 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
2609 struct GNUNET_TIME_Relative timeout,
2610 GNUNET_STREAM_DataProcessor proc,
2613 struct GNUNET_STREAM_IOReadHandle *read_handle;
2615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2620 /* Return NULL if there is already a read handle; the user has to cancel that
2621 first before continuing or has to wait until it is completed */
2622 if (NULL != socket->read_handle) return NULL;
2624 GNUNET_assert (NULL != proc);
2626 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
2627 read_handle->proc = proc;
2628 read_handle->proc_cls = proc_cls;
2629 socket->read_handle = read_handle;
2631 /* Check if we have a packet at bitmap 0 */
2632 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
2635 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
2640 /* Setup the read timeout task */
2641 socket->read_io_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
2644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2653 * Cancel pending write operation.
2655 * @param ioh handle to operation to cancel
2658 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
2660 /* FIXME: Should cancel the write retransmission task */
2666 * Cancel pending read operation.
2668 * @param ioh handle to operation to cancel
2671 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)