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 socket the socket the written message was bound to
111 typedef void (*SendFinishCallback) (void *cls,
112 struct GNUNET_STREAM_Socket *socket);
116 * The send message queue
123 struct GNUNET_STREAM_MessageHeader *message;
126 * Callback to be called when the message is sent
128 SendFinishCallback finish_cb;
131 * The closure for finish_cb
136 * The next message in queue. Should be NULL in the last message
138 struct MessageQueue *next;
141 * The next message in queue. Should be NULL in the last message
143 struct MessageQueue *prev;
148 * The STREAM Socket Handler
150 struct GNUNET_STREAM_Socket
154 * The peer identity of the peer at the other end of the stream
156 struct GNUNET_PeerIdentity other_peer;
159 * Retransmission timeout
161 struct GNUNET_TIME_Relative retransmit_timeout;
164 * The Acknowledgement Bitmap
166 GNUNET_STREAM_AckBitmap ack_bitmap;
169 * Time when the Acknowledgement was queued
171 struct GNUNET_TIME_Absolute ack_time_registered;
174 * Queued Acknowledgement deadline
176 struct GNUNET_TIME_Relative ack_time_deadline;
179 * The task for sending timely Acks
181 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
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_IOHandle *write_handle;
224 * The read IO_handle associated with this socket
226 struct GNUNET_STREAM_IOHandle *read_handle;
229 * Buffer for storing received messages
231 void *receive_buffer;
234 * The state of the protocol associated with this socket
239 * The status of the socket
241 enum GNUNET_STREAM_Status status;
244 * The number of previous timeouts; FIXME: currently not used
246 unsigned int retries;
249 * The session id associated with this stream connection
250 * FIXME: Not used currently, may be removed
255 * Write sequence number. Set to random when sending HELLO(client) and
258 uint32_t write_sequence_number;
261 * Read sequence number. This number's value is determined during handshake
263 uint32_t read_sequence_number;
266 * receiver's available buffer after the last acknowledged packet
268 uint32_t receive_window_available;
273 * A socket for listening
275 struct GNUNET_STREAM_ListenSocket
281 struct GNUNET_MESH_Handle *mesh;
284 * The callback function which is called after successful opening socket
286 GNUNET_STREAM_ListenCallback listen_cb;
289 * The call back closure
296 GNUNET_MESH_ApplicationType port;
303 struct GNUNET_STREAM_IOHandle
306 * The packet_buffers associated with this Handle
308 struct GNUNET_STREAM_DataMessage *messages[64];
311 * The bitmap of this IOHandle; Corresponding bit for a message is set when
312 * it has been acknowledged by the receiver
314 GNUNET_STREAM_AckBitmap ack_bitmap;
317 * receiver's available buffer
319 uint32_t receive_window_available;
322 * Number of packets sent before waiting for an ack
324 * FIXME: Do we need this?
326 unsigned int sent_packets;
331 * Default value in seconds for various timeouts
333 static unsigned int default_timeout = 300;
337 * Callback function for sending hello message
339 * @param cls closure the socket
340 * @param size number of bytes available in buf
341 * @param buf where the callee should write the message
342 * @return number of bytes written to buf
345 send_message_notify (void *cls, size_t size, void *buf)
347 struct GNUNET_STREAM_Socket *socket = cls;
348 struct MessageQueue *head;
351 socket->transmit_handle = NULL; /* Remove the transmit handle */
352 head = socket->queue_head;
354 return 0; /* just to be safe */
355 if (0 == size) /* request timed out */
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "Message sending timed out. Retry %d \n",
361 socket->transmit_handle =
362 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
365 /* FIXME: exponential backoff */
366 socket->retransmit_timeout,
368 ntohs (head->message->header.size),
369 &send_message_notify,
374 ret = ntohs (head->message->header.size);
375 GNUNET_assert (size >= ret);
376 memcpy (buf, head->message, ret);
377 if (NULL != head->finish_cb)
379 head->finish_cb (socket, head->finish_cb_cls);
381 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
384 GNUNET_free (head->message);
386 head = socket->queue_head;
387 if (NULL != head) /* more pending messages to send */
390 socket->transmit_handle =
391 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
394 /* FIXME: exponential backoff */
395 socket->retransmit_timeout,
397 ntohs (head->message->header.size),
398 &send_message_notify,
406 * Queues a message for sending using the mesh connection of a socket
408 * @param socket the socket whose mesh connection is used
409 * @param message the message to be sent
410 * @param finish_cb the callback to be called when the message is sent
411 * @param finish_cb_cls the closure for the callback
414 queue_message (struct GNUNET_STREAM_Socket *socket,
415 struct GNUNET_STREAM_MessageHeader *message,
416 SendFinishCallback finish_cb,
419 struct MessageQueue *queue_entity;
421 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
422 queue_entity->message = message;
423 queue_entity->finish_cb = finish_cb;
424 queue_entity->finish_cb_cls = finish_cb_cls;
425 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
428 if (NULL == socket->transmit_handle)
431 socket->transmit_handle =
432 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
435 socket->retransmit_timeout,
437 ntohs (message->header.size),
438 &send_message_notify,
445 * Callback function for sending ack message
447 * @param cls closure the ACK message created in ack_task
448 * @param size number of bytes available in buffer
449 * @param buf where the callee should write the message
450 * @return number of bytes written to buf
453 send_ack_notify (void *cls, size_t size, void *buf)
455 struct GNUNET_STREAM_AckMessage *ack_msg = cls;
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 "%s called with size 0\n", __func__);
463 GNUNET_assert (ack_msg->header.header.size <= size);
465 size = ack_msg->header.header.size;
466 memcpy (buf, ack_msg, size);
472 * Task for sending ACK message
474 * @param cls the socket
475 * @param tc the Task context
479 const struct GNUNET_SCHEDULER_TaskContext *tc)
481 struct GNUNET_STREAM_Socket *socket = cls;
482 struct GNUNET_STREAM_AckMessage *ack_msg;
484 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
489 socket->ack_task_id = 0;
491 /* Create the ACK Message */
492 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
493 ack_msg->header.header.size = htons (sizeof (struct
494 GNUNET_STREAM_AckMessage));
495 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
496 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
497 ack_msg->base_sequence_number = htonl (socket->write_sequence_number);
498 ack_msg->receive_window_remaining = htonl (socket->receive_window_available);
500 /* Request MESH for sending ACK */
501 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
504 socket->retransmit_timeout,
506 ntohs (ack_msg->header.header.size),
515 * Function to modify a bit in GNUNET_STREAM_AckBitmap
517 * @param bitmap the bitmap to modify
518 * @param bit the bit number to modify
519 * @param value GNUNET_YES to on, GNUNET_NO to off
522 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
526 GNUNET_assert (bit < 64);
527 if (GNUNET_YES == value)
528 *bitmap |= (1LL << bit);
530 *bitmap &= ~(1LL << bit);
535 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
537 * @param bitmap address of the bitmap that has to be checked
538 * @param bit the bit number to check
539 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
542 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
545 GNUNET_assert (bit < 64);
546 return 0 != (*bitmap & (1LL << bit));
552 * Function called when Data Message is sent
554 * @param cls the io_handle corresponding to the Data Message
555 * @param socket the socket which was used
558 write_data_finish_cb (void *cls,
559 struct GNUNET_STREAM_Socket *socket)
561 struct GNUNET_STREAM_IOHandle *io_handle = cls;
563 io_handle->sent_packets++;
568 * Writes data using the given socket. The amount of data written is limited by
569 * the receive_window_size
571 * @param socket the socket to use
574 write_data (struct GNUNET_STREAM_Socket *socket)
576 struct GNUNET_STREAM_IOHandle *io_handle = socket->write_handle;
581 /* Find the last acknowledged packet */
582 for (packet=0; packet < 64; packet++)
584 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
590 /* Resend packets which weren't ack'ed */
591 for (packet=0; packet < ack_packet; packet++)
593 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
596 queue_message (socket,
597 &io_handle->messages[packet]->header,
602 packet = ack_packet + 1;
603 /* Now send new packets if there is enough buffer space */
604 while (io_handle->receive_window_available -=
605 io_handle->messages[packet]->header.header.size > 0)
607 queue_message (socket,
608 &io_handle->messages[packet]->header,
609 &write_data_finish_cb,
616 * Handler for DATA messages; Same for both client and server
618 * @param socket the socket through which the ack was received
619 * @param tunnel connection to the other end
620 * @param sender who sent the message
621 * @param ack the acknowledgment message
622 * @param atsi performance data for the connection
623 * @return GNUNET_OK to keep the connection open,
624 * GNUNET_SYSERR to close it (signal serious error)
627 handle_data (struct GNUNET_STREAM_Socket *socket,
628 struct GNUNET_MESH_Tunnel *tunnel,
629 const struct GNUNET_PeerIdentity *sender,
630 const struct GNUNET_STREAM_DataMessage *msg,
631 const struct GNUNET_ATS_Information*atsi)
636 size = msg->header.header.size;
637 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
640 return GNUNET_SYSERR;
643 switch (socket->state)
645 case STATE_ESTABLISHED:
646 case STATE_TRANSMIT_CLOSED:
647 case STATE_TRANSMIT_CLOSE_WAIT:
648 GNUNET_assert (NULL != socket->receive_buffer);
649 /* check if the message's sequence number is greater than the one we are
651 if (ntohl (msg->sequence_number) - socket->read_sequence_number < 64)
655 else /* We are receiving a retransmitted message */
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "Message with sequence number %d retransmitted\n",
659 ntohl (socket->read_sequence_number));
662 /* Copy Data to buffer and send acknowledgements */
663 size -= sizeof (struct GNUNET_STREAM_DataMessage);
665 memcpy (socket->receive_buffer
666 + (ntohl (msg->sequence_number) - socket->read_sequence_number)
671 /* Modify the ACK bitmap */
672 ackbitmap_modify_bit (&socket->ack_bitmap,
673 ntohl (msg->sequence_number) -
674 socket->read_sequence_number,
677 /* Start ACK sending task if one is not already present */
678 if (0 == socket->ack_task_id)
680 socket->ack_task_id =
681 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
690 /* FIXME: call statistics */
697 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
699 * @param cls the socket (set from GNUNET_MESH_connect)
700 * @param tunnel connection to the other end
701 * @param tunnel_ctx place to store local state associated with the tunnel
702 * @param sender who sent the message
703 * @param message the actual message
704 * @param atsi performance data for the connection
705 * @return GNUNET_OK to keep the connection open,
706 * GNUNET_SYSERR to close it (signal serious error)
709 client_handle_data (void *cls,
710 struct GNUNET_MESH_Tunnel *tunnel,
712 const struct GNUNET_PeerIdentity *sender,
713 const struct GNUNET_MessageHeader *message,
714 const struct GNUNET_ATS_Information*atsi)
716 struct GNUNET_STREAM_Socket *socket = cls;
718 return handle_data (socket,
721 (const struct GNUNET_STREAM_DataMessage *) message,
727 * Callback to set state to ESTABLISHED
729 * @param cls the closure from queue_message FIXME: document
730 * @param socket the socket to requiring state change
733 set_state_established (void *cls,
734 struct GNUNET_STREAM_Socket *socket)
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attaining ESTABLISHED state\n");
737 /* Initialize the receive buffer */
738 socket->receive_buffer = GNUNET_malloc (RECEIVE_BUFFER_SIZE);
739 socket->state = STATE_ESTABLISHED;
744 * Callback to set state to HELLO_WAIT
746 * @param cls the closure from queue_message
747 * @param socket the socket to requiring state change
750 set_state_hello_wait (void *cls,
751 struct GNUNET_STREAM_Socket *socket)
753 GNUNET_assert (STATE_INIT == socket->state);
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attaining HELLO_WAIT state\n");
755 socket->state = STATE_HELLO_WAIT;
760 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
762 * @param cls the socket (set from GNUNET_MESH_connect)
763 * @param tunnel connection to the other end
764 * @param tunnel_ctx this is NULL
765 * @param sender who sent the message
766 * @param message the actual message
767 * @param atsi performance data for the connection
768 * @return GNUNET_OK to keep the connection open,
769 * GNUNET_SYSERR to close it (signal serious error)
772 client_handle_hello_ack (void *cls,
773 struct GNUNET_MESH_Tunnel *tunnel,
775 const struct GNUNET_PeerIdentity *sender,
776 const struct GNUNET_MessageHeader *message,
777 const struct GNUNET_ATS_Information*atsi)
779 struct GNUNET_STREAM_Socket *socket = cls;
780 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
781 struct GNUNET_STREAM_HelloAckMessage *reply;
783 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
784 GNUNET_assert (socket->tunnel == tunnel);
785 switch (socket->state)
787 case STATE_HELLO_WAIT:
788 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
789 socket->receive_window_available = ntohl (ack_msg->receive_window_size);
790 /* Get the random sequence number */
791 socket->write_sequence_number =
792 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
794 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
795 reply->header.header.size =
796 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
797 reply->header.header.type =
798 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
799 reply->sequence_number = htonl (socket->write_sequence_number);
800 reply->receive_window_size = htonl (RECEIVE_BUFFER_SIZE);
801 queue_message (socket,
803 &set_state_established,
806 case STATE_ESTABLISHED:
807 case STATE_RECEIVE_CLOSE_WAIT:
808 // call statistics (# ACKs ignored++)
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
813 "Server sent HELLO_ACK when in state %d\n", socket->state);
814 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
815 return GNUNET_SYSERR;
822 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
824 * @param cls the socket (set from GNUNET_MESH_connect)
825 * @param tunnel connection to the other end
826 * @param tunnel_ctx this is NULL
827 * @param sender who sent the message
828 * @param message the actual message
829 * @param atsi performance data for the connection
830 * @return GNUNET_OK to keep the connection open,
831 * GNUNET_SYSERR to close it (signal serious error)
834 client_handle_reset (void *cls,
835 struct GNUNET_MESH_Tunnel *tunnel,
837 const struct GNUNET_PeerIdentity *sender,
838 const struct GNUNET_MessageHeader *message,
839 const struct GNUNET_ATS_Information*atsi)
841 struct GNUNET_STREAM_Socket *socket = cls;
848 * Common message handler for handling TRANSMIT_CLOSE messages
850 * @param socket the socket through which the ack was received
851 * @param tunnel connection to the other end
852 * @param sender who sent the message
853 * @param ack the acknowledgment message
854 * @param atsi performance data for the connection
855 * @return GNUNET_OK to keep the connection open,
856 * GNUNET_SYSERR to close it (signal serious error)
859 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
860 struct GNUNET_MESH_Tunnel *tunnel,
861 const struct GNUNET_PeerIdentity *sender,
862 const struct GNUNET_STREAM_MessageHeader *msg,
863 const struct GNUNET_ATS_Information*atsi)
865 struct GNUNET_STREAM_MessageHeader *reply;
867 switch (socket->state)
869 case STATE_ESTABLISHED:
870 socket->state = STATE_RECEIVE_CLOSED;
872 /* Send TRANSMIT_CLOSE_ACK */
873 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
875 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
876 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
877 queue_message (socket, reply, NULL, NULL);
881 /* FIXME: Call statistics? */
889 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
891 * @param cls the socket (set from GNUNET_MESH_connect)
892 * @param tunnel connection to the other end
893 * @param tunnel_ctx this is NULL
894 * @param sender who sent the message
895 * @param message the actual message
896 * @param atsi performance data for the connection
897 * @return GNUNET_OK to keep the connection open,
898 * GNUNET_SYSERR to close it (signal serious error)
901 client_handle_transmit_close (void *cls,
902 struct GNUNET_MESH_Tunnel *tunnel,
904 const struct GNUNET_PeerIdentity *sender,
905 const struct GNUNET_MessageHeader *message,
906 const struct GNUNET_ATS_Information*atsi)
908 struct GNUNET_STREAM_Socket *socket = cls;
910 return handle_transmit_close (socket,
913 (struct GNUNET_STREAM_MessageHeader *)message,
919 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
921 * @param cls the socket (set from GNUNET_MESH_connect)
922 * @param tunnel connection to the other end
923 * @param tunnel_ctx this is NULL
924 * @param sender who sent the message
925 * @param message the actual message
926 * @param atsi performance data for the connection
927 * @return GNUNET_OK to keep the connection open,
928 * GNUNET_SYSERR to close it (signal serious error)
931 client_handle_transmit_close_ack (void *cls,
932 struct GNUNET_MESH_Tunnel *tunnel,
934 const struct GNUNET_PeerIdentity *sender,
935 const struct GNUNET_MessageHeader *message,
936 const struct GNUNET_ATS_Information*atsi)
938 struct GNUNET_STREAM_Socket *socket = cls;
945 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
947 * @param cls the socket (set from GNUNET_MESH_connect)
948 * @param tunnel connection to the other end
949 * @param tunnel_ctx this is NULL
950 * @param sender who sent the message
951 * @param message the actual message
952 * @param atsi performance data for the connection
953 * @return GNUNET_OK to keep the connection open,
954 * GNUNET_SYSERR to close it (signal serious error)
957 client_handle_receive_close (void *cls,
958 struct GNUNET_MESH_Tunnel *tunnel,
960 const struct GNUNET_PeerIdentity *sender,
961 const struct GNUNET_MessageHeader *message,
962 const struct GNUNET_ATS_Information*atsi)
964 struct GNUNET_STREAM_Socket *socket = cls;
971 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
973 * @param cls the socket (set from GNUNET_MESH_connect)
974 * @param tunnel connection to the other end
975 * @param tunnel_ctx this is NULL
976 * @param sender who sent the message
977 * @param message the actual message
978 * @param atsi performance data for the connection
979 * @return GNUNET_OK to keep the connection open,
980 * GNUNET_SYSERR to close it (signal serious error)
983 client_handle_receive_close_ack (void *cls,
984 struct GNUNET_MESH_Tunnel *tunnel,
986 const struct GNUNET_PeerIdentity *sender,
987 const struct GNUNET_MessageHeader *message,
988 const struct GNUNET_ATS_Information*atsi)
990 struct GNUNET_STREAM_Socket *socket = cls;
997 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
999 * @param cls the socket (set from GNUNET_MESH_connect)
1000 * @param tunnel connection to the other end
1001 * @param tunnel_ctx this is NULL
1002 * @param sender who sent the message
1003 * @param message the actual message
1004 * @param atsi performance data for the connection
1005 * @return GNUNET_OK to keep the connection open,
1006 * GNUNET_SYSERR to close it (signal serious error)
1009 client_handle_close (void *cls,
1010 struct GNUNET_MESH_Tunnel *tunnel,
1012 const struct GNUNET_PeerIdentity *sender,
1013 const struct GNUNET_MessageHeader *message,
1014 const struct GNUNET_ATS_Information*atsi)
1016 struct GNUNET_STREAM_Socket *socket = cls;
1023 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1025 * @param cls the socket (set from GNUNET_MESH_connect)
1026 * @param tunnel connection to the other end
1027 * @param tunnel_ctx this is NULL
1028 * @param sender who sent the message
1029 * @param message the actual message
1030 * @param atsi performance data for the connection
1031 * @return GNUNET_OK to keep the connection open,
1032 * GNUNET_SYSERR to close it (signal serious error)
1035 client_handle_close_ack (void *cls,
1036 struct GNUNET_MESH_Tunnel *tunnel,
1038 const struct GNUNET_PeerIdentity *sender,
1039 const struct GNUNET_MessageHeader *message,
1040 const struct GNUNET_ATS_Information*atsi)
1042 struct GNUNET_STREAM_Socket *socket = cls;
1047 /*****************************/
1048 /* Server's Message Handlers */
1049 /*****************************/
1052 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1054 * @param cls the closure
1055 * @param tunnel connection to the other end
1056 * @param tunnel_ctx the socket
1057 * @param sender who sent the message
1058 * @param message the actual message
1059 * @param atsi performance data for the connection
1060 * @return GNUNET_OK to keep the connection open,
1061 * GNUNET_SYSERR to close it (signal serious error)
1064 server_handle_data (void *cls,
1065 struct GNUNET_MESH_Tunnel *tunnel,
1067 const struct GNUNET_PeerIdentity *sender,
1068 const struct GNUNET_MessageHeader *message,
1069 const struct GNUNET_ATS_Information*atsi)
1071 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1078 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1080 * @param cls the closure
1081 * @param tunnel connection to the other end
1082 * @param tunnel_ctx the socket
1083 * @param sender who sent the message
1084 * @param message the actual message
1085 * @param atsi performance data for the connection
1086 * @return GNUNET_OK to keep the connection open,
1087 * GNUNET_SYSERR to close it (signal serious error)
1090 server_handle_hello (void *cls,
1091 struct GNUNET_MESH_Tunnel *tunnel,
1093 const struct GNUNET_PeerIdentity *sender,
1094 const struct GNUNET_MessageHeader *message,
1095 const struct GNUNET_ATS_Information*atsi)
1097 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1098 struct GNUNET_STREAM_HelloAckMessage *reply;
1100 GNUNET_assert (socket->tunnel == tunnel);
1101 if (STATE_INIT == socket->state)
1103 /* Get the random sequence number */
1104 socket->write_sequence_number =
1105 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1107 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1108 reply->header.header.size =
1109 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1110 reply->header.header.type =
1111 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1112 reply->sequence_number = htonl (socket->write_sequence_number);
1113 queue_message (socket,
1115 &set_state_hello_wait,
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1121 "Client sent HELLO when in state %d\n", socket->state);
1122 /* FIXME: Send RESET? */
1130 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1132 * @param cls the closure
1133 * @param tunnel connection to the other end
1134 * @param tunnel_ctx the socket
1135 * @param sender who sent the message
1136 * @param message the actual message
1137 * @param atsi performance data for the connection
1138 * @return GNUNET_OK to keep the connection open,
1139 * GNUNET_SYSERR to close it (signal serious error)
1142 server_handle_hello_ack (void *cls,
1143 struct GNUNET_MESH_Tunnel *tunnel,
1145 const struct GNUNET_PeerIdentity *sender,
1146 const struct GNUNET_MessageHeader *message,
1147 const struct GNUNET_ATS_Information*atsi)
1149 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1150 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1152 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1153 GNUNET_assert (socket->tunnel == tunnel);
1154 if (STATE_HELLO_WAIT == socket->state)
1156 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1157 socket->receive_window_available =
1158 ntohl (ack_message->receive_window_size);
1159 socket->state = STATE_ESTABLISHED;
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "Client sent HELLO_ACK when in state %d\n", socket->state);
1165 /* FIXME: Send RESET? */
1173 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1175 * @param cls the closure
1176 * @param tunnel connection to the other end
1177 * @param tunnel_ctx the socket
1178 * @param sender who sent the message
1179 * @param message the actual message
1180 * @param atsi performance data for the connection
1181 * @return GNUNET_OK to keep the connection open,
1182 * GNUNET_SYSERR to close it (signal serious error)
1185 server_handle_reset (void *cls,
1186 struct GNUNET_MESH_Tunnel *tunnel,
1188 const struct GNUNET_PeerIdentity *sender,
1189 const struct GNUNET_MessageHeader *message,
1190 const struct GNUNET_ATS_Information*atsi)
1192 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1199 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1201 * @param cls the closure
1202 * @param tunnel connection to the other end
1203 * @param tunnel_ctx the socket
1204 * @param sender who sent the message
1205 * @param message the actual message
1206 * @param atsi performance data for the connection
1207 * @return GNUNET_OK to keep the connection open,
1208 * GNUNET_SYSERR to close it (signal serious error)
1211 server_handle_transmit_close (void *cls,
1212 struct GNUNET_MESH_Tunnel *tunnel,
1214 const struct GNUNET_PeerIdentity *sender,
1215 const struct GNUNET_MessageHeader *message,
1216 const struct GNUNET_ATS_Information*atsi)
1218 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1220 return handle_transmit_close (socket,
1223 (struct GNUNET_STREAM_MessageHeader *)message,
1229 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1231 * @param cls the closure
1232 * @param tunnel connection to the other end
1233 * @param tunnel_ctx the socket
1234 * @param sender who sent the message
1235 * @param message the actual message
1236 * @param atsi performance data for the connection
1237 * @return GNUNET_OK to keep the connection open,
1238 * GNUNET_SYSERR to close it (signal serious error)
1241 server_handle_transmit_close_ack (void *cls,
1242 struct GNUNET_MESH_Tunnel *tunnel,
1244 const struct GNUNET_PeerIdentity *sender,
1245 const struct GNUNET_MessageHeader *message,
1246 const struct GNUNET_ATS_Information*atsi)
1248 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1255 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1257 * @param cls the closure
1258 * @param tunnel connection to the other end
1259 * @param tunnel_ctx the socket
1260 * @param sender who sent the message
1261 * @param message the actual message
1262 * @param atsi performance data for the connection
1263 * @return GNUNET_OK to keep the connection open,
1264 * GNUNET_SYSERR to close it (signal serious error)
1267 server_handle_receive_close (void *cls,
1268 struct GNUNET_MESH_Tunnel *tunnel,
1270 const struct GNUNET_PeerIdentity *sender,
1271 const struct GNUNET_MessageHeader *message,
1272 const struct GNUNET_ATS_Information*atsi)
1274 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1281 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1283 * @param cls the closure
1284 * @param tunnel connection to the other end
1285 * @param tunnel_ctx the socket
1286 * @param sender who sent the message
1287 * @param message the actual message
1288 * @param atsi performance data for the connection
1289 * @return GNUNET_OK to keep the connection open,
1290 * GNUNET_SYSERR to close it (signal serious error)
1293 server_handle_receive_close_ack (void *cls,
1294 struct GNUNET_MESH_Tunnel *tunnel,
1296 const struct GNUNET_PeerIdentity *sender,
1297 const struct GNUNET_MessageHeader *message,
1298 const struct GNUNET_ATS_Information*atsi)
1300 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1307 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1309 * @param cls the closure
1310 * @param tunnel connection to the other end
1311 * @param tunnel_ctx the socket
1312 * @param sender who sent the message
1313 * @param message the actual message
1314 * @param atsi performance data for the connection
1315 * @return GNUNET_OK to keep the connection open,
1316 * GNUNET_SYSERR to close it (signal serious error)
1319 server_handle_close (void *cls,
1320 struct GNUNET_MESH_Tunnel *tunnel,
1322 const struct GNUNET_PeerIdentity *sender,
1323 const struct GNUNET_MessageHeader *message,
1324 const struct GNUNET_ATS_Information*atsi)
1326 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1333 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1335 * @param cls the closure
1336 * @param tunnel connection to the other end
1337 * @param tunnel_ctx the socket
1338 * @param sender who sent the message
1339 * @param message the actual message
1340 * @param atsi performance data for the connection
1341 * @return GNUNET_OK to keep the connection open,
1342 * GNUNET_SYSERR to close it (signal serious error)
1345 server_handle_close_ack (void *cls,
1346 struct GNUNET_MESH_Tunnel *tunnel,
1348 const struct GNUNET_PeerIdentity *sender,
1349 const struct GNUNET_MessageHeader *message,
1350 const struct GNUNET_ATS_Information*atsi)
1352 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1359 * Message Handler for mesh
1361 * @param socket the socket through which the ack was received
1362 * @param tunnel connection to the other end
1363 * @param sender who sent the message
1364 * @param ack the acknowledgment message
1365 * @param atsi performance data for the connection
1366 * @return GNUNET_OK to keep the connection open,
1367 * GNUNET_SYSERR to close it (signal serious error)
1370 handle_ack (struct GNUNET_STREAM_Socket *socket,
1371 struct GNUNET_MESH_Tunnel *tunnel,
1372 const struct GNUNET_PeerIdentity *sender,
1373 const struct GNUNET_STREAM_AckMessage *ack,
1374 const struct GNUNET_ATS_Information*atsi)
1376 switch (socket->state)
1378 case (STATE_ESTABLISHED):
1379 if (NULL == socket->write_handle)
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382 "Received DATA ACK when write_handle is NULL\n");
1386 socket->write_handle->ack_bitmap = GNUNET_ntohll (ack->bitmap);
1387 socket->write_handle->receive_window_available =
1388 ntohl (ack->receive_window_remaining);
1389 write_data (socket);
1399 * Message Handler for mesh
1401 * @param cls the 'struct GNUNET_STREAM_Socket'
1402 * @param tunnel connection to the other end
1403 * @param tunnel_ctx unused
1404 * @param sender who sent the message
1405 * @param message the actual message
1406 * @param atsi performance data for the connection
1407 * @return GNUNET_OK to keep the connection open,
1408 * GNUNET_SYSERR to close it (signal serious error)
1411 client_handle_ack (void *cls,
1412 struct GNUNET_MESH_Tunnel *tunnel,
1414 const struct GNUNET_PeerIdentity *sender,
1415 const struct GNUNET_MessageHeader *message,
1416 const struct GNUNET_ATS_Information*atsi)
1418 struct GNUNET_STREAM_Socket *socket = cls;
1419 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1421 return handle_ack (socket, tunnel, sender, ack, atsi);
1426 * Message Handler for mesh
1428 * @param cls the server's listen socket
1429 * @param tunnel connection to the other end
1430 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
1431 * @param sender who sent the message
1432 * @param message the actual message
1433 * @param atsi performance data for the connection
1434 * @return GNUNET_OK to keep the connection open,
1435 * GNUNET_SYSERR to close it (signal serious error)
1438 server_handle_ack (void *cls,
1439 struct GNUNET_MESH_Tunnel *tunnel,
1441 const struct GNUNET_PeerIdentity *sender,
1442 const struct GNUNET_MessageHeader *message,
1443 const struct GNUNET_ATS_Information*atsi)
1445 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1446 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1448 return handle_ack (socket, tunnel, sender, ack, atsi);
1453 * For client message handlers, the stream socket is in the
1456 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
1457 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1458 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1459 sizeof (struct GNUNET_STREAM_AckMessage) },
1460 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1461 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1462 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1463 sizeof (struct GNUNET_STREAM_MessageHeader)},
1464 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1465 sizeof (struct GNUNET_STREAM_MessageHeader)},
1466 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1467 sizeof (struct GNUNET_STREAM_MessageHeader)},
1468 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1469 sizeof (struct GNUNET_STREAM_MessageHeader)},
1470 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1471 sizeof (struct GNUNET_STREAM_MessageHeader)},
1472 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1473 sizeof (struct GNUNET_STREAM_MessageHeader)},
1474 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1475 sizeof (struct GNUNET_STREAM_MessageHeader)},
1481 * For server message handlers, the stream socket is in the
1482 * tunnel context, and the listen socket in the closure argument.
1484 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
1485 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1486 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1487 sizeof (struct GNUNET_STREAM_AckMessage) },
1488 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
1489 sizeof (struct GNUNET_STREAM_MessageHeader)},
1490 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1491 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1492 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1493 sizeof (struct GNUNET_STREAM_MessageHeader)},
1494 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1495 sizeof (struct GNUNET_STREAM_MessageHeader)},
1496 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1497 sizeof (struct GNUNET_STREAM_MessageHeader)},
1498 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1499 sizeof (struct GNUNET_STREAM_MessageHeader)},
1500 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1501 sizeof (struct GNUNET_STREAM_MessageHeader)},
1502 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1503 sizeof (struct GNUNET_STREAM_MessageHeader)},
1504 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1505 sizeof (struct GNUNET_STREAM_MessageHeader)},
1511 * Function called when our target peer is connected to our tunnel
1513 * @param cls the socket for which this tunnel is created
1514 * @param peer the peer identity of the target
1515 * @param atsi performance data for the connection
1518 mesh_peer_connect_callback (void *cls,
1519 const struct GNUNET_PeerIdentity *peer,
1520 const struct GNUNET_ATS_Information * atsi)
1522 struct GNUNET_STREAM_Socket *socket = cls;
1523 struct GNUNET_STREAM_MessageHeader *message;
1525 if (0 != memcmp (&socket->other_peer,
1527 sizeof (struct GNUNET_PeerIdentity)))
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1530 "A peer (%s) which is not our target has connected to our tunnel",
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1536 "Target peer %s connected\n", GNUNET_i2s (peer));
1538 /* Set state to INIT */
1539 socket->state = STATE_INIT;
1541 /* Send HELLO message */
1542 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1543 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
1544 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1545 queue_message (socket,
1547 &set_state_hello_wait,
1550 /* Call open callback */
1551 if (NULL == socket->open_cb)
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1554 "STREAM_open callback is NULL\n");
1558 socket->open_cb (socket->open_cls, socket);
1564 * Function called when our target peer is disconnected from our tunnel
1566 * @param cls the socket associated which this tunnel
1567 * @param peer the peer identity of the target
1570 mesh_peer_disconnect_callback (void *cls,
1571 const struct GNUNET_PeerIdentity *peer)
1583 * Tries to open a stream to the target peer
1585 * @param cfg configuration to use
1586 * @param target the target peer to which the stream has to be opened
1587 * @param app_port the application port number which uniquely identifies this
1589 * @param open_cb this function will be called after stream has be established
1590 * @param open_cb_cls the closure for open_cb
1591 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
1592 * @return if successful it returns the stream socket; NULL if stream cannot be
1595 struct GNUNET_STREAM_Socket *
1596 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
1597 const struct GNUNET_PeerIdentity *target,
1598 GNUNET_MESH_ApplicationType app_port,
1599 GNUNET_STREAM_OpenCallback open_cb,
1603 struct GNUNET_STREAM_Socket *socket;
1604 enum GNUNET_STREAM_Option option;
1605 va_list vargs; /* Variable arguments */
1607 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
1608 socket->other_peer = *target;
1609 socket->open_cb = open_cb;
1610 socket->open_cls = open_cb_cls;
1613 socket->retransmit_timeout =
1614 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
1616 va_start (vargs, open_cb_cls); /* Parse variable args */
1618 option = va_arg (vargs, enum GNUNET_STREAM_Option);
1621 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
1622 /* Expect struct GNUNET_TIME_Relative */
1623 socket->retransmit_timeout = va_arg (vargs,
1624 struct GNUNET_TIME_Relative);
1626 case GNUNET_STREAM_OPTION_END:
1629 } while (GNUNET_STREAM_OPTION_END != option);
1630 va_end (vargs); /* End of variable args parsing */
1632 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
1633 1, /* QUEUE size as parameter? */
1635 NULL, /* No inbound tunnel handler */
1636 NULL, /* No inbound tunnel cleaner */
1637 client_message_handlers,
1638 NULL); /* We don't get inbound tunnels */
1639 // FIXME: if (NULL == socket->mesh) ...
1641 /* Now create the mesh tunnel to target */
1642 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
1643 NULL, /* Tunnel context */
1644 &mesh_peer_connect_callback,
1645 &mesh_peer_disconnect_callback,
1647 // FIXME: if (NULL == socket->tunnel) ...
1656 * @param socket the stream socket
1659 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
1661 struct MessageQueue *head;
1663 /* Clear Transmit handles */
1664 if (NULL != socket->transmit_handle)
1666 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
1667 socket->transmit_handle = NULL;
1670 /* Clear existing message queue */
1671 while (NULL != (head = socket->queue_head)) {
1672 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
1675 GNUNET_free (head->message);
1679 /* Close associated tunnel */
1680 if (NULL != socket->tunnel)
1682 GNUNET_MESH_tunnel_destroy (socket->tunnel);
1683 socket->tunnel = NULL;
1686 /* Close mesh connection */
1687 if (NULL != socket->mesh)
1689 GNUNET_MESH_disconnect (socket->mesh);
1690 socket->mesh = NULL;
1693 /* Release receive buffer */
1694 if (NULL != socket->receive_buffer)
1696 GNUNET_free (socket->receive_buffer);
1699 GNUNET_free (socket);
1704 * Method called whenever a peer creates a tunnel to us
1706 * @param cls closure
1707 * @param tunnel new handle to the tunnel
1708 * @param initiator peer that started the tunnel
1709 * @param atsi performance information for the tunnel
1710 * @return initial tunnel context for the tunnel
1711 * (can be NULL -- that's not an error)
1714 new_tunnel_notify (void *cls,
1715 struct GNUNET_MESH_Tunnel *tunnel,
1716 const struct GNUNET_PeerIdentity *initiator,
1717 const struct GNUNET_ATS_Information *atsi)
1719 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
1720 struct GNUNET_STREAM_Socket *socket;
1722 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
1723 socket->tunnel = tunnel;
1724 socket->session_id = 0; /* FIXME */
1725 socket->other_peer = *initiator;
1726 socket->state = STATE_INIT;
1728 if (GNUNET_SYSERR == lsocket->listen_cb (lsocket->listen_cb_cls,
1730 &socket->other_peer))
1732 socket->state = STATE_CLOSED;
1733 /* FIXME: Send CLOSE message and then free */
1734 GNUNET_free (socket);
1735 GNUNET_MESH_tunnel_destroy (tunnel); /* Destroy the tunnel */
1742 * Function called whenever an inbound tunnel is destroyed. Should clean up
1743 * any associated state. This function is NOT called if the client has
1744 * explicitly asked for the tunnel to be destroyed using
1745 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
1748 * @param cls closure (set from GNUNET_MESH_connect)
1749 * @param tunnel connection to the other end (henceforth invalid)
1750 * @param tunnel_ctx place where local state associated
1751 * with the tunnel is stored
1754 tunnel_cleaner (void *cls,
1755 const struct GNUNET_MESH_Tunnel *tunnel,
1758 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
1760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1761 "Peer %s has terminated connection abruptly\n",
1762 GNUNET_i2s (&socket->other_peer));
1764 socket->status = GNUNET_STREAM_SHUTDOWN;
1765 /* Clear Transmit handles */
1766 if (NULL != socket->transmit_handle)
1768 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
1769 socket->transmit_handle = NULL;
1771 socket->tunnel = NULL;
1776 * Listens for stream connections for a specific application ports
1778 * @param cfg the configuration to use
1779 * @param app_port the application port for which new streams will be accepted
1780 * @param listen_cb this function will be called when a peer tries to establish
1782 * @param listen_cb_cls closure for listen_cb
1783 * @return listen socket, NULL for any error
1785 struct GNUNET_STREAM_ListenSocket *
1786 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
1787 GNUNET_MESH_ApplicationType app_port,
1788 GNUNET_STREAM_ListenCallback listen_cb,
1789 void *listen_cb_cls)
1791 /* FIXME: Add variable args for passing configration options? */
1792 struct GNUNET_STREAM_ListenSocket *lsocket;
1793 GNUNET_MESH_ApplicationType app_types[2];
1795 app_types[0] = app_port;
1797 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
1798 lsocket->port = app_port;
1799 lsocket->listen_cb = listen_cb;
1800 lsocket->listen_cb_cls = listen_cb_cls;
1801 lsocket->mesh = GNUNET_MESH_connect (cfg,
1802 10, /* FIXME: QUEUE size as parameter? */
1803 lsocket, /* Closure */
1806 server_message_handlers,
1813 * Closes the listen socket
1815 * @param lsocket the listen socket
1818 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
1820 /* Close MESH connection */
1821 GNUNET_MESH_disconnect (lsocket->mesh);
1823 GNUNET_free (lsocket);
1828 * Tries to write the given data to the stream
1830 * @param socket the socket representing a stream
1831 * @param data the data buffer from where the data is written into the stream
1832 * @param size the number of bytes to be written from the data buffer
1833 * @param timeout the timeout period
1834 * @param write_cont the function to call upon writing some bytes into the stream
1835 * @param write_cont_cls the closure
1836 * @return handle to cancel the operation
1838 struct GNUNET_STREAM_IOHandle *
1839 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
1842 struct GNUNET_TIME_Relative timeout,
1843 GNUNET_STREAM_CompletionContinuation write_cont,
1844 void *write_cont_cls)
1846 unsigned int num_needed_packets;
1847 unsigned int packet;
1848 struct GNUNET_STREAM_IOHandle *io_handle;
1850 struct GNUNET_STREAM_DataMessage *data_msg;
1853 /* There is already a write request pending */
1854 if (NULL != socket->write_handle) return NULL;
1855 if (!(STATE_ESTABLISHED == socket->state
1856 || STATE_RECEIVE_CLOSE_WAIT == socket->state
1857 || STATE_RECEIVE_CLOSED == socket->state))
1859 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1860 "Attempting to write on a closed (OR) not-yet-established"
1865 num_needed_packets = ceil (size / max_payload_size);
1866 if (64 < num_needed_packets)
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 "Given buffer cannot be accommodated in 64 packets\n");
1870 num_needed_packets = 64;
1873 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOHandle));
1874 io_handle->receive_window_available = socket->receive_window_available;
1876 /* Divide the given buffer into packets for sending */
1877 for (packet=0; packet < num_needed_packets; packet++)
1879 if ((packet + 1) * max_payload_size < size)
1881 packet_size = MAX_PACKET_SIZE;
1885 packet_size = size - packet * max_payload_size
1886 + sizeof (struct GNUNET_STREAM_DataMessage);
1888 io_handle->messages[packet] = GNUNET_malloc (packet_size);
1889 io_handle->messages[packet]->header.header.size = htons (packet_size);
1890 io_handle->messages[packet]->header.header.type =
1891 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
1892 io_handle->messages[packet]->sequence_number =
1893 htons (socket->write_sequence_number++);
1894 data_msg = io_handle->messages[packet];
1895 memcpy (&data_msg[1],
1897 packet_size - sizeof (struct GNUNET_STREAM_DataMessage));
1898 sweep += packet_size - sizeof (struct GNUNET_STREAM_DataMessage);
1901 write_data (socket);