2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * Checks for matching the sender and socket->other_peer in server
26 * Decrement PEER intern count during socket close and listen close to free the
27 * memory used for PEER interning
31 * @file stream/stream_api.c
32 * @brief Implementation of the stream library
33 * @author Sree Harsha Totakura
36 #include "gnunet_common.h"
37 #include "gnunet_crypto_lib.h"
38 #include "gnunet_stream_lib.h"
39 #include "gnunet_testing_lib.h"
40 #include "stream_protocol.h"
44 * The maximum packet size of a stream packet
46 #define MAX_PACKET_SIZE 64000
49 * The maximum payload a data message packet can carry
51 static size_t max_payload_size =
52 MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
57 #define RECEIVE_BUFFER_SIZE 4096000
60 * states in the Protocol
65 * Client initialization state
70 * Listener initialization state
75 * Pre-connection establishment state
80 * State where a connection has been established
85 * State where the socket is closed on our side and waiting to be ACK'ed
87 STATE_RECEIVE_CLOSE_WAIT,
90 * State where the socket is closed for reading
95 * State where the socket is closed on our side and waiting to be ACK'ed
97 STATE_TRANSMIT_CLOSE_WAIT,
100 * State where the socket is closed for writing
102 STATE_TRANSMIT_CLOSED,
105 * State where the socket is closed on our side and waiting to be ACK'ed
110 * State where the socket is closed
117 * Functions of this type are called when a message is written
119 * @param cls the closure from queue_message
120 * @param socket the socket the written message was bound to
122 typedef void (*SendFinishCallback) (void *cls,
123 struct GNUNET_STREAM_Socket *socket);
127 * The send message queue
134 struct GNUNET_STREAM_MessageHeader *message;
137 * Callback to be called when the message is sent
139 SendFinishCallback finish_cb;
142 * The closure for finish_cb
147 * The next message in queue. Should be NULL in the last message
149 struct MessageQueue *next;
152 * The next message in queue. Should be NULL in the first message
154 struct MessageQueue *prev;
159 * The STREAM Socket Handler
161 struct GNUNET_STREAM_Socket
164 * Retransmission timeout
166 struct GNUNET_TIME_Relative retransmit_timeout;
169 * The Acknowledgement Bitmap
171 GNUNET_STREAM_AckBitmap ack_bitmap;
174 * Time when the Acknowledgement was queued
176 struct GNUNET_TIME_Absolute ack_time_registered;
179 * Queued Acknowledgement deadline
181 struct GNUNET_TIME_Relative ack_time_deadline;
186 struct GNUNET_MESH_Handle *mesh;
189 * The mesh tunnel handle
191 struct GNUNET_MESH_Tunnel *tunnel;
194 * Stream open closure
199 * Stream open callback
201 GNUNET_STREAM_OpenCallback open_cb;
204 * The current transmit handle (if a pending transmit request exists)
206 struct GNUNET_MESH_TransmitHandle *transmit_handle;
209 * The current message associated with the transmit handle
211 struct MessageQueue *queue_head;
214 * The queue tail, should always point to the last message in queue
216 struct MessageQueue *queue_tail;
219 * The write IO_handle associated with this socket
221 struct GNUNET_STREAM_IOWriteHandle *write_handle;
224 * The read IO_handle associated with this socket
226 struct GNUNET_STREAM_IOReadHandle *read_handle;
229 * Buffer for storing received messages
231 void *receive_buffer;
234 * The listen socket from which this socket is derived. Should be NULL if it
235 * is not a derived socket
237 struct GNUNET_STREAM_ListenSocket *lsocket;
240 * Task identifier for the read io timeout task
242 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task;
245 * Task identifier for retransmission task after timeout
247 GNUNET_SCHEDULER_TaskIdentifier retransmission_timeout_task_id;
250 * The task for sending timely Acks
252 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
255 * Task scheduled to continue a read operation.
257 GNUNET_SCHEDULER_TaskIdentifier read_task_id;
260 * The state of the protocol associated with this socket
265 * The status of the socket
267 enum GNUNET_STREAM_Status status;
270 * The number of previous timeouts; FIXME: currently not used
272 unsigned int retries;
275 * The peer identity of the peer at the other end of the stream
277 GNUNET_PEER_Id other_peer;
280 * Our Peer Identity (for debugging)
282 GNUNET_PEER_Id our_id;
285 * The application port number (type: uint32_t)
287 GNUNET_MESH_ApplicationType app_port;
290 * The session id associated with this stream connection
291 * FIXME: Not used currently, may be removed
296 * Write sequence number. Set to random when sending HELLO(client) and
299 uint32_t write_sequence_number;
302 * Read sequence number. This number's value is determined during handshake
304 uint32_t read_sequence_number;
307 * The receiver buffer size
309 uint32_t receive_buffer_size;
312 * The receiver buffer boundaries
314 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
317 * receiver's available buffer after the last acknowledged packet
319 uint32_t receiver_window_available;
322 * The offset pointer used during write operation
324 uint32_t write_offset;
327 * The offset after which we are expecting data
329 uint32_t read_offset;
332 * The offset upto which user has read from the received buffer
334 uint32_t copy_offset;
339 * A socket for listening
341 struct GNUNET_STREAM_ListenSocket
346 struct GNUNET_MESH_Handle *mesh;
349 * The callback function which is called after successful opening socket
351 GNUNET_STREAM_ListenCallback listen_cb;
354 * The call back closure
359 * Our interned Peer's identity
361 GNUNET_PEER_Id our_id;
365 * FIXME: Remove if not required!
367 GNUNET_MESH_ApplicationType port;
372 * The IO Write Handle
374 struct GNUNET_STREAM_IOWriteHandle
377 * The packet_buffers associated with this Handle
379 struct GNUNET_STREAM_DataMessage *messages[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
382 * The write continuation callback
384 GNUNET_STREAM_CompletionContinuation write_cont;
387 * Write continuation closure
389 void *write_cont_cls;
392 * The bitmap of this IOHandle; Corresponding bit for a message is set when
393 * it has been acknowledged by the receiver
395 GNUNET_STREAM_AckBitmap ack_bitmap;
398 * Number of bytes in this write handle
403 * Number of packets sent before waiting for an ack
405 * FIXME: Do we need this?
407 unsigned int sent_packets;
414 struct GNUNET_STREAM_IOReadHandle
417 * Callback for the read processor
419 GNUNET_STREAM_DataProcessor proc;
422 * The closure pointer for the read processor callback
429 * Default value in seconds for various timeouts
431 static unsigned int default_timeout = 10;
434 * Callback function for sending queued message
436 * @param cls closure the socket
437 * @param size number of bytes available in buf
438 * @param buf where the callee should write the message
439 * @return number of bytes written to buf
442 send_message_notify (void *cls, size_t size, void *buf)
444 struct GNUNET_STREAM_Socket *socket = cls;
445 struct GNUNET_PeerIdentity target;
446 struct MessageQueue *head;
449 socket->transmit_handle = NULL; /* Remove the transmit handle */
450 head = socket->queue_head;
452 return 0; /* just to be safe */
453 GNUNET_PEER_resolve (socket->other_peer, &target);
454 if (0 == size) /* request timed out */
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Message sending timed out. Retry %d \n",
460 socket->transmit_handle =
461 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
464 /* FIXME: exponential backoff */
465 socket->retransmit_timeout,
467 ntohs (head->message->header.size),
468 &send_message_notify,
473 ret = ntohs (head->message->header.size);
474 GNUNET_assert (size >= ret);
475 memcpy (buf, head->message, ret);
476 if (NULL != head->finish_cb)
478 head->finish_cb (head->finish_cb_cls, socket);
480 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
483 GNUNET_free (head->message);
485 head = socket->queue_head;
486 if (NULL != head) /* more pending messages to send */
489 socket->transmit_handle =
490 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
493 /* FIXME: exponential backoff */
494 socket->retransmit_timeout,
496 ntohs (head->message->header.size),
497 &send_message_notify,
505 * Queues a message for sending using the mesh connection of a socket
507 * @param socket the socket whose mesh connection is used
508 * @param message the message to be sent
509 * @param finish_cb the callback to be called when the message is sent
510 * @param finish_cb_cls the closure for the callback
513 queue_message (struct GNUNET_STREAM_Socket *socket,
514 struct GNUNET_STREAM_MessageHeader *message,
515 SendFinishCallback finish_cb,
518 struct MessageQueue *queue_entity;
519 struct GNUNET_PeerIdentity target;
522 ((ntohs (message->header.type) >= GNUNET_MESSAGE_TYPE_STREAM_DATA)
523 && (ntohs (message->header.type) <= GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK));
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "%x: Queueing message of type %d and size %d\n",
528 ntohs (message->header.type),
529 ntohs (message->header.size));
530 GNUNET_assert (NULL != message);
531 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
532 queue_entity->message = message;
533 queue_entity->finish_cb = finish_cb;
534 queue_entity->finish_cb_cls = finish_cb_cls;
535 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
538 if (NULL == socket->transmit_handle)
541 GNUNET_PEER_resolve (socket->other_peer, &target);
542 socket->transmit_handle =
543 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
546 socket->retransmit_timeout,
548 ntohs (message->header.size),
549 &send_message_notify,
556 * Copies a message and queues it for sending using the mesh connection of
559 * @param socket the socket whose mesh connection is used
560 * @param message the message to be sent
561 * @param finish_cb the callback to be called when the message is sent
562 * @param finish_cb_cls the closure for the callback
565 copy_and_queue_message (struct GNUNET_STREAM_Socket *socket,
566 const struct GNUNET_STREAM_MessageHeader *message,
567 SendFinishCallback finish_cb,
570 struct GNUNET_STREAM_MessageHeader *msg_copy;
573 size = ntohs (message->header.size);
574 msg_copy = GNUNET_malloc (size);
575 memcpy (msg_copy, message, size);
576 queue_message (socket, msg_copy, finish_cb, finish_cb_cls);
581 * Callback function for sending ack message
583 * @param cls closure the ACK message created in ack_task
584 * @param size number of bytes available in buffer
585 * @param buf where the callee should write the message
586 * @return number of bytes written to buf
589 send_ack_notify (void *cls, size_t size, void *buf)
591 struct GNUNET_STREAM_AckMessage *ack_msg = cls;
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
596 "%s called with size 0\n", __func__);
599 GNUNET_assert (ntohs (ack_msg->header.header.size) <= size);
601 size = ntohs (ack_msg->header.header.size);
602 memcpy (buf, ack_msg, size);
607 * Writes data using the given socket. The amount of data written is limited by
608 * the receiver_window_size
610 * @param socket the socket to use
613 write_data (struct GNUNET_STREAM_Socket *socket);
616 * Task for retransmitting data messages if they aren't ACK before their ack
619 * @param cls the socket
620 * @param tc the Task context
623 retransmission_timeout_task (void *cls,
624 const struct GNUNET_SCHEDULER_TaskContext *tc)
626 struct GNUNET_STREAM_Socket *socket = cls;
628 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "%x: Retransmitting DATA...\n", socket->our_id);
633 socket->retransmission_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
639 * Task for sending ACK message
641 * @param cls the socket
642 * @param tc the Task context
646 const struct GNUNET_SCHEDULER_TaskContext *tc)
648 struct GNUNET_STREAM_Socket *socket = cls;
649 struct GNUNET_STREAM_AckMessage *ack_msg;
650 struct GNUNET_PeerIdentity target;
652 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
657 socket->ack_task_id = 0;
659 /* Create the ACK Message */
660 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
661 ack_msg->header.header.size = htons (sizeof (struct
662 GNUNET_STREAM_AckMessage));
663 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
664 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
665 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
666 ack_msg->receive_window_remaining =
667 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
669 GNUNET_PEER_resolve (socket->other_peer, &target);
670 /* Request MESH for sending ACK */
671 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
674 socket->retransmit_timeout,
676 ntohs (ack_msg->header.header.size),
685 * Function to modify a bit in GNUNET_STREAM_AckBitmap
687 * @param bitmap the bitmap to modify
688 * @param bit the bit number to modify
689 * @param value GNUNET_YES to on, GNUNET_NO to off
692 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
696 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
697 if (GNUNET_YES == value)
698 *bitmap |= (1LL << bit);
700 *bitmap &= ~(1LL << bit);
705 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
707 * @param bitmap address of the bitmap that has to be checked
708 * @param bit the bit number to check
709 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
712 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
715 GNUNET_assert (bit < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH);
716 return 0 != (*bitmap & (1LL << bit));
722 * Function called when Data Message is sent
724 * @param cls the io_handle corresponding to the Data Message
725 * @param socket the socket which was used
728 write_data_finish_cb (void *cls,
729 struct GNUNET_STREAM_Socket *socket)
731 struct GNUNET_STREAM_IOWriteHandle *io_handle = cls;
733 io_handle->sent_packets++;
738 * Writes data using the given socket. The amount of data written is limited by
739 * the receiver_window_size
741 * @param socket the socket to use
744 write_data (struct GNUNET_STREAM_Socket *socket)
746 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
747 int packet; /* Although an int, should never be negative */
751 /* Find the last acknowledged packet */
752 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
754 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
757 else if (NULL == io_handle->messages[packet])
760 /* Resend packets which weren't ack'ed */
761 for (packet=0; packet < ack_packet; packet++)
763 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "%x: Placing DATA message with sequence %u in send queue\n",
769 ntohl (io_handle->messages[packet]->sequence_number));
771 copy_and_queue_message (socket,
772 &io_handle->messages[packet]->header,
777 packet = ack_packet + 1;
778 /* Now send new packets if there is enough buffer space */
779 while ( (NULL != io_handle->messages[packet]) &&
780 (socket->receiver_window_available
781 >= ntohs (io_handle->messages[packet]->header.header.size)) )
783 socket->receiver_window_available -=
784 ntohs (io_handle->messages[packet]->header.header.size);
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "%x: Placing DATA message with sequence %u in send queue\n",
788 ntohl (io_handle->messages[packet]->sequence_number));
789 copy_and_queue_message (socket,
790 &io_handle->messages[packet]->header,
791 &write_data_finish_cb,
796 if (GNUNET_SCHEDULER_NO_TASK == socket->retransmission_timeout_task_id)
797 socket->retransmission_timeout_task_id =
798 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
799 (GNUNET_TIME_UNIT_SECONDS, 5),
800 &retransmission_timeout_task,
806 * Task for calling the read processor
808 * @param cls the socket
809 * @param tc the task context
812 call_read_processor (void *cls,
813 const struct GNUNET_SCHEDULER_TaskContext *tc)
815 struct GNUNET_STREAM_Socket *socket = cls;
817 size_t valid_read_size;
819 uint32_t sequence_increase;
820 uint32_t offset_increase;
822 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
823 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
826 GNUNET_assert (NULL != socket->read_handle);
827 GNUNET_assert (NULL != socket->read_handle->proc);
829 /* Check the bitmap for any holes */
830 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
832 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
836 /* We only call read processor if we have the first packet */
837 GNUNET_assert (0 < packet);
840 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
842 GNUNET_assert (0 != valid_read_size);
844 /* Cancel the read_io_timeout_task */
845 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task);
846 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
848 /* Call the data processor */
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "%x: Calling read processor\n",
853 socket->read_handle->proc (socket->read_handle->proc_cls,
855 socket->receive_buffer + socket->copy_offset,
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
858 "%x: Read processor read %d bytes\n",
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "%x: Read processor completed successfully\n",
865 /* Free the read handle */
866 GNUNET_free (socket->read_handle);
867 socket->read_handle = NULL;
869 GNUNET_assert (read_size <= valid_read_size);
870 socket->copy_offset += read_size;
872 /* Determine upto which packet we can remove from the buffer */
873 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
875 if (socket->copy_offset == socket->receive_buffer_boundaries[packet])
877 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
881 /* If no packets can be removed we can't move the buffer */
882 if (0 == packet) return;
884 sequence_increase = packet;
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "%x: Sequence increase after read processor completion: %u\n",
890 /* Shift the data in the receive buffer */
891 memmove (socket->receive_buffer,
892 socket->receive_buffer
893 + socket->receive_buffer_boundaries[sequence_increase-1],
894 socket->receive_buffer_size - socket->receive_buffer_boundaries[sequence_increase-1]);
896 /* Shift the bitmap */
897 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
899 /* Set read_sequence_number */
900 socket->read_sequence_number += sequence_increase;
902 /* Set read_offset */
903 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
904 socket->read_offset += offset_increase;
906 /* Fix copy_offset */
907 GNUNET_assert (offset_increase <= socket->copy_offset);
908 socket->copy_offset -= offset_increase;
910 /* Fix relative boundaries */
911 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
913 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
915 socket->receive_buffer_boundaries[packet] =
916 socket->receive_buffer_boundaries[packet + sequence_increase]
920 socket->receive_buffer_boundaries[packet] = 0;
926 * Cancels the existing read io handle
928 * @param cls the closure from the SCHEDULER call
929 * @param tc the task context
932 read_io_timeout (void *cls,
933 const struct GNUNET_SCHEDULER_TaskContext *tc)
935 struct GNUNET_STREAM_Socket *socket = cls;
936 GNUNET_STREAM_DataProcessor proc;
939 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
940 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 "%x: Read task timedout - Cancelling it\n",
945 GNUNET_SCHEDULER_cancel (socket->read_task_id);
946 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
948 GNUNET_assert (NULL != socket->read_handle);
949 proc = socket->read_handle->proc;
950 proc_cls = socket->read_handle->proc_cls;
952 GNUNET_free (socket->read_handle);
953 socket->read_handle = NULL;
954 /* Call the read processor to signal timeout */
956 GNUNET_STREAM_TIMEOUT,
963 * Handler for DATA messages; Same for both client and server
965 * @param socket the socket through which the ack was received
966 * @param tunnel connection to the other end
967 * @param sender who sent the message
968 * @param msg the data message
969 * @param atsi performance data for the connection
970 * @return GNUNET_OK to keep the connection open,
971 * GNUNET_SYSERR to close it (signal serious error)
974 handle_data (struct GNUNET_STREAM_Socket *socket,
975 struct GNUNET_MESH_Tunnel *tunnel,
976 const struct GNUNET_PeerIdentity *sender,
977 const struct GNUNET_STREAM_DataMessage *msg,
978 const struct GNUNET_ATS_Information*atsi)
981 uint32_t bytes_needed;
982 uint32_t relative_offset;
983 uint32_t relative_sequence_number;
986 size = htons (msg->header.header.size);
987 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
990 return GNUNET_SYSERR;
993 if (GNUNET_PEER_search (sender) != socket->other_peer)
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 "%x: Received DATA from non-confirming peer\n",
1001 switch (socket->state)
1003 case STATE_ESTABLISHED:
1004 case STATE_TRANSMIT_CLOSED:
1005 case STATE_TRANSMIT_CLOSE_WAIT:
1007 /* check if the message's sequence number is in the range we are
1009 relative_sequence_number =
1010 ntohl (msg->sequence_number) - socket->read_sequence_number;
1011 if ( relative_sequence_number > GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH)
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "%x: Ignoring received message with sequence number %u\n",
1016 ntohl (msg->sequence_number));
1017 /* Start ACK sending task if one is not already present */
1018 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1020 socket->ack_task_id =
1021 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1022 (msg->ack_deadline),
1029 /* Check if we have already seen this message */
1030 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
1031 relative_sequence_number))
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 "%x: Ignoring already received message with sequence "
1037 ntohl (msg->sequence_number));
1038 /* Start ACK sending task if one is not already present */
1039 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1041 socket->ack_task_id =
1042 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1043 (msg->ack_deadline),
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "%x: Receiving DATA with sequence number: %u and size: %d "
1054 ntohl (msg->sequence_number),
1055 ntohs (msg->header.header.size),
1056 socket->other_peer);
1058 /* Check if we have to allocate the buffer */
1059 size -= sizeof (struct GNUNET_STREAM_DataMessage);
1060 relative_offset = ntohl (msg->offset) - socket->read_offset;
1061 bytes_needed = relative_offset + size;
1062 if (bytes_needed > socket->receive_buffer_size)
1064 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
1066 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
1068 socket->receive_buffer_size = bytes_needed;
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "%x: Cannot accommodate packet %d as buffer is",
1076 ntohl (msg->sequence_number));
1081 /* Copy Data to buffer */
1083 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
1084 memcpy (socket->receive_buffer + relative_offset,
1087 socket->receive_buffer_boundaries[relative_sequence_number] =
1088 relative_offset + size;
1090 /* Modify the ACK bitmap */
1091 ackbitmap_modify_bit (&socket->ack_bitmap,
1092 relative_sequence_number,
1095 /* Start ACK sending task if one is not already present */
1096 if (GNUNET_SCHEDULER_NO_TASK == socket->ack_task_id)
1098 socket->ack_task_id =
1099 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
1100 (msg->ack_deadline),
1105 if ((NULL != socket->read_handle) /* A read handle is waiting */
1106 /* There is no current read task */
1107 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task_id)
1108 /* We have the first packet */
1109 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "%x: Scheduling read processor\n",
1116 socket->read_task_id =
1117 GNUNET_SCHEDULER_add_now (&call_read_processor,
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1125 "%x: Received data message when it cannot be handled\n",
1133 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1135 * @param cls the socket (set from GNUNET_MESH_connect)
1136 * @param tunnel connection to the other end
1137 * @param tunnel_ctx place to store local state associated with the tunnel
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_data (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;
1154 return handle_data (socket,
1157 (const struct GNUNET_STREAM_DataMessage *) message,
1163 * Callback to set state to ESTABLISHED
1165 * @param cls the closure from queue_message FIXME: document
1166 * @param socket the socket to requiring state change
1169 set_state_established (void *cls,
1170 struct GNUNET_STREAM_Socket *socket)
1172 struct GNUNET_PeerIdentity initiator_pid;
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 "%x: Attaining ESTABLISHED state\n",
1177 socket->write_offset = 0;
1178 socket->read_offset = 0;
1179 socket->state = STATE_ESTABLISHED;
1180 /* FIXME: What if listen_cb is NULL */
1181 if (NULL != socket->lsocket)
1183 GNUNET_PEER_resolve (socket->other_peer, &initiator_pid);
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "%x: Calling listen callback\n",
1187 if (GNUNET_SYSERR ==
1188 socket->lsocket->listen_cb (socket->lsocket->listen_cb_cls,
1192 socket->state = STATE_CLOSED;
1193 /* FIXME: We should close in a decent way */
1194 GNUNET_MESH_tunnel_destroy (socket->tunnel); /* Destroy the tunnel */
1195 GNUNET_free (socket);
1198 else if (socket->open_cb)
1199 socket->open_cb (socket->open_cls, socket);
1204 * Callback to set state to HELLO_WAIT
1206 * @param cls the closure from queue_message
1207 * @param socket the socket to requiring state change
1210 set_state_hello_wait (void *cls,
1211 struct GNUNET_STREAM_Socket *socket)
1213 GNUNET_assert (STATE_INIT == socket->state);
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 "%x: Attaining HELLO_WAIT state\n",
1217 socket->state = STATE_HELLO_WAIT;
1222 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1224 * @param cls the socket (set from GNUNET_MESH_connect)
1225 * @param tunnel connection to the other end
1226 * @param tunnel_ctx this is NULL
1227 * @param sender who sent the message
1228 * @param message the actual message
1229 * @param atsi performance data for the connection
1230 * @return GNUNET_OK to keep the connection open,
1231 * GNUNET_SYSERR to close it (signal serious error)
1234 client_handle_hello_ack (void *cls,
1235 struct GNUNET_MESH_Tunnel *tunnel,
1237 const struct GNUNET_PeerIdentity *sender,
1238 const struct GNUNET_MessageHeader *message,
1239 const struct GNUNET_ATS_Information*atsi)
1241 struct GNUNET_STREAM_Socket *socket = cls;
1242 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1243 struct GNUNET_STREAM_HelloAckMessage *reply;
1245 if (GNUNET_PEER_search (sender) != socket->other_peer)
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "%x: Received HELLO_ACK from non-confirming peer\n",
1252 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "%x: Received HELLO_ACK from %x\n",
1256 socket->other_peer);
1258 GNUNET_assert (socket->tunnel == tunnel);
1259 switch (socket->state)
1261 case STATE_HELLO_WAIT:
1262 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "%x: Read sequence number %u\n",
1266 (unsigned int) socket->read_sequence_number);
1267 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1268 /* Get the random sequence number */
1269 socket->write_sequence_number =
1270 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1272 "%x: Generated write sequence number %u\n",
1274 (unsigned int) socket->write_sequence_number);
1276 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1277 reply->header.header.size =
1278 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1279 reply->header.header.type =
1280 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1281 reply->sequence_number = htonl (socket->write_sequence_number);
1282 reply->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1283 queue_message (socket,
1285 &set_state_established,
1288 case STATE_ESTABLISHED:
1289 case STATE_RECEIVE_CLOSE_WAIT:
1290 // call statistics (# ACKs ignored++)
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1295 "%x: Server %x sent HELLO_ACK when in state %d\n",
1299 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1300 return GNUNET_SYSERR;
1307 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1309 * @param cls the socket (set from GNUNET_MESH_connect)
1310 * @param tunnel connection to the other end
1311 * @param tunnel_ctx this is NULL
1312 * @param sender who sent the message
1313 * @param message the actual message
1314 * @param atsi performance data for the connection
1315 * @return GNUNET_OK to keep the connection open,
1316 * GNUNET_SYSERR to close it (signal serious error)
1319 client_handle_reset (void *cls,
1320 struct GNUNET_MESH_Tunnel *tunnel,
1322 const struct GNUNET_PeerIdentity *sender,
1323 const struct GNUNET_MessageHeader *message,
1324 const struct GNUNET_ATS_Information*atsi)
1326 struct GNUNET_STREAM_Socket *socket = cls;
1333 * Common message handler for handling TRANSMIT_CLOSE messages
1335 * @param socket the socket through which the ack was received
1336 * @param tunnel connection to the other end
1337 * @param sender who sent the message
1338 * @param msg the transmit close message
1339 * @param atsi performance data for the connection
1340 * @return GNUNET_OK to keep the connection open,
1341 * GNUNET_SYSERR to close it (signal serious error)
1344 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1345 struct GNUNET_MESH_Tunnel *tunnel,
1346 const struct GNUNET_PeerIdentity *sender,
1347 const struct GNUNET_STREAM_MessageHeader *msg,
1348 const struct GNUNET_ATS_Information*atsi)
1350 struct GNUNET_STREAM_MessageHeader *reply;
1352 switch (socket->state)
1354 case STATE_ESTABLISHED:
1355 socket->state = STATE_RECEIVE_CLOSED;
1357 /* Send TRANSMIT_CLOSE_ACK */
1358 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1359 reply->header.type =
1360 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1361 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1362 queue_message (socket, reply, NULL, NULL);
1366 /* FIXME: Call statistics? */
1374 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1376 * @param cls the socket (set from GNUNET_MESH_connect)
1377 * @param tunnel connection to the other end
1378 * @param tunnel_ctx this is NULL
1379 * @param sender who sent the message
1380 * @param message the actual message
1381 * @param atsi performance data for the connection
1382 * @return GNUNET_OK to keep the connection open,
1383 * GNUNET_SYSERR to close it (signal serious error)
1386 client_handle_transmit_close (void *cls,
1387 struct GNUNET_MESH_Tunnel *tunnel,
1389 const struct GNUNET_PeerIdentity *sender,
1390 const struct GNUNET_MessageHeader *message,
1391 const struct GNUNET_ATS_Information*atsi)
1393 struct GNUNET_STREAM_Socket *socket = cls;
1395 return handle_transmit_close (socket,
1398 (struct GNUNET_STREAM_MessageHeader *)message,
1404 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1406 * @param cls the socket (set from GNUNET_MESH_connect)
1407 * @param tunnel connection to the other end
1408 * @param tunnel_ctx this is NULL
1409 * @param sender who sent the message
1410 * @param message the actual message
1411 * @param atsi performance data for the connection
1412 * @return GNUNET_OK to keep the connection open,
1413 * GNUNET_SYSERR to close it (signal serious error)
1416 client_handle_transmit_close_ack (void *cls,
1417 struct GNUNET_MESH_Tunnel *tunnel,
1419 const struct GNUNET_PeerIdentity *sender,
1420 const struct GNUNET_MessageHeader *message,
1421 const struct GNUNET_ATS_Information*atsi)
1423 struct GNUNET_STREAM_Socket *socket = cls;
1430 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1432 * @param cls the socket (set from GNUNET_MESH_connect)
1433 * @param tunnel connection to the other end
1434 * @param tunnel_ctx this is NULL
1435 * @param sender who sent the message
1436 * @param message the actual message
1437 * @param atsi performance data for the connection
1438 * @return GNUNET_OK to keep the connection open,
1439 * GNUNET_SYSERR to close it (signal serious error)
1442 client_handle_receive_close (void *cls,
1443 struct GNUNET_MESH_Tunnel *tunnel,
1445 const struct GNUNET_PeerIdentity *sender,
1446 const struct GNUNET_MessageHeader *message,
1447 const struct GNUNET_ATS_Information*atsi)
1449 struct GNUNET_STREAM_Socket *socket = cls;
1456 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1458 * @param cls the socket (set from GNUNET_MESH_connect)
1459 * @param tunnel connection to the other end
1460 * @param tunnel_ctx this is NULL
1461 * @param sender who sent the message
1462 * @param message the actual message
1463 * @param atsi performance data for the connection
1464 * @return GNUNET_OK to keep the connection open,
1465 * GNUNET_SYSERR to close it (signal serious error)
1468 client_handle_receive_close_ack (void *cls,
1469 struct GNUNET_MESH_Tunnel *tunnel,
1471 const struct GNUNET_PeerIdentity *sender,
1472 const struct GNUNET_MessageHeader *message,
1473 const struct GNUNET_ATS_Information*atsi)
1475 struct GNUNET_STREAM_Socket *socket = cls;
1482 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1484 * @param cls the socket (set from GNUNET_MESH_connect)
1485 * @param tunnel connection to the other end
1486 * @param tunnel_ctx this is NULL
1487 * @param sender who sent the message
1488 * @param message the actual message
1489 * @param atsi performance data for the connection
1490 * @return GNUNET_OK to keep the connection open,
1491 * GNUNET_SYSERR to close it (signal serious error)
1494 client_handle_close (void *cls,
1495 struct GNUNET_MESH_Tunnel *tunnel,
1497 const struct GNUNET_PeerIdentity *sender,
1498 const struct GNUNET_MessageHeader *message,
1499 const struct GNUNET_ATS_Information*atsi)
1501 struct GNUNET_STREAM_Socket *socket = cls;
1508 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1510 * @param cls the socket (set from GNUNET_MESH_connect)
1511 * @param tunnel connection to the other end
1512 * @param tunnel_ctx this is NULL
1513 * @param sender who sent the message
1514 * @param message the actual message
1515 * @param atsi performance data for the connection
1516 * @return GNUNET_OK to keep the connection open,
1517 * GNUNET_SYSERR to close it (signal serious error)
1520 client_handle_close_ack (void *cls,
1521 struct GNUNET_MESH_Tunnel *tunnel,
1523 const struct GNUNET_PeerIdentity *sender,
1524 const struct GNUNET_MessageHeader *message,
1525 const struct GNUNET_ATS_Information*atsi)
1527 struct GNUNET_STREAM_Socket *socket = cls;
1532 /*****************************/
1533 /* Server's Message Handlers */
1534 /*****************************/
1537 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1539 * @param cls the closure
1540 * @param tunnel connection to the other end
1541 * @param tunnel_ctx the socket
1542 * @param sender who sent the message
1543 * @param message the actual message
1544 * @param atsi performance data for the connection
1545 * @return GNUNET_OK to keep the connection open,
1546 * GNUNET_SYSERR to close it (signal serious error)
1549 server_handle_data (void *cls,
1550 struct GNUNET_MESH_Tunnel *tunnel,
1552 const struct GNUNET_PeerIdentity *sender,
1553 const struct GNUNET_MessageHeader *message,
1554 const struct GNUNET_ATS_Information*atsi)
1556 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1558 return handle_data (socket,
1561 (const struct GNUNET_STREAM_DataMessage *)message,
1567 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1569 * @param cls the closure
1570 * @param tunnel connection to the other end
1571 * @param tunnel_ctx the socket
1572 * @param sender who sent the message
1573 * @param message the actual message
1574 * @param atsi performance data for the connection
1575 * @return GNUNET_OK to keep the connection open,
1576 * GNUNET_SYSERR to close it (signal serious error)
1579 server_handle_hello (void *cls,
1580 struct GNUNET_MESH_Tunnel *tunnel,
1582 const struct GNUNET_PeerIdentity *sender,
1583 const struct GNUNET_MessageHeader *message,
1584 const struct GNUNET_ATS_Information*atsi)
1586 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1587 struct GNUNET_STREAM_HelloAckMessage *reply;
1589 if (GNUNET_PEER_search (sender) != socket->other_peer)
1591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1592 "%x: Received HELLO from non-confirming peer\n",
1597 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO ==
1598 ntohs (message->type));
1599 GNUNET_assert (socket->tunnel == tunnel);
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1601 "%x: Received HELLO from %x\n",
1603 socket->other_peer);
1605 if (STATE_INIT == socket->state)
1607 /* Get the random sequence number */
1608 socket->write_sequence_number =
1609 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1611 "%x: Generated write sequence number %u\n",
1613 (unsigned int) socket->write_sequence_number);
1615 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1616 reply->header.header.size =
1617 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1618 reply->header.header.type =
1619 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1620 reply->sequence_number = htonl (socket->write_sequence_number);
1621 reply->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1622 queue_message (socket,
1624 &set_state_hello_wait,
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630 "Client sent HELLO when in state %d\n", socket->state);
1631 /* FIXME: Send RESET? */
1639 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1641 * @param cls the closure
1642 * @param tunnel connection to the other end
1643 * @param tunnel_ctx the socket
1644 * @param sender who sent the message
1645 * @param message the actual message
1646 * @param atsi performance data for the connection
1647 * @return GNUNET_OK to keep the connection open,
1648 * GNUNET_SYSERR to close it (signal serious error)
1651 server_handle_hello_ack (void *cls,
1652 struct GNUNET_MESH_Tunnel *tunnel,
1654 const struct GNUNET_PeerIdentity *sender,
1655 const struct GNUNET_MessageHeader *message,
1656 const struct GNUNET_ATS_Information*atsi)
1658 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1659 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1661 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
1662 ntohs (message->type));
1663 GNUNET_assert (socket->tunnel == tunnel);
1664 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1665 if (STATE_HELLO_WAIT == socket->state)
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668 "%x: Received HELLO_ACK from %x\n",
1670 socket->other_peer);
1671 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1673 "%x: Read sequence number %u\n",
1675 (unsigned int) socket->read_sequence_number);
1676 socket->receiver_window_available =
1677 ntohl (ack_message->receiver_window_size);
1678 /* Attain ESTABLISHED state */
1679 set_state_established (NULL, socket);
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 "Client sent HELLO_ACK when in state %d\n", socket->state);
1685 /* FIXME: Send RESET? */
1693 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1695 * @param cls the closure
1696 * @param tunnel connection to the other end
1697 * @param tunnel_ctx the socket
1698 * @param sender who sent the message
1699 * @param message the actual message
1700 * @param atsi performance data for the connection
1701 * @return GNUNET_OK to keep the connection open,
1702 * GNUNET_SYSERR to close it (signal serious error)
1705 server_handle_reset (void *cls,
1706 struct GNUNET_MESH_Tunnel *tunnel,
1708 const struct GNUNET_PeerIdentity *sender,
1709 const struct GNUNET_MessageHeader *message,
1710 const struct GNUNET_ATS_Information*atsi)
1712 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1719 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1721 * @param cls the closure
1722 * @param tunnel connection to the other end
1723 * @param tunnel_ctx the socket
1724 * @param sender who sent the message
1725 * @param message the actual message
1726 * @param atsi performance data for the connection
1727 * @return GNUNET_OK to keep the connection open,
1728 * GNUNET_SYSERR to close it (signal serious error)
1731 server_handle_transmit_close (void *cls,
1732 struct GNUNET_MESH_Tunnel *tunnel,
1734 const struct GNUNET_PeerIdentity *sender,
1735 const struct GNUNET_MessageHeader *message,
1736 const struct GNUNET_ATS_Information*atsi)
1738 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1740 return handle_transmit_close (socket,
1743 (struct GNUNET_STREAM_MessageHeader *)message,
1749 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1751 * @param cls the closure
1752 * @param tunnel connection to the other end
1753 * @param tunnel_ctx the socket
1754 * @param sender who sent the message
1755 * @param message the actual message
1756 * @param atsi performance data for the connection
1757 * @return GNUNET_OK to keep the connection open,
1758 * GNUNET_SYSERR to close it (signal serious error)
1761 server_handle_transmit_close_ack (void *cls,
1762 struct GNUNET_MESH_Tunnel *tunnel,
1764 const struct GNUNET_PeerIdentity *sender,
1765 const struct GNUNET_MessageHeader *message,
1766 const struct GNUNET_ATS_Information*atsi)
1768 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1775 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1777 * @param cls the closure
1778 * @param tunnel connection to the other end
1779 * @param tunnel_ctx the socket
1780 * @param sender who sent the message
1781 * @param message the actual message
1782 * @param atsi performance data for the connection
1783 * @return GNUNET_OK to keep the connection open,
1784 * GNUNET_SYSERR to close it (signal serious error)
1787 server_handle_receive_close (void *cls,
1788 struct GNUNET_MESH_Tunnel *tunnel,
1790 const struct GNUNET_PeerIdentity *sender,
1791 const struct GNUNET_MessageHeader *message,
1792 const struct GNUNET_ATS_Information*atsi)
1794 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1801 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1803 * @param cls the closure
1804 * @param tunnel connection to the other end
1805 * @param tunnel_ctx the socket
1806 * @param sender who sent the message
1807 * @param message the actual message
1808 * @param atsi performance data for the connection
1809 * @return GNUNET_OK to keep the connection open,
1810 * GNUNET_SYSERR to close it (signal serious error)
1813 server_handle_receive_close_ack (void *cls,
1814 struct GNUNET_MESH_Tunnel *tunnel,
1816 const struct GNUNET_PeerIdentity *sender,
1817 const struct GNUNET_MessageHeader *message,
1818 const struct GNUNET_ATS_Information*atsi)
1820 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1827 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1829 * @param cls the closure
1830 * @param tunnel connection to the other end
1831 * @param tunnel_ctx the socket
1832 * @param sender who sent the message
1833 * @param message the actual message
1834 * @param atsi performance data for the connection
1835 * @return GNUNET_OK to keep the connection open,
1836 * GNUNET_SYSERR to close it (signal serious error)
1839 server_handle_close (void *cls,
1840 struct GNUNET_MESH_Tunnel *tunnel,
1842 const struct GNUNET_PeerIdentity *sender,
1843 const struct GNUNET_MessageHeader *message,
1844 const struct GNUNET_ATS_Information*atsi)
1846 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1853 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1855 * @param cls the closure
1856 * @param tunnel connection to the other end
1857 * @param tunnel_ctx the socket
1858 * @param sender who sent the message
1859 * @param message the actual message
1860 * @param atsi performance data for the connection
1861 * @return GNUNET_OK to keep the connection open,
1862 * GNUNET_SYSERR to close it (signal serious error)
1865 server_handle_close_ack (void *cls,
1866 struct GNUNET_MESH_Tunnel *tunnel,
1868 const struct GNUNET_PeerIdentity *sender,
1869 const struct GNUNET_MessageHeader *message,
1870 const struct GNUNET_ATS_Information*atsi)
1872 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1879 * Message Handler for mesh
1881 * @param socket the socket through which the ack was received
1882 * @param tunnel connection to the other end
1883 * @param sender who sent the message
1884 * @param ack the acknowledgment message
1885 * @param atsi performance data for the connection
1886 * @return GNUNET_OK to keep the connection open,
1887 * GNUNET_SYSERR to close it (signal serious error)
1890 handle_ack (struct GNUNET_STREAM_Socket *socket,
1891 struct GNUNET_MESH_Tunnel *tunnel,
1892 const struct GNUNET_PeerIdentity *sender,
1893 const struct GNUNET_STREAM_AckMessage *ack,
1894 const struct GNUNET_ATS_Information*atsi)
1896 unsigned int packet;
1897 int need_retransmission;
1900 if (GNUNET_PEER_search (sender) != socket->other_peer)
1902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903 "%x: Received ACK from non-confirming peer\n",
1908 switch (socket->state)
1910 case (STATE_ESTABLISHED):
1911 if (NULL == socket->write_handle)
1913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1914 "%x: Received DATA_ACK when write_handle is NULL\n",
1918 /* FIXME: increment in the base sequence number is breaking current flow
1920 if (!((socket->write_sequence_number
1921 - ntohl (ack->base_sequence_number)) < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
1923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1924 "%x: Received DATA_ACK with unexpected base sequence "
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1928 "%x: Current write sequence: %u; Ack's base sequence: %u\n",
1930 socket->write_sequence_number,
1931 ntohl (ack->base_sequence_number));
1934 /* FIXME: include the case when write_handle is cancelled - ignore the
1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1938 "%x: Received DATA_ACK from %x\n",
1940 socket->other_peer);
1942 /* Cancel the retransmission task */
1943 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
1945 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
1946 socket->retransmission_timeout_task_id =
1947 GNUNET_SCHEDULER_NO_TASK;
1950 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1952 if (NULL == socket->write_handle->messages[packet]) break;
1953 if (ntohl (ack->base_sequence_number)
1954 >= ntohl (socket->write_handle->messages[packet]->sequence_number))
1955 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
1960 ackbitmap_is_bit_set (&socket->write_handle->ack_bitmap,
1961 ntohl (socket->write_handle->messages[packet]->sequence_number)
1962 - ntohl (ack->base_sequence_number)))
1963 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
1968 /* Update the receive window remaining
1969 FIXME : Should update with the value from a data ack with greater
1971 socket->receiver_window_available =
1972 ntohl (ack->receive_window_remaining);
1974 /* Check if we have received all acknowledgements */
1975 need_retransmission = GNUNET_NO;
1976 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1978 if (NULL == socket->write_handle->messages[packet]) break;
1979 if (GNUNET_YES != ackbitmap_is_bit_set
1980 (&socket->write_handle->ack_bitmap,packet))
1982 need_retransmission = GNUNET_YES;
1986 if (GNUNET_YES == need_retransmission)
1988 write_data (socket);
1990 else /* We have to call the write continuation callback now */
1992 /* Free the packets */
1993 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1995 GNUNET_free_non_null (socket->write_handle->messages[packet]);
1997 if (NULL != socket->write_handle->write_cont)
1998 socket->write_handle->write_cont
1999 (socket->write_handle->write_cont_cls,
2001 socket->write_handle->size);
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2003 "%x: Write completion callback completed\n",
2005 /* We are done with the write handle - Freeing it */
2006 GNUNET_free (socket->write_handle);
2007 socket->write_handle = NULL;
2018 * Message Handler for mesh
2020 * @param cls the 'struct GNUNET_STREAM_Socket'
2021 * @param tunnel connection to the other end
2022 * @param tunnel_ctx unused
2023 * @param sender who sent the message
2024 * @param message the actual message
2025 * @param atsi performance data for the connection
2026 * @return GNUNET_OK to keep the connection open,
2027 * GNUNET_SYSERR to close it (signal serious error)
2030 client_handle_ack (void *cls,
2031 struct GNUNET_MESH_Tunnel *tunnel,
2033 const struct GNUNET_PeerIdentity *sender,
2034 const struct GNUNET_MessageHeader *message,
2035 const struct GNUNET_ATS_Information*atsi)
2037 struct GNUNET_STREAM_Socket *socket = cls;
2038 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2040 return handle_ack (socket, tunnel, sender, ack, atsi);
2045 * Message Handler for mesh
2047 * @param cls the server's listen socket
2048 * @param tunnel connection to the other end
2049 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2050 * @param sender who sent the message
2051 * @param message the actual message
2052 * @param atsi performance data for the connection
2053 * @return GNUNET_OK to keep the connection open,
2054 * GNUNET_SYSERR to close it (signal serious error)
2057 server_handle_ack (void *cls,
2058 struct GNUNET_MESH_Tunnel *tunnel,
2060 const struct GNUNET_PeerIdentity *sender,
2061 const struct GNUNET_MessageHeader *message,
2062 const struct GNUNET_ATS_Information*atsi)
2064 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2065 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2067 return handle_ack (socket, tunnel, sender, ack, atsi);
2072 * For client message handlers, the stream socket is in the
2075 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2076 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2077 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2078 sizeof (struct GNUNET_STREAM_AckMessage) },
2079 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2080 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2081 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2082 sizeof (struct GNUNET_STREAM_MessageHeader)},
2083 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2084 sizeof (struct GNUNET_STREAM_MessageHeader)},
2085 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2086 sizeof (struct GNUNET_STREAM_MessageHeader)},
2087 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2088 sizeof (struct GNUNET_STREAM_MessageHeader)},
2089 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2090 sizeof (struct GNUNET_STREAM_MessageHeader)},
2091 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2092 sizeof (struct GNUNET_STREAM_MessageHeader)},
2093 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2094 sizeof (struct GNUNET_STREAM_MessageHeader)},
2100 * For server message handlers, the stream socket is in the
2101 * tunnel context, and the listen socket in the closure argument.
2103 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2104 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2105 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2106 sizeof (struct GNUNET_STREAM_AckMessage) },
2107 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2108 sizeof (struct GNUNET_STREAM_MessageHeader)},
2109 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2110 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2111 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2112 sizeof (struct GNUNET_STREAM_MessageHeader)},
2113 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2114 sizeof (struct GNUNET_STREAM_MessageHeader)},
2115 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2116 sizeof (struct GNUNET_STREAM_MessageHeader)},
2117 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2118 sizeof (struct GNUNET_STREAM_MessageHeader)},
2119 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2120 sizeof (struct GNUNET_STREAM_MessageHeader)},
2121 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2122 sizeof (struct GNUNET_STREAM_MessageHeader)},
2123 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2124 sizeof (struct GNUNET_STREAM_MessageHeader)},
2130 * Function called when our target peer is connected to our tunnel
2132 * @param cls the socket for which this tunnel is created
2133 * @param peer the peer identity of the target
2134 * @param atsi performance data for the connection
2137 mesh_peer_connect_callback (void *cls,
2138 const struct GNUNET_PeerIdentity *peer,
2139 const struct GNUNET_ATS_Information * atsi)
2141 struct GNUNET_STREAM_Socket *socket = cls;
2142 struct GNUNET_STREAM_MessageHeader *message;
2143 GNUNET_PEER_Id connected_peer;
2145 connected_peer = GNUNET_PEER_search (peer);
2147 if (connected_peer != socket->other_peer)
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2150 "%x: A peer which is not our target has connected",
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2157 "%x: Target peer %x connected\n",
2161 /* Set state to INIT */
2162 socket->state = STATE_INIT;
2164 /* Send HELLO message */
2165 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2166 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
2167 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2168 queue_message (socket,
2170 &set_state_hello_wait,
2173 /* Call open callback */
2174 if (NULL == socket->open_cb)
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 "STREAM_open callback is NULL\n");
2183 * Function called when our target peer is disconnected from our tunnel
2185 * @param cls the socket associated which this tunnel
2186 * @param peer the peer identity of the target
2189 mesh_peer_disconnect_callback (void *cls,
2190 const struct GNUNET_PeerIdentity *peer)
2197 * Method called whenever a peer creates a tunnel to us
2199 * @param cls closure
2200 * @param tunnel new handle to the tunnel
2201 * @param initiator peer that started the tunnel
2202 * @param atsi performance information for the tunnel
2203 * @return initial tunnel context for the tunnel
2204 * (can be NULL -- that's not an error)
2207 new_tunnel_notify (void *cls,
2208 struct GNUNET_MESH_Tunnel *tunnel,
2209 const struct GNUNET_PeerIdentity *initiator,
2210 const struct GNUNET_ATS_Information *atsi)
2212 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2213 struct GNUNET_STREAM_Socket *socket;
2215 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2216 from the same peer again until the socket is closed */
2218 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2219 socket->other_peer = GNUNET_PEER_intern (initiator);
2220 socket->tunnel = tunnel;
2221 socket->session_id = 0; /* FIXME */
2222 socket->state = STATE_INIT;
2223 socket->lsocket = lsocket;
2224 socket->our_id = lsocket->our_id;
2226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2227 "%x: Peer %x initiated tunnel to us\n",
2229 socket->other_peer);
2231 /* FIXME: Copy MESH handle from lsocket to socket */
2238 * Function called whenever an inbound tunnel is destroyed. Should clean up
2239 * any associated state. This function is NOT called if the client has
2240 * explicitly asked for the tunnel to be destroyed using
2241 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2244 * @param cls closure (set from GNUNET_MESH_connect)
2245 * @param tunnel connection to the other end (henceforth invalid)
2246 * @param tunnel_ctx place where local state associated
2247 * with the tunnel is stored
2250 tunnel_cleaner (void *cls,
2251 const struct GNUNET_MESH_Tunnel *tunnel,
2254 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2257 "%x: Peer %x has terminated connection abruptly\n",
2259 socket->other_peer);
2261 socket->status = GNUNET_STREAM_SHUTDOWN;
2263 /* Clear Transmit handles */
2264 if (NULL != socket->transmit_handle)
2266 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2267 socket->transmit_handle = NULL;
2269 socket->tunnel = NULL;
2279 * Tries to open a stream to the target peer
2281 * @param cfg configuration to use
2282 * @param target the target peer to which the stream has to be opened
2283 * @param app_port the application port number which uniquely identifies this
2285 * @param open_cb this function will be called after stream has be established
2286 * @param open_cb_cls the closure for open_cb
2287 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2288 * @return if successful it returns the stream socket; NULL if stream cannot be
2291 struct GNUNET_STREAM_Socket *
2292 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2293 const struct GNUNET_PeerIdentity *target,
2294 GNUNET_MESH_ApplicationType app_port,
2295 GNUNET_STREAM_OpenCallback open_cb,
2299 struct GNUNET_STREAM_Socket *socket;
2300 struct GNUNET_PeerIdentity own_peer_id;
2301 enum GNUNET_STREAM_Option option;
2302 va_list vargs; /* Variable arguments */
2304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2307 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2308 socket->other_peer = GNUNET_PEER_intern (target);
2309 socket->open_cb = open_cb;
2310 socket->open_cls = open_cb_cls;
2311 GNUNET_TESTING_get_peer_identity (cfg, &own_peer_id);
2312 socket->our_id = GNUNET_PEER_intern (&own_peer_id);
2315 socket->retransmit_timeout =
2316 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
2318 va_start (vargs, open_cb_cls); /* Parse variable args */
2320 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2323 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2324 /* Expect struct GNUNET_TIME_Relative */
2325 socket->retransmit_timeout = va_arg (vargs,
2326 struct GNUNET_TIME_Relative);
2328 case GNUNET_STREAM_OPTION_END:
2331 } while (GNUNET_STREAM_OPTION_END != option);
2332 va_end (vargs); /* End of variable args parsing */
2333 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2334 10, /* QUEUE size as parameter? */
2336 NULL, /* No inbound tunnel handler */
2337 &tunnel_cleaner, /* FIXME: not required? */
2338 client_message_handlers,
2339 &app_port); /* We don't get inbound tunnels */
2340 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2342 GNUNET_free (socket);
2346 /* Now create the mesh tunnel to target */
2347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348 "Creating MESH Tunnel\n");
2349 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2350 NULL, /* Tunnel context */
2351 &mesh_peer_connect_callback,
2352 &mesh_peer_disconnect_callback,
2354 GNUNET_assert (NULL != socket->tunnel);
2355 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2359 "%s() END\n", __func__);
2365 * Shutdown the stream for reading or writing (man 2 shutdown).
2367 * @param socket the stream socket
2368 * @param how SHUT_RD, SHUT_WR or SHUT_RDWR
2371 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2381 * @param socket the stream socket
2384 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
2386 struct MessageQueue *head;
2388 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
2390 /* socket closed with read task pending!? */
2392 GNUNET_SCHEDULER_cancel (socket->read_task_id);
2393 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
2396 /* Terminate the ack'ing tasks if they are still present */
2397 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
2399 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2400 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2403 /* Clear Transmit handles */
2404 if (NULL != socket->transmit_handle)
2406 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2407 socket->transmit_handle = NULL;
2410 /* Clear existing message queue */
2411 while (NULL != (head = socket->queue_head)) {
2412 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2415 GNUNET_free (head->message);
2419 /* Close associated tunnel */
2420 if (NULL != socket->tunnel)
2422 GNUNET_MESH_tunnel_destroy (socket->tunnel);
2423 socket->tunnel = NULL;
2426 /* Close mesh connection */
2427 if (NULL != socket->mesh && NULL == socket->lsocket)
2429 GNUNET_MESH_disconnect (socket->mesh);
2430 socket->mesh = NULL;
2433 /* Release receive buffer */
2434 if (NULL != socket->receive_buffer)
2436 GNUNET_free (socket->receive_buffer);
2439 GNUNET_free (socket);
2444 * Listens for stream connections for a specific application ports
2446 * @param cfg the configuration to use
2447 * @param app_port the application port for which new streams will be accepted
2448 * @param listen_cb this function will be called when a peer tries to establish
2450 * @param listen_cb_cls closure for listen_cb
2451 * @return listen socket, NULL for any error
2453 struct GNUNET_STREAM_ListenSocket *
2454 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
2455 GNUNET_MESH_ApplicationType app_port,
2456 GNUNET_STREAM_ListenCallback listen_cb,
2457 void *listen_cb_cls)
2459 /* FIXME: Add variable args for passing configration options? */
2460 struct GNUNET_STREAM_ListenSocket *lsocket;
2461 struct GNUNET_PeerIdentity our_peer_id;
2463 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
2464 lsocket->port = app_port;
2465 lsocket->listen_cb = listen_cb;
2466 lsocket->listen_cb_cls = listen_cb_cls;
2467 GNUNET_TESTING_get_peer_identity (cfg, &our_peer_id);
2468 lsocket->our_id = GNUNET_PEER_intern (&our_peer_id);
2469 lsocket->mesh = GNUNET_MESH_connect (cfg,
2470 10, /* FIXME: QUEUE size as parameter? */
2471 lsocket, /* Closure */
2474 server_message_handlers,
2476 GNUNET_assert (NULL != lsocket->mesh);
2482 * Closes the listen socket
2484 * @param lsocket the listen socket
2487 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
2489 /* Close MESH connection */
2490 GNUNET_assert (NULL != lsocket->mesh);
2491 GNUNET_MESH_disconnect (lsocket->mesh);
2493 GNUNET_free (lsocket);
2498 * Tries to write the given data to the stream
2500 * @param socket the socket representing a stream
2501 * @param data the data buffer from where the data is written into the stream
2502 * @param size the number of bytes to be written from the data buffer
2503 * @param timeout the timeout period
2504 * @param write_cont the function to call upon writing some bytes into the stream
2505 * @param write_cont_cls the closure
2506 * @return handle to cancel the operation
2508 struct GNUNET_STREAM_IOWriteHandle *
2509 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
2512 struct GNUNET_TIME_Relative timeout,
2513 GNUNET_STREAM_CompletionContinuation write_cont,
2514 void *write_cont_cls)
2516 unsigned int num_needed_packets;
2517 unsigned int packet;
2518 struct GNUNET_STREAM_IOWriteHandle *io_handle;
2519 uint32_t packet_size;
2520 uint32_t payload_size;
2521 struct GNUNET_STREAM_DataMessage *data_msg;
2523 struct GNUNET_TIME_Relative ack_deadline;
2525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2528 /* Return NULL if there is already a write request pending */
2529 if (NULL != socket->write_handle)
2534 if (!((STATE_ESTABLISHED == socket->state)
2535 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
2536 || (STATE_RECEIVE_CLOSED == socket->state)))
2538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2539 "%x: Attempting to write on a closed (OR) not-yet-established"
2544 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
2545 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
2546 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
2547 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
2548 io_handle->write_cont = write_cont;
2549 io_handle->write_cont_cls = write_cont_cls;
2550 io_handle->size = size;
2552 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2553 determined from RTT */
2554 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
2555 /* Divide the given buffer into packets for sending */
2556 for (packet=0; packet < num_needed_packets; packet++)
2558 if ((packet + 1) * max_payload_size < size)
2560 payload_size = max_payload_size;
2561 packet_size = MAX_PACKET_SIZE;
2565 payload_size = size - packet * max_payload_size;
2566 packet_size = payload_size + sizeof (struct
2567 GNUNET_STREAM_DataMessage);
2569 io_handle->messages[packet] = GNUNET_malloc (packet_size);
2570 io_handle->messages[packet]->header.header.size = htons (packet_size);
2571 io_handle->messages[packet]->header.header.type =
2572 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
2573 io_handle->messages[packet]->sequence_number =
2574 htonl (socket->write_sequence_number++);
2575 io_handle->messages[packet]->offset = htonl (socket->write_offset);
2577 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2578 determined from RTT */
2579 io_handle->messages[packet]->ack_deadline =
2580 GNUNET_TIME_relative_hton (ack_deadline);
2581 data_msg = io_handle->messages[packet];
2582 /* Copy data from given buffer to the packet */
2583 memcpy (&data_msg[1],
2586 sweep += payload_size;
2587 socket->write_offset += payload_size;
2589 socket->write_handle = io_handle;
2590 write_data (socket);
2592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2593 "%s() END\n", __func__);
2600 * Tries to read data from the stream
2602 * @param socket the socket representing a stream
2603 * @param timeout the timeout period
2604 * @param proc function to call with data (once only)
2605 * @param proc_cls the closure for proc
2606 * @return handle to cancel the operation
2608 struct GNUNET_STREAM_IOReadHandle *
2609 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
2610 struct GNUNET_TIME_Relative timeout,
2611 GNUNET_STREAM_DataProcessor proc,
2614 struct GNUNET_STREAM_IOReadHandle *read_handle;
2616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2621 /* Return NULL if there is already a read handle; the user has to cancel that
2622 first before continuing or has to wait until it is completed */
2623 if (NULL != socket->read_handle) return NULL;
2625 GNUNET_assert (NULL != proc);
2627 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
2628 read_handle->proc = proc;
2629 read_handle->proc_cls = proc_cls;
2630 socket->read_handle = read_handle;
2632 /* Check if we have a packet at bitmap 0 */
2633 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
2636 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
2641 /* Setup the read timeout task */
2642 socket->read_io_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
2645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2654 * Cancel pending write operation.
2656 * @param ioh handle to operation to cancel
2659 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
2661 /* FIXME: Should cancel the write retransmission task */
2667 * Cancel pending read operation.
2669 * @param ioh handle to operation to cancel
2672 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)