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.
22 * @file stream/stream_api.c
23 * @brief Implementation of the stream library
24 * @author Sree Harsha Totakura
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_stream_lib.h"
30 #include "stream_protocol.h"
34 * The maximum packet size of a stream packet
36 #define MAX_PACKET_SIZE 64000
39 * The maximum payload a data message packet can carry
41 static size_t max_payload_size =
42 MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
47 #define RECEIVE_BUFFER_SIZE 4096000
50 * states in the Protocol
55 * Client initialization state
60 * Listener initialization state
65 * Pre-connection establishment state
70 * State where a connection has been established
75 * State where the socket is closed on our side and waiting to be ACK'ed
77 STATE_RECEIVE_CLOSE_WAIT,
80 * State where the socket is closed for reading
85 * State where the socket is closed on our side and waiting to be ACK'ed
87 STATE_TRANSMIT_CLOSE_WAIT,
90 * State where the socket is closed for writing
92 STATE_TRANSMIT_CLOSED,
95 * State where the socket is closed on our side and waiting to be ACK'ed
100 * State where the socket is closed
107 * Functions of this type are called when a message is written
109 * @param cls the closure from queue_message
110 * @param socket the socket the written message was bound to
112 typedef void (*SendFinishCallback) (void *cls,
113 struct GNUNET_STREAM_Socket *socket);
117 * The send message queue
124 struct GNUNET_STREAM_MessageHeader *message;
127 * Callback to be called when the message is sent
129 SendFinishCallback finish_cb;
132 * The closure for finish_cb
137 * The next message in queue. Should be NULL in the last message
139 struct MessageQueue *next;
142 * The next message in queue. Should be NULL in the last message
144 struct MessageQueue *prev;
149 * The STREAM Socket Handler
151 struct GNUNET_STREAM_Socket
155 * The peer identity of the peer at the other end of the stream
157 struct GNUNET_PeerIdentity other_peer;
160 * Retransmission timeout
162 struct GNUNET_TIME_Relative retransmit_timeout;
165 * The Acknowledgement Bitmap
167 GNUNET_STREAM_AckBitmap ack_bitmap;
170 * Time when the Acknowledgement was queued
172 struct GNUNET_TIME_Absolute ack_time_registered;
175 * Queued Acknowledgement deadline
177 struct GNUNET_TIME_Relative ack_time_deadline;
180 * The task for sending timely Acks
182 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
187 struct GNUNET_MESH_Handle *mesh;
190 * The mesh tunnel handle
192 struct GNUNET_MESH_Tunnel *tunnel;
195 * Stream open closure
200 * Stream open callback
202 GNUNET_STREAM_OpenCallback open_cb;
205 * The current transmit handle (if a pending transmit request exists)
207 struct GNUNET_MESH_TransmitHandle *transmit_handle;
210 * The current message associated with the transmit handle
212 struct MessageQueue *queue_head;
215 * The queue tail, should always point to the last message in queue
217 struct MessageQueue *queue_tail;
220 * The write IO_handle associated with this socket
222 struct GNUNET_STREAM_IOHandle *write_handle;
225 * The read IO_handle associated with this socket
227 struct GNUNET_STREAM_IOHandle *read_handle;
230 * Buffer for storing received messages
232 void *receive_buffer;
235 * The state of the protocol associated with this socket
240 * The status of the socket
242 enum GNUNET_STREAM_Status status;
245 * The number of previous timeouts; FIXME: currently not used
247 unsigned int retries;
250 * The session id associated with this stream connection
251 * FIXME: Not used currently, may be removed
256 * Write sequence number. Set to random when sending HELLO(client) and
259 uint32_t write_sequence_number;
262 * Read sequence number. This number's value is determined during handshake
264 uint32_t read_sequence_number;
267 * receiver's available buffer after the last acknowledged packet
269 uint32_t receive_window_available;
274 * A socket for listening
276 struct GNUNET_STREAM_ListenSocket
282 struct GNUNET_MESH_Handle *mesh;
285 * The callback function which is called after successful opening socket
287 GNUNET_STREAM_ListenCallback listen_cb;
290 * The call back closure
297 GNUNET_MESH_ApplicationType port;
306 struct GNUNET_STREAM_IOHandle
309 * The packet_buffers associated with this Handle
311 struct GNUNET_STREAM_DataMessage *messages[64];
314 * The bitmap of this IOHandle; Corresponding bit for a message is set when
315 * it has been acknowledged by the receiver
317 GNUNET_STREAM_AckBitmap ack_bitmap;
320 * receiver's available buffer
322 uint32_t receive_window_available;
325 * Number of packets sent before waiting for an ack
327 * FIXME: Do we need this?
329 unsigned int sent_packets;
334 * Default value in seconds for various timeouts
336 static unsigned int default_timeout = 300;
340 * Callback function for sending hello message
342 * @param cls closure the socket
343 * @param size number of bytes available in buf
344 * @param buf where the callee should write the message
345 * @return number of bytes written to buf
348 send_message_notify (void *cls, size_t size, void *buf)
350 struct GNUNET_STREAM_Socket *socket = cls;
351 struct MessageQueue *head;
354 socket->transmit_handle = NULL; /* Remove the transmit handle */
355 head = socket->queue_head;
357 return 0; /* just to be safe */
358 if (0 == size) /* request timed out */
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Message sending timed out. Retry %d \n",
364 socket->transmit_handle =
365 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
368 /* FIXME: exponential backoff */
369 socket->retransmit_timeout,
371 ntohs (head->message->header.size),
372 &send_message_notify,
377 ret = ntohs (head->message->header.size);
378 GNUNET_assert (size >= ret);
379 memcpy (buf, head->message, ret);
380 if (NULL != head->finish_cb)
382 head->finish_cb (socket, head->finish_cb_cls);
384 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
387 GNUNET_free (head->message);
389 head = socket->queue_head;
390 if (NULL != head) /* more pending messages to send */
393 socket->transmit_handle =
394 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
397 /* FIXME: exponential backoff */
398 socket->retransmit_timeout,
400 ntohs (head->message->header.size),
401 &send_message_notify,
409 * Queues a message for sending using the mesh connection of a socket
411 * @param socket the socket whose mesh connection is used
412 * @param message the message to be sent
413 * @param finish_cb the callback to be called when the message is sent
414 * @param finish_cb_cls the closure for the callback
417 queue_message (struct GNUNET_STREAM_Socket *socket,
418 struct GNUNET_STREAM_MessageHeader *message,
419 SendFinishCallback finish_cb,
422 struct MessageQueue *queue_entity;
424 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
425 queue_entity->message = message;
426 queue_entity->finish_cb = finish_cb;
427 queue_entity->finish_cb_cls = finish_cb_cls;
428 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
431 if (NULL == socket->transmit_handle)
434 socket->transmit_handle =
435 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
438 socket->retransmit_timeout,
440 ntohs (message->header.size),
441 &send_message_notify,
448 * Callback function for sending ack message
450 * @param cls closure the ACK message created in ack_task
451 * @param size number of bytes available in buffer
452 * @param buf where the callee should write the message
453 * @return number of bytes written to buf
456 send_ack_notify (void *cls, size_t size, void *buf)
458 struct GNUNET_STREAM_AckMessage *ack_msg = cls;
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "%s called with size 0\n", __func__);
466 GNUNET_assert (ack_msg->header.header.size <= size);
468 size = ack_msg->header.header.size;
469 memcpy (buf, ack_msg, size);
475 * Task for sending ACK message
477 * @param cls the socket
478 * @param tc the Task context
482 const struct GNUNET_SCHEDULER_TaskContext *tc)
484 struct GNUNET_STREAM_Socket *socket = cls;
485 struct GNUNET_STREAM_AckMessage *ack_msg;
487 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
492 socket->ack_task_id = 0;
494 /* Create the ACK Message */
495 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
496 ack_msg->header.header.size = htons (sizeof (struct
497 GNUNET_STREAM_AckMessage));
498 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
499 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
500 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
501 ack_msg->receive_window_remaining = htonl (socket->receive_window_available);
503 /* Request MESH for sending ACK */
504 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
507 socket->retransmit_timeout,
509 ntohs (ack_msg->header.header.size),
518 * Function to modify a bit in GNUNET_STREAM_AckBitmap
520 * @param bitmap the bitmap to modify
521 * @param bit the bit number to modify
522 * @param value GNUNET_YES to on, GNUNET_NO to off
525 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
529 GNUNET_assert (bit < 64);
530 if (GNUNET_YES == value)
531 *bitmap |= (1LL << bit);
533 *bitmap &= ~(1LL << bit);
538 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
540 * @param bitmap address of the bitmap that has to be checked
541 * @param bit the bit number to check
542 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
545 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
548 GNUNET_assert (bit < 64);
549 return 0 != (*bitmap & (1LL << bit));
555 * Function called when Data Message is sent
557 * @param cls the io_handle corresponding to the Data Message
558 * @param socket the socket which was used
561 write_data_finish_cb (void *cls,
562 struct GNUNET_STREAM_Socket *socket)
564 struct GNUNET_STREAM_IOHandle *io_handle = cls;
566 io_handle->sent_packets++;
571 * Writes data using the given socket. The amount of data written is limited by
572 * the receive_window_size
574 * @param socket the socket to use
577 write_data (struct GNUNET_STREAM_Socket *socket)
579 struct GNUNET_STREAM_IOHandle *io_handle = socket->write_handle;
584 /* Find the last acknowledged packet */
585 for (packet=0; packet < 64; packet++)
587 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
590 else if (NULL == io_handle->messages[packet])
593 /* Resend packets which weren't ack'ed */
594 for (packet=0; packet < ack_packet; packet++)
596 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
599 queue_message (socket,
600 &io_handle->messages[packet]->header,
605 packet = ack_packet + 1;
606 /* Now send new packets if there is enough buffer space */
607 while ( (NULL != io_handle->messages[packet]) &&
608 (io_handle->receive_window_available >= ntohs (io_handle->messages[packet]->header.header.size)) )
610 io_handle->receive_window_available -= ntohs (io_handle->messages[packet]->header.header.size);
611 queue_message (socket,
612 &io_handle->messages[packet]->header,
613 &write_data_finish_cb,
621 * Handler for DATA messages; Same for both client and server
623 * @param socket the socket through which the ack was received
624 * @param tunnel connection to the other end
625 * @param sender who sent the message
626 * @param msg the data message
627 * @param atsi performance data for the connection
628 * @return GNUNET_OK to keep the connection open,
629 * GNUNET_SYSERR to close it (signal serious error)
632 handle_data (struct GNUNET_STREAM_Socket *socket,
633 struct GNUNET_MESH_Tunnel *tunnel,
634 const struct GNUNET_PeerIdentity *sender,
635 const struct GNUNET_STREAM_DataMessage *msg,
636 const struct GNUNET_ATS_Information*atsi)
641 size = msg->header.header.size;
642 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
645 return GNUNET_SYSERR;
648 switch (socket->state)
650 case STATE_ESTABLISHED:
651 case STATE_TRANSMIT_CLOSED:
652 case STATE_TRANSMIT_CLOSE_WAIT:
653 GNUNET_assert (NULL != socket->receive_buffer);
654 /* check if the message's sequence number is greater than the one we are
656 if (ntohl (msg->sequence_number) - socket->read_sequence_number >= 64)
657 {/* We are receiving a retransmitted message */
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "Message with sequence number %d retransmitted\n",
660 ntohl (socket->read_sequence_number));
664 /* Copy Data to buffer and send acknowledgement for this packet */
665 size -= sizeof (struct GNUNET_STREAM_DataMessage);
667 memcpy (socket->receive_buffer
668 + (ntohl (msg->sequence_number) - socket->read_sequence_number)
673 /* Modify the ACK bitmap */
674 ackbitmap_modify_bit (&socket->ack_bitmap,
675 ntohl (msg->sequence_number) -
676 socket->read_sequence_number,
679 /* Start ACK sending task if one is not already present */
680 if (0 == socket->ack_task_id)
682 socket->ack_task_id =
683 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
692 /* FIXME: call statistics */
699 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
701 * @param cls the socket (set from GNUNET_MESH_connect)
702 * @param tunnel connection to the other end
703 * @param tunnel_ctx place to store local state associated with the tunnel
704 * @param sender who sent the message
705 * @param message the actual message
706 * @param atsi performance data for the connection
707 * @return GNUNET_OK to keep the connection open,
708 * GNUNET_SYSERR to close it (signal serious error)
711 client_handle_data (void *cls,
712 struct GNUNET_MESH_Tunnel *tunnel,
714 const struct GNUNET_PeerIdentity *sender,
715 const struct GNUNET_MessageHeader *message,
716 const struct GNUNET_ATS_Information*atsi)
718 struct GNUNET_STREAM_Socket *socket = cls;
720 return handle_data (socket,
723 (const struct GNUNET_STREAM_DataMessage *) message,
729 * Callback to set state to ESTABLISHED
731 * @param cls the closure from queue_message FIXME: document
732 * @param socket the socket to requiring state change
735 set_state_established (void *cls,
736 struct GNUNET_STREAM_Socket *socket)
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attaining ESTABLISHED state\n");
739 /* Initialize the receive buffer */
740 socket->receive_buffer = GNUNET_malloc (RECEIVE_BUFFER_SIZE);
741 socket->state = STATE_ESTABLISHED;
746 * Callback to set state to HELLO_WAIT
748 * @param cls the closure from queue_message
749 * @param socket the socket to requiring state change
752 set_state_hello_wait (void *cls,
753 struct GNUNET_STREAM_Socket *socket)
755 GNUNET_assert (STATE_INIT == socket->state);
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attaining HELLO_WAIT state\n");
757 socket->state = STATE_HELLO_WAIT;
762 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
764 * @param cls the socket (set from GNUNET_MESH_connect)
765 * @param tunnel connection to the other end
766 * @param tunnel_ctx this is NULL
767 * @param sender who sent the message
768 * @param message the actual message
769 * @param atsi performance data for the connection
770 * @return GNUNET_OK to keep the connection open,
771 * GNUNET_SYSERR to close it (signal serious error)
774 client_handle_hello_ack (void *cls,
775 struct GNUNET_MESH_Tunnel *tunnel,
777 const struct GNUNET_PeerIdentity *sender,
778 const struct GNUNET_MessageHeader *message,
779 const struct GNUNET_ATS_Information*atsi)
781 struct GNUNET_STREAM_Socket *socket = cls;
782 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
783 struct GNUNET_STREAM_HelloAckMessage *reply;
785 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
786 GNUNET_assert (socket->tunnel == tunnel);
787 switch (socket->state)
789 case STATE_HELLO_WAIT:
790 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
791 socket->receive_window_available = ntohl (ack_msg->receive_window_size);
792 /* Get the random sequence number */
793 socket->write_sequence_number =
794 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
796 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
797 reply->header.header.size =
798 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
799 reply->header.header.type =
800 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
801 reply->sequence_number = htonl (socket->write_sequence_number);
802 reply->receive_window_size = htonl (RECEIVE_BUFFER_SIZE);
803 queue_message (socket,
805 &set_state_established,
808 case STATE_ESTABLISHED:
809 case STATE_RECEIVE_CLOSE_WAIT:
810 // call statistics (# ACKs ignored++)
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815 "Server sent HELLO_ACK when in state %d\n", socket->state);
816 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
817 return GNUNET_SYSERR;
824 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
826 * @param cls the socket (set from GNUNET_MESH_connect)
827 * @param tunnel connection to the other end
828 * @param tunnel_ctx this is NULL
829 * @param sender who sent the message
830 * @param message the actual message
831 * @param atsi performance data for the connection
832 * @return GNUNET_OK to keep the connection open,
833 * GNUNET_SYSERR to close it (signal serious error)
836 client_handle_reset (void *cls,
837 struct GNUNET_MESH_Tunnel *tunnel,
839 const struct GNUNET_PeerIdentity *sender,
840 const struct GNUNET_MessageHeader *message,
841 const struct GNUNET_ATS_Information*atsi)
843 struct GNUNET_STREAM_Socket *socket = cls;
850 * Common message handler for handling TRANSMIT_CLOSE messages
852 * @param socket the socket through which the ack was received
853 * @param tunnel connection to the other end
854 * @param sender who sent the message
855 * @param msg the transmit close message
856 * @param atsi performance data for the connection
857 * @return GNUNET_OK to keep the connection open,
858 * GNUNET_SYSERR to close it (signal serious error)
861 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
862 struct GNUNET_MESH_Tunnel *tunnel,
863 const struct GNUNET_PeerIdentity *sender,
864 const struct GNUNET_STREAM_MessageHeader *msg,
865 const struct GNUNET_ATS_Information*atsi)
867 struct GNUNET_STREAM_MessageHeader *reply;
869 switch (socket->state)
871 case STATE_ESTABLISHED:
872 socket->state = STATE_RECEIVE_CLOSED;
874 /* Send TRANSMIT_CLOSE_ACK */
875 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
877 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
878 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
879 queue_message (socket, reply, NULL, NULL);
883 /* FIXME: Call statistics? */
891 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
893 * @param cls the socket (set from GNUNET_MESH_connect)
894 * @param tunnel connection to the other end
895 * @param tunnel_ctx this is NULL
896 * @param sender who sent the message
897 * @param message the actual message
898 * @param atsi performance data for the connection
899 * @return GNUNET_OK to keep the connection open,
900 * GNUNET_SYSERR to close it (signal serious error)
903 client_handle_transmit_close (void *cls,
904 struct GNUNET_MESH_Tunnel *tunnel,
906 const struct GNUNET_PeerIdentity *sender,
907 const struct GNUNET_MessageHeader *message,
908 const struct GNUNET_ATS_Information*atsi)
910 struct GNUNET_STREAM_Socket *socket = cls;
912 return handle_transmit_close (socket,
915 (struct GNUNET_STREAM_MessageHeader *)message,
921 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
923 * @param cls the socket (set from GNUNET_MESH_connect)
924 * @param tunnel connection to the other end
925 * @param tunnel_ctx this is NULL
926 * @param sender who sent the message
927 * @param message the actual message
928 * @param atsi performance data for the connection
929 * @return GNUNET_OK to keep the connection open,
930 * GNUNET_SYSERR to close it (signal serious error)
933 client_handle_transmit_close_ack (void *cls,
934 struct GNUNET_MESH_Tunnel *tunnel,
936 const struct GNUNET_PeerIdentity *sender,
937 const struct GNUNET_MessageHeader *message,
938 const struct GNUNET_ATS_Information*atsi)
940 struct GNUNET_STREAM_Socket *socket = cls;
947 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
949 * @param cls the socket (set from GNUNET_MESH_connect)
950 * @param tunnel connection to the other end
951 * @param tunnel_ctx this is NULL
952 * @param sender who sent the message
953 * @param message the actual message
954 * @param atsi performance data for the connection
955 * @return GNUNET_OK to keep the connection open,
956 * GNUNET_SYSERR to close it (signal serious error)
959 client_handle_receive_close (void *cls,
960 struct GNUNET_MESH_Tunnel *tunnel,
962 const struct GNUNET_PeerIdentity *sender,
963 const struct GNUNET_MessageHeader *message,
964 const struct GNUNET_ATS_Information*atsi)
966 struct GNUNET_STREAM_Socket *socket = cls;
973 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
975 * @param cls the socket (set from GNUNET_MESH_connect)
976 * @param tunnel connection to the other end
977 * @param tunnel_ctx this is NULL
978 * @param sender who sent the message
979 * @param message the actual message
980 * @param atsi performance data for the connection
981 * @return GNUNET_OK to keep the connection open,
982 * GNUNET_SYSERR to close it (signal serious error)
985 client_handle_receive_close_ack (void *cls,
986 struct GNUNET_MESH_Tunnel *tunnel,
988 const struct GNUNET_PeerIdentity *sender,
989 const struct GNUNET_MessageHeader *message,
990 const struct GNUNET_ATS_Information*atsi)
992 struct GNUNET_STREAM_Socket *socket = cls;
999 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1001 * @param cls the socket (set from GNUNET_MESH_connect)
1002 * @param tunnel connection to the other end
1003 * @param tunnel_ctx this is NULL
1004 * @param sender who sent the message
1005 * @param message the actual message
1006 * @param atsi performance data for the connection
1007 * @return GNUNET_OK to keep the connection open,
1008 * GNUNET_SYSERR to close it (signal serious error)
1011 client_handle_close (void *cls,
1012 struct GNUNET_MESH_Tunnel *tunnel,
1014 const struct GNUNET_PeerIdentity *sender,
1015 const struct GNUNET_MessageHeader *message,
1016 const struct GNUNET_ATS_Information*atsi)
1018 struct GNUNET_STREAM_Socket *socket = cls;
1025 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1027 * @param cls the socket (set from GNUNET_MESH_connect)
1028 * @param tunnel connection to the other end
1029 * @param tunnel_ctx this is NULL
1030 * @param sender who sent the message
1031 * @param message the actual message
1032 * @param atsi performance data for the connection
1033 * @return GNUNET_OK to keep the connection open,
1034 * GNUNET_SYSERR to close it (signal serious error)
1037 client_handle_close_ack (void *cls,
1038 struct GNUNET_MESH_Tunnel *tunnel,
1040 const struct GNUNET_PeerIdentity *sender,
1041 const struct GNUNET_MessageHeader *message,
1042 const struct GNUNET_ATS_Information*atsi)
1044 struct GNUNET_STREAM_Socket *socket = cls;
1049 /*****************************/
1050 /* Server's Message Handlers */
1051 /*****************************/
1054 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1056 * @param cls the closure
1057 * @param tunnel connection to the other end
1058 * @param tunnel_ctx the socket
1059 * @param sender who sent the message
1060 * @param message the actual message
1061 * @param atsi performance data for the connection
1062 * @return GNUNET_OK to keep the connection open,
1063 * GNUNET_SYSERR to close it (signal serious error)
1066 server_handle_data (void *cls,
1067 struct GNUNET_MESH_Tunnel *tunnel,
1069 const struct GNUNET_PeerIdentity *sender,
1070 const struct GNUNET_MessageHeader *message,
1071 const struct GNUNET_ATS_Information*atsi)
1073 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1075 return handle_data (socket,
1078 (const struct GNUNET_STREAM_DataMessage *)message,
1084 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1086 * @param cls the closure
1087 * @param tunnel connection to the other end
1088 * @param tunnel_ctx the socket
1089 * @param sender who sent the message
1090 * @param message the actual message
1091 * @param atsi performance data for the connection
1092 * @return GNUNET_OK to keep the connection open,
1093 * GNUNET_SYSERR to close it (signal serious error)
1096 server_handle_hello (void *cls,
1097 struct GNUNET_MESH_Tunnel *tunnel,
1099 const struct GNUNET_PeerIdentity *sender,
1100 const struct GNUNET_MessageHeader *message,
1101 const struct GNUNET_ATS_Information*atsi)
1103 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1104 struct GNUNET_STREAM_HelloAckMessage *reply;
1106 GNUNET_assert (socket->tunnel == tunnel);
1107 if (STATE_INIT == socket->state)
1109 /* Get the random sequence number */
1110 socket->write_sequence_number =
1111 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1113 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1114 reply->header.header.size =
1115 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1116 reply->header.header.type =
1117 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1118 reply->sequence_number = htonl (socket->write_sequence_number);
1119 queue_message (socket,
1121 &set_state_hello_wait,
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1127 "Client sent HELLO when in state %d\n", socket->state);
1128 /* FIXME: Send RESET? */
1136 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1138 * @param cls the closure
1139 * @param tunnel connection to the other end
1140 * @param tunnel_ctx the socket
1141 * @param sender who sent the message
1142 * @param message the actual message
1143 * @param atsi performance data for the connection
1144 * @return GNUNET_OK to keep the connection open,
1145 * GNUNET_SYSERR to close it (signal serious error)
1148 server_handle_hello_ack (void *cls,
1149 struct GNUNET_MESH_Tunnel *tunnel,
1151 const struct GNUNET_PeerIdentity *sender,
1152 const struct GNUNET_MessageHeader *message,
1153 const struct GNUNET_ATS_Information*atsi)
1155 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1156 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1158 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1159 GNUNET_assert (socket->tunnel == tunnel);
1160 if (STATE_HELLO_WAIT == socket->state)
1162 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1163 socket->receive_window_available =
1164 ntohl (ack_message->receive_window_size);
1165 socket->state = STATE_ESTABLISHED;
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Client sent HELLO_ACK when in state %d\n", socket->state);
1171 /* FIXME: Send RESET? */
1179 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1181 * @param cls the closure
1182 * @param tunnel connection to the other end
1183 * @param tunnel_ctx the socket
1184 * @param sender who sent the message
1185 * @param message the actual message
1186 * @param atsi performance data for the connection
1187 * @return GNUNET_OK to keep the connection open,
1188 * GNUNET_SYSERR to close it (signal serious error)
1191 server_handle_reset (void *cls,
1192 struct GNUNET_MESH_Tunnel *tunnel,
1194 const struct GNUNET_PeerIdentity *sender,
1195 const struct GNUNET_MessageHeader *message,
1196 const struct GNUNET_ATS_Information*atsi)
1198 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1205 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1207 * @param cls the closure
1208 * @param tunnel connection to the other end
1209 * @param tunnel_ctx the socket
1210 * @param sender who sent the message
1211 * @param message the actual message
1212 * @param atsi performance data for the connection
1213 * @return GNUNET_OK to keep the connection open,
1214 * GNUNET_SYSERR to close it (signal serious error)
1217 server_handle_transmit_close (void *cls,
1218 struct GNUNET_MESH_Tunnel *tunnel,
1220 const struct GNUNET_PeerIdentity *sender,
1221 const struct GNUNET_MessageHeader *message,
1222 const struct GNUNET_ATS_Information*atsi)
1224 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1226 return handle_transmit_close (socket,
1229 (struct GNUNET_STREAM_MessageHeader *)message,
1235 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1237 * @param cls the closure
1238 * @param tunnel connection to the other end
1239 * @param tunnel_ctx the socket
1240 * @param sender who sent the message
1241 * @param message the actual message
1242 * @param atsi performance data for the connection
1243 * @return GNUNET_OK to keep the connection open,
1244 * GNUNET_SYSERR to close it (signal serious error)
1247 server_handle_transmit_close_ack (void *cls,
1248 struct GNUNET_MESH_Tunnel *tunnel,
1250 const struct GNUNET_PeerIdentity *sender,
1251 const struct GNUNET_MessageHeader *message,
1252 const struct GNUNET_ATS_Information*atsi)
1254 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1261 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1263 * @param cls the closure
1264 * @param tunnel connection to the other end
1265 * @param tunnel_ctx the socket
1266 * @param sender who sent the message
1267 * @param message the actual message
1268 * @param atsi performance data for the connection
1269 * @return GNUNET_OK to keep the connection open,
1270 * GNUNET_SYSERR to close it (signal serious error)
1273 server_handle_receive_close (void *cls,
1274 struct GNUNET_MESH_Tunnel *tunnel,
1276 const struct GNUNET_PeerIdentity *sender,
1277 const struct GNUNET_MessageHeader *message,
1278 const struct GNUNET_ATS_Information*atsi)
1280 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1287 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1289 * @param cls the closure
1290 * @param tunnel connection to the other end
1291 * @param tunnel_ctx the socket
1292 * @param sender who sent the message
1293 * @param message the actual message
1294 * @param atsi performance data for the connection
1295 * @return GNUNET_OK to keep the connection open,
1296 * GNUNET_SYSERR to close it (signal serious error)
1299 server_handle_receive_close_ack (void *cls,
1300 struct GNUNET_MESH_Tunnel *tunnel,
1302 const struct GNUNET_PeerIdentity *sender,
1303 const struct GNUNET_MessageHeader *message,
1304 const struct GNUNET_ATS_Information*atsi)
1306 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1313 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1315 * @param cls the closure
1316 * @param tunnel connection to the other end
1317 * @param tunnel_ctx the socket
1318 * @param sender who sent the message
1319 * @param message the actual message
1320 * @param atsi performance data for the connection
1321 * @return GNUNET_OK to keep the connection open,
1322 * GNUNET_SYSERR to close it (signal serious error)
1325 server_handle_close (void *cls,
1326 struct GNUNET_MESH_Tunnel *tunnel,
1328 const struct GNUNET_PeerIdentity *sender,
1329 const struct GNUNET_MessageHeader *message,
1330 const struct GNUNET_ATS_Information*atsi)
1332 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1339 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1341 * @param cls the closure
1342 * @param tunnel connection to the other end
1343 * @param tunnel_ctx the socket
1344 * @param sender who sent the message
1345 * @param message the actual message
1346 * @param atsi performance data for the connection
1347 * @return GNUNET_OK to keep the connection open,
1348 * GNUNET_SYSERR to close it (signal serious error)
1351 server_handle_close_ack (void *cls,
1352 struct GNUNET_MESH_Tunnel *tunnel,
1354 const struct GNUNET_PeerIdentity *sender,
1355 const struct GNUNET_MessageHeader *message,
1356 const struct GNUNET_ATS_Information*atsi)
1358 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1365 * Message Handler for mesh
1367 * @param socket the socket through which the ack was received
1368 * @param tunnel connection to the other end
1369 * @param sender who sent the message
1370 * @param ack the acknowledgment message
1371 * @param atsi performance data for the connection
1372 * @return GNUNET_OK to keep the connection open,
1373 * GNUNET_SYSERR to close it (signal serious error)
1376 handle_ack (struct GNUNET_STREAM_Socket *socket,
1377 struct GNUNET_MESH_Tunnel *tunnel,
1378 const struct GNUNET_PeerIdentity *sender,
1379 const struct GNUNET_STREAM_AckMessage *ack,
1380 const struct GNUNET_ATS_Information*atsi)
1382 switch (socket->state)
1384 case (STATE_ESTABLISHED):
1385 if (NULL == socket->write_handle)
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "Received DATA ACK when write_handle is NULL\n");
1392 socket->write_handle->ack_bitmap = GNUNET_ntohll (ack->bitmap);
1393 socket->write_handle->receive_window_available =
1394 ntohl (ack->receive_window_remaining);
1395 write_data (socket);
1405 * Message Handler for mesh
1407 * @param cls the 'struct GNUNET_STREAM_Socket'
1408 * @param tunnel connection to the other end
1409 * @param tunnel_ctx unused
1410 * @param sender who sent the message
1411 * @param message the actual message
1412 * @param atsi performance data for the connection
1413 * @return GNUNET_OK to keep the connection open,
1414 * GNUNET_SYSERR to close it (signal serious error)
1417 client_handle_ack (void *cls,
1418 struct GNUNET_MESH_Tunnel *tunnel,
1420 const struct GNUNET_PeerIdentity *sender,
1421 const struct GNUNET_MessageHeader *message,
1422 const struct GNUNET_ATS_Information*atsi)
1424 struct GNUNET_STREAM_Socket *socket = cls;
1425 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1427 return handle_ack (socket, tunnel, sender, ack, atsi);
1432 * Message Handler for mesh
1434 * @param cls the server's listen socket
1435 * @param tunnel connection to the other end
1436 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
1437 * @param sender who sent the message
1438 * @param message the actual message
1439 * @param atsi performance data for the connection
1440 * @return GNUNET_OK to keep the connection open,
1441 * GNUNET_SYSERR to close it (signal serious error)
1444 server_handle_ack (void *cls,
1445 struct GNUNET_MESH_Tunnel *tunnel,
1447 const struct GNUNET_PeerIdentity *sender,
1448 const struct GNUNET_MessageHeader *message,
1449 const struct GNUNET_ATS_Information*atsi)
1451 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1452 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1454 return handle_ack (socket, tunnel, sender, ack, atsi);
1459 * For client message handlers, the stream socket is in the
1462 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
1463 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1464 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1465 sizeof (struct GNUNET_STREAM_AckMessage) },
1466 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1467 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1468 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1469 sizeof (struct GNUNET_STREAM_MessageHeader)},
1470 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1471 sizeof (struct GNUNET_STREAM_MessageHeader)},
1472 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1473 sizeof (struct GNUNET_STREAM_MessageHeader)},
1474 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1475 sizeof (struct GNUNET_STREAM_MessageHeader)},
1476 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1477 sizeof (struct GNUNET_STREAM_MessageHeader)},
1478 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1479 sizeof (struct GNUNET_STREAM_MessageHeader)},
1480 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1481 sizeof (struct GNUNET_STREAM_MessageHeader)},
1487 * For server message handlers, the stream socket is in the
1488 * tunnel context, and the listen socket in the closure argument.
1490 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
1491 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1492 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1493 sizeof (struct GNUNET_STREAM_AckMessage) },
1494 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
1495 sizeof (struct GNUNET_STREAM_MessageHeader)},
1496 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1497 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1498 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1499 sizeof (struct GNUNET_STREAM_MessageHeader)},
1500 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1501 sizeof (struct GNUNET_STREAM_MessageHeader)},
1502 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1503 sizeof (struct GNUNET_STREAM_MessageHeader)},
1504 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1505 sizeof (struct GNUNET_STREAM_MessageHeader)},
1506 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1507 sizeof (struct GNUNET_STREAM_MessageHeader)},
1508 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1509 sizeof (struct GNUNET_STREAM_MessageHeader)},
1510 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1511 sizeof (struct GNUNET_STREAM_MessageHeader)},
1517 * Function called when our target peer is connected to our tunnel
1519 * @param cls the socket for which this tunnel is created
1520 * @param peer the peer identity of the target
1521 * @param atsi performance data for the connection
1524 mesh_peer_connect_callback (void *cls,
1525 const struct GNUNET_PeerIdentity *peer,
1526 const struct GNUNET_ATS_Information * atsi)
1528 struct GNUNET_STREAM_Socket *socket = cls;
1529 struct GNUNET_STREAM_MessageHeader *message;
1531 if (0 != memcmp (&socket->other_peer,
1533 sizeof (struct GNUNET_PeerIdentity)))
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1536 "A peer (%s) which is not our target has connected to our tunnel",
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 "Target peer %s connected\n", GNUNET_i2s (peer));
1544 /* Set state to INIT */
1545 socket->state = STATE_INIT;
1547 /* Send HELLO message */
1548 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1549 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
1550 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1551 queue_message (socket,
1553 &set_state_hello_wait,
1556 /* Call open callback */
1557 if (NULL == socket->open_cb)
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "STREAM_open callback is NULL\n");
1564 socket->open_cb (socket->open_cls, socket);
1570 * Function called when our target peer is disconnected from our tunnel
1572 * @param cls the socket associated which this tunnel
1573 * @param peer the peer identity of the target
1576 mesh_peer_disconnect_callback (void *cls,
1577 const struct GNUNET_PeerIdentity *peer)
1589 * Tries to open a stream to the target peer
1591 * @param cfg configuration to use
1592 * @param target the target peer to which the stream has to be opened
1593 * @param app_port the application port number which uniquely identifies this
1595 * @param open_cb this function will be called after stream has be established
1596 * @param open_cb_cls the closure for open_cb
1597 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
1598 * @return if successful it returns the stream socket; NULL if stream cannot be
1601 struct GNUNET_STREAM_Socket *
1602 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
1603 const struct GNUNET_PeerIdentity *target,
1604 GNUNET_MESH_ApplicationType app_port,
1605 GNUNET_STREAM_OpenCallback open_cb,
1609 struct GNUNET_STREAM_Socket *socket;
1610 enum GNUNET_STREAM_Option option;
1611 va_list vargs; /* Variable arguments */
1613 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
1614 socket->other_peer = *target;
1615 socket->open_cb = open_cb;
1616 socket->open_cls = open_cb_cls;
1619 socket->retransmit_timeout =
1620 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
1622 va_start (vargs, open_cb_cls); /* Parse variable args */
1624 option = va_arg (vargs, enum GNUNET_STREAM_Option);
1627 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
1628 /* Expect struct GNUNET_TIME_Relative */
1629 socket->retransmit_timeout = va_arg (vargs,
1630 struct GNUNET_TIME_Relative);
1632 case GNUNET_STREAM_OPTION_END:
1635 } while (GNUNET_STREAM_OPTION_END != option);
1636 va_end (vargs); /* End of variable args parsing */
1638 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
1639 1, /* QUEUE size as parameter? */
1641 NULL, /* No inbound tunnel handler */
1642 NULL, /* No inbound tunnel cleaner */
1643 client_message_handlers,
1644 NULL); /* We don't get inbound tunnels */
1645 // FIXME: if (NULL == socket->mesh) ...
1647 /* Now create the mesh tunnel to target */
1648 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
1649 NULL, /* Tunnel context */
1650 &mesh_peer_connect_callback,
1651 &mesh_peer_disconnect_callback,
1653 // FIXME: if (NULL == socket->tunnel) ...
1662 * @param socket the stream socket
1665 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
1667 struct MessageQueue *head;
1669 /* Clear Transmit handles */
1670 if (NULL != socket->transmit_handle)
1672 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
1673 socket->transmit_handle = NULL;
1676 /* Clear existing message queue */
1677 while (NULL != (head = socket->queue_head)) {
1678 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
1681 GNUNET_free (head->message);
1685 /* Close associated tunnel */
1686 if (NULL != socket->tunnel)
1688 GNUNET_MESH_tunnel_destroy (socket->tunnel);
1689 socket->tunnel = NULL;
1692 /* Close mesh connection */
1693 if (NULL != socket->mesh)
1695 GNUNET_MESH_disconnect (socket->mesh);
1696 socket->mesh = NULL;
1699 /* Release receive buffer */
1700 if (NULL != socket->receive_buffer)
1702 GNUNET_free (socket->receive_buffer);
1705 GNUNET_free (socket);
1710 * Method called whenever a peer creates a tunnel to us
1712 * @param cls closure
1713 * @param tunnel new handle to the tunnel
1714 * @param initiator peer that started the tunnel
1715 * @param atsi performance information for the tunnel
1716 * @return initial tunnel context for the tunnel
1717 * (can be NULL -- that's not an error)
1720 new_tunnel_notify (void *cls,
1721 struct GNUNET_MESH_Tunnel *tunnel,
1722 const struct GNUNET_PeerIdentity *initiator,
1723 const struct GNUNET_ATS_Information *atsi)
1725 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
1726 struct GNUNET_STREAM_Socket *socket;
1728 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
1729 socket->tunnel = tunnel;
1730 socket->session_id = 0; /* FIXME */
1731 socket->other_peer = *initiator;
1732 socket->state = STATE_INIT;
1734 if (GNUNET_SYSERR == lsocket->listen_cb (lsocket->listen_cb_cls,
1736 &socket->other_peer))
1738 socket->state = STATE_CLOSED;
1739 /* FIXME: Send CLOSE message and then free */
1740 GNUNET_free (socket);
1741 GNUNET_MESH_tunnel_destroy (tunnel); /* Destroy the tunnel */
1748 * Function called whenever an inbound tunnel is destroyed. Should clean up
1749 * any associated state. This function is NOT called if the client has
1750 * explicitly asked for the tunnel to be destroyed using
1751 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
1754 * @param cls closure (set from GNUNET_MESH_connect)
1755 * @param tunnel connection to the other end (henceforth invalid)
1756 * @param tunnel_ctx place where local state associated
1757 * with the tunnel is stored
1760 tunnel_cleaner (void *cls,
1761 const struct GNUNET_MESH_Tunnel *tunnel,
1764 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767 "Peer %s has terminated connection abruptly\n",
1768 GNUNET_i2s (&socket->other_peer));
1770 socket->status = GNUNET_STREAM_SHUTDOWN;
1771 /* Clear Transmit handles */
1772 if (NULL != socket->transmit_handle)
1774 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
1775 socket->transmit_handle = NULL;
1777 socket->tunnel = NULL;
1782 * Listens for stream connections for a specific application ports
1784 * @param cfg the configuration to use
1785 * @param app_port the application port for which new streams will be accepted
1786 * @param listen_cb this function will be called when a peer tries to establish
1788 * @param listen_cb_cls closure for listen_cb
1789 * @return listen socket, NULL for any error
1791 struct GNUNET_STREAM_ListenSocket *
1792 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
1793 GNUNET_MESH_ApplicationType app_port,
1794 GNUNET_STREAM_ListenCallback listen_cb,
1795 void *listen_cb_cls)
1797 /* FIXME: Add variable args for passing configration options? */
1798 struct GNUNET_STREAM_ListenSocket *lsocket;
1799 GNUNET_MESH_ApplicationType app_types[2];
1801 app_types[0] = app_port;
1803 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
1804 lsocket->port = app_port;
1805 lsocket->listen_cb = listen_cb;
1806 lsocket->listen_cb_cls = listen_cb_cls;
1807 lsocket->mesh = GNUNET_MESH_connect (cfg,
1808 10, /* FIXME: QUEUE size as parameter? */
1809 lsocket, /* Closure */
1812 server_message_handlers,
1819 * Closes the listen socket
1821 * @param lsocket the listen socket
1824 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
1826 /* Close MESH connection */
1827 GNUNET_MESH_disconnect (lsocket->mesh);
1829 GNUNET_free (lsocket);
1834 * Tries to write the given data to the stream
1836 * @param socket the socket representing a stream
1837 * @param data the data buffer from where the data is written into the stream
1838 * @param size the number of bytes to be written from the data buffer
1839 * @param timeout the timeout period
1840 * @param write_cont the function to call upon writing some bytes into the stream
1841 * @param write_cont_cls the closure
1842 * @return handle to cancel the operation
1844 struct GNUNET_STREAM_IOHandle *
1845 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
1848 struct GNUNET_TIME_Relative timeout,
1849 GNUNET_STREAM_CompletionContinuation write_cont,
1850 void *write_cont_cls)
1852 unsigned int num_needed_packets;
1853 unsigned int packet;
1854 struct GNUNET_STREAM_IOHandle *io_handle;
1856 struct GNUNET_STREAM_DataMessage *data_msg;
1859 /* Return NULL if there is already a write request pending */
1860 if (NULL != socket->write_handle)
1865 if (!((STATE_ESTABLISHED == socket->state)
1866 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
1867 || (STATE_RECEIVE_CLOSED == socket->state)))
1869 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1870 "Attempting to write on a closed (OR) not-yet-established"
1874 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
1875 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
1876 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
1877 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOHandle));
1878 io_handle->receive_window_available = socket->receive_window_available;
1880 /* Divide the given buffer into packets for sending */
1881 for (packet=0; packet < num_needed_packets; packet++)
1883 if ((packet + 1) * max_payload_size < size)
1885 packet_size = MAX_PACKET_SIZE;
1889 packet_size = size - packet * max_payload_size
1890 + sizeof (struct GNUNET_STREAM_DataMessage);
1892 io_handle->messages[packet] = GNUNET_malloc (packet_size);
1893 io_handle->messages[packet]->header.header.size = htons (packet_size);
1894 io_handle->messages[packet]->header.header.type =
1895 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
1896 io_handle->messages[packet]->sequence_number =
1897 htons (socket->write_sequence_number++);
1899 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
1900 determined from RTT */
1901 io_handle->messages[packet]->ack_deadline =
1902 GNUNET_TIME_relative_hton (GNUNET_TIME_relative_multiply
1903 (GNUNET_TIME_UNIT_SECONDS, 5));
1904 data_msg = io_handle->messages[packet];
1905 /* Copy data from given buffer to the packet */
1906 memcpy (&data_msg[1],
1908 packet_size - sizeof (struct GNUNET_STREAM_DataMessage));
1909 sweep += packet_size - sizeof (struct GNUNET_STREAM_DataMessage);
1911 socket->write_handle = io_handle;
1912 write_data (socket);
1919 * Tries to read data from the stream
1921 * @param socket the socket representing a stream
1922 * @param timeout the timeout period
1923 * @param proc function to call with data (once only)
1924 * @param proc_cls the closure for proc
1925 * @return handle to cancel the operation
1927 struct GNUNET_STREAM_IOHandle *
1928 GNUNET_STREAM_read (const struct GNUNET_STREAM_Socket *socket,
1929 struct GNUNET_TIME_Relative timeout,
1930 GNUNET_STREAM_DataProcessor proc,
1933 /* Check the bitmap for any holes */
1935 /* Deem the data from the starting of the bitmap upto a hole as available
1938 /* Create an IO handle */
1940 /* Call the Data processor with this available data */
1942 /* Update the read_sequence_number to the first hole in the bitmap */
1944 /* Shift the bitmap so that the first hole is now at the start */