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 * Returns a new HelloAckMessage. Also sets the write sequence number for the
1225 * @param socket the socket for which this HelloAckMessage has to be generated
1226 * @return the HelloAckMessage
1228 static struct GNUNET_STREAM_HelloAckMessage *
1229 generate_hello_ack_msg (struct GNUNET_STREAM_Socket *socket)
1231 struct GNUNET_STREAM_HelloAckMessage *msg;
1233 /* Get the random sequence number */
1234 socket->write_sequence_number =
1235 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "%x: Generated write sequence number %u\n",
1239 (unsigned int) socket->write_sequence_number);
1241 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1242 msg->header.header.size =
1243 htons (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1244 msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1245 msg->sequence_number = htonl (socket->write_sequence_number);
1246 msg->receiver_window_size = htonl (RECEIVE_BUFFER_SIZE);
1253 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1255 * @param cls the socket (set from GNUNET_MESH_connect)
1256 * @param tunnel connection to the other end
1257 * @param tunnel_ctx this is NULL
1258 * @param sender who sent the message
1259 * @param message the actual message
1260 * @param atsi performance data for the connection
1261 * @return GNUNET_OK to keep the connection open,
1262 * GNUNET_SYSERR to close it (signal serious error)
1265 client_handle_hello_ack (void *cls,
1266 struct GNUNET_MESH_Tunnel *tunnel,
1268 const struct GNUNET_PeerIdentity *sender,
1269 const struct GNUNET_MessageHeader *message,
1270 const struct GNUNET_ATS_Information*atsi)
1272 struct GNUNET_STREAM_Socket *socket = cls;
1273 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1274 struct GNUNET_STREAM_HelloAckMessage *reply;
1276 if (GNUNET_PEER_search (sender) != socket->other_peer)
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "%x: Received HELLO_ACK from non-confirming peer\n",
1283 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 "%x: Received HELLO_ACK from %x\n",
1287 socket->other_peer);
1289 GNUNET_assert (socket->tunnel == tunnel);
1290 switch (socket->state)
1292 case STATE_HELLO_WAIT:
1293 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1295 "%x: Read sequence number %u\n",
1297 (unsigned int) socket->read_sequence_number);
1298 socket->receiver_window_available = ntohl (ack_msg->receiver_window_size);
1299 reply = generate_hello_ack_msg (socket);
1300 queue_message (socket,
1302 &set_state_established,
1305 case STATE_ESTABLISHED:
1306 case STATE_RECEIVE_CLOSE_WAIT:
1307 // call statistics (# ACKs ignored++)
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1312 "%x: Server %x sent HELLO_ACK when in state %d\n",
1316 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1317 return GNUNET_SYSERR;
1324 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1326 * @param cls the socket (set from GNUNET_MESH_connect)
1327 * @param tunnel connection to the other end
1328 * @param tunnel_ctx this is NULL
1329 * @param sender who sent the message
1330 * @param message the actual message
1331 * @param atsi performance data for the connection
1332 * @return GNUNET_OK to keep the connection open,
1333 * GNUNET_SYSERR to close it (signal serious error)
1336 client_handle_reset (void *cls,
1337 struct GNUNET_MESH_Tunnel *tunnel,
1339 const struct GNUNET_PeerIdentity *sender,
1340 const struct GNUNET_MessageHeader *message,
1341 const struct GNUNET_ATS_Information*atsi)
1343 struct GNUNET_STREAM_Socket *socket = cls;
1350 * Common message handler for handling TRANSMIT_CLOSE messages
1352 * @param socket the socket through which the ack was received
1353 * @param tunnel connection to the other end
1354 * @param sender who sent the message
1355 * @param msg the transmit close message
1356 * @param atsi performance data for the connection
1357 * @return GNUNET_OK to keep the connection open,
1358 * GNUNET_SYSERR to close it (signal serious error)
1361 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1362 struct GNUNET_MESH_Tunnel *tunnel,
1363 const struct GNUNET_PeerIdentity *sender,
1364 const struct GNUNET_STREAM_MessageHeader *msg,
1365 const struct GNUNET_ATS_Information*atsi)
1367 struct GNUNET_STREAM_MessageHeader *reply;
1369 switch (socket->state)
1371 case STATE_ESTABLISHED:
1372 socket->state = STATE_RECEIVE_CLOSED;
1374 /* Send TRANSMIT_CLOSE_ACK */
1375 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1376 reply->header.type =
1377 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1378 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1379 queue_message (socket, reply, NULL, NULL);
1383 /* FIXME: Call statistics? */
1391 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1393 * @param cls the socket (set from GNUNET_MESH_connect)
1394 * @param tunnel connection to the other end
1395 * @param tunnel_ctx this is NULL
1396 * @param sender who sent the message
1397 * @param message the actual message
1398 * @param atsi performance data for the connection
1399 * @return GNUNET_OK to keep the connection open,
1400 * GNUNET_SYSERR to close it (signal serious error)
1403 client_handle_transmit_close (void *cls,
1404 struct GNUNET_MESH_Tunnel *tunnel,
1406 const struct GNUNET_PeerIdentity *sender,
1407 const struct GNUNET_MessageHeader *message,
1408 const struct GNUNET_ATS_Information*atsi)
1410 struct GNUNET_STREAM_Socket *socket = cls;
1412 return handle_transmit_close (socket,
1415 (struct GNUNET_STREAM_MessageHeader *)message,
1421 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1423 * @param cls the socket (set from GNUNET_MESH_connect)
1424 * @param tunnel connection to the other end
1425 * @param tunnel_ctx this is NULL
1426 * @param sender who sent the message
1427 * @param message the actual message
1428 * @param atsi performance data for the connection
1429 * @return GNUNET_OK to keep the connection open,
1430 * GNUNET_SYSERR to close it (signal serious error)
1433 client_handle_transmit_close_ack (void *cls,
1434 struct GNUNET_MESH_Tunnel *tunnel,
1436 const struct GNUNET_PeerIdentity *sender,
1437 const struct GNUNET_MessageHeader *message,
1438 const struct GNUNET_ATS_Information*atsi)
1440 struct GNUNET_STREAM_Socket *socket = cls;
1447 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1449 * @param cls the socket (set from GNUNET_MESH_connect)
1450 * @param tunnel connection to the other end
1451 * @param tunnel_ctx this is NULL
1452 * @param sender who sent the message
1453 * @param message the actual message
1454 * @param atsi performance data for the connection
1455 * @return GNUNET_OK to keep the connection open,
1456 * GNUNET_SYSERR to close it (signal serious error)
1459 client_handle_receive_close (void *cls,
1460 struct GNUNET_MESH_Tunnel *tunnel,
1462 const struct GNUNET_PeerIdentity *sender,
1463 const struct GNUNET_MessageHeader *message,
1464 const struct GNUNET_ATS_Information*atsi)
1466 struct GNUNET_STREAM_Socket *socket = cls;
1473 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1475 * @param cls the socket (set from GNUNET_MESH_connect)
1476 * @param tunnel connection to the other end
1477 * @param tunnel_ctx this is NULL
1478 * @param sender who sent the message
1479 * @param message the actual message
1480 * @param atsi performance data for the connection
1481 * @return GNUNET_OK to keep the connection open,
1482 * GNUNET_SYSERR to close it (signal serious error)
1485 client_handle_receive_close_ack (void *cls,
1486 struct GNUNET_MESH_Tunnel *tunnel,
1488 const struct GNUNET_PeerIdentity *sender,
1489 const struct GNUNET_MessageHeader *message,
1490 const struct GNUNET_ATS_Information*atsi)
1492 struct GNUNET_STREAM_Socket *socket = cls;
1499 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1501 * @param cls the socket (set from GNUNET_MESH_connect)
1502 * @param tunnel connection to the other end
1503 * @param tunnel_ctx this is NULL
1504 * @param sender who sent the message
1505 * @param message the actual message
1506 * @param atsi performance data for the connection
1507 * @return GNUNET_OK to keep the connection open,
1508 * GNUNET_SYSERR to close it (signal serious error)
1511 client_handle_close (void *cls,
1512 struct GNUNET_MESH_Tunnel *tunnel,
1514 const struct GNUNET_PeerIdentity *sender,
1515 const struct GNUNET_MessageHeader *message,
1516 const struct GNUNET_ATS_Information*atsi)
1518 struct GNUNET_STREAM_Socket *socket = cls;
1525 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1527 * @param cls the socket (set from GNUNET_MESH_connect)
1528 * @param tunnel connection to the other end
1529 * @param tunnel_ctx this is NULL
1530 * @param sender who sent the message
1531 * @param message the actual message
1532 * @param atsi performance data for the connection
1533 * @return GNUNET_OK to keep the connection open,
1534 * GNUNET_SYSERR to close it (signal serious error)
1537 client_handle_close_ack (void *cls,
1538 struct GNUNET_MESH_Tunnel *tunnel,
1540 const struct GNUNET_PeerIdentity *sender,
1541 const struct GNUNET_MessageHeader *message,
1542 const struct GNUNET_ATS_Information*atsi)
1544 struct GNUNET_STREAM_Socket *socket = cls;
1549 /*****************************/
1550 /* Server's Message Handlers */
1551 /*****************************/
1554 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1556 * @param cls the closure
1557 * @param tunnel connection to the other end
1558 * @param tunnel_ctx the socket
1559 * @param sender who sent the message
1560 * @param message the actual message
1561 * @param atsi performance data for the connection
1562 * @return GNUNET_OK to keep the connection open,
1563 * GNUNET_SYSERR to close it (signal serious error)
1566 server_handle_data (void *cls,
1567 struct GNUNET_MESH_Tunnel *tunnel,
1569 const struct GNUNET_PeerIdentity *sender,
1570 const struct GNUNET_MessageHeader *message,
1571 const struct GNUNET_ATS_Information*atsi)
1573 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1575 return handle_data (socket,
1578 (const struct GNUNET_STREAM_DataMessage *)message,
1584 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1586 * @param cls the closure
1587 * @param tunnel connection to the other end
1588 * @param tunnel_ctx the socket
1589 * @param sender who sent the message
1590 * @param message the actual message
1591 * @param atsi performance data for the connection
1592 * @return GNUNET_OK to keep the connection open,
1593 * GNUNET_SYSERR to close it (signal serious error)
1596 server_handle_hello (void *cls,
1597 struct GNUNET_MESH_Tunnel *tunnel,
1599 const struct GNUNET_PeerIdentity *sender,
1600 const struct GNUNET_MessageHeader *message,
1601 const struct GNUNET_ATS_Information*atsi)
1603 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1604 struct GNUNET_STREAM_HelloAckMessage *reply;
1606 if (GNUNET_PEER_search (sender) != socket->other_peer)
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1609 "%x: Received HELLO from non-confirming peer\n",
1614 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO ==
1615 ntohs (message->type));
1616 GNUNET_assert (socket->tunnel == tunnel);
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618 "%x: Received HELLO from %x\n",
1620 socket->other_peer);
1622 if (STATE_INIT == socket->state)
1624 reply = generate_hello_ack_msg (socket);
1625 queue_message (socket,
1627 &set_state_hello_wait,
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1633 "Client sent HELLO when in state %d\n", socket->state);
1634 /* FIXME: Send RESET? */
1642 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1644 * @param cls the closure
1645 * @param tunnel connection to the other end
1646 * @param tunnel_ctx the socket
1647 * @param sender who sent the message
1648 * @param message the actual message
1649 * @param atsi performance data for the connection
1650 * @return GNUNET_OK to keep the connection open,
1651 * GNUNET_SYSERR to close it (signal serious error)
1654 server_handle_hello_ack (void *cls,
1655 struct GNUNET_MESH_Tunnel *tunnel,
1657 const struct GNUNET_PeerIdentity *sender,
1658 const struct GNUNET_MessageHeader *message,
1659 const struct GNUNET_ATS_Information*atsi)
1661 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1662 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1664 GNUNET_assert (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK ==
1665 ntohs (message->type));
1666 GNUNET_assert (socket->tunnel == tunnel);
1667 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1668 if (STATE_HELLO_WAIT == socket->state)
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1671 "%x: Received HELLO_ACK from %x\n",
1673 socket->other_peer);
1674 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "%x: Read sequence number %u\n",
1678 (unsigned int) socket->read_sequence_number);
1679 socket->receiver_window_available =
1680 ntohl (ack_message->receiver_window_size);
1681 /* Attain ESTABLISHED state */
1682 set_state_established (NULL, socket);
1686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1687 "Client sent HELLO_ACK when in state %d\n", socket->state);
1688 /* FIXME: Send RESET? */
1696 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1698 * @param cls the closure
1699 * @param tunnel connection to the other end
1700 * @param tunnel_ctx the socket
1701 * @param sender who sent the message
1702 * @param message the actual message
1703 * @param atsi performance data for the connection
1704 * @return GNUNET_OK to keep the connection open,
1705 * GNUNET_SYSERR to close it (signal serious error)
1708 server_handle_reset (void *cls,
1709 struct GNUNET_MESH_Tunnel *tunnel,
1711 const struct GNUNET_PeerIdentity *sender,
1712 const struct GNUNET_MessageHeader *message,
1713 const struct GNUNET_ATS_Information*atsi)
1715 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1722 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1724 * @param cls the closure
1725 * @param tunnel connection to the other end
1726 * @param tunnel_ctx the socket
1727 * @param sender who sent the message
1728 * @param message the actual message
1729 * @param atsi performance data for the connection
1730 * @return GNUNET_OK to keep the connection open,
1731 * GNUNET_SYSERR to close it (signal serious error)
1734 server_handle_transmit_close (void *cls,
1735 struct GNUNET_MESH_Tunnel *tunnel,
1737 const struct GNUNET_PeerIdentity *sender,
1738 const struct GNUNET_MessageHeader *message,
1739 const struct GNUNET_ATS_Information*atsi)
1741 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1743 return handle_transmit_close (socket,
1746 (struct GNUNET_STREAM_MessageHeader *)message,
1752 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1754 * @param cls the closure
1755 * @param tunnel connection to the other end
1756 * @param tunnel_ctx the socket
1757 * @param sender who sent the message
1758 * @param message the actual message
1759 * @param atsi performance data for the connection
1760 * @return GNUNET_OK to keep the connection open,
1761 * GNUNET_SYSERR to close it (signal serious error)
1764 server_handle_transmit_close_ack (void *cls,
1765 struct GNUNET_MESH_Tunnel *tunnel,
1767 const struct GNUNET_PeerIdentity *sender,
1768 const struct GNUNET_MessageHeader *message,
1769 const struct GNUNET_ATS_Information*atsi)
1771 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1778 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1780 * @param cls the closure
1781 * @param tunnel connection to the other end
1782 * @param tunnel_ctx the socket
1783 * @param sender who sent the message
1784 * @param message the actual message
1785 * @param atsi performance data for the connection
1786 * @return GNUNET_OK to keep the connection open,
1787 * GNUNET_SYSERR to close it (signal serious error)
1790 server_handle_receive_close (void *cls,
1791 struct GNUNET_MESH_Tunnel *tunnel,
1793 const struct GNUNET_PeerIdentity *sender,
1794 const struct GNUNET_MessageHeader *message,
1795 const struct GNUNET_ATS_Information*atsi)
1797 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1804 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1806 * @param cls the closure
1807 * @param tunnel connection to the other end
1808 * @param tunnel_ctx the socket
1809 * @param sender who sent the message
1810 * @param message the actual message
1811 * @param atsi performance data for the connection
1812 * @return GNUNET_OK to keep the connection open,
1813 * GNUNET_SYSERR to close it (signal serious error)
1816 server_handle_receive_close_ack (void *cls,
1817 struct GNUNET_MESH_Tunnel *tunnel,
1819 const struct GNUNET_PeerIdentity *sender,
1820 const struct GNUNET_MessageHeader *message,
1821 const struct GNUNET_ATS_Information*atsi)
1823 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1830 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1832 * @param cls the closure
1833 * @param tunnel connection to the other end
1834 * @param tunnel_ctx the socket
1835 * @param sender who sent the message
1836 * @param message the actual message
1837 * @param atsi performance data for the connection
1838 * @return GNUNET_OK to keep the connection open,
1839 * GNUNET_SYSERR to close it (signal serious error)
1842 server_handle_close (void *cls,
1843 struct GNUNET_MESH_Tunnel *tunnel,
1845 const struct GNUNET_PeerIdentity *sender,
1846 const struct GNUNET_MessageHeader *message,
1847 const struct GNUNET_ATS_Information*atsi)
1849 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1856 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1858 * @param cls the closure
1859 * @param tunnel connection to the other end
1860 * @param tunnel_ctx the socket
1861 * @param sender who sent the message
1862 * @param message the actual message
1863 * @param atsi performance data for the connection
1864 * @return GNUNET_OK to keep the connection open,
1865 * GNUNET_SYSERR to close it (signal serious error)
1868 server_handle_close_ack (void *cls,
1869 struct GNUNET_MESH_Tunnel *tunnel,
1871 const struct GNUNET_PeerIdentity *sender,
1872 const struct GNUNET_MessageHeader *message,
1873 const struct GNUNET_ATS_Information*atsi)
1875 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1882 * Message Handler for mesh
1884 * @param socket the socket through which the ack was received
1885 * @param tunnel connection to the other end
1886 * @param sender who sent the message
1887 * @param ack the acknowledgment message
1888 * @param atsi performance data for the connection
1889 * @return GNUNET_OK to keep the connection open,
1890 * GNUNET_SYSERR to close it (signal serious error)
1893 handle_ack (struct GNUNET_STREAM_Socket *socket,
1894 struct GNUNET_MESH_Tunnel *tunnel,
1895 const struct GNUNET_PeerIdentity *sender,
1896 const struct GNUNET_STREAM_AckMessage *ack,
1897 const struct GNUNET_ATS_Information*atsi)
1899 unsigned int packet;
1900 int need_retransmission;
1903 if (GNUNET_PEER_search (sender) != socket->other_peer)
1905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1906 "%x: Received ACK from non-confirming peer\n",
1911 switch (socket->state)
1913 case (STATE_ESTABLISHED):
1914 if (NULL == socket->write_handle)
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "%x: Received DATA_ACK when write_handle is NULL\n",
1921 /* FIXME: increment in the base sequence number is breaking current flow
1923 if (!((socket->write_sequence_number
1924 - ntohl (ack->base_sequence_number)) < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH))
1926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1927 "%x: Received DATA_ACK with unexpected base sequence "
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931 "%x: Current write sequence: %u; Ack's base sequence: %u\n",
1933 socket->write_sequence_number,
1934 ntohl (ack->base_sequence_number));
1937 /* FIXME: include the case when write_handle is cancelled - ignore the
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 "%x: Received DATA_ACK from %x\n",
1943 socket->other_peer);
1945 /* Cancel the retransmission task */
1946 if (GNUNET_SCHEDULER_NO_TASK != socket->retransmission_timeout_task_id)
1948 GNUNET_SCHEDULER_cancel (socket->retransmission_timeout_task_id);
1949 socket->retransmission_timeout_task_id =
1950 GNUNET_SCHEDULER_NO_TASK;
1953 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1955 if (NULL == socket->write_handle->messages[packet]) break;
1956 if (ntohl (ack->base_sequence_number)
1957 >= ntohl (socket->write_handle->messages[packet]->sequence_number))
1958 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
1963 ackbitmap_is_bit_set (&socket->write_handle->ack_bitmap,
1964 ntohl (socket->write_handle->messages[packet]->sequence_number)
1965 - ntohl (ack->base_sequence_number)))
1966 ackbitmap_modify_bit (&socket->write_handle->ack_bitmap,
1971 /* Update the receive window remaining
1972 FIXME : Should update with the value from a data ack with greater
1974 socket->receiver_window_available =
1975 ntohl (ack->receive_window_remaining);
1977 /* Check if we have received all acknowledgements */
1978 need_retransmission = GNUNET_NO;
1979 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1981 if (NULL == socket->write_handle->messages[packet]) break;
1982 if (GNUNET_YES != ackbitmap_is_bit_set
1983 (&socket->write_handle->ack_bitmap,packet))
1985 need_retransmission = GNUNET_YES;
1989 if (GNUNET_YES == need_retransmission)
1991 write_data (socket);
1993 else /* We have to call the write continuation callback now */
1995 /* Free the packets */
1996 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
1998 GNUNET_free_non_null (socket->write_handle->messages[packet]);
2000 if (NULL != socket->write_handle->write_cont)
2001 socket->write_handle->write_cont
2002 (socket->write_handle->write_cont_cls,
2004 socket->write_handle->size);
2005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2006 "%x: Write completion callback completed\n",
2008 /* We are done with the write handle - Freeing it */
2009 GNUNET_free (socket->write_handle);
2010 socket->write_handle = NULL;
2021 * Message Handler for mesh
2023 * @param cls the 'struct GNUNET_STREAM_Socket'
2024 * @param tunnel connection to the other end
2025 * @param tunnel_ctx unused
2026 * @param sender who sent the message
2027 * @param message the actual message
2028 * @param atsi performance data for the connection
2029 * @return GNUNET_OK to keep the connection open,
2030 * GNUNET_SYSERR to close it (signal serious error)
2033 client_handle_ack (void *cls,
2034 struct GNUNET_MESH_Tunnel *tunnel,
2036 const struct GNUNET_PeerIdentity *sender,
2037 const struct GNUNET_MessageHeader *message,
2038 const struct GNUNET_ATS_Information*atsi)
2040 struct GNUNET_STREAM_Socket *socket = cls;
2041 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2043 return handle_ack (socket, tunnel, sender, ack, atsi);
2048 * Message Handler for mesh
2050 * @param cls the server's listen socket
2051 * @param tunnel connection to the other end
2052 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
2053 * @param sender who sent the message
2054 * @param message the actual message
2055 * @param atsi performance data for the connection
2056 * @return GNUNET_OK to keep the connection open,
2057 * GNUNET_SYSERR to close it (signal serious error)
2060 server_handle_ack (void *cls,
2061 struct GNUNET_MESH_Tunnel *tunnel,
2063 const struct GNUNET_PeerIdentity *sender,
2064 const struct GNUNET_MessageHeader *message,
2065 const struct GNUNET_ATS_Information*atsi)
2067 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
2068 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
2070 return handle_ack (socket, tunnel, sender, ack, atsi);
2075 * For client message handlers, the stream socket is in the
2078 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
2079 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2080 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2081 sizeof (struct GNUNET_STREAM_AckMessage) },
2082 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2083 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2084 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2085 sizeof (struct GNUNET_STREAM_MessageHeader)},
2086 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2087 sizeof (struct GNUNET_STREAM_MessageHeader)},
2088 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2089 sizeof (struct GNUNET_STREAM_MessageHeader)},
2090 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2091 sizeof (struct GNUNET_STREAM_MessageHeader)},
2092 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2093 sizeof (struct GNUNET_STREAM_MessageHeader)},
2094 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2095 sizeof (struct GNUNET_STREAM_MessageHeader)},
2096 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2097 sizeof (struct GNUNET_STREAM_MessageHeader)},
2103 * For server message handlers, the stream socket is in the
2104 * tunnel context, and the listen socket in the closure argument.
2106 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
2107 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
2108 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
2109 sizeof (struct GNUNET_STREAM_AckMessage) },
2110 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
2111 sizeof (struct GNUNET_STREAM_MessageHeader)},
2112 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
2113 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
2114 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
2115 sizeof (struct GNUNET_STREAM_MessageHeader)},
2116 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
2117 sizeof (struct GNUNET_STREAM_MessageHeader)},
2118 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
2119 sizeof (struct GNUNET_STREAM_MessageHeader)},
2120 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
2121 sizeof (struct GNUNET_STREAM_MessageHeader)},
2122 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
2123 sizeof (struct GNUNET_STREAM_MessageHeader)},
2124 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
2125 sizeof (struct GNUNET_STREAM_MessageHeader)},
2126 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
2127 sizeof (struct GNUNET_STREAM_MessageHeader)},
2133 * Function called when our target peer is connected to our tunnel
2135 * @param cls the socket for which this tunnel is created
2136 * @param peer the peer identity of the target
2137 * @param atsi performance data for the connection
2140 mesh_peer_connect_callback (void *cls,
2141 const struct GNUNET_PeerIdentity *peer,
2142 const struct GNUNET_ATS_Information * atsi)
2144 struct GNUNET_STREAM_Socket *socket = cls;
2145 struct GNUNET_STREAM_MessageHeader *message;
2146 GNUNET_PEER_Id connected_peer;
2148 connected_peer = GNUNET_PEER_search (peer);
2150 if (connected_peer != socket->other_peer)
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2153 "%x: A peer which is not our target has connected",
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2160 "%x: Target peer %x connected\n",
2164 /* Set state to INIT */
2165 socket->state = STATE_INIT;
2167 /* Send HELLO message */
2168 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2169 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
2170 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2171 queue_message (socket,
2173 &set_state_hello_wait,
2176 /* Call open callback */
2177 if (NULL == socket->open_cb)
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2180 "STREAM_open callback is NULL\n");
2186 * Function called when our target peer is disconnected from our tunnel
2188 * @param cls the socket associated which this tunnel
2189 * @param peer the peer identity of the target
2192 mesh_peer_disconnect_callback (void *cls,
2193 const struct GNUNET_PeerIdentity *peer)
2200 * Method called whenever a peer creates a tunnel to us
2202 * @param cls closure
2203 * @param tunnel new handle to the tunnel
2204 * @param initiator peer that started the tunnel
2205 * @param atsi performance information for the tunnel
2206 * @return initial tunnel context for the tunnel
2207 * (can be NULL -- that's not an error)
2210 new_tunnel_notify (void *cls,
2211 struct GNUNET_MESH_Tunnel *tunnel,
2212 const struct GNUNET_PeerIdentity *initiator,
2213 const struct GNUNET_ATS_Information *atsi)
2215 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
2216 struct GNUNET_STREAM_Socket *socket;
2218 /* FIXME: If a tunnel is already created, we should not accept new tunnels
2219 from the same peer again until the socket is closed */
2221 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2222 socket->other_peer = GNUNET_PEER_intern (initiator);
2223 socket->tunnel = tunnel;
2224 socket->session_id = 0; /* FIXME */
2225 socket->state = STATE_INIT;
2226 socket->lsocket = lsocket;
2227 socket->our_id = lsocket->our_id;
2229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2230 "%x: Peer %x initiated tunnel to us\n",
2232 socket->other_peer);
2234 /* FIXME: Copy MESH handle from lsocket to socket */
2241 * Function called whenever an inbound tunnel is destroyed. Should clean up
2242 * any associated state. This function is NOT called if the client has
2243 * explicitly asked for the tunnel to be destroyed using
2244 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
2247 * @param cls closure (set from GNUNET_MESH_connect)
2248 * @param tunnel connection to the other end (henceforth invalid)
2249 * @param tunnel_ctx place where local state associated
2250 * with the tunnel is stored
2253 tunnel_cleaner (void *cls,
2254 const struct GNUNET_MESH_Tunnel *tunnel,
2257 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
2259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2260 "%x: Peer %x has terminated connection abruptly\n",
2262 socket->other_peer);
2264 socket->status = GNUNET_STREAM_SHUTDOWN;
2266 /* Clear Transmit handles */
2267 if (NULL != socket->transmit_handle)
2269 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2270 socket->transmit_handle = NULL;
2272 socket->tunnel = NULL;
2282 * Tries to open a stream to the target peer
2284 * @param cfg configuration to use
2285 * @param target the target peer to which the stream has to be opened
2286 * @param app_port the application port number which uniquely identifies this
2288 * @param open_cb this function will be called after stream has be established
2289 * @param open_cb_cls the closure for open_cb
2290 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
2291 * @return if successful it returns the stream socket; NULL if stream cannot be
2294 struct GNUNET_STREAM_Socket *
2295 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
2296 const struct GNUNET_PeerIdentity *target,
2297 GNUNET_MESH_ApplicationType app_port,
2298 GNUNET_STREAM_OpenCallback open_cb,
2302 struct GNUNET_STREAM_Socket *socket;
2303 struct GNUNET_PeerIdentity own_peer_id;
2304 enum GNUNET_STREAM_Option option;
2305 va_list vargs; /* Variable arguments */
2307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2310 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
2311 socket->other_peer = GNUNET_PEER_intern (target);
2312 socket->open_cb = open_cb;
2313 socket->open_cls = open_cb_cls;
2314 GNUNET_TESTING_get_peer_identity (cfg, &own_peer_id);
2315 socket->our_id = GNUNET_PEER_intern (&own_peer_id);
2318 socket->retransmit_timeout =
2319 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
2321 va_start (vargs, open_cb_cls); /* Parse variable args */
2323 option = va_arg (vargs, enum GNUNET_STREAM_Option);
2326 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
2327 /* Expect struct GNUNET_TIME_Relative */
2328 socket->retransmit_timeout = va_arg (vargs,
2329 struct GNUNET_TIME_Relative);
2331 case GNUNET_STREAM_OPTION_END:
2334 } while (GNUNET_STREAM_OPTION_END != option);
2335 va_end (vargs); /* End of variable args parsing */
2336 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
2337 10, /* QUEUE size as parameter? */
2339 NULL, /* No inbound tunnel handler */
2340 &tunnel_cleaner, /* FIXME: not required? */
2341 client_message_handlers,
2342 &app_port); /* We don't get inbound tunnels */
2343 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
2345 GNUNET_free (socket);
2349 /* Now create the mesh tunnel to target */
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2351 "Creating MESH Tunnel\n");
2352 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
2353 NULL, /* Tunnel context */
2354 &mesh_peer_connect_callback,
2355 &mesh_peer_disconnect_callback,
2357 GNUNET_assert (NULL != socket->tunnel);
2358 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 "%s() END\n", __func__);
2368 * Shutdown the stream for reading or writing (man 2 shutdown).
2370 * @param socket the stream socket
2371 * @param how SHUT_RD, SHUT_WR or SHUT_RDWR
2374 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2384 * @param socket the stream socket
2387 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
2389 struct MessageQueue *head;
2391 if (socket->read_task_id != GNUNET_SCHEDULER_NO_TASK)
2393 /* socket closed with read task pending!? */
2395 GNUNET_SCHEDULER_cancel (socket->read_task_id);
2396 socket->read_task_id = GNUNET_SCHEDULER_NO_TASK;
2399 /* Terminate the ack'ing tasks if they are still present */
2400 if (socket->ack_task_id != GNUNET_SCHEDULER_NO_TASK)
2402 GNUNET_SCHEDULER_cancel (socket->ack_task_id);
2403 socket->ack_task_id = GNUNET_SCHEDULER_NO_TASK;
2406 /* Clear Transmit handles */
2407 if (NULL != socket->transmit_handle)
2409 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2410 socket->transmit_handle = NULL;
2413 /* Clear existing message queue */
2414 while (NULL != (head = socket->queue_head)) {
2415 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2418 GNUNET_free (head->message);
2422 /* Close associated tunnel */
2423 if (NULL != socket->tunnel)
2425 GNUNET_MESH_tunnel_destroy (socket->tunnel);
2426 socket->tunnel = NULL;
2429 /* Close mesh connection */
2430 if (NULL != socket->mesh && NULL == socket->lsocket)
2432 GNUNET_MESH_disconnect (socket->mesh);
2433 socket->mesh = NULL;
2436 /* Release receive buffer */
2437 if (NULL != socket->receive_buffer)
2439 GNUNET_free (socket->receive_buffer);
2442 GNUNET_free (socket);
2447 * Listens for stream connections for a specific application ports
2449 * @param cfg the configuration to use
2450 * @param app_port the application port for which new streams will be accepted
2451 * @param listen_cb this function will be called when a peer tries to establish
2453 * @param listen_cb_cls closure for listen_cb
2454 * @return listen socket, NULL for any error
2456 struct GNUNET_STREAM_ListenSocket *
2457 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
2458 GNUNET_MESH_ApplicationType app_port,
2459 GNUNET_STREAM_ListenCallback listen_cb,
2460 void *listen_cb_cls)
2462 /* FIXME: Add variable args for passing configration options? */
2463 struct GNUNET_STREAM_ListenSocket *lsocket;
2464 struct GNUNET_PeerIdentity our_peer_id;
2466 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
2467 lsocket->port = app_port;
2468 lsocket->listen_cb = listen_cb;
2469 lsocket->listen_cb_cls = listen_cb_cls;
2470 GNUNET_TESTING_get_peer_identity (cfg, &our_peer_id);
2471 lsocket->our_id = GNUNET_PEER_intern (&our_peer_id);
2472 lsocket->mesh = GNUNET_MESH_connect (cfg,
2473 10, /* FIXME: QUEUE size as parameter? */
2474 lsocket, /* Closure */
2477 server_message_handlers,
2479 GNUNET_assert (NULL != lsocket->mesh);
2485 * Closes the listen socket
2487 * @param lsocket the listen socket
2490 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
2492 /* Close MESH connection */
2493 GNUNET_assert (NULL != lsocket->mesh);
2494 GNUNET_MESH_disconnect (lsocket->mesh);
2496 GNUNET_free (lsocket);
2501 * Tries to write the given data to the stream
2503 * @param socket the socket representing a stream
2504 * @param data the data buffer from where the data is written into the stream
2505 * @param size the number of bytes to be written from the data buffer
2506 * @param timeout the timeout period
2507 * @param write_cont the function to call upon writing some bytes into the stream
2508 * @param write_cont_cls the closure
2509 * @return handle to cancel the operation
2511 struct GNUNET_STREAM_IOWriteHandle *
2512 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
2515 struct GNUNET_TIME_Relative timeout,
2516 GNUNET_STREAM_CompletionContinuation write_cont,
2517 void *write_cont_cls)
2519 unsigned int num_needed_packets;
2520 unsigned int packet;
2521 struct GNUNET_STREAM_IOWriteHandle *io_handle;
2522 uint32_t packet_size;
2523 uint32_t payload_size;
2524 struct GNUNET_STREAM_DataMessage *data_msg;
2526 struct GNUNET_TIME_Relative ack_deadline;
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2531 /* Return NULL if there is already a write request pending */
2532 if (NULL != socket->write_handle)
2537 if (!((STATE_ESTABLISHED == socket->state)
2538 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
2539 || (STATE_RECEIVE_CLOSED == socket->state)))
2541 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2542 "%x: Attempting to write on a closed (OR) not-yet-established"
2547 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
2548 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
2549 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
2550 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
2551 io_handle->write_cont = write_cont;
2552 io_handle->write_cont_cls = write_cont_cls;
2553 io_handle->size = size;
2555 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2556 determined from RTT */
2557 ack_deadline = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
2558 /* Divide the given buffer into packets for sending */
2559 for (packet=0; packet < num_needed_packets; packet++)
2561 if ((packet + 1) * max_payload_size < size)
2563 payload_size = max_payload_size;
2564 packet_size = MAX_PACKET_SIZE;
2568 payload_size = size - packet * max_payload_size;
2569 packet_size = payload_size + sizeof (struct
2570 GNUNET_STREAM_DataMessage);
2572 io_handle->messages[packet] = GNUNET_malloc (packet_size);
2573 io_handle->messages[packet]->header.header.size = htons (packet_size);
2574 io_handle->messages[packet]->header.header.type =
2575 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
2576 io_handle->messages[packet]->sequence_number =
2577 htonl (socket->write_sequence_number++);
2578 io_handle->messages[packet]->offset = htonl (socket->write_offset);
2580 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2581 determined from RTT */
2582 io_handle->messages[packet]->ack_deadline =
2583 GNUNET_TIME_relative_hton (ack_deadline);
2584 data_msg = io_handle->messages[packet];
2585 /* Copy data from given buffer to the packet */
2586 memcpy (&data_msg[1],
2589 sweep += payload_size;
2590 socket->write_offset += payload_size;
2592 socket->write_handle = io_handle;
2593 write_data (socket);
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2596 "%s() END\n", __func__);
2603 * Tries to read data from the stream
2605 * @param socket the socket representing a stream
2606 * @param timeout the timeout period
2607 * @param proc function to call with data (once only)
2608 * @param proc_cls the closure for proc
2609 * @return handle to cancel the operation
2611 struct GNUNET_STREAM_IOReadHandle *
2612 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
2613 struct GNUNET_TIME_Relative timeout,
2614 GNUNET_STREAM_DataProcessor proc,
2617 struct GNUNET_STREAM_IOReadHandle *read_handle;
2619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2624 /* Return NULL if there is already a read handle; the user has to cancel that
2625 first before continuing or has to wait until it is completed */
2626 if (NULL != socket->read_handle) return NULL;
2628 GNUNET_assert (NULL != proc);
2630 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
2631 read_handle->proc = proc;
2632 read_handle->proc_cls = proc_cls;
2633 socket->read_handle = read_handle;
2635 /* Check if we have a packet at bitmap 0 */
2636 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
2639 socket->read_task_id = GNUNET_SCHEDULER_add_now (&call_read_processor,
2644 /* Setup the read timeout task */
2645 socket->read_io_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2657 * Cancel pending write operation.
2659 * @param ioh handle to operation to cancel
2662 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
2664 /* FIXME: Should cancel the write retransmission task */
2670 * Cancel pending read operation.
2672 * @param ioh handle to operation to cancel
2675 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)