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 receive_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 (ack_msg->header.header.size <= size);
600 size = 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 receive_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 receive_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",
769 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->receive_window_available >= ntohs (io_handle->messages[packet]->header.header.size)) )
782 socket->receive_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",
788 io_handle->messages[packet]->sequence_number);
789 copy_and_queue_message (socket,
790 &io_handle->messages[packet]->header,
791 &write_data_finish_cb,
796 GNUNET_SCHEDULER_add_delayed
797 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
798 &retransmission_timeout_task,
804 * Task for calling the read processor
806 * @param cls the socket
807 * @param tc the task context
810 call_read_processor (void *cls,
811 const struct GNUNET_SCHEDULER_TaskContext *tc)
813 struct GNUNET_STREAM_Socket *socket = cls;
815 size_t valid_read_size;
817 uint32_t sequence_increase;
818 uint32_t offset_increase;
820 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
821 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
824 GNUNET_assert (NULL != socket->read_handle);
825 GNUNET_assert (NULL != socket->read_handle->proc);
827 /* Check the bitmap for any holes */
828 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
830 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
834 /* We only call read processor if we have the first packet */
835 GNUNET_assert (0 < packet);
838 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
840 GNUNET_assert (0 != valid_read_size);
842 /* Cancel the read_io_timeout_task */
843 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task);
844 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
846 /* Call the data processor */
848 socket->read_handle->proc (socket->read_handle->proc_cls,
850 socket->receive_buffer + socket->copy_offset,
852 /* Free the read handle */
853 GNUNET_free (socket->read_handle);
854 socket->read_handle = NULL;
856 GNUNET_assert (read_size <= valid_read_size);
857 socket->copy_offset += read_size;
859 /* Determine upto which packet we can remove from the buffer */
860 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
861 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
864 /* If no packets can be removed we can't move the buffer */
865 if (0 == packet) return;
867 sequence_increase = packet;
869 /* Shift the data in the receive buffer */
870 memmove (socket->receive_buffer,
871 socket->receive_buffer
872 + socket->receive_buffer_boundaries[sequence_increase-1],
873 socket->receive_buffer_size - socket->receive_buffer_boundaries[sequence_increase-1]);
875 /* Shift the bitmap */
876 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
878 /* Set read_sequence_number */
879 socket->read_sequence_number += sequence_increase;
881 /* Set read_offset */
882 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
883 socket->read_offset += offset_increase;
885 /* Fix copy_offset */
886 GNUNET_assert (offset_increase <= socket->copy_offset);
887 socket->copy_offset -= offset_increase;
889 /* Fix relative boundaries */
890 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
892 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
894 socket->receive_buffer_boundaries[packet] =
895 socket->receive_buffer_boundaries[packet + sequence_increase]
899 socket->receive_buffer_boundaries[packet] = 0;
905 * Cancels the existing read io handle
907 * @param cls the closure from the SCHEDULER call
908 * @param tc the task context
911 read_io_timeout (void *cls,
912 const struct GNUNET_SCHEDULER_TaskContext *tc)
914 struct GNUNET_STREAM_Socket *socket = cls;
916 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
917 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
919 GNUNET_SCHEDULER_cancel (socket->read_task_id);
920 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
922 GNUNET_assert (NULL != socket->read_handle);
924 GNUNET_free (socket->read_handle);
925 socket->read_handle = NULL;
930 * Handler for DATA messages; Same for both client and server
932 * @param socket the socket through which the ack was received
933 * @param tunnel connection to the other end
934 * @param sender who sent the message
935 * @param msg the data message
936 * @param atsi performance data for the connection
937 * @return GNUNET_OK to keep the connection open,
938 * GNUNET_SYSERR to close it (signal serious error)
941 handle_data (struct GNUNET_STREAM_Socket *socket,
942 struct GNUNET_MESH_Tunnel *tunnel,
943 const struct GNUNET_PeerIdentity *sender,
944 const struct GNUNET_STREAM_DataMessage *msg,
945 const struct GNUNET_ATS_Information*atsi)
948 uint32_t bytes_needed;
949 uint32_t relative_offset;
950 uint32_t relative_sequence_number;
953 size = htons (msg->header.header.size);
954 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
957 return GNUNET_SYSERR;
960 if (GNUNET_PEER_search (sender) != socket->other_peer)
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
963 "%x: Received DATA from non-confirming peer\n",
968 switch (socket->state)
970 case STATE_ESTABLISHED:
971 case STATE_TRANSMIT_CLOSED:
972 case STATE_TRANSMIT_CLOSE_WAIT:
974 /* check if the message's sequence number is in the range we are
976 relative_sequence_number =
977 ntohl (msg->sequence_number) - socket->read_sequence_number;
978 if ( relative_sequence_number > 64)
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "%x: Ignoring received message with sequence number %u\n",
983 ntohl (msg->sequence_number));
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988 "%x: Receiving DATA with sequence number: %u and size: %d "
991 ntohl (msg->sequence_number),
992 ntohs (msg->header.header.size),
995 /* Check if we have to allocate the buffer */
996 size -= sizeof (struct GNUNET_STREAM_DataMessage);
997 relative_offset = ntohl (msg->offset) - socket->read_offset;
998 bytes_needed = relative_offset + size;
1000 if (bytes_needed > socket->receive_buffer_size)
1002 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1004 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1006 socket->receive_buffer_size = bytes_needed;
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011 "%x: Cannot accommodate packet %d as buffer is",
1014 ntohl (msg->sequence_number));
1019 /* Copy Data to buffer */
1021 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1022 memcpy (socket->receive_buffer + relative_offset,
1025 socket->receive_buffer_boundaries[relative_sequence_number] =
1026 relative_offset + size;
1028 /* Modify the ACK bitmap */
1029 ackbitmap_modify_bit (&socket->ack_bitmap,
1030 relative_sequence_number,
1033 /* Start ACK sending task if one is not already present */
1034 if (0 == socket->ack_task_id)
1036 socket->ack_task_id =
1037 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1038 (msg->ack_deadline),
1043 if ((NULL != socket->read_handle) /* A read handle is waiting */
1044 /* There is no current read task */
1045 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
1046 /* We have the first packet */
1047 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
1050 socket->read_task_id =
1051 GNUNET_SCHEDULER_add_now (&call_read_processor,
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "%x: Received data message when it cannot be handled\n",
1067 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1069 * @param cls the socket (set from GNUNET_MESH_connect)
1070 * @param tunnel connection to the other end
1071 * @param tunnel_ctx place to store local state associated with the tunnel
1072 * @param sender who sent the message
1073 * @param message the actual message
1074 * @param atsi performance data for the connection
1075 * @return GNUNET_OK to keep the connection open,
1076 * GNUNET_SYSERR to close it (signal serious error)
1079 client_handle_data (void *cls,
1080 struct GNUNET_MESH_Tunnel *tunnel,
1082 const struct GNUNET_PeerIdentity *sender,
1083 const struct GNUNET_MessageHeader *message,
1084 const struct GNUNET_ATS_Information*atsi)
1086 struct GNUNET_STREAM_Socket *socket = cls;
1088 return handle_data (socket,
1091 (const struct GNUNET_STREAM_DataMessage *) message,
1097 * Callback to set state to ESTABLISHED
1099 * @param cls the closure from queue_message FIXME: document
1100 * @param socket the socket to requiring state change
1103 set_state_established (void *cls,
1104 struct GNUNET_STREAM_Socket *socket)
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1107 "%x: Attaining ESTABLISHED state\n",
1109 socket->write_offset = 0;
1110 socket->read_offset = 0;
1111 socket->state = STATE_ESTABLISHED;
1112 if (socket->open_cb)
1113 socket->open_cb (socket->open_cls, socket);
1118 * Callback to set state to HELLO_WAIT
1120 * @param cls the closure from queue_message
1121 * @param socket the socket to requiring state change
1124 set_state_hello_wait (void *cls,
1125 struct GNUNET_STREAM_Socket *socket)
1127 GNUNET_assert (STATE_INIT == socket->state);
1128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129 "%x: Attaining HELLO_WAIT state\n",
1131 socket->state = STATE_HELLO_WAIT;
1136 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1138 * @param cls the socket (set from GNUNET_MESH_connect)
1139 * @param tunnel connection to the other end
1140 * @param tunnel_ctx this is NULL
1141 * @param sender who sent the message
1142 * @param message the actual message
1143 * @param atsi performance data for the connection
1144 * @return GNUNET_OK to keep the connection open,
1145 * GNUNET_SYSERR to close it (signal serious error)
1148 client_handle_hello_ack (void *cls,
1149 struct GNUNET_MESH_Tunnel *tunnel,
1151 const struct GNUNET_PeerIdentity *sender,
1152 const struct GNUNET_MessageHeader *message,
1153 const struct GNUNET_ATS_Information*atsi)
1155 struct GNUNET_STREAM_Socket *socket = cls;
1156 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1157 struct GNUNET_STREAM_HelloAckMessage *reply;
1159 if (GNUNET_PEER_search (sender) != socket->other_peer)
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "%x: Received HELLO_ACK from non-confirming peer\n",
1166 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168 "%x: Received HELLO_ACK from %x\n",
1170 socket->other_peer);
1172 GNUNET_assert (socket->tunnel == tunnel);
1173 switch (socket->state)
1175 case STATE_HELLO_WAIT:
1176 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 "%x: Read sequence number %u\n",
1180 (unsigned int) socket->read_sequence_number);
1181 socket->receive_window_available = ntohl (ack_msg->receive_window_size);
1182 /* Get the random sequence number */
1183 socket->write_sequence_number =
1184 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "%x: Generated write sequence number %u\n",
1188 (unsigned int) socket->write_sequence_number);
1190 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1191 reply->header.header.size =
1192 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1193 reply->header.header.type =
1194 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1195 reply->sequence_number = htonl (socket->write_sequence_number);
1196 reply->receive_window_size = htonl (RECEIVE_BUFFER_SIZE);
1197 queue_message (socket,
1199 &set_state_established,
1202 case STATE_ESTABLISHED:
1203 case STATE_RECEIVE_CLOSE_WAIT:
1204 // call statistics (# ACKs ignored++)
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "%x: Server %x sent HELLO_ACK when in state %d\n",
1213 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1214 return GNUNET_SYSERR;
1221 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
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_reset (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;
1247 * Common message handler for handling TRANSMIT_CLOSE messages
1249 * @param socket the socket through which the ack was received
1250 * @param tunnel connection to the other end
1251 * @param sender who sent the message
1252 * @param msg the transmit close message
1253 * @param atsi performance data for the connection
1254 * @return GNUNET_OK to keep the connection open,
1255 * GNUNET_SYSERR to close it (signal serious error)
1258 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1259 struct GNUNET_MESH_Tunnel *tunnel,
1260 const struct GNUNET_PeerIdentity *sender,
1261 const struct GNUNET_STREAM_MessageHeader *msg,
1262 const struct GNUNET_ATS_Information*atsi)
1264 struct GNUNET_STREAM_MessageHeader *reply;
1266 switch (socket->state)
1268 case STATE_ESTABLISHED:
1269 socket->state = STATE_RECEIVE_CLOSED;
1271 /* Send TRANSMIT_CLOSE_ACK */
1272 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1273 reply->header.type =
1274 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1275 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1276 queue_message (socket, reply, NULL, NULL);
1280 /* FIXME: Call statistics? */
1288 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1290 * @param cls the socket (set from GNUNET_MESH_connect)
1291 * @param tunnel connection to the other end
1292 * @param tunnel_ctx this is NULL
1293 * @param sender who sent the message
1294 * @param message the actual message
1295 * @param atsi performance data for the connection
1296 * @return GNUNET_OK to keep the connection open,
1297 * GNUNET_SYSERR to close it (signal serious error)
1300 client_handle_transmit_close (void *cls,
1301 struct GNUNET_MESH_Tunnel *tunnel,
1303 const struct GNUNET_PeerIdentity *sender,
1304 const struct GNUNET_MessageHeader *message,
1305 const struct GNUNET_ATS_Information*atsi)
1307 struct GNUNET_STREAM_Socket *socket = cls;
1309 return handle_transmit_close (socket,
1312 (struct GNUNET_STREAM_MessageHeader *)message,
1318 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1320 * @param cls the socket (set from GNUNET_MESH_connect)
1321 * @param tunnel connection to the other end
1322 * @param tunnel_ctx this is NULL
1323 * @param sender who sent the message
1324 * @param message the actual message
1325 * @param atsi performance data for the connection
1326 * @return GNUNET_OK to keep the connection open,
1327 * GNUNET_SYSERR to close it (signal serious error)
1330 client_handle_transmit_close_ack (void *cls,
1331 struct GNUNET_MESH_Tunnel *tunnel,
1333 const struct GNUNET_PeerIdentity *sender,
1334 const struct GNUNET_MessageHeader *message,
1335 const struct GNUNET_ATS_Information*atsi)
1337 struct GNUNET_STREAM_Socket *socket = cls;
1344 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1346 * @param cls the socket (set from GNUNET_MESH_connect)
1347 * @param tunnel connection to the other end
1348 * @param tunnel_ctx this is NULL
1349 * @param sender who sent the message
1350 * @param message the actual message
1351 * @param atsi performance data for the connection
1352 * @return GNUNET_OK to keep the connection open,
1353 * GNUNET_SYSERR to close it (signal serious error)
1356 client_handle_receive_close (void *cls,
1357 struct GNUNET_MESH_Tunnel *tunnel,
1359 const struct GNUNET_PeerIdentity *sender,
1360 const struct GNUNET_MessageHeader *message,
1361 const struct GNUNET_ATS_Information*atsi)
1363 struct GNUNET_STREAM_Socket *socket = cls;
1370 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1372 * @param cls the socket (set from GNUNET_MESH_connect)
1373 * @param tunnel connection to the other end
1374 * @param tunnel_ctx this is NULL
1375 * @param sender who sent the message
1376 * @param message the actual message
1377 * @param atsi performance data for the connection
1378 * @return GNUNET_OK to keep the connection open,
1379 * GNUNET_SYSERR to close it (signal serious error)
1382 client_handle_receive_close_ack (void *cls,
1383 struct GNUNET_MESH_Tunnel *tunnel,
1385 const struct GNUNET_PeerIdentity *sender,
1386 const struct GNUNET_MessageHeader *message,
1387 const struct GNUNET_ATS_Information*atsi)
1389 struct GNUNET_STREAM_Socket *socket = cls;
1396 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1398 * @param cls the socket (set from GNUNET_MESH_connect)
1399 * @param tunnel connection to the other end
1400 * @param tunnel_ctx this is NULL
1401 * @param sender who sent the message
1402 * @param message the actual message
1403 * @param atsi performance data for the connection
1404 * @return GNUNET_OK to keep the connection open,
1405 * GNUNET_SYSERR to close it (signal serious error)
1408 client_handle_close (void *cls,
1409 struct GNUNET_MESH_Tunnel *tunnel,
1411 const struct GNUNET_PeerIdentity *sender,
1412 const struct GNUNET_MessageHeader *message,
1413 const struct GNUNET_ATS_Information*atsi)
1415 struct GNUNET_STREAM_Socket *socket = cls;
1422 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1424 * @param cls the socket (set from GNUNET_MESH_connect)
1425 * @param tunnel connection to the other end
1426 * @param tunnel_ctx this is NULL
1427 * @param sender who sent the message
1428 * @param message the actual message
1429 * @param atsi performance data for the connection
1430 * @return GNUNET_OK to keep the connection open,
1431 * GNUNET_SYSERR to close it (signal serious error)
1434 client_handle_close_ack (void *cls,
1435 struct GNUNET_MESH_Tunnel *tunnel,
1437 const struct GNUNET_PeerIdentity *sender,
1438 const struct GNUNET_MessageHeader *message,
1439 const struct GNUNET_ATS_Information*atsi)
1441 struct GNUNET_STREAM_Socket *socket = cls;
1446 /*****************************/
1447 /* Server's Message Handlers */
1448 /*****************************/
1451 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1453 * @param cls the closure
1454 * @param tunnel connection to the other end
1455 * @param tunnel_ctx the socket
1456 * @param sender who sent the message
1457 * @param message the actual message
1458 * @param atsi performance data for the connection
1459 * @return GNUNET_OK to keep the connection open,
1460 * GNUNET_SYSERR to close it (signal serious error)
1463 server_handle_data (void *cls,
1464 struct GNUNET_MESH_Tunnel *tunnel,
1466 const struct GNUNET_PeerIdentity *sender,
1467 const struct GNUNET_MessageHeader *message,
1468 const struct GNUNET_ATS_Information*atsi)
1470 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1472 return handle_data (socket,
1475 (const struct GNUNET_STREAM_DataMessage *)message,
1481 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1483 * @param cls the closure
1484 * @param tunnel connection to the other end
1485 * @param tunnel_ctx the socket
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 server_handle_hello (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 = *tunnel_ctx;
1501 struct GNUNET_STREAM_HelloAckMessage *reply;
1503 if (GNUNET_PEER_search (sender) != socket->other_peer)
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1506 "%x: Received HELLO from non-confirming peer\n",
1511 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO ==
1512 ntohs (message->type));
1513 GNUNET_assert (socket->tunnel == tunnel);
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 "%x: Received HELLO from %x\n",
1517 socket->other_peer);
1519 if (STATE_INIT == socket->state)
1521 /* Get the random sequence number */
1522 socket->write_sequence_number =
1523 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "%x: Generated write sequence number %u\n",
1527 (unsigned int) socket->write_sequence_number);
1529 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1530 reply->header.header.size =
1531 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1532 reply->header.header.type =
1533 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1534 reply->sequence_number = htonl (socket->write_sequence_number);
1535 queue_message (socket,
1537 &set_state_hello_wait,
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "Client sent HELLO when in state %d\n", socket->state);
1544 /* FIXME: Send RESET? */
1552 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1554 * @param cls the closure
1555 * @param tunnel connection to the other end
1556 * @param tunnel_ctx the socket
1557 * @param sender who sent the message
1558 * @param message the actual message
1559 * @param atsi performance data for the connection
1560 * @return GNUNET_OK to keep the connection open,
1561 * GNUNET_SYSERR to close it (signal serious error)
1564 server_handle_hello_ack (void *cls,
1565 struct GNUNET_MESH_Tunnel *tunnel,
1567 const struct GNUNET_PeerIdentity *sender,
1568 const struct GNUNET_MessageHeader *message,
1569 const struct GNUNET_ATS_Information*atsi)
1571 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1572 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1574 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
1575 ntohs (message->type));
1576 GNUNET_assert (socket->tunnel == tunnel);
1577 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1578 if (STATE_HELLO_WAIT == socket->state)
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1581 "%x: Received HELLO_ACK from %x\n",
1583 socket->other_peer);
1584 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 "%x: Read sequence number %u\n",
1588 (unsigned int) socket->read_sequence_number);
1589 socket->receive_window_available =
1590 ntohl (ack_message->receive_window_size);
1591 /* Attain ESTABLISHED state */
1592 set_state_established (NULL, socket);
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1597 "Client sent HELLO_ACK when in state %d\n", socket->state);
1598 /* FIXME: Send RESET? */
1606 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1608 * @param cls the closure
1609 * @param tunnel connection to the other end
1610 * @param tunnel_ctx the socket
1611 * @param sender who sent the message
1612 * @param message the actual message
1613 * @param atsi performance data for the connection
1614 * @return GNUNET_OK to keep the connection open,
1615 * GNUNET_SYSERR to close it (signal serious error)
1618 server_handle_reset (void *cls,
1619 struct GNUNET_MESH_Tunnel *tunnel,
1621 const struct GNUNET_PeerIdentity *sender,
1622 const struct GNUNET_MessageHeader *message,
1623 const struct GNUNET_ATS_Information*atsi)
1625 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1632 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1634 * @param cls the closure
1635 * @param tunnel connection to the other end
1636 * @param tunnel_ctx the socket
1637 * @param sender who sent the message
1638 * @param message the actual message
1639 * @param atsi performance data for the connection
1640 * @return GNUNET_OK to keep the connection open,
1641 * GNUNET_SYSERR to close it (signal serious error)
1644 server_handle_transmit_close (void *cls,
1645 struct GNUNET_MESH_Tunnel *tunnel,
1647 const struct GNUNET_PeerIdentity *sender,
1648 const struct GNUNET_MessageHeader *message,
1649 const struct GNUNET_ATS_Information*atsi)
1651 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1653 return handle_transmit_close (socket,
1656 (struct GNUNET_STREAM_MessageHeader *)message,
1662 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1664 * @param cls the closure
1665 * @param tunnel connection to the other end
1666 * @param tunnel_ctx the socket
1667 * @param sender who sent the message
1668 * @param message the actual message
1669 * @param atsi performance data for the connection
1670 * @return GNUNET_OK to keep the connection open,
1671 * GNUNET_SYSERR to close it (signal serious error)
1674 server_handle_transmit_close_ack (void *cls,
1675 struct GNUNET_MESH_Tunnel *tunnel,
1677 const struct GNUNET_PeerIdentity *sender,
1678 const struct GNUNET_MessageHeader *message,
1679 const struct GNUNET_ATS_Information*atsi)
1681 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1688 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1690 * @param cls the closure
1691 * @param tunnel connection to the other end
1692 * @param tunnel_ctx the socket
1693 * @param sender who sent the message
1694 * @param message the actual message
1695 * @param atsi performance data for the connection
1696 * @return GNUNET_OK to keep the connection open,
1697 * GNUNET_SYSERR to close it (signal serious error)
1700 server_handle_receive_close (void *cls,
1701 struct GNUNET_MESH_Tunnel *tunnel,
1703 const struct GNUNET_PeerIdentity *sender,
1704 const struct GNUNET_MessageHeader *message,
1705 const struct GNUNET_ATS_Information*atsi)
1707 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1714 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1716 * @param cls the closure
1717 * @param tunnel connection to the other end
1718 * @param tunnel_ctx the socket
1719 * @param sender who sent the message
1720 * @param message the actual message
1721 * @param atsi performance data for the connection
1722 * @return GNUNET_OK to keep the connection open,
1723 * GNUNET_SYSERR to close it (signal serious error)
1726 server_handle_receive_close_ack (void *cls,
1727 struct GNUNET_MESH_Tunnel *tunnel,
1729 const struct GNUNET_PeerIdentity *sender,
1730 const struct GNUNET_MessageHeader *message,
1731 const struct GNUNET_ATS_Information*atsi)
1733 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1740 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1742 * @param cls the closure
1743 * @param tunnel connection to the other end
1744 * @param tunnel_ctx the socket
1745 * @param sender who sent the message
1746 * @param message the actual message
1747 * @param atsi performance data for the connection
1748 * @return GNUNET_OK to keep the connection open,
1749 * GNUNET_SYSERR to close it (signal serious error)
1752 server_handle_close (void *cls,
1753 struct GNUNET_MESH_Tunnel *tunnel,
1755 const struct GNUNET_PeerIdentity *sender,
1756 const struct GNUNET_MessageHeader *message,
1757 const struct GNUNET_ATS_Information*atsi)
1759 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1766 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1768 * @param cls the closure
1769 * @param tunnel connection to the other end
1770 * @param tunnel_ctx the socket
1771 * @param sender who sent the message
1772 * @param message the actual message
1773 * @param atsi performance data for the connection
1774 * @return GNUNET_OK to keep the connection open,
1775 * GNUNET_SYSERR to close it (signal serious error)
1778 server_handle_close_ack (void *cls,
1779 struct GNUNET_MESH_Tunnel *tunnel,
1781 const struct GNUNET_PeerIdentity *sender,
1782 const struct GNUNET_MessageHeader *message,
1783 const struct GNUNET_ATS_Information*atsi)
1785 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1792 * Message Handler for mesh
1794 * @param socket the socket through which the ack was received
1795 * @param tunnel connection to the other end
1796 * @param sender who sent the message
1797 * @param ack the acknowledgment message
1798 * @param atsi performance data for the connection
1799 * @return GNUNET_OK to keep the connection open,
1800 * GNUNET_SYSERR to close it (signal serious error)
1803 handle_ack (struct GNUNET_STREAM_Socket *socket,
1804 struct GNUNET_MESH_Tunnel *tunnel,
1805 const struct GNUNET_PeerIdentity *sender,
1806 const struct GNUNET_STREAM_AckMessage *ack,
1807 const struct GNUNET_ATS_Information*atsi)
1809 unsigned int packet;
1810 int need_retransmission;
1812 if (GNUNET_PEER_search (sender) != socket->other_peer)
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1815 "%x: Received ACK from non-confirming peer\n",
1820 switch (socket->state)
1822 case (STATE_ESTABLISHED):
1823 if (NULL == socket->write_handle)
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 "%x: Received DATA_ACK when write_handle is NULL\n",
1831 if (!((socket->write_sequence_number
1832 - htonl (ack->base_sequence_number)) < 64))
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1835 "%x: Received DATA_ACK with unexpected base sequence",
1840 /* FIXME: include the case when write_handle is cancelled - ignore the
1843 /* Cancel the retransmission task */
1844 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
1846 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
1847 socket->retransmission_timeout_task_id =
1848 GNUNET_SCHEDULER_NO_TASK;
1851 socket->write_handle->ack_bitmap = GNUNET_ntohll (ack->bitmap);
1852 socket->receive_window_available =
1853 ntohl (ack->receive_window_remaining);
1855 /* Check if we have received all acknowledgements */
1856 need_retransmission = GNUNET_NO;
1857 for (packet=0; packet < 64; packet++)
1859 if (NULL == socket->write_handle->messages[packet]) break;
1860 if (GNUNET_YES != ackbitmap_is_bit_set
1861 (&socket->write_handle->ack_bitmap,packet))
1863 need_retransmission = GNUNET_YES;
1867 if (GNUNET_YES == need_retransmission)
1869 write_data (socket);
1871 else /* We have to call the write continuation callback now */
1874 /* Free the packets */
1875 for (packet=0; packet < 64; packet++)
1877 GNUNET_free_non_null (socket->write_handle->messages[packet]);
1879 if (NULL != socket->write_handle->write_cont)
1880 socket->write_handle->write_cont
1881 (socket->write_handle->write_cont_cls,
1883 socket->write_handle->size);
1884 /* We are done with the write handle - Freeing it */
1885 GNUNET_free (socket->write_handle);
1886 socket->write_handle = NULL;
1897 * Message Handler for mesh
1899 * @param cls the 'struct GNUNET_STREAM_Socket'
1900 * @param tunnel connection to the other end
1901 * @param tunnel_ctx unused
1902 * @param sender who sent the message
1903 * @param message the actual message
1904 * @param atsi performance data for the connection
1905 * @return GNUNET_OK to keep the connection open,
1906 * GNUNET_SYSERR to close it (signal serious error)
1909 client_handle_ack (void *cls,
1910 struct GNUNET_MESH_Tunnel *tunnel,
1912 const struct GNUNET_PeerIdentity *sender,
1913 const struct GNUNET_MessageHeader *message,
1914 const struct GNUNET_ATS_Information*atsi)
1916 struct GNUNET_STREAM_Socket *socket = cls;
1917 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1919 return handle_ack (socket, tunnel, sender, ack, atsi);
1924 * Message Handler for mesh
1926 * @param cls the server's listen socket
1927 * @param tunnel connection to the other end
1928 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
1929 * @param sender who sent the message
1930 * @param message the actual message
1931 * @param atsi performance data for the connection
1932 * @return GNUNET_OK to keep the connection open,
1933 * GNUNET_SYSERR to close it (signal serious error)
1936 server_handle_ack (void *cls,
1937 struct GNUNET_MESH_Tunnel *tunnel,
1939 const struct GNUNET_PeerIdentity *sender,
1940 const struct GNUNET_MessageHeader *message,
1941 const struct GNUNET_ATS_Information*atsi)
1943 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1944 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1946 return handle_ack (socket, tunnel, sender, ack, atsi);
1951 * For client message handlers, the stream socket is in the
1954 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
1955 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1956 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1957 sizeof (struct GNUNET_STREAM_AckMessage) },
1958 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1959 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1960 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1961 sizeof (struct GNUNET_STREAM_MessageHeader)},
1962 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1963 sizeof (struct GNUNET_STREAM_MessageHeader)},
1964 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1965 sizeof (struct GNUNET_STREAM_MessageHeader)},
1966 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1967 sizeof (struct GNUNET_STREAM_MessageHeader)},
1968 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1969 sizeof (struct GNUNET_STREAM_MessageHeader)},
1970 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1971 sizeof (struct GNUNET_STREAM_MessageHeader)},
1972 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1973 sizeof (struct GNUNET_STREAM_MessageHeader)},
1979 * For server message handlers, the stream socket is in the
1980 * tunnel context, and the listen socket in the closure argument.
1982 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
1983 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1984 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1985 sizeof (struct GNUNET_STREAM_AckMessage) },
1986 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
1987 sizeof (struct GNUNET_STREAM_MessageHeader)},
1988 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1989 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1990 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1991 sizeof (struct GNUNET_STREAM_MessageHeader)},
1992 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1993 sizeof (struct GNUNET_STREAM_MessageHeader)},
1994 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1995 sizeof (struct GNUNET_STREAM_MessageHeader)},
1996 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1997 sizeof (struct GNUNET_STREAM_MessageHeader)},
1998 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1999 sizeof (struct GNUNET_STREAM_MessageHeader)},
2000 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2001 sizeof (struct GNUNET_STREAM_MessageHeader)},
2002 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2003 sizeof (struct GNUNET_STREAM_MessageHeader)},
2009 * Function called when our target peer is connected to our tunnel
2011 * @param cls the socket for which this tunnel is created
2012 * @param peer the peer identity of the target
2013 * @param atsi performance data for the connection
2016 mesh_peer_connect_callback (void *cls,
2017 const struct GNUNET_PeerIdentity *peer,
2018 const struct GNUNET_ATS_Information * atsi)
2020 struct GNUNET_STREAM_Socket *socket = cls;
2021 struct GNUNET_STREAM_MessageHeader *message;
2022 GNUNET_PEER_Id connected_peer;
2024 connected_peer = GNUNET_PEER_search (peer);
2026 if (connected_peer != socket->other_peer)
2028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2029 "%x: A peer which is not our target has connected",
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2036 "%x: Target peer %x connected\n",
2040 /* Set state to INIT */
2041 socket->state = STATE_INIT;
2043 /* Send HELLO message */
2044 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2045 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
2046 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2047 queue_message (socket,
2049 &set_state_hello_wait,
2052 /* Call open callback */
2053 if (NULL == socket->open_cb)
2055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2056 "STREAM_open callback is NULL\n");
2062 * Function called when our target peer is disconnected from our tunnel
2064 * @param cls the socket associated which this tunnel
2065 * @param peer the peer identity of the target
2068 mesh_peer_disconnect_callback (void *cls,
2069 const struct GNUNET_PeerIdentity *peer)
2076 * Method called whenever a peer creates a tunnel to us
2078 * @param cls closure
2079 * @param tunnel new handle to the tunnel
2080 * @param initiator peer that started the tunnel
2081 * @param atsi performance information for the tunnel
2082 * @return initial tunnel context for the tunnel
2083 * (can be NULL -- that's not an error)
2086 new_tunnel_notify (void *cls,
2087 struct GNUNET_MESH_Tunnel *tunnel,
2088 const struct GNUNET_PeerIdentity *initiator,
2089 const struct GNUNET_ATS_Information *atsi)
2091 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2092 struct GNUNET_STREAM_Socket *socket;
2094 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2095 from the same peer again until the socket is closed */
2097 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2098 socket->other_peer = GNUNET_PEER_intern (initiator);
2099 socket->tunnel = tunnel;
2100 socket->session_id = 0; /* FIXME */
2101 socket->state = STATE_INIT;
2102 socket->derived = GNUNET_YES;
2103 socket->our_id = lsocket->our_id;
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2106 "%x: Peer %x initiated tunnel to us\n",
2108 socket->other_peer);
2110 /* FIXME: Copy MESH handle from lsocket to socket */
2111 /* FIXME: What if listen_cb is NULL */
2112 if (GNUNET_SYSERR == lsocket->listen_cb (lsocket->listen_cb_cls,
2116 socket->state = STATE_CLOSED;
2117 /* FIXME: Send CLOSE message and then free */
2118 GNUNET_free (socket);
2119 GNUNET_MESH_tunnel_destroy (tunnel); /* Destroy the tunnel */
2126 * Function called whenever an inbound tunnel is destroyed. Should clean up
2127 * any associated state. This function is NOT called if the client has
2128 * explicitly asked for the tunnel to be destroyed using
2129 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2132 * @param cls closure (set from GNUNET_MESH_connect)
2133 * @param tunnel connection to the other end (henceforth invalid)
2134 * @param tunnel_ctx place where local state associated
2135 * with the tunnel is stored
2138 tunnel_cleaner (void *cls,
2139 const struct GNUNET_MESH_Tunnel *tunnel,
2142 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 "%x: Peer %x has terminated connection abruptly\n",
2147 socket->other_peer);
2149 socket->status = GNUNET_STREAM_SHUTDOWN;
2151 /* Clear Transmit handles */
2152 if (NULL != socket->transmit_handle)
2154 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2155 socket->transmit_handle = NULL;
2157 socket->tunnel = NULL;
2167 * Tries to open a stream to the target peer
2169 * @param cfg configuration to use
2170 * @param target the target peer to which the stream has to be opened
2171 * @param app_port the application port number which uniquely identifies this
2173 * @param open_cb this function will be called after stream has be established
2174 * @param open_cb_cls the closure for open_cb
2175 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2176 * @return if successful it returns the stream socket; NULL if stream cannot be
2179 struct GNUNET_STREAM_Socket *
2180 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2181 const struct GNUNET_PeerIdentity *target,
2182 GNUNET_MESH_ApplicationType app_port,
2183 GNUNET_STREAM_OpenCallback open_cb,
2187 struct GNUNET_STREAM_Socket *socket;
2188 struct GNUNET_PeerIdentity own_peer_id;
2189 enum GNUNET_STREAM_Option option;
2190 va_list vargs; /* Variable arguments */
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2196 socket->other_peer = GNUNET_PEER_intern (target);
2197 socket->open_cb = open_cb;
2198 socket->open_cls = open_cb_cls;
2199 GNUNET_TESTING_get_peer_identity (cfg, &own_peer_id);
2200 socket->our_id = GNUNET_PEER_intern (&own_peer_id);
2203 socket->retransmit_timeout =
2204 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
2206 va_start (vargs, open_cb_cls); /* Parse variable args */
2208 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2211 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2212 /* Expect struct GNUNET_TIME_Relative */
2213 socket->retransmit_timeout = va_arg (vargs,
2214 struct GNUNET_TIME_Relative);
2216 case GNUNET_STREAM_OPTION_END:
2219 } while (GNUNET_STREAM_OPTION_END != option);
2220 va_end (vargs); /* End of variable args parsing */
2221 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2222 10, /* QUEUE size as parameter? */
2224 NULL, /* No inbound tunnel handler */
2225 &tunnel_cleaner, /* FIXME: not required? */
2226 client_message_handlers,
2227 &app_port); /* We don't get inbound tunnels */
2228 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2230 GNUNET_free (socket);
2234 /* Now create the mesh tunnel to target */
2235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2236 "Creating MESH Tunnel\n");
2237 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2238 NULL, /* Tunnel context */
2239 &mesh_peer_connect_callback,
2240 &mesh_peer_disconnect_callback,
2242 GNUNET_assert (NULL != socket->tunnel);
2243 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2247 "%s() END\n", __func__);
2253 * Shutdown the stream for reading or writing (man 2 shutdown).
2255 * @param socket the stream socket
2256 * @param how SHUT_RD, SHUT_WR or SHUT_RDWR
2259 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2269 * @param socket the stream socket
2272 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
2274 struct MessageQueue *head;
2276 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
2278 /* socket closed with read task pending!? */
2280 GNUNET_SCHEDULER_cancel (socket->read_task_id);
2281 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
2284 /* Clear Transmit handles */
2285 if (NULL != socket->transmit_handle)
2287 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2288 socket->transmit_handle = NULL;
2291 /* Clear existing message queue */
2292 while (NULL != (head = socket->queue_head)) {
2293 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2296 GNUNET_free (head->message);
2300 /* Close associated tunnel */
2301 if (NULL != socket->tunnel)
2303 GNUNET_MESH_tunnel_destroy (socket->tunnel);
2304 socket->tunnel = NULL;
2307 /* Close mesh connection */
2308 if (NULL != socket->mesh && GNUNET_YES != socket->derived)
2310 GNUNET_MESH_disconnect (socket->mesh);
2311 socket->mesh = NULL;
2314 /* Release receive buffer */
2315 if (NULL != socket->receive_buffer)
2317 GNUNET_free (socket->receive_buffer);
2320 GNUNET_free (socket);
2325 * Listens for stream connections for a specific application ports
2327 * @param cfg the configuration to use
2328 * @param app_port the application port for which new streams will be accepted
2329 * @param listen_cb this function will be called when a peer tries to establish
2331 * @param listen_cb_cls closure for listen_cb
2332 * @return listen socket, NULL for any error
2334 struct GNUNET_STREAM_ListenSocket *
2335 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
2336 GNUNET_MESH_ApplicationType app_port,
2337 GNUNET_STREAM_ListenCallback listen_cb,
2338 void *listen_cb_cls)
2340 /* FIXME: Add variable args for passing configration options? */
2341 struct GNUNET_STREAM_ListenSocket *lsocket;
2342 struct GNUNET_PeerIdentity our_peer_id;
2344 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
2345 lsocket->port = app_port;
2346 lsocket->listen_cb = listen_cb;
2347 lsocket->listen_cb_cls = listen_cb_cls;
2348 GNUNET_TESTING_get_peer_identity (cfg, &our_peer_id);
2349 lsocket->our_id = GNUNET_PEER_intern (&our_peer_id);
2350 lsocket->mesh = GNUNET_MESH_connect (cfg,
2351 10, /* FIXME: QUEUE size as parameter? */
2352 lsocket, /* Closure */
2355 server_message_handlers,
2357 GNUNET_assert (NULL != lsocket->mesh);
2363 * Closes the listen socket
2365 * @param lsocket the listen socket
2368 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
2370 /* Close MESH connection */
2371 GNUNET_assert (NULL != lsocket->mesh);
2372 GNUNET_MESH_disconnect (lsocket->mesh);
2374 GNUNET_free (lsocket);
2379 * Tries to write the given data to the stream
2381 * @param socket the socket representing a stream
2382 * @param data the data buffer from where the data is written into the stream
2383 * @param size the number of bytes to be written from the data buffer
2384 * @param timeout the timeout period
2385 * @param write_cont the function to call upon writing some bytes into the stream
2386 * @param write_cont_cls the closure
2387 * @return handle to cancel the operation
2389 struct GNUNET_STREAM_IOWriteHandle *
2390 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
2393 struct GNUNET_TIME_Relative timeout,
2394 GNUNET_STREAM_CompletionContinuation write_cont,
2395 void *write_cont_cls)
2397 unsigned int num_needed_packets;
2398 unsigned int packet;
2399 struct GNUNET_STREAM_IOWriteHandle *io_handle;
2400 uint32_t packet_size;
2401 uint32_t payload_size;
2402 struct GNUNET_STREAM_DataMessage *data_msg;
2404 struct GNUNET_TIME_Relative ack_deadline;
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2409 /* Return NULL if there is already a write request pending */
2410 if (NULL != socket->write_handle)
2415 if (!((STATE_ESTABLISHED == socket->state)
2416 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
2417 || (STATE_RECEIVE_CLOSED == socket->state)))
2419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2420 "%x: Attempting to write on a closed (OR) not-yet-established"
2425 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
2426 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
2427 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
2428 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
2429 io_handle->write_cont = write_cont;
2430 io_handle->write_cont_cls = write_cont_cls;
2431 io_handle->size = size;
2433 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2434 determined from RTT */
2435 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
2436 /* Divide the given buffer into packets for sending */
2437 for (packet=0; packet < num_needed_packets; packet++)
2439 if ((packet + 1) * max_payload_size < size)
2441 payload_size = max_payload_size;
2442 packet_size = MAX_PACKET_SIZE;
2446 payload_size = size - packet * max_payload_size;
2447 packet_size = payload_size + sizeof (struct
2448 GNUNET_STREAM_DataMessage);
2450 io_handle->messages[packet] = GNUNET_malloc (packet_size);
2451 io_handle->messages[packet]->header.header.size = htons (packet_size);
2452 io_handle->messages[packet]->header.header.type =
2453 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
2454 io_handle->messages[packet]->sequence_number =
2455 htons (socket->write_sequence_number++);
2456 io_handle->messages[packet]->offset = htons (socket->write_offset);
2458 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2459 determined from RTT */
2460 io_handle->messages[packet]->ack_deadline =
2461 GNUNET_TIME_relative_hton (ack_deadline);
2462 data_msg = io_handle->messages[packet];
2463 /* Copy data from given buffer to the packet */
2464 memcpy (&data_msg[1],
2467 sweep += payload_size;
2468 socket->write_offset += payload_size;
2470 socket->write_handle = io_handle;
2471 write_data (socket);
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2474 "%s() END\n", __func__);
2481 * Tries to read data from the stream
2483 * @param socket the socket representing a stream
2484 * @param timeout the timeout period
2485 * @param proc function to call with data (once only)
2486 * @param proc_cls the closure for proc
2487 * @return handle to cancel the operation
2489 struct GNUNET_STREAM_IOReadHandle *
2490 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
2491 struct GNUNET_TIME_Relative timeout,
2492 GNUNET_STREAM_DataProcessor proc,
2495 struct GNUNET_STREAM_IOReadHandle *read_handle;
2497 /* Return NULL if there is already a read handle; the user has to cancel that
2498 first before continuing or has to wait until it is completed */
2499 if (NULL != socket->read_handle) return NULL;
2501 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
2502 read_handle->proc = proc;
2503 socket->read_handle = read_handle;
2505 /* Check if we have a packet at bitmap 0 */
2506 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
2509 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
2514 /* Setup the read timeout task */
2515 socket->read_io_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
2523 * Cancel pending write operation.
2525 * @param ioh handle to operation to cancel
2528 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
2535 * Cancel pending read operation.
2537 * @param ioh handle to operation to cancel
2540 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)