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 * Task identifier for the read io timeout task
236 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task;
239 * Task identifier for retransmission task after timeout
241 GNUNET_SCHEDULER_TaskIdentifier retransmission_timeout_task_id;
244 * The task for sending timely Acks
246 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
249 * Task scheduled to continue a read operation.
251 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
254 * The state of the protocol associated with this socket
259 * The status of the socket
261 enum GNUNET_STREAM_Status status;
264 * The number of previous timeouts; FIXME: currently not used
266 unsigned int retries;
269 * Is this socket derived from listen socket?
271 unsigned int derived;
274 * The peer identity of the peer at the other end of the stream
276 GNUNET_PEER_Id other_peer;
279 * Our Peer Identity (for debugging)
281 GNUNET_PEER_Id our_id;
284 * The application port number (type: uint32_t)
286 GNUNET_MESH_ApplicationType app_port;
289 * The session id associated with this stream connection
290 * FIXME: Not used currently, may be removed
295 * Write sequence number. Set to random when sending HELLO(client) and
298 uint32_t write_sequence_number;
301 * Read sequence number. This number's value is determined during handshake
303 uint32_t read_sequence_number;
306 * The receiver buffer size
308 uint32_t receive_buffer_size;
311 * The receiver buffer boundaries
313 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
316 * receiver's available buffer after the last acknowledged packet
318 uint32_t receiver_window_available;
321 * The offset pointer used during write operation
323 uint32_t write_offset;
326 * The offset after which we are expecting data
328 uint32_t read_offset;
331 * The offset upto which user has read from the received buffer
333 uint32_t copy_offset;
338 * A socket for listening
340 struct GNUNET_STREAM_ListenSocket
345 struct GNUNET_MESH_Handle *mesh;
348 * The callback function which is called after successful opening socket
350 GNUNET_STREAM_ListenCallback listen_cb;
353 * The call back closure
358 * Our interned Peer's identity
360 GNUNET_PEER_Id our_id;
364 * FIXME: Remove if not required!
366 GNUNET_MESH_ApplicationType port;
371 * The IO Write Handle
373 struct GNUNET_STREAM_IOWriteHandle
376 * The packet_buffers associated with this Handle
378 struct GNUNET_STREAM_DataMessage *messages[64];
381 * The write continuation callback
383 GNUNET_STREAM_CompletionContinuation write_cont;
386 * Write continuation closure
388 void *write_cont_cls;
391 * The bitmap of this IOHandle; Corresponding bit for a message is set when
392 * it has been acknowledged by the receiver
394 GNUNET_STREAM_AckBitmap ack_bitmap;
397 * Number of bytes in this write handle
402 * Number of packets sent before waiting for an ack
404 * FIXME: Do we need this?
406 unsigned int sent_packets;
413 struct GNUNET_STREAM_IOReadHandle
416 * Callback for the read processor
418 GNUNET_STREAM_DataProcessor proc;
421 * The closure pointer for the read processor callback
428 * Default value in seconds for various timeouts
430 static unsigned int default_timeout = 10;
433 * Callback function for sending hello message
435 * @param cls closure the socket
436 * @param size number of bytes available in buf
437 * @param buf where the callee should write the message
438 * @return number of bytes written to buf
441 send_message_notify (void *cls, size_t size, void *buf)
443 struct GNUNET_STREAM_Socket *socket = cls;
444 struct GNUNET_PeerIdentity target;
445 struct MessageQueue *head;
448 socket->transmit_handle = NULL; /* Remove the transmit handle */
449 head = socket->queue_head;
451 return 0; /* just to be safe */
452 GNUNET_PEER_resolve (socket->other_peer, &target);
453 if (0 == size) /* request timed out */
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Message sending timed out. Retry %d \n",
459 socket->transmit_handle =
460 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
463 /* FIXME: exponential backoff */
464 socket->retransmit_timeout,
466 ntohs (head->message->header.size),
467 &send_message_notify,
472 ret = ntohs (head->message->header.size);
473 GNUNET_assert (size >= ret);
474 memcpy (buf, head->message, ret);
475 if (NULL != head->finish_cb)
477 head->finish_cb (head->finish_cb_cls, socket);
479 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
482 GNUNET_free (head->message);
484 head = socket->queue_head;
485 if (NULL != head) /* more pending messages to send */
488 socket->transmit_handle =
489 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
492 /* FIXME: exponential backoff */
493 socket->retransmit_timeout,
495 ntohs (head->message->header.size),
496 &send_message_notify,
504 * Queues a message for sending using the mesh connection of a socket
506 * @param socket the socket whose mesh connection is used
507 * @param message the message to be sent
508 * @param finish_cb the callback to be called when the message is sent
509 * @param finish_cb_cls the closure for the callback
512 queue_message (struct GNUNET_STREAM_Socket *socket,
513 struct GNUNET_STREAM_MessageHeader *message,
514 SendFinishCallback finish_cb,
517 struct MessageQueue *queue_entity;
518 struct GNUNET_PeerIdentity target;
521 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
522 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "%x: Queueing message of type %d and size %d\n",
527 ntohs (message->header.type),
528 ntohs (message->header.size));
529 GNUNET_assert (NULL != message);
530 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
531 queue_entity->message = message;
532 queue_entity->finish_cb = finish_cb;
533 queue_entity->finish_cb_cls = finish_cb_cls;
534 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
537 if (NULL == socket->transmit_handle)
540 GNUNET_PEER_resolve (socket->other_peer, &target);
541 socket->transmit_handle =
542 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
545 socket->retransmit_timeout,
547 ntohs (message->header.size),
548 &send_message_notify,
555 * Copies a message and queues it for sending using the mesh connection of
558 * @param socket the socket whose mesh connection is used
559 * @param message the message to be sent
560 * @param finish_cb the callback to be called when the message is sent
561 * @param finish_cb_cls the closure for the callback
564 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
565 const struct GNUNET_STREAM_MessageHeader *message,
566 SendFinishCallback finish_cb,
569 struct GNUNET_STREAM_MessageHeader *msg_copy;
572 size = ntohs (message->header.size);
573 msg_copy = GNUNET_malloc (size);
574 memcpy (msg_copy, message, size);
575 queue_message (socket, msg_copy, finish_cb, finish_cb_cls);
580 * Callback function for sending ack message
582 * @param cls closure the ACK message created in ack_task
583 * @param size number of bytes available in buffer
584 * @param buf where the callee should write the message
585 * @return number of bytes written to buf
588 send_ack_notify (void *cls, size_t size, void *buf)
590 struct GNUNET_STREAM_AckMessage *ack_msg = cls;
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595 "%s called with size 0\n", __func__);
598 GNUNET_assert (ntohs (ack_msg->header.header.size) <= size);
600 size = ntohs (ack_msg->header.header.size);
601 memcpy (buf, ack_msg, size);
606 * Writes data using the given socket. The amount of data written is limited by
607 * the receiver_window_size
609 * @param socket the socket to use
612 write_data (struct GNUNET_STREAM_Socket *socket);
615 * Task for retransmitting data messages if they aren't ACK before their ack
618 * @param cls the socket
619 * @param tc the Task context
622 retransmission_timeout_task (void *cls,
623 const struct GNUNET_SCHEDULER_TaskContext *tc)
625 struct GNUNET_STREAM_Socket *socket = cls;
627 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 "%x: Retransmitting DATA...\n", socket->our_id);
632 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
638 * Task for sending ACK message
640 * @param cls the socket
641 * @param tc the Task context
645 const struct GNUNET_SCHEDULER_TaskContext *tc)
647 struct GNUNET_STREAM_Socket *socket = cls;
648 struct GNUNET_STREAM_AckMessage *ack_msg;
649 struct GNUNET_PeerIdentity target;
651 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
656 socket->ack_task_id = 0;
658 /* Create the ACK Message */
659 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
660 ack_msg->header.header.size = htons (sizeof (struct
661 GNUNET_STREAM_AckMessage));
662 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
663 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
664 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
665 ack_msg->receive_window_remaining =
666 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
668 GNUNET_PEER_resolve (socket->other_peer, &target);
669 /* Request MESH for sending ACK */
670 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
673 socket->retransmit_timeout,
675 ntohs (ack_msg->header.header.size),
684 * Function to modify a bit in GNUNET_STREAM_AckBitmap
686 * @param bitmap the bitmap to modify
687 * @param bit the bit number to modify
688 * @param value GNUNET_YES to on, GNUNET_NO to off
691 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
695 GNUNET_assert (bit < 64);
696 if (GNUNET_YES == value)
697 *bitmap |= (1LL << bit);
699 *bitmap &= ~(1LL << bit);
704 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
706 * @param bitmap address of the bitmap that has to be checked
707 * @param bit the bit number to check
708 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
711 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
714 GNUNET_assert (bit < 64);
715 return 0 != (*bitmap & (1LL << bit));
721 * Function called when Data Message is sent
723 * @param cls the io_handle corresponding to the Data Message
724 * @param socket the socket which was used
727 write_data_finish_cb (void *cls,
728 struct GNUNET_STREAM_Socket *socket)
730 struct GNUNET_STREAM_IOWriteHandle *io_handle = cls;
732 io_handle->sent_packets++;
737 * Writes data using the given socket. The amount of data written is limited by
738 * the receiver_window_size
740 * @param socket the socket to use
743 write_data (struct GNUNET_STREAM_Socket *socket)
745 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
746 int packet; /* Although an int, should never be negative */
750 /* Find the last acknowledged packet */
751 for (packet=0; packet < 64; packet++)
753 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
756 else if (NULL == io_handle->messages[packet])
759 /* Resend packets which weren't ack'ed */
760 for (packet=0; packet < ack_packet; packet++)
762 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "%x: Placing DATA message with sequence %u in send queue\n",
768 ntohl (io_handle->messages[packet]->sequence_number));
770 copy_and_queue_message (socket,
771 &io_handle->messages[packet]->header,
776 packet = ack_packet + 1;
777 /* Now send new packets if there is enough buffer space */
778 while ( (NULL != io_handle->messages[packet]) &&
779 (socket->receiver_window_available >= ntohs (io_handle->messages[packet]->header.header.size)) )
781 socket->receiver_window_available -=
782 ntohs (io_handle->messages[packet]->header.header.size);
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "%x: Placing DATA message with sequence %u in send queue\n",
786 ntohl (io_handle->messages[packet]->sequence_number));
787 copy_and_queue_message (socket,
788 &io_handle->messages[packet]->header,
789 &write_data_finish_cb,
794 GNUNET_SCHEDULER_add_delayed
795 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
796 &retransmission_timeout_task,
802 * Task for calling the read processor
804 * @param cls the socket
805 * @param tc the task context
808 call_read_processor (void *cls,
809 const struct GNUNET_SCHEDULER_TaskContext *tc)
811 struct GNUNET_STREAM_Socket *socket = cls;
813 size_t valid_read_size;
815 uint32_t sequence_increase;
816 uint32_t offset_increase;
818 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
819 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
822 GNUNET_assert (NULL != socket->read_handle);
823 GNUNET_assert (NULL != socket->read_handle->proc);
825 /* Check the bitmap for any holes */
826 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
828 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
832 /* We only call read processor if we have the first packet */
833 GNUNET_assert (0 < packet);
836 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
838 GNUNET_assert (0 != valid_read_size);
840 /* Cancel the read_io_timeout_task */
841 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task);
842 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
844 /* Call the data processor */
846 socket->read_handle->proc (socket->read_handle->proc_cls,
848 socket->receive_buffer + socket->copy_offset,
850 /* Free the read handle */
851 GNUNET_free (socket->read_handle);
852 socket->read_handle = NULL;
854 GNUNET_assert (read_size <= valid_read_size);
855 socket->copy_offset += read_size;
857 /* Determine upto which packet we can remove from the buffer */
858 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
859 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
862 /* If no packets can be removed we can't move the buffer */
863 if (0 == packet) return;
865 sequence_increase = packet;
867 /* Shift the data in the receive buffer */
868 memmove (socket->receive_buffer,
869 socket->receive_buffer
870 + socket->receive_buffer_boundaries[sequence_increase-1],
871 socket->receive_buffer_size - socket->receive_buffer_boundaries[sequence_increase-1]);
873 /* Shift the bitmap */
874 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
876 /* Set read_sequence_number */
877 socket->read_sequence_number += sequence_increase;
879 /* Set read_offset */
880 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
881 socket->read_offset += offset_increase;
883 /* Fix copy_offset */
884 GNUNET_assert (offset_increase <= socket->copy_offset);
885 socket->copy_offset -= offset_increase;
887 /* Fix relative boundaries */
888 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
890 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
892 socket->receive_buffer_boundaries[packet] =
893 socket->receive_buffer_boundaries[packet + sequence_increase]
897 socket->receive_buffer_boundaries[packet] = 0;
903 * Cancels the existing read io handle
905 * @param cls the closure from the SCHEDULER call
906 * @param tc the task context
909 read_io_timeout (void *cls,
910 const struct GNUNET_SCHEDULER_TaskContext *tc)
912 struct GNUNET_STREAM_Socket *socket = cls;
914 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
915 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
917 GNUNET_SCHEDULER_cancel (socket->read_task_id);
918 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
920 GNUNET_assert (NULL != socket->read_handle);
922 GNUNET_free (socket->read_handle);
923 socket->read_handle = NULL;
928 * Handler for DATA messages; Same for both client and server
930 * @param socket the socket through which the ack was received
931 * @param tunnel connection to the other end
932 * @param sender who sent the message
933 * @param msg the data message
934 * @param atsi performance data for the connection
935 * @return GNUNET_OK to keep the connection open,
936 * GNUNET_SYSERR to close it (signal serious error)
939 handle_data (struct GNUNET_STREAM_Socket *socket,
940 struct GNUNET_MESH_Tunnel *tunnel,
941 const struct GNUNET_PeerIdentity *sender,
942 const struct GNUNET_STREAM_DataMessage *msg,
943 const struct GNUNET_ATS_Information*atsi)
946 uint32_t bytes_needed;
947 uint32_t relative_offset;
948 uint32_t relative_sequence_number;
951 size = htons (msg->header.header.size);
952 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
955 return GNUNET_SYSERR;
958 if (GNUNET_PEER_search (sender) != socket->other_peer)
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "%x: Received DATA from non-confirming peer\n",
966 switch (socket->state)
968 case STATE_ESTABLISHED:
969 case STATE_TRANSMIT_CLOSED:
970 case STATE_TRANSMIT_CLOSE_WAIT:
972 /* check if the message's sequence number is in the range we are
974 relative_sequence_number =
975 ntohl (msg->sequence_number) - socket->read_sequence_number;
976 if ( relative_sequence_number > 64)
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "%x: Ignoring received message with sequence number %u\n",
981 ntohl (msg->sequence_number));
985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
986 "%x: Receiving DATA with sequence number: %u and size: %d from %x\n",
988 ntohl (msg->sequence_number),
989 ntohs (msg->header.header.size),
992 /* Check if we have to allocate the buffer */
993 size -= sizeof (struct GNUNET_STREAM_DataMessage);
994 relative_offset = ntohl (msg->offset) - socket->read_offset;
995 bytes_needed = relative_offset + size;
997 if (bytes_needed > socket->receive_buffer_size)
999 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1001 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1003 socket->receive_buffer_size = bytes_needed;
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "%x: Cannot accommodate packet %d as buffer is",
1011 ntohl (msg->sequence_number));
1016 /* Copy Data to buffer */
1018 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1019 memcpy (socket->receive_buffer + relative_offset,
1022 socket->receive_buffer_boundaries[relative_sequence_number] =
1023 relative_offset + size;
1025 /* Modify the ACK bitmap */
1026 ackbitmap_modify_bit (&socket->ack_bitmap,
1027 relative_sequence_number,
1030 /* Start ACK sending task if one is not already present */
1031 if (0 == socket->ack_task_id)
1033 socket->ack_task_id =
1034 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1035 (msg->ack_deadline),
1040 if ((NULL != socket->read_handle) /* A read handle is waiting */
1041 /* There is no current read task */
1042 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
1043 /* We have the first packet */
1044 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
1047 socket->read_task_id =
1048 GNUNET_SCHEDULER_add_now (&call_read_processor,
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "%x: Received data message when it cannot be handled\n",
1064 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1066 * @param cls the socket (set from GNUNET_MESH_connect)
1067 * @param tunnel connection to the other end
1068 * @param tunnel_ctx place to store local state associated with the tunnel
1069 * @param sender who sent the message
1070 * @param message the actual message
1071 * @param atsi performance data for the connection
1072 * @return GNUNET_OK to keep the connection open,
1073 * GNUNET_SYSERR to close it (signal serious error)
1076 client_handle_data (void *cls,
1077 struct GNUNET_MESH_Tunnel *tunnel,
1079 const struct GNUNET_PeerIdentity *sender,
1080 const struct GNUNET_MessageHeader *message,
1081 const struct GNUNET_ATS_Information*atsi)
1083 struct GNUNET_STREAM_Socket *socket = cls;
1085 return handle_data (socket,
1088 (const struct GNUNET_STREAM_DataMessage *) message,
1094 * Callback to set state to ESTABLISHED
1096 * @param cls the closure from queue_message FIXME: document
1097 * @param socket the socket to requiring state change
1100 set_state_established (void *cls,
1101 struct GNUNET_STREAM_Socket *socket)
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "%x: Attaining ESTABLISHED state\n",
1106 socket->write_offset = 0;
1107 socket->read_offset = 0;
1108 socket->state = STATE_ESTABLISHED;
1109 if (socket->open_cb)
1110 socket->open_cb (socket->open_cls, socket);
1115 * Callback to set state to HELLO_WAIT
1117 * @param cls the closure from queue_message
1118 * @param socket the socket to requiring state change
1121 set_state_hello_wait (void *cls,
1122 struct GNUNET_STREAM_Socket *socket)
1124 GNUNET_assert (STATE_INIT == socket->state);
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126 "%x: Attaining HELLO_WAIT state\n",
1128 socket->state = STATE_HELLO_WAIT;
1133 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1135 * @param cls the socket (set from GNUNET_MESH_connect)
1136 * @param tunnel connection to the other end
1137 * @param tunnel_ctx this is NULL
1138 * @param sender who sent the message
1139 * @param message the actual message
1140 * @param atsi performance data for the connection
1141 * @return GNUNET_OK to keep the connection open,
1142 * GNUNET_SYSERR to close it (signal serious error)
1145 client_handle_hello_ack (void *cls,
1146 struct GNUNET_MESH_Tunnel *tunnel,
1148 const struct GNUNET_PeerIdentity *sender,
1149 const struct GNUNET_MessageHeader *message,
1150 const struct GNUNET_ATS_Information*atsi)
1152 struct GNUNET_STREAM_Socket *socket = cls;
1153 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1154 struct GNUNET_STREAM_HelloAckMessage *reply;
1156 if (GNUNET_PEER_search (sender) != socket->other_peer)
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 "%x: Received HELLO_ACK from non-confirming peer\n",
1163 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 "%x: Received HELLO_ACK from %x\n",
1167 socket->other_peer);
1169 GNUNET_assert (socket->tunnel == tunnel);
1170 switch (socket->state)
1172 case STATE_HELLO_WAIT:
1173 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 "%x: Read sequence number %u\n",
1177 (unsigned int) socket->read_sequence_number);
1178 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1179 /* Get the random sequence number */
1180 socket->write_sequence_number =
1181 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "%x: Generated write sequence number %u\n",
1185 (unsigned int) socket->write_sequence_number);
1187 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1188 reply->header.header.size =
1189 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1190 reply->header.header.type =
1191 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1192 reply->sequence_number = htonl (socket->write_sequence_number);
1193 reply->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1194 queue_message (socket,
1196 &set_state_established,
1199 case STATE_ESTABLISHED:
1200 case STATE_RECEIVE_CLOSE_WAIT:
1201 // call statistics (# ACKs ignored++)
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1206 "%x: Server %x sent HELLO_ACK when in state %d\n",
1210 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1211 return GNUNET_SYSERR;
1218 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1220 * @param cls the socket (set from GNUNET_MESH_connect)
1221 * @param tunnel connection to the other end
1222 * @param tunnel_ctx this is NULL
1223 * @param sender who sent the message
1224 * @param message the actual message
1225 * @param atsi performance data for the connection
1226 * @return GNUNET_OK to keep the connection open,
1227 * GNUNET_SYSERR to close it (signal serious error)
1230 client_handle_reset (void *cls,
1231 struct GNUNET_MESH_Tunnel *tunnel,
1233 const struct GNUNET_PeerIdentity *sender,
1234 const struct GNUNET_MessageHeader *message,
1235 const struct GNUNET_ATS_Information*atsi)
1237 struct GNUNET_STREAM_Socket *socket = cls;
1244 * Common message handler for handling TRANSMIT_CLOSE messages
1246 * @param socket the socket through which the ack was received
1247 * @param tunnel connection to the other end
1248 * @param sender who sent the message
1249 * @param msg the transmit close message
1250 * @param atsi performance data for the connection
1251 * @return GNUNET_OK to keep the connection open,
1252 * GNUNET_SYSERR to close it (signal serious error)
1255 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1256 struct GNUNET_MESH_Tunnel *tunnel,
1257 const struct GNUNET_PeerIdentity *sender,
1258 const struct GNUNET_STREAM_MessageHeader *msg,
1259 const struct GNUNET_ATS_Information*atsi)
1261 struct GNUNET_STREAM_MessageHeader *reply;
1263 switch (socket->state)
1265 case STATE_ESTABLISHED:
1266 socket->state = STATE_RECEIVE_CLOSED;
1268 /* Send TRANSMIT_CLOSE_ACK */
1269 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1270 reply->header.type =
1271 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1272 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1273 queue_message (socket, reply, NULL, NULL);
1277 /* FIXME: Call statistics? */
1285 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1287 * @param cls the socket (set from GNUNET_MESH_connect)
1288 * @param tunnel connection to the other end
1289 * @param tunnel_ctx this is NULL
1290 * @param sender who sent the message
1291 * @param message the actual message
1292 * @param atsi performance data for the connection
1293 * @return GNUNET_OK to keep the connection open,
1294 * GNUNET_SYSERR to close it (signal serious error)
1297 client_handle_transmit_close (void *cls,
1298 struct GNUNET_MESH_Tunnel *tunnel,
1300 const struct GNUNET_PeerIdentity *sender,
1301 const struct GNUNET_MessageHeader *message,
1302 const struct GNUNET_ATS_Information*atsi)
1304 struct GNUNET_STREAM_Socket *socket = cls;
1306 return handle_transmit_close (socket,
1309 (struct GNUNET_STREAM_MessageHeader *)message,
1315 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1317 * @param cls the socket (set from GNUNET_MESH_connect)
1318 * @param tunnel connection to the other end
1319 * @param tunnel_ctx this is NULL
1320 * @param sender who sent the message
1321 * @param message the actual message
1322 * @param atsi performance data for the connection
1323 * @return GNUNET_OK to keep the connection open,
1324 * GNUNET_SYSERR to close it (signal serious error)
1327 client_handle_transmit_close_ack (void *cls,
1328 struct GNUNET_MESH_Tunnel *tunnel,
1330 const struct GNUNET_PeerIdentity *sender,
1331 const struct GNUNET_MessageHeader *message,
1332 const struct GNUNET_ATS_Information*atsi)
1334 struct GNUNET_STREAM_Socket *socket = cls;
1341 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1343 * @param cls the socket (set from GNUNET_MESH_connect)
1344 * @param tunnel connection to the other end
1345 * @param tunnel_ctx this is NULL
1346 * @param sender who sent the message
1347 * @param message the actual message
1348 * @param atsi performance data for the connection
1349 * @return GNUNET_OK to keep the connection open,
1350 * GNUNET_SYSERR to close it (signal serious error)
1353 client_handle_receive_close (void *cls,
1354 struct GNUNET_MESH_Tunnel *tunnel,
1356 const struct GNUNET_PeerIdentity *sender,
1357 const struct GNUNET_MessageHeader *message,
1358 const struct GNUNET_ATS_Information*atsi)
1360 struct GNUNET_STREAM_Socket *socket = cls;
1367 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1369 * @param cls the socket (set from GNUNET_MESH_connect)
1370 * @param tunnel connection to the other end
1371 * @param tunnel_ctx this is NULL
1372 * @param sender who sent the message
1373 * @param message the actual message
1374 * @param atsi performance data for the connection
1375 * @return GNUNET_OK to keep the connection open,
1376 * GNUNET_SYSERR to close it (signal serious error)
1379 client_handle_receive_close_ack (void *cls,
1380 struct GNUNET_MESH_Tunnel *tunnel,
1382 const struct GNUNET_PeerIdentity *sender,
1383 const struct GNUNET_MessageHeader *message,
1384 const struct GNUNET_ATS_Information*atsi)
1386 struct GNUNET_STREAM_Socket *socket = cls;
1393 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1395 * @param cls the socket (set from GNUNET_MESH_connect)
1396 * @param tunnel connection to the other end
1397 * @param tunnel_ctx this is NULL
1398 * @param sender who sent the message
1399 * @param message the actual message
1400 * @param atsi performance data for the connection
1401 * @return GNUNET_OK to keep the connection open,
1402 * GNUNET_SYSERR to close it (signal serious error)
1405 client_handle_close (void *cls,
1406 struct GNUNET_MESH_Tunnel *tunnel,
1408 const struct GNUNET_PeerIdentity *sender,
1409 const struct GNUNET_MessageHeader *message,
1410 const struct GNUNET_ATS_Information*atsi)
1412 struct GNUNET_STREAM_Socket *socket = cls;
1419 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1421 * @param cls the socket (set from GNUNET_MESH_connect)
1422 * @param tunnel connection to the other end
1423 * @param tunnel_ctx this is NULL
1424 * @param sender who sent the message
1425 * @param message the actual message
1426 * @param atsi performance data for the connection
1427 * @return GNUNET_OK to keep the connection open,
1428 * GNUNET_SYSERR to close it (signal serious error)
1431 client_handle_close_ack (void *cls,
1432 struct GNUNET_MESH_Tunnel *tunnel,
1434 const struct GNUNET_PeerIdentity *sender,
1435 const struct GNUNET_MessageHeader *message,
1436 const struct GNUNET_ATS_Information*atsi)
1438 struct GNUNET_STREAM_Socket *socket = cls;
1443 /*****************************/
1444 /* Server's Message Handlers */
1445 /*****************************/
1448 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1450 * @param cls the closure
1451 * @param tunnel connection to the other end
1452 * @param tunnel_ctx the socket
1453 * @param sender who sent the message
1454 * @param message the actual message
1455 * @param atsi performance data for the connection
1456 * @return GNUNET_OK to keep the connection open,
1457 * GNUNET_SYSERR to close it (signal serious error)
1460 server_handle_data (void *cls,
1461 struct GNUNET_MESH_Tunnel *tunnel,
1463 const struct GNUNET_PeerIdentity *sender,
1464 const struct GNUNET_MessageHeader *message,
1465 const struct GNUNET_ATS_Information*atsi)
1467 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1469 return handle_data (socket,
1472 (const struct GNUNET_STREAM_DataMessage *)message,
1478 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1480 * @param cls the closure
1481 * @param tunnel connection to the other end
1482 * @param tunnel_ctx the socket
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 server_handle_hello (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 = *tunnel_ctx;
1498 struct GNUNET_STREAM_HelloAckMessage *reply;
1500 if (GNUNET_PEER_search (sender) != socket->other_peer)
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 "%x: Received HELLO from non-confirming peer\n",
1508 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO ==
1509 ntohs (message->type));
1510 GNUNET_assert (socket->tunnel == tunnel);
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "%x: Received HELLO from %x\n",
1514 socket->other_peer);
1516 if (STATE_INIT == socket->state)
1518 /* Get the random sequence number */
1519 socket->write_sequence_number =
1520 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 "%x: Generated write sequence number %u\n",
1524 (unsigned int) socket->write_sequence_number);
1526 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1527 reply->header.header.size =
1528 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1529 reply->header.header.type =
1530 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1531 reply->sequence_number = htonl (socket->write_sequence_number);
1532 reply->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1533 queue_message (socket,
1535 &set_state_hello_wait,
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541 "Client sent HELLO when in state %d\n", socket->state);
1542 /* FIXME: Send RESET? */
1550 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1552 * @param cls the closure
1553 * @param tunnel connection to the other end
1554 * @param tunnel_ctx the socket
1555 * @param sender who sent the message
1556 * @param message the actual message
1557 * @param atsi performance data for the connection
1558 * @return GNUNET_OK to keep the connection open,
1559 * GNUNET_SYSERR to close it (signal serious error)
1562 server_handle_hello_ack (void *cls,
1563 struct GNUNET_MESH_Tunnel *tunnel,
1565 const struct GNUNET_PeerIdentity *sender,
1566 const struct GNUNET_MessageHeader *message,
1567 const struct GNUNET_ATS_Information*atsi)
1569 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1570 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1572 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
1573 ntohs (message->type));
1574 GNUNET_assert (socket->tunnel == tunnel);
1575 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1576 if (STATE_HELLO_WAIT == socket->state)
1578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1579 "%x: Received HELLO_ACK from %x\n",
1581 socket->other_peer);
1582 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1584 "%x: Read sequence number %u\n",
1586 (unsigned int) socket->read_sequence_number);
1587 socket->receiver_window_available =
1588 ntohl (ack_message->receiver_window_size);
1589 /* Attain ESTABLISHED state */
1590 set_state_established (NULL, socket);
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1595 "Client sent HELLO_ACK when in state %d\n", socket->state);
1596 /* FIXME: Send RESET? */
1604 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1606 * @param cls the closure
1607 * @param tunnel connection to the other end
1608 * @param tunnel_ctx the socket
1609 * @param sender who sent the message
1610 * @param message the actual message
1611 * @param atsi performance data for the connection
1612 * @return GNUNET_OK to keep the connection open,
1613 * GNUNET_SYSERR to close it (signal serious error)
1616 server_handle_reset (void *cls,
1617 struct GNUNET_MESH_Tunnel *tunnel,
1619 const struct GNUNET_PeerIdentity *sender,
1620 const struct GNUNET_MessageHeader *message,
1621 const struct GNUNET_ATS_Information*atsi)
1623 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1630 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1632 * @param cls the closure
1633 * @param tunnel connection to the other end
1634 * @param tunnel_ctx the socket
1635 * @param sender who sent the message
1636 * @param message the actual message
1637 * @param atsi performance data for the connection
1638 * @return GNUNET_OK to keep the connection open,
1639 * GNUNET_SYSERR to close it (signal serious error)
1642 server_handle_transmit_close (void *cls,
1643 struct GNUNET_MESH_Tunnel *tunnel,
1645 const struct GNUNET_PeerIdentity *sender,
1646 const struct GNUNET_MessageHeader *message,
1647 const struct GNUNET_ATS_Information*atsi)
1649 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1651 return handle_transmit_close (socket,
1654 (struct GNUNET_STREAM_MessageHeader *)message,
1660 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1662 * @param cls the closure
1663 * @param tunnel connection to the other end
1664 * @param tunnel_ctx the socket
1665 * @param sender who sent the message
1666 * @param message the actual message
1667 * @param atsi performance data for the connection
1668 * @return GNUNET_OK to keep the connection open,
1669 * GNUNET_SYSERR to close it (signal serious error)
1672 server_handle_transmit_close_ack (void *cls,
1673 struct GNUNET_MESH_Tunnel *tunnel,
1675 const struct GNUNET_PeerIdentity *sender,
1676 const struct GNUNET_MessageHeader *message,
1677 const struct GNUNET_ATS_Information*atsi)
1679 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1686 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1688 * @param cls the closure
1689 * @param tunnel connection to the other end
1690 * @param tunnel_ctx the socket
1691 * @param sender who sent the message
1692 * @param message the actual message
1693 * @param atsi performance data for the connection
1694 * @return GNUNET_OK to keep the connection open,
1695 * GNUNET_SYSERR to close it (signal serious error)
1698 server_handle_receive_close (void *cls,
1699 struct GNUNET_MESH_Tunnel *tunnel,
1701 const struct GNUNET_PeerIdentity *sender,
1702 const struct GNUNET_MessageHeader *message,
1703 const struct GNUNET_ATS_Information*atsi)
1705 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1712 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1714 * @param cls the closure
1715 * @param tunnel connection to the other end
1716 * @param tunnel_ctx the socket
1717 * @param sender who sent the message
1718 * @param message the actual message
1719 * @param atsi performance data for the connection
1720 * @return GNUNET_OK to keep the connection open,
1721 * GNUNET_SYSERR to close it (signal serious error)
1724 server_handle_receive_close_ack (void *cls,
1725 struct GNUNET_MESH_Tunnel *tunnel,
1727 const struct GNUNET_PeerIdentity *sender,
1728 const struct GNUNET_MessageHeader *message,
1729 const struct GNUNET_ATS_Information*atsi)
1731 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1738 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1740 * @param cls the closure
1741 * @param tunnel connection to the other end
1742 * @param tunnel_ctx the socket
1743 * @param sender who sent the message
1744 * @param message the actual message
1745 * @param atsi performance data for the connection
1746 * @return GNUNET_OK to keep the connection open,
1747 * GNUNET_SYSERR to close it (signal serious error)
1750 server_handle_close (void *cls,
1751 struct GNUNET_MESH_Tunnel *tunnel,
1753 const struct GNUNET_PeerIdentity *sender,
1754 const struct GNUNET_MessageHeader *message,
1755 const struct GNUNET_ATS_Information*atsi)
1757 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1764 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1766 * @param cls the closure
1767 * @param tunnel connection to the other end
1768 * @param tunnel_ctx the socket
1769 * @param sender who sent the message
1770 * @param message the actual message
1771 * @param atsi performance data for the connection
1772 * @return GNUNET_OK to keep the connection open,
1773 * GNUNET_SYSERR to close it (signal serious error)
1776 server_handle_close_ack (void *cls,
1777 struct GNUNET_MESH_Tunnel *tunnel,
1779 const struct GNUNET_PeerIdentity *sender,
1780 const struct GNUNET_MessageHeader *message,
1781 const struct GNUNET_ATS_Information*atsi)
1783 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1790 * Message Handler for mesh
1792 * @param socket the socket through which the ack was received
1793 * @param tunnel connection to the other end
1794 * @param sender who sent the message
1795 * @param ack the acknowledgment message
1796 * @param atsi performance data for the connection
1797 * @return GNUNET_OK to keep the connection open,
1798 * GNUNET_SYSERR to close it (signal serious error)
1801 handle_ack (struct GNUNET_STREAM_Socket *socket,
1802 struct GNUNET_MESH_Tunnel *tunnel,
1803 const struct GNUNET_PeerIdentity *sender,
1804 const struct GNUNET_STREAM_AckMessage *ack,
1805 const struct GNUNET_ATS_Information*atsi)
1807 unsigned int packet;
1808 int need_retransmission;
1810 if (GNUNET_PEER_search (sender) != socket->other_peer)
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1813 "%x: Received ACK from non-confirming peer\n",
1818 switch (socket->state)
1820 case (STATE_ESTABLISHED):
1821 if (NULL == socket->write_handle)
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1824 "%x: Received DATA_ACK when write_handle is NULL\n",
1829 if (!((socket->write_sequence_number
1830 - htonl (ack->base_sequence_number)) < 64))
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 "%x: Received DATA_ACK with unexpected base sequence",
1838 /* FIXME: include the case when write_handle is cancelled - ignore the
1841 /* Cancel the retransmission task */
1842 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
1844 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
1845 socket->retransmission_timeout_task_id =
1846 GNUNET_SCHEDULER_NO_TASK;
1849 socket->write_handle->ack_bitmap = GNUNET_ntohll (ack->bitmap);
1850 socket->receiver_window_available =
1851 ntohl (ack->receive_window_remaining);
1853 /* Check if we have received all acknowledgements */
1854 need_retransmission = GNUNET_NO;
1855 for (packet=0; packet < 64; packet++)
1857 if (NULL == socket->write_handle->messages[packet]) break;
1858 if (GNUNET_YES != ackbitmap_is_bit_set
1859 (&socket->write_handle->ack_bitmap,packet))
1861 need_retransmission = GNUNET_YES;
1865 if (GNUNET_YES == need_retransmission)
1867 write_data (socket);
1869 else /* We have to call the write continuation callback now */
1872 /* Free the packets */
1873 for (packet=0; packet < 64; packet++)
1875 GNUNET_free_non_null (socket->write_handle->messages[packet]);
1877 if (NULL != socket->write_handle->write_cont)
1878 socket->write_handle->write_cont
1879 (socket->write_handle->write_cont_cls,
1881 socket->write_handle->size);
1882 /* We are done with the write handle - Freeing it */
1883 GNUNET_free (socket->write_handle);
1884 socket->write_handle = NULL;
1895 * Message Handler for mesh
1897 * @param cls the 'struct GNUNET_STREAM_Socket'
1898 * @param tunnel connection to the other end
1899 * @param tunnel_ctx unused
1900 * @param sender who sent the message
1901 * @param message the actual message
1902 * @param atsi performance data for the connection
1903 * @return GNUNET_OK to keep the connection open,
1904 * GNUNET_SYSERR to close it (signal serious error)
1907 client_handle_ack (void *cls,
1908 struct GNUNET_MESH_Tunnel *tunnel,
1910 const struct GNUNET_PeerIdentity *sender,
1911 const struct GNUNET_MessageHeader *message,
1912 const struct GNUNET_ATS_Information*atsi)
1914 struct GNUNET_STREAM_Socket *socket = cls;
1915 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1917 return handle_ack (socket, tunnel, sender, ack, atsi);
1922 * Message Handler for mesh
1924 * @param cls the server's listen socket
1925 * @param tunnel connection to the other end
1926 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
1927 * @param sender who sent the message
1928 * @param message the actual message
1929 * @param atsi performance data for the connection
1930 * @return GNUNET_OK to keep the connection open,
1931 * GNUNET_SYSERR to close it (signal serious error)
1934 server_handle_ack (void *cls,
1935 struct GNUNET_MESH_Tunnel *tunnel,
1937 const struct GNUNET_PeerIdentity *sender,
1938 const struct GNUNET_MessageHeader *message,
1939 const struct GNUNET_ATS_Information*atsi)
1941 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1942 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1944 return handle_ack (socket, tunnel, sender, ack, atsi);
1949 * For client message handlers, the stream socket is in the
1952 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
1953 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1954 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1955 sizeof (struct GNUNET_STREAM_AckMessage) },
1956 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1957 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1958 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1959 sizeof (struct GNUNET_STREAM_MessageHeader)},
1960 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1961 sizeof (struct GNUNET_STREAM_MessageHeader)},
1962 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1963 sizeof (struct GNUNET_STREAM_MessageHeader)},
1964 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1965 sizeof (struct GNUNET_STREAM_MessageHeader)},
1966 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1967 sizeof (struct GNUNET_STREAM_MessageHeader)},
1968 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1969 sizeof (struct GNUNET_STREAM_MessageHeader)},
1970 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1971 sizeof (struct GNUNET_STREAM_MessageHeader)},
1977 * For server message handlers, the stream socket is in the
1978 * tunnel context, and the listen socket in the closure argument.
1980 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
1981 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1982 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1983 sizeof (struct GNUNET_STREAM_AckMessage) },
1984 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
1985 sizeof (struct GNUNET_STREAM_MessageHeader)},
1986 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1987 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1988 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1989 sizeof (struct GNUNET_STREAM_MessageHeader)},
1990 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1991 sizeof (struct GNUNET_STREAM_MessageHeader)},
1992 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1993 sizeof (struct GNUNET_STREAM_MessageHeader)},
1994 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1995 sizeof (struct GNUNET_STREAM_MessageHeader)},
1996 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1997 sizeof (struct GNUNET_STREAM_MessageHeader)},
1998 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1999 sizeof (struct GNUNET_STREAM_MessageHeader)},
2000 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2001 sizeof (struct GNUNET_STREAM_MessageHeader)},
2007 * Function called when our target peer is connected to our tunnel
2009 * @param cls the socket for which this tunnel is created
2010 * @param peer the peer identity of the target
2011 * @param atsi performance data for the connection
2014 mesh_peer_connect_callback (void *cls,
2015 const struct GNUNET_PeerIdentity *peer,
2016 const struct GNUNET_ATS_Information * atsi)
2018 struct GNUNET_STREAM_Socket *socket = cls;
2019 struct GNUNET_STREAM_MessageHeader *message;
2020 GNUNET_PEER_Id connected_peer;
2022 connected_peer = GNUNET_PEER_search (peer);
2024 if (connected_peer != socket->other_peer)
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027 "%x: A peer which is not our target has connected",
2033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2034 "%x: Target peer %x connected\n",
2038 /* Set state to INIT */
2039 socket->state = STATE_INIT;
2041 /* Send HELLO message */
2042 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2043 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
2044 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2045 queue_message (socket,
2047 &set_state_hello_wait,
2050 /* Call open callback */
2051 if (NULL == socket->open_cb)
2053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2054 "STREAM_open callback is NULL\n");
2060 * Function called when our target peer is disconnected from our tunnel
2062 * @param cls the socket associated which this tunnel
2063 * @param peer the peer identity of the target
2066 mesh_peer_disconnect_callback (void *cls,
2067 const struct GNUNET_PeerIdentity *peer)
2074 * Method called whenever a peer creates a tunnel to us
2076 * @param cls closure
2077 * @param tunnel new handle to the tunnel
2078 * @param initiator peer that started the tunnel
2079 * @param atsi performance information for the tunnel
2080 * @return initial tunnel context for the tunnel
2081 * (can be NULL -- that's not an error)
2084 new_tunnel_notify (void *cls,
2085 struct GNUNET_MESH_Tunnel *tunnel,
2086 const struct GNUNET_PeerIdentity *initiator,
2087 const struct GNUNET_ATS_Information *atsi)
2089 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2090 struct GNUNET_STREAM_Socket *socket;
2092 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2093 from the same peer again until the socket is closed */
2095 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2096 socket->other_peer = GNUNET_PEER_intern (initiator);
2097 socket->tunnel = tunnel;
2098 socket->session_id = 0; /* FIXME */
2099 socket->state = STATE_INIT;
2100 socket->derived = GNUNET_YES;
2101 socket->our_id = lsocket->our_id;
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2104 "%x: Peer %x initiated tunnel to us\n",
2106 socket->other_peer);
2108 /* FIXME: Copy MESH handle from lsocket to socket */
2109 /* FIXME: What if listen_cb is NULL */
2110 if (GNUNET_SYSERR == lsocket->listen_cb (lsocket->listen_cb_cls,
2114 socket->state = STATE_CLOSED;
2115 /* FIXME: Send CLOSE message and then free */
2116 GNUNET_free (socket);
2117 GNUNET_MESH_tunnel_destroy (tunnel); /* Destroy the tunnel */
2124 * Function called whenever an inbound tunnel is destroyed. Should clean up
2125 * any associated state. This function is NOT called if the client has
2126 * explicitly asked for the tunnel to be destroyed using
2127 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2130 * @param cls closure (set from GNUNET_MESH_connect)
2131 * @param tunnel connection to the other end (henceforth invalid)
2132 * @param tunnel_ctx place where local state associated
2133 * with the tunnel is stored
2136 tunnel_cleaner (void *cls,
2137 const struct GNUNET_MESH_Tunnel *tunnel,
2140 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2143 "%x: Peer %x has terminated connection abruptly\n",
2145 socket->other_peer);
2147 socket->status = GNUNET_STREAM_SHUTDOWN;
2149 /* Clear Transmit handles */
2150 if (NULL != socket->transmit_handle)
2152 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2153 socket->transmit_handle = NULL;
2155 socket->tunnel = NULL;
2165 * Tries to open a stream to the target peer
2167 * @param cfg configuration to use
2168 * @param target the target peer to which the stream has to be opened
2169 * @param app_port the application port number which uniquely identifies this
2171 * @param open_cb this function will be called after stream has be established
2172 * @param open_cb_cls the closure for open_cb
2173 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2174 * @return if successful it returns the stream socket; NULL if stream cannot be
2177 struct GNUNET_STREAM_Socket *
2178 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2179 const struct GNUNET_PeerIdentity *target,
2180 GNUNET_MESH_ApplicationType app_port,
2181 GNUNET_STREAM_OpenCallback open_cb,
2185 struct GNUNET_STREAM_Socket *socket;
2186 struct GNUNET_PeerIdentity own_peer_id;
2187 enum GNUNET_STREAM_Option option;
2188 va_list vargs; /* Variable arguments */
2190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2194 socket->other_peer = GNUNET_PEER_intern (target);
2195 socket->open_cb = open_cb;
2196 socket->open_cls = open_cb_cls;
2197 GNUNET_TESTING_get_peer_identity (cfg, &own_peer_id);
2198 socket->our_id = GNUNET_PEER_intern (&own_peer_id);
2201 socket->retransmit_timeout =
2202 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
2204 va_start (vargs, open_cb_cls); /* Parse variable args */
2206 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2209 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2210 /* Expect struct GNUNET_TIME_Relative */
2211 socket->retransmit_timeout = va_arg (vargs,
2212 struct GNUNET_TIME_Relative);
2214 case GNUNET_STREAM_OPTION_END:
2217 } while (GNUNET_STREAM_OPTION_END != option);
2218 va_end (vargs); /* End of variable args parsing */
2219 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2220 10, /* QUEUE size as parameter? */
2222 NULL, /* No inbound tunnel handler */
2223 &tunnel_cleaner, /* FIXME: not required? */
2224 client_message_handlers,
2225 &app_port); /* We don't get inbound tunnels */
2226 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2228 GNUNET_free (socket);
2232 /* Now create the mesh tunnel to target */
2233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2234 "Creating MESH Tunnel\n");
2235 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2236 NULL, /* Tunnel context */
2237 &mesh_peer_connect_callback,
2238 &mesh_peer_disconnect_callback,
2240 GNUNET_assert (NULL != socket->tunnel);
2241 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2245 "%s() END\n", __func__);
2251 * Shutdown the stream for reading or writing (man 2 shutdown).
2253 * @param socket the stream socket
2254 * @param how SHUT_RD, SHUT_WR or SHUT_RDWR
2257 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2267 * @param socket the stream socket
2270 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
2272 struct MessageQueue *head;
2274 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
2276 /* socket closed with read task pending!? */
2278 GNUNET_SCHEDULER_cancel (socket->read_task_id);
2279 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
2282 /* Clear Transmit handles */
2283 if (NULL != socket->transmit_handle)
2285 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2286 socket->transmit_handle = NULL;
2289 /* Clear existing message queue */
2290 while (NULL != (head = socket->queue_head)) {
2291 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2294 GNUNET_free (head->message);
2298 /* Close associated tunnel */
2299 if (NULL != socket->tunnel)
2301 GNUNET_MESH_tunnel_destroy (socket->tunnel);
2302 socket->tunnel = NULL;
2305 /* Close mesh connection */
2306 if (NULL != socket->mesh && GNUNET_YES != socket->derived)
2308 GNUNET_MESH_disconnect (socket->mesh);
2309 socket->mesh = NULL;
2312 /* Release receive buffer */
2313 if (NULL != socket->receive_buffer)
2315 GNUNET_free (socket->receive_buffer);
2318 GNUNET_free (socket);
2323 * Listens for stream connections for a specific application ports
2325 * @param cfg the configuration to use
2326 * @param app_port the application port for which new streams will be accepted
2327 * @param listen_cb this function will be called when a peer tries to establish
2329 * @param listen_cb_cls closure for listen_cb
2330 * @return listen socket, NULL for any error
2332 struct GNUNET_STREAM_ListenSocket *
2333 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
2334 GNUNET_MESH_ApplicationType app_port,
2335 GNUNET_STREAM_ListenCallback listen_cb,
2336 void *listen_cb_cls)
2338 /* FIXME: Add variable args for passing configration options? */
2339 struct GNUNET_STREAM_ListenSocket *lsocket;
2340 struct GNUNET_PeerIdentity our_peer_id;
2342 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
2343 lsocket->port = app_port;
2344 lsocket->listen_cb = listen_cb;
2345 lsocket->listen_cb_cls = listen_cb_cls;
2346 GNUNET_TESTING_get_peer_identity (cfg, &our_peer_id);
2347 lsocket->our_id = GNUNET_PEER_intern (&our_peer_id);
2348 lsocket->mesh = GNUNET_MESH_connect (cfg,
2349 10, /* FIXME: QUEUE size as parameter? */
2350 lsocket, /* Closure */
2353 server_message_handlers,
2355 GNUNET_assert (NULL != lsocket->mesh);
2361 * Closes the listen socket
2363 * @param lsocket the listen socket
2366 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
2368 /* Close MESH connection */
2369 GNUNET_assert (NULL != lsocket->mesh);
2370 GNUNET_MESH_disconnect (lsocket->mesh);
2372 GNUNET_free (lsocket);
2377 * Tries to write the given data to the stream
2379 * @param socket the socket representing a stream
2380 * @param data the data buffer from where the data is written into the stream
2381 * @param size the number of bytes to be written from the data buffer
2382 * @param timeout the timeout period
2383 * @param write_cont the function to call upon writing some bytes into the stream
2384 * @param write_cont_cls the closure
2385 * @return handle to cancel the operation
2387 struct GNUNET_STREAM_IOWriteHandle *
2388 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
2391 struct GNUNET_TIME_Relative timeout,
2392 GNUNET_STREAM_CompletionContinuation write_cont,
2393 void *write_cont_cls)
2395 unsigned int num_needed_packets;
2396 unsigned int packet;
2397 struct GNUNET_STREAM_IOWriteHandle *io_handle;
2398 uint32_t packet_size;
2399 uint32_t payload_size;
2400 struct GNUNET_STREAM_DataMessage *data_msg;
2402 struct GNUNET_TIME_Relative ack_deadline;
2404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2407 /* Return NULL if there is already a write request pending */
2408 if (NULL != socket->write_handle)
2413 if (!((STATE_ESTABLISHED == socket->state)
2414 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
2415 || (STATE_RECEIVE_CLOSED == socket->state)))
2417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2418 "%x: Attempting to write on a closed (OR) not-yet-established"
2423 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
2424 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
2425 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
2426 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
2427 io_handle->write_cont = write_cont;
2428 io_handle->write_cont_cls = write_cont_cls;
2429 io_handle->size = size;
2431 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2432 determined from RTT */
2433 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
2434 /* Divide the given buffer into packets for sending */
2435 for (packet=0; packet < num_needed_packets; packet++)
2437 if ((packet + 1) * max_payload_size < size)
2439 payload_size = max_payload_size;
2440 packet_size = MAX_PACKET_SIZE;
2444 payload_size = size - packet * max_payload_size;
2445 packet_size = payload_size + sizeof (struct
2446 GNUNET_STREAM_DataMessage);
2448 io_handle->messages[packet] = GNUNET_malloc (packet_size);
2449 io_handle->messages[packet]->header.header.size = htons (packet_size);
2450 io_handle->messages[packet]->header.header.type =
2451 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
2452 io_handle->messages[packet]->sequence_number =
2453 htonl (socket->write_sequence_number++);
2454 io_handle->messages[packet]->offset = htonl (socket->write_offset);
2456 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2457 determined from RTT */
2458 io_handle->messages[packet]->ack_deadline =
2459 GNUNET_TIME_relative_hton (ack_deadline);
2460 data_msg = io_handle->messages[packet];
2461 /* Copy data from given buffer to the packet */
2462 memcpy (&data_msg[1],
2465 sweep += payload_size;
2466 socket->write_offset += payload_size;
2468 socket->write_handle = io_handle;
2469 write_data (socket);
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2472 "%s() END\n", __func__);
2479 * Tries to read data from the stream
2481 * @param socket the socket representing a stream
2482 * @param timeout the timeout period
2483 * @param proc function to call with data (once only)
2484 * @param proc_cls the closure for proc
2485 * @return handle to cancel the operation
2487 struct GNUNET_STREAM_IOReadHandle *
2488 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
2489 struct GNUNET_TIME_Relative timeout,
2490 GNUNET_STREAM_DataProcessor proc,
2493 struct GNUNET_STREAM_IOReadHandle *read_handle;
2495 /* Return NULL if there is already a read handle; the user has to cancel that
2496 first before continuing or has to wait until it is completed */
2497 if (NULL != socket->read_handle) return NULL;
2499 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
2500 read_handle->proc = proc;
2501 socket->read_handle = read_handle;
2503 /* Check if we have a packet at bitmap 0 */
2504 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
2507 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
2512 /* Setup the read timeout task */
2513 socket->read_io_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
2521 * Cancel pending write operation.
2523 * @param ioh handle to operation to cancel
2526 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
2533 * Cancel pending read operation.
2535 * @param ioh handle to operation to cancel
2538 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)