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 * Copy MESH handle from lsocket to socket
24 * Checks for matching the sender and socket->other_peer in server
28 * @file stream/stream_api.c
29 * @brief Implementation of the stream library
30 * @author Sree Harsha Totakura
33 #include "gnunet_common.h"
34 #include "gnunet_crypto_lib.h"
35 #include "gnunet_stream_lib.h"
36 #include "stream_protocol.h"
40 * The maximum packet size of a stream packet
42 #define MAX_PACKET_SIZE 64000
45 * The maximum payload a data message packet can carry
47 static size_t max_payload_size =
48 MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
53 #define RECEIVE_BUFFER_SIZE 4096000
56 * states in the Protocol
61 * Client initialization state
66 * Listener initialization state
71 * Pre-connection establishment state
76 * State where a connection has been established
81 * State where the socket is closed on our side and waiting to be ACK'ed
83 STATE_RECEIVE_CLOSE_WAIT,
86 * State where the socket is closed for reading
91 * State where the socket is closed on our side and waiting to be ACK'ed
93 STATE_TRANSMIT_CLOSE_WAIT,
96 * State where the socket is closed for writing
98 STATE_TRANSMIT_CLOSED,
101 * State where the socket is closed on our side and waiting to be ACK'ed
106 * State where the socket is closed
113 * Functions of this type are called when a message is written
115 * @param cls the closure from queue_message
116 * @param socket the socket the written message was bound to
118 typedef void (*SendFinishCallback) (void *cls,
119 struct GNUNET_STREAM_Socket *socket);
123 * The send message queue
130 struct GNUNET_STREAM_MessageHeader *message;
133 * Callback to be called when the message is sent
135 SendFinishCallback finish_cb;
138 * The closure for finish_cb
143 * The next message in queue. Should be NULL in the last message
145 struct MessageQueue *next;
148 * The next message in queue. Should be NULL in the first message
150 struct MessageQueue *prev;
155 * The STREAM Socket Handler
157 struct GNUNET_STREAM_Socket
161 * The peer identity of the peer at the other end of the stream
163 struct GNUNET_PeerIdentity other_peer;
166 * Retransmission timeout
168 struct GNUNET_TIME_Relative retransmit_timeout;
171 * The Acknowledgement Bitmap
173 GNUNET_STREAM_AckBitmap ack_bitmap;
176 * Time when the Acknowledgement was queued
178 struct GNUNET_TIME_Absolute ack_time_registered;
181 * Queued Acknowledgement deadline
183 struct GNUNET_TIME_Relative ack_time_deadline;
186 * The task for sending timely Acks
188 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
191 * Task scheduled to continue a read operation.
193 GNUNET_SCHEDULER_TaskIdentifier read_task;
198 struct GNUNET_MESH_Handle *mesh;
201 * The mesh tunnel handle
203 struct GNUNET_MESH_Tunnel *tunnel;
206 * Stream open closure
211 * Stream open callback
213 GNUNET_STREAM_OpenCallback open_cb;
216 * The current transmit handle (if a pending transmit request exists)
218 struct GNUNET_MESH_TransmitHandle *transmit_handle;
221 * The current message associated with the transmit handle
223 struct MessageQueue *queue_head;
226 * The queue tail, should always point to the last message in queue
228 struct MessageQueue *queue_tail;
231 * The write IO_handle associated with this socket
233 struct GNUNET_STREAM_IOWriteHandle *write_handle;
236 * The read IO_handle associated with this socket
238 struct GNUNET_STREAM_IOReadHandle *read_handle;
241 * Buffer for storing received messages
243 void *receive_buffer;
246 * Task identifier for the read io timeout task
248 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task;
251 * The state of the protocol associated with this socket
256 * The status of the socket
258 enum GNUNET_STREAM_Status status;
261 * The number of previous timeouts; FIXME: currently not used
263 unsigned int retries;
266 * Is this socket derived from listen socket?
268 unsigned int derived;
271 * The application port number (type: uint32_t)
273 GNUNET_MESH_ApplicationType app_port;
276 * The session id associated with this stream connection
277 * FIXME: Not used currently, may be removed
282 * Write sequence number. Set to random when sending HELLO(client) and
285 uint32_t write_sequence_number;
288 * Read sequence number. This number's value is determined during handshake
290 uint32_t read_sequence_number;
293 * The receiver buffer size
295 uint32_t receive_buffer_size;
298 * The receiver buffer boundaries
300 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
303 * receiver's available buffer after the last acknowledged packet
305 uint32_t receive_window_available;
308 * The offset pointer used during write operation
310 uint32_t write_offset;
313 * The offset after which we are expecting data
315 uint32_t read_offset;
318 * The offset upto which user has read from the received buffer
320 uint32_t copy_offset;
325 * A socket for listening
327 struct GNUNET_STREAM_ListenSocket
333 struct GNUNET_MESH_Handle *mesh;
336 * The callback function which is called after successful opening socket
338 GNUNET_STREAM_ListenCallback listen_cb;
341 * The call back closure
347 * FIXME: Remove if not required!
349 GNUNET_MESH_ApplicationType port;
354 * The IO Write Handle
356 struct GNUNET_STREAM_IOWriteHandle
359 * The packet_buffers associated with this Handle
361 struct GNUNET_STREAM_DataMessage *messages[64];
364 * The bitmap of this IOHandle; Corresponding bit for a message is set when
365 * it has been acknowledged by the receiver
367 GNUNET_STREAM_AckBitmap ack_bitmap;
370 * Number of packets sent before waiting for an ack
372 * FIXME: Do we need this?
374 unsigned int sent_packets;
381 struct GNUNET_STREAM_IOReadHandle
384 * Callback for the read processor
386 GNUNET_STREAM_DataProcessor proc;
389 * The closure pointer for the read processor callback
396 * Default value in seconds for various timeouts
398 static unsigned int default_timeout = 300;
402 * Callback function for sending hello message
404 * @param cls closure the socket
405 * @param size number of bytes available in buf
406 * @param buf where the callee should write the message
407 * @return number of bytes written to buf
410 send_message_notify (void *cls, size_t size, void *buf)
412 struct GNUNET_STREAM_Socket *socket = cls;
413 struct MessageQueue *head;
416 socket->transmit_handle = NULL; /* Remove the transmit handle */
417 head = socket->queue_head;
419 return 0; /* just to be safe */
420 if (0 == size) /* request timed out */
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Message sending timed out. Retry %d \n",
426 socket->transmit_handle =
427 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
430 /* FIXME: exponential backoff */
431 socket->retransmit_timeout,
433 ntohs (head->message->header.size),
434 &send_message_notify,
439 ret = ntohs (head->message->header.size);
440 GNUNET_assert (size >= ret);
441 memcpy (buf, head->message, ret);
442 if (NULL != head->finish_cb)
444 head->finish_cb (head->finish_cb_cls, socket);
446 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
449 GNUNET_free (head->message);
451 head = socket->queue_head;
452 if (NULL != head) /* more pending messages to send */
455 socket->transmit_handle =
456 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
459 /* FIXME: exponential backoff */
460 socket->retransmit_timeout,
462 ntohs (head->message->header.size),
463 &send_message_notify,
471 * Queues a message for sending using the mesh connection of a socket
473 * @param socket the socket whose mesh connection is used
474 * @param message the message to be sent
475 * @param finish_cb the callback to be called when the message is sent
476 * @param finish_cb_cls the closure for the callback
479 queue_message (struct GNUNET_STREAM_Socket *socket,
480 struct GNUNET_STREAM_MessageHeader *message,
481 SendFinishCallback finish_cb,
484 struct MessageQueue *queue_entity;
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Queueing message of type %d and size %d\n",
488 ntohs (message->header.type),
489 ntohs (message->header.size));
490 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
491 queue_entity->message = message;
492 queue_entity->finish_cb = finish_cb;
493 queue_entity->finish_cb_cls = finish_cb_cls;
494 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
497 if (NULL == socket->transmit_handle)
500 socket->transmit_handle =
501 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
504 socket->retransmit_timeout,
506 ntohs (message->header.size),
507 &send_message_notify,
514 * Callback function for sending ack message
516 * @param cls closure the ACK message created in ack_task
517 * @param size number of bytes available in buffer
518 * @param buf where the callee should write the message
519 * @return number of bytes written to buf
522 send_ack_notify (void *cls, size_t size, void *buf)
524 struct GNUNET_STREAM_AckMessage *ack_msg = cls;
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "%s called with size 0\n", __func__);
532 GNUNET_assert (ack_msg->header.header.size <= size);
534 size = ack_msg->header.header.size;
535 memcpy (buf, ack_msg, size);
541 * Task for sending ACK message
543 * @param cls the socket
544 * @param tc the Task context
548 const struct GNUNET_SCHEDULER_TaskContext *tc)
550 struct GNUNET_STREAM_Socket *socket = cls;
551 struct GNUNET_STREAM_AckMessage *ack_msg;
553 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
558 socket->ack_task_id = 0;
560 /* Create the ACK Message */
561 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
562 ack_msg->header.header.size = htons (sizeof (struct
563 GNUNET_STREAM_AckMessage));
564 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
565 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
566 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
567 ack_msg->receive_window_remaining =
568 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
570 /* Request MESH for sending ACK */
571 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
574 socket->retransmit_timeout,
576 ntohs (ack_msg->header.header.size),
585 * Function to modify a bit in GNUNET_STREAM_AckBitmap
587 * @param bitmap the bitmap to modify
588 * @param bit the bit number to modify
589 * @param value GNUNET_YES to on, GNUNET_NO to off
592 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
596 GNUNET_assert (bit < 64);
597 if (GNUNET_YES == value)
598 *bitmap |= (1LL << bit);
600 *bitmap &= ~(1LL << bit);
605 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
607 * @param bitmap address of the bitmap that has to be checked
608 * @param bit the bit number to check
609 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
612 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
615 GNUNET_assert (bit < 64);
616 return 0 != (*bitmap & (1LL << bit));
622 * Function called when Data Message is sent
624 * @param cls the io_handle corresponding to the Data Message
625 * @param socket the socket which was used
628 write_data_finish_cb (void *cls,
629 struct GNUNET_STREAM_Socket *socket)
631 struct GNUNET_STREAM_IOWriteHandle *io_handle = cls;
633 io_handle->sent_packets++;
638 * Writes data using the given socket. The amount of data written is limited by
639 * the receive_window_size
641 * @param socket the socket to use
644 write_data (struct GNUNET_STREAM_Socket *socket)
646 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
651 /* Find the last acknowledged packet */
652 for (packet=0; packet < 64; packet++)
654 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
657 else if (NULL == io_handle->messages[packet])
660 /* Resend packets which weren't ack'ed */
661 for (packet=0; packet < ack_packet; packet++)
663 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
666 queue_message (socket,
667 &io_handle->messages[packet]->header,
672 packet = ack_packet + 1;
673 /* Now send new packets if there is enough buffer space */
674 while ( (NULL != io_handle->messages[packet]) &&
675 (socket->receive_window_available >= ntohs (io_handle->messages[packet]->header.header.size)) )
677 socket->receive_window_available -= ntohs (io_handle->messages[packet]->header.header.size);
678 queue_message (socket,
679 &io_handle->messages[packet]->header,
680 &write_data_finish_cb,
688 * Task for calling the read processor
690 * @param cls the socket
691 * @param tc the task context
694 call_read_processor (void *cls,
695 const struct GNUNET_SCHEDULER_TaskContext *tc)
697 struct GNUNET_STREAM_Socket *socket = cls;
699 size_t valid_read_size;
701 uint32_t sequence_increase;
702 uint32_t offset_increase;
704 socket->read_task = GNUNET_SCHEDULER_NO_TASK;
705 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
708 GNUNET_assert (NULL != socket->read_handle);
709 GNUNET_assert (NULL != socket->read_handle->proc);
711 /* Check the bitmap for any holes */
712 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
714 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
718 /* We only call read processor if we have the first packet */
719 GNUNET_assert (0 < packet);
722 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
724 GNUNET_assert (0 != valid_read_size);
726 /* Cancel the read_io_timeout_task */
727 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task);
728 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
730 /* Call the data processor */
732 socket->read_handle->proc (socket->read_handle->proc_cls,
734 socket->receive_buffer + socket->copy_offset,
736 /* Free the read handle */
737 GNUNET_free (socket->read_handle);
738 socket->read_handle = NULL;
740 GNUNET_assert (read_size <= valid_read_size);
741 socket->copy_offset += read_size;
743 /* Determine upto which packet we can remove from the buffer */
744 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
745 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
748 /* If no packets can be removed we can't move the buffer */
749 if (0 == packet) return;
751 sequence_increase = packet;
753 /* Shift the data in the receive buffer */
754 memmove (socket->receive_buffer,
755 socket->receive_buffer
756 + socket->receive_buffer_boundaries[sequence_increase-1],
757 socket->receive_buffer_size - socket->receive_buffer_boundaries[sequence_increase-1]);
759 /* Shift the bitmap */
760 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
762 /* Set read_sequence_number */
763 socket->read_sequence_number += sequence_increase;
765 /* Set read_offset */
766 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
767 socket->read_offset += offset_increase;
769 /* Fix copy_offset */
770 GNUNET_assert (offset_increase <= socket->copy_offset);
771 socket->copy_offset -= offset_increase;
773 /* Fix relative boundaries */
774 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
776 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
778 socket->receive_buffer_boundaries[packet] =
779 socket->receive_buffer_boundaries[packet + sequence_increase]
783 socket->receive_buffer_boundaries[packet] = 0;
789 * Cancels the existing read io handle
791 * @param cls the closure from the SCHEDULER call
792 * @param tc the task context
795 read_io_timeout (void *cls,
796 const struct GNUNET_SCHEDULER_TaskContext *tc)
798 struct GNUNET_STREAM_Socket *socket = cls;
800 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
801 if (socket->read_task != GNUNET_SCHEDULER_NO_TASK)
803 GNUNET_SCHEDULER_cancel (socket->read_task);
804 socket->read_task = GNUNET_SCHEDULER_NO_TASK;
806 GNUNET_assert (NULL != socket->read_handle);
808 GNUNET_free (socket->read_handle);
809 socket->read_handle = NULL;
814 * Handler for DATA messages; Same for both client and server
816 * @param socket the socket through which the ack was received
817 * @param tunnel connection to the other end
818 * @param sender who sent the message
819 * @param msg the data message
820 * @param atsi performance data for the connection
821 * @return GNUNET_OK to keep the connection open,
822 * GNUNET_SYSERR to close it (signal serious error)
825 handle_data (struct GNUNET_STREAM_Socket *socket,
826 struct GNUNET_MESH_Tunnel *tunnel,
827 const struct GNUNET_PeerIdentity *sender,
828 const struct GNUNET_STREAM_DataMessage *msg,
829 const struct GNUNET_ATS_Information*atsi)
832 uint32_t bytes_needed;
833 uint32_t relative_offset;
834 uint32_t relative_sequence_number;
837 size = htons (msg->header.header.size);
838 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
841 return GNUNET_SYSERR;
844 switch (socket->state)
846 case STATE_ESTABLISHED:
847 case STATE_TRANSMIT_CLOSED:
848 case STATE_TRANSMIT_CLOSE_WAIT:
850 /* check if the message's sequence number is in the range we are
852 relative_sequence_number =
853 ntohl (msg->sequence_number) - socket->read_sequence_number;
854 if ( relative_sequence_number > 64)
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Ignoring received message with sequence number %d",
858 ntohl (msg->sequence_number));
862 /* Check if we have to allocate the buffer */
863 size -= sizeof (struct GNUNET_STREAM_DataMessage);
864 relative_offset = ntohl (msg->offset) - socket->read_offset;
865 bytes_needed = relative_offset + size;
867 if (bytes_needed > socket->receive_buffer_size)
869 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
871 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
873 socket->receive_buffer_size = bytes_needed;
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "Cannot accommodate packet %d as buffer is full\n",
879 ntohl (msg->sequence_number));
884 /* Copy Data to buffer */
886 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
887 memcpy (socket->receive_buffer + relative_offset,
890 socket->receive_buffer_boundaries[relative_sequence_number] =
891 relative_offset + size;
893 /* Modify the ACK bitmap */
894 ackbitmap_modify_bit (&socket->ack_bitmap,
895 relative_sequence_number,
898 /* Start ACK sending task if one is not already present */
899 if (0 == socket->ack_task_id)
901 socket->ack_task_id =
902 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
908 if ((NULL != socket->read_handle) /* A read handle is waiting */
909 /* There is no current read task */
910 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task)
911 /* We have the first packet */
912 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
916 GNUNET_SCHEDULER_add_now (&call_read_processor,
923 /* FIXME: call statistics */
930 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
932 * @param cls the socket (set from GNUNET_MESH_connect)
933 * @param tunnel connection to the other end
934 * @param tunnel_ctx place to store local state associated with the tunnel
935 * @param sender who sent the message
936 * @param message the actual message
937 * @param atsi performance data for the connection
938 * @return GNUNET_OK to keep the connection open,
939 * GNUNET_SYSERR to close it (signal serious error)
942 client_handle_data (void *cls,
943 struct GNUNET_MESH_Tunnel *tunnel,
945 const struct GNUNET_PeerIdentity *sender,
946 const struct GNUNET_MessageHeader *message,
947 const struct GNUNET_ATS_Information*atsi)
949 struct GNUNET_STREAM_Socket *socket = cls;
951 return handle_data (socket,
954 (const struct GNUNET_STREAM_DataMessage *) message,
960 * Callback to set state to ESTABLISHED
962 * @param cls the closure from queue_message FIXME: document
963 * @param socket the socket to requiring state change
966 set_state_established (void *cls,
967 struct GNUNET_STREAM_Socket *socket)
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attaining ESTABLISHED state\n");
970 socket->write_offset = 0;
971 socket->read_offset = 0;
972 socket->state = STATE_ESTABLISHED;
974 socket->open_cb (socket->open_cls, socket);
979 * Callback to set state to HELLO_WAIT
981 * @param cls the closure from queue_message
982 * @param socket the socket to requiring state change
985 set_state_hello_wait (void *cls,
986 struct GNUNET_STREAM_Socket *socket)
988 GNUNET_assert (STATE_INIT == socket->state);
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attaining HELLO_WAIT state\n");
990 socket->state = STATE_HELLO_WAIT;
995 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
997 * @param cls the socket (set from GNUNET_MESH_connect)
998 * @param tunnel connection to the other end
999 * @param tunnel_ctx this is NULL
1000 * @param sender who sent the message
1001 * @param message the actual message
1002 * @param atsi performance data for the connection
1003 * @return GNUNET_OK to keep the connection open,
1004 * GNUNET_SYSERR to close it (signal serious error)
1007 client_handle_hello_ack (void *cls,
1008 struct GNUNET_MESH_Tunnel *tunnel,
1010 const struct GNUNET_PeerIdentity *sender,
1011 const struct GNUNET_MessageHeader *message,
1012 const struct GNUNET_ATS_Information*atsi)
1014 struct GNUNET_STREAM_Socket *socket = cls;
1015 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1016 struct GNUNET_STREAM_HelloAckMessage *reply;
1018 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1019 GNUNET_assert (socket->tunnel == tunnel);
1020 switch (socket->state)
1022 case STATE_HELLO_WAIT:
1023 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1024 socket->receive_window_available = ntohl (ack_msg->receive_window_size);
1025 /* Get the random sequence number */
1026 socket->write_sequence_number =
1027 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1029 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1030 reply->header.header.size =
1031 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1032 reply->header.header.type =
1033 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1034 reply->sequence_number = htonl (socket->write_sequence_number);
1035 reply->receive_window_size = htonl (RECEIVE_BUFFER_SIZE);
1036 queue_message (socket,
1038 &set_state_established,
1041 case STATE_ESTABLISHED:
1042 case STATE_RECEIVE_CLOSE_WAIT:
1043 // call statistics (# ACKs ignored++)
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Server sent HELLO_ACK when in state %d\n", socket->state);
1049 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1050 return GNUNET_SYSERR;
1057 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1059 * @param cls the socket (set from GNUNET_MESH_connect)
1060 * @param tunnel connection to the other end
1061 * @param tunnel_ctx this is NULL
1062 * @param sender who sent the message
1063 * @param message the actual message
1064 * @param atsi performance data for the connection
1065 * @return GNUNET_OK to keep the connection open,
1066 * GNUNET_SYSERR to close it (signal serious error)
1069 client_handle_reset (void *cls,
1070 struct GNUNET_MESH_Tunnel *tunnel,
1072 const struct GNUNET_PeerIdentity *sender,
1073 const struct GNUNET_MessageHeader *message,
1074 const struct GNUNET_ATS_Information*atsi)
1076 struct GNUNET_STREAM_Socket *socket = cls;
1083 * Common message handler for handling TRANSMIT_CLOSE messages
1085 * @param socket the socket through which the ack was received
1086 * @param tunnel connection to the other end
1087 * @param sender who sent the message
1088 * @param msg the transmit close message
1089 * @param atsi performance data for the connection
1090 * @return GNUNET_OK to keep the connection open,
1091 * GNUNET_SYSERR to close it (signal serious error)
1094 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1095 struct GNUNET_MESH_Tunnel *tunnel,
1096 const struct GNUNET_PeerIdentity *sender,
1097 const struct GNUNET_STREAM_MessageHeader *msg,
1098 const struct GNUNET_ATS_Information*atsi)
1100 struct GNUNET_STREAM_MessageHeader *reply;
1102 switch (socket->state)
1104 case STATE_ESTABLISHED:
1105 socket->state = STATE_RECEIVE_CLOSED;
1107 /* Send TRANSMIT_CLOSE_ACK */
1108 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1109 reply->header.type =
1110 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1111 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1112 queue_message (socket, reply, NULL, NULL);
1116 /* FIXME: Call statistics? */
1124 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1126 * @param cls the socket (set from GNUNET_MESH_connect)
1127 * @param tunnel connection to the other end
1128 * @param tunnel_ctx this is NULL
1129 * @param sender who sent the message
1130 * @param message the actual message
1131 * @param atsi performance data for the connection
1132 * @return GNUNET_OK to keep the connection open,
1133 * GNUNET_SYSERR to close it (signal serious error)
1136 client_handle_transmit_close (void *cls,
1137 struct GNUNET_MESH_Tunnel *tunnel,
1139 const struct GNUNET_PeerIdentity *sender,
1140 const struct GNUNET_MessageHeader *message,
1141 const struct GNUNET_ATS_Information*atsi)
1143 struct GNUNET_STREAM_Socket *socket = cls;
1145 return handle_transmit_close (socket,
1148 (struct GNUNET_STREAM_MessageHeader *)message,
1154 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1156 * @param cls the socket (set from GNUNET_MESH_connect)
1157 * @param tunnel connection to the other end
1158 * @param tunnel_ctx this is NULL
1159 * @param sender who sent the message
1160 * @param message the actual message
1161 * @param atsi performance data for the connection
1162 * @return GNUNET_OK to keep the connection open,
1163 * GNUNET_SYSERR to close it (signal serious error)
1166 client_handle_transmit_close_ack (void *cls,
1167 struct GNUNET_MESH_Tunnel *tunnel,
1169 const struct GNUNET_PeerIdentity *sender,
1170 const struct GNUNET_MessageHeader *message,
1171 const struct GNUNET_ATS_Information*atsi)
1173 struct GNUNET_STREAM_Socket *socket = cls;
1180 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1182 * @param cls the socket (set from GNUNET_MESH_connect)
1183 * @param tunnel connection to the other end
1184 * @param tunnel_ctx this is NULL
1185 * @param sender who sent the message
1186 * @param message the actual message
1187 * @param atsi performance data for the connection
1188 * @return GNUNET_OK to keep the connection open,
1189 * GNUNET_SYSERR to close it (signal serious error)
1192 client_handle_receive_close (void *cls,
1193 struct GNUNET_MESH_Tunnel *tunnel,
1195 const struct GNUNET_PeerIdentity *sender,
1196 const struct GNUNET_MessageHeader *message,
1197 const struct GNUNET_ATS_Information*atsi)
1199 struct GNUNET_STREAM_Socket *socket = cls;
1206 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1208 * @param cls the socket (set from GNUNET_MESH_connect)
1209 * @param tunnel connection to the other end
1210 * @param tunnel_ctx this is NULL
1211 * @param sender who sent the message
1212 * @param message the actual message
1213 * @param atsi performance data for the connection
1214 * @return GNUNET_OK to keep the connection open,
1215 * GNUNET_SYSERR to close it (signal serious error)
1218 client_handle_receive_close_ack (void *cls,
1219 struct GNUNET_MESH_Tunnel *tunnel,
1221 const struct GNUNET_PeerIdentity *sender,
1222 const struct GNUNET_MessageHeader *message,
1223 const struct GNUNET_ATS_Information*atsi)
1225 struct GNUNET_STREAM_Socket *socket = cls;
1232 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1234 * @param cls the socket (set from GNUNET_MESH_connect)
1235 * @param tunnel connection to the other end
1236 * @param tunnel_ctx this is NULL
1237 * @param sender who sent the message
1238 * @param message the actual message
1239 * @param atsi performance data for the connection
1240 * @return GNUNET_OK to keep the connection open,
1241 * GNUNET_SYSERR to close it (signal serious error)
1244 client_handle_close (void *cls,
1245 struct GNUNET_MESH_Tunnel *tunnel,
1247 const struct GNUNET_PeerIdentity *sender,
1248 const struct GNUNET_MessageHeader *message,
1249 const struct GNUNET_ATS_Information*atsi)
1251 struct GNUNET_STREAM_Socket *socket = cls;
1258 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1260 * @param cls the socket (set from GNUNET_MESH_connect)
1261 * @param tunnel connection to the other end
1262 * @param tunnel_ctx this is NULL
1263 * @param sender who sent the message
1264 * @param message the actual message
1265 * @param atsi performance data for the connection
1266 * @return GNUNET_OK to keep the connection open,
1267 * GNUNET_SYSERR to close it (signal serious error)
1270 client_handle_close_ack (void *cls,
1271 struct GNUNET_MESH_Tunnel *tunnel,
1273 const struct GNUNET_PeerIdentity *sender,
1274 const struct GNUNET_MessageHeader *message,
1275 const struct GNUNET_ATS_Information*atsi)
1277 struct GNUNET_STREAM_Socket *socket = cls;
1282 /*****************************/
1283 /* Server's Message Handlers */
1284 /*****************************/
1287 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
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_data (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;
1308 return handle_data (socket,
1311 (const struct GNUNET_STREAM_DataMessage *)message,
1317 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1319 * @param cls the closure
1320 * @param tunnel connection to the other end
1321 * @param tunnel_ctx the socket
1322 * @param sender who sent the message
1323 * @param message the actual message
1324 * @param atsi performance data for the connection
1325 * @return GNUNET_OK to keep the connection open,
1326 * GNUNET_SYSERR to close it (signal serious error)
1329 server_handle_hello (void *cls,
1330 struct GNUNET_MESH_Tunnel *tunnel,
1332 const struct GNUNET_PeerIdentity *sender,
1333 const struct GNUNET_MessageHeader *message,
1334 const struct GNUNET_ATS_Information*atsi)
1336 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1337 struct GNUNET_STREAM_HelloAckMessage *reply;
1339 GNUNET_assert (socket->tunnel == tunnel);
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1341 "Received HELLO from %s\n", GNUNET_i2s(sender));
1343 /* Catch possible protocol breaks */
1344 GNUNET_break_op (0 != memcmp (&socket->other_peer,
1346 sizeof (struct GNUNET_PeerIdentity)));
1348 if (STATE_INIT == socket->state)
1350 /* Get the random sequence number */
1351 socket->write_sequence_number =
1352 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1354 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1355 reply->header.header.size =
1356 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1357 reply->header.header.type =
1358 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1359 reply->sequence_number = htonl (socket->write_sequence_number);
1360 queue_message (socket,
1362 &set_state_hello_wait,
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368 "Client sent HELLO when in state %d\n", socket->state);
1369 /* FIXME: Send RESET? */
1377 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1379 * @param cls the closure
1380 * @param tunnel connection to the other end
1381 * @param tunnel_ctx the socket
1382 * @param sender who sent the message
1383 * @param message the actual message
1384 * @param atsi performance data for the connection
1385 * @return GNUNET_OK to keep the connection open,
1386 * GNUNET_SYSERR to close it (signal serious error)
1389 server_handle_hello_ack (void *cls,
1390 struct GNUNET_MESH_Tunnel *tunnel,
1392 const struct GNUNET_PeerIdentity *sender,
1393 const struct GNUNET_MessageHeader *message,
1394 const struct GNUNET_ATS_Information*atsi)
1396 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1397 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1399 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1400 GNUNET_assert (socket->tunnel == tunnel);
1401 if (STATE_HELLO_WAIT == socket->state)
1403 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1404 socket->receive_window_available =
1405 ntohl (ack_message->receive_window_size);
1406 /* Attain ESTABLISHED state */
1407 set_state_established (NULL, socket);
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "Client sent HELLO_ACK when in state %d\n", socket->state);
1413 /* FIXME: Send RESET? */
1421 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1423 * @param cls the closure
1424 * @param tunnel connection to the other end
1425 * @param tunnel_ctx the socket
1426 * @param sender who sent the message
1427 * @param message the actual message
1428 * @param atsi performance data for the connection
1429 * @return GNUNET_OK to keep the connection open,
1430 * GNUNET_SYSERR to close it (signal serious error)
1433 server_handle_reset (void *cls,
1434 struct GNUNET_MESH_Tunnel *tunnel,
1436 const struct GNUNET_PeerIdentity *sender,
1437 const struct GNUNET_MessageHeader *message,
1438 const struct GNUNET_ATS_Information*atsi)
1440 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1447 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1449 * @param cls the closure
1450 * @param tunnel connection to the other end
1451 * @param tunnel_ctx the socket
1452 * @param sender who sent the message
1453 * @param message the actual message
1454 * @param atsi performance data for the connection
1455 * @return GNUNET_OK to keep the connection open,
1456 * GNUNET_SYSERR to close it (signal serious error)
1459 server_handle_transmit_close (void *cls,
1460 struct GNUNET_MESH_Tunnel *tunnel,
1462 const struct GNUNET_PeerIdentity *sender,
1463 const struct GNUNET_MessageHeader *message,
1464 const struct GNUNET_ATS_Information*atsi)
1466 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1468 return handle_transmit_close (socket,
1471 (struct GNUNET_STREAM_MessageHeader *)message,
1477 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1479 * @param cls the closure
1480 * @param tunnel connection to the other end
1481 * @param tunnel_ctx the socket
1482 * @param sender who sent the message
1483 * @param message the actual message
1484 * @param atsi performance data for the connection
1485 * @return GNUNET_OK to keep the connection open,
1486 * GNUNET_SYSERR to close it (signal serious error)
1489 server_handle_transmit_close_ack (void *cls,
1490 struct GNUNET_MESH_Tunnel *tunnel,
1492 const struct GNUNET_PeerIdentity *sender,
1493 const struct GNUNET_MessageHeader *message,
1494 const struct GNUNET_ATS_Information*atsi)
1496 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1503 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1505 * @param cls the closure
1506 * @param tunnel connection to the other end
1507 * @param tunnel_ctx the socket
1508 * @param sender who sent the message
1509 * @param message the actual message
1510 * @param atsi performance data for the connection
1511 * @return GNUNET_OK to keep the connection open,
1512 * GNUNET_SYSERR to close it (signal serious error)
1515 server_handle_receive_close (void *cls,
1516 struct GNUNET_MESH_Tunnel *tunnel,
1518 const struct GNUNET_PeerIdentity *sender,
1519 const struct GNUNET_MessageHeader *message,
1520 const struct GNUNET_ATS_Information*atsi)
1522 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1529 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1531 * @param cls the closure
1532 * @param tunnel connection to the other end
1533 * @param tunnel_ctx the socket
1534 * @param sender who sent the message
1535 * @param message the actual message
1536 * @param atsi performance data for the connection
1537 * @return GNUNET_OK to keep the connection open,
1538 * GNUNET_SYSERR to close it (signal serious error)
1541 server_handle_receive_close_ack (void *cls,
1542 struct GNUNET_MESH_Tunnel *tunnel,
1544 const struct GNUNET_PeerIdentity *sender,
1545 const struct GNUNET_MessageHeader *message,
1546 const struct GNUNET_ATS_Information*atsi)
1548 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1555 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1557 * @param cls the closure
1558 * @param tunnel connection to the other end
1559 * @param tunnel_ctx the socket
1560 * @param sender who sent the message
1561 * @param message the actual message
1562 * @param atsi performance data for the connection
1563 * @return GNUNET_OK to keep the connection open,
1564 * GNUNET_SYSERR to close it (signal serious error)
1567 server_handle_close (void *cls,
1568 struct GNUNET_MESH_Tunnel *tunnel,
1570 const struct GNUNET_PeerIdentity *sender,
1571 const struct GNUNET_MessageHeader *message,
1572 const struct GNUNET_ATS_Information*atsi)
1574 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1581 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1583 * @param cls the closure
1584 * @param tunnel connection to the other end
1585 * @param tunnel_ctx the socket
1586 * @param sender who sent the message
1587 * @param message the actual message
1588 * @param atsi performance data for the connection
1589 * @return GNUNET_OK to keep the connection open,
1590 * GNUNET_SYSERR to close it (signal serious error)
1593 server_handle_close_ack (void *cls,
1594 struct GNUNET_MESH_Tunnel *tunnel,
1596 const struct GNUNET_PeerIdentity *sender,
1597 const struct GNUNET_MessageHeader *message,
1598 const struct GNUNET_ATS_Information*atsi)
1600 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1607 * Message Handler for mesh
1609 * @param socket the socket through which the ack was received
1610 * @param tunnel connection to the other end
1611 * @param sender who sent the message
1612 * @param ack the acknowledgment message
1613 * @param atsi performance data for the connection
1614 * @return GNUNET_OK to keep the connection open,
1615 * GNUNET_SYSERR to close it (signal serious error)
1618 handle_ack (struct GNUNET_STREAM_Socket *socket,
1619 struct GNUNET_MESH_Tunnel *tunnel,
1620 const struct GNUNET_PeerIdentity *sender,
1621 const struct GNUNET_STREAM_AckMessage *ack,
1622 const struct GNUNET_ATS_Information*atsi)
1624 switch (socket->state)
1626 case (STATE_ESTABLISHED):
1627 if (NULL == socket->write_handle)
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630 "Received DATA ACK when write_handle is NULL\n");
1634 socket->write_handle->ack_bitmap = GNUNET_ntohll (ack->bitmap);
1635 socket->receive_window_available =
1636 ntohl (ack->receive_window_remaining);
1637 write_data (socket);
1647 * Message Handler for mesh
1649 * @param cls the 'struct GNUNET_STREAM_Socket'
1650 * @param tunnel connection to the other end
1651 * @param tunnel_ctx unused
1652 * @param sender who sent the message
1653 * @param message the actual message
1654 * @param atsi performance data for the connection
1655 * @return GNUNET_OK to keep the connection open,
1656 * GNUNET_SYSERR to close it (signal serious error)
1659 client_handle_ack (void *cls,
1660 struct GNUNET_MESH_Tunnel *tunnel,
1662 const struct GNUNET_PeerIdentity *sender,
1663 const struct GNUNET_MessageHeader *message,
1664 const struct GNUNET_ATS_Information*atsi)
1666 struct GNUNET_STREAM_Socket *socket = cls;
1667 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1669 return handle_ack (socket, tunnel, sender, ack, atsi);
1674 * Message Handler for mesh
1676 * @param cls the server's listen socket
1677 * @param tunnel connection to the other end
1678 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
1679 * @param sender who sent the message
1680 * @param message the actual message
1681 * @param atsi performance data for the connection
1682 * @return GNUNET_OK to keep the connection open,
1683 * GNUNET_SYSERR to close it (signal serious error)
1686 server_handle_ack (void *cls,
1687 struct GNUNET_MESH_Tunnel *tunnel,
1689 const struct GNUNET_PeerIdentity *sender,
1690 const struct GNUNET_MessageHeader *message,
1691 const struct GNUNET_ATS_Information*atsi)
1693 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1694 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1696 return handle_ack (socket, tunnel, sender, ack, atsi);
1701 * For client message handlers, the stream socket is in the
1704 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
1705 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1706 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1707 sizeof (struct GNUNET_STREAM_AckMessage) },
1708 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1709 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1710 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1711 sizeof (struct GNUNET_STREAM_MessageHeader)},
1712 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1713 sizeof (struct GNUNET_STREAM_MessageHeader)},
1714 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1715 sizeof (struct GNUNET_STREAM_MessageHeader)},
1716 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1717 sizeof (struct GNUNET_STREAM_MessageHeader)},
1718 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1719 sizeof (struct GNUNET_STREAM_MessageHeader)},
1720 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1721 sizeof (struct GNUNET_STREAM_MessageHeader)},
1722 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1723 sizeof (struct GNUNET_STREAM_MessageHeader)},
1729 * For server message handlers, the stream socket is in the
1730 * tunnel context, and the listen socket in the closure argument.
1732 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
1733 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1734 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1735 sizeof (struct GNUNET_STREAM_AckMessage) },
1736 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
1737 sizeof (struct GNUNET_STREAM_MessageHeader)},
1738 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1739 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1740 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1741 sizeof (struct GNUNET_STREAM_MessageHeader)},
1742 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1743 sizeof (struct GNUNET_STREAM_MessageHeader)},
1744 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1745 sizeof (struct GNUNET_STREAM_MessageHeader)},
1746 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1747 sizeof (struct GNUNET_STREAM_MessageHeader)},
1748 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1749 sizeof (struct GNUNET_STREAM_MessageHeader)},
1750 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1751 sizeof (struct GNUNET_STREAM_MessageHeader)},
1752 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1753 sizeof (struct GNUNET_STREAM_MessageHeader)},
1759 * Function called when our target peer is connected to our tunnel
1761 * @param cls the socket for which this tunnel is created
1762 * @param peer the peer identity of the target
1763 * @param atsi performance data for the connection
1766 mesh_peer_connect_callback (void *cls,
1767 const struct GNUNET_PeerIdentity *peer,
1768 const struct GNUNET_ATS_Information * atsi)
1770 struct GNUNET_STREAM_Socket *socket = cls;
1771 struct GNUNET_STREAM_MessageHeader *message;
1773 if (0 != memcmp (&socket->other_peer,
1775 sizeof (struct GNUNET_PeerIdentity)))
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1778 "A peer (%s) which is not our target has connected to our tunnel",
1783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1784 "Target peer %s connected\n", GNUNET_i2s (peer));
1786 /* Set state to INIT */
1787 socket->state = STATE_INIT;
1789 /* Send HELLO message */
1790 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1791 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
1792 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1793 queue_message (socket,
1795 &set_state_hello_wait,
1798 /* Call open callback */
1799 if (NULL == socket->open_cb)
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1802 "STREAM_open callback is NULL\n");
1808 * Function called when our target peer is disconnected from our tunnel
1810 * @param cls the socket associated which this tunnel
1811 * @param peer the peer identity of the target
1814 mesh_peer_disconnect_callback (void *cls,
1815 const struct GNUNET_PeerIdentity *peer)
1822 * Method called whenever a peer creates a tunnel to us
1824 * @param cls closure
1825 * @param tunnel new handle to the tunnel
1826 * @param initiator peer that started the tunnel
1827 * @param atsi performance information for the tunnel
1828 * @return initial tunnel context for the tunnel
1829 * (can be NULL -- that's not an error)
1832 new_tunnel_notify (void *cls,
1833 struct GNUNET_MESH_Tunnel *tunnel,
1834 const struct GNUNET_PeerIdentity *initiator,
1835 const struct GNUNET_ATS_Information *atsi)
1837 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
1838 struct GNUNET_STREAM_Socket *socket;
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841 "Peer %s initiated tunnel to us\n", GNUNET_i2s (initiator));
1842 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
1843 socket->tunnel = tunnel;
1844 socket->session_id = 0; /* FIXME */
1845 socket->other_peer = *initiator;
1846 socket->state = STATE_INIT;
1847 socket->derived = GNUNET_YES;
1849 /* FIXME: Copy MESH handle from lsocket to socket */
1851 if (GNUNET_SYSERR == lsocket->listen_cb (lsocket->listen_cb_cls,
1853 &socket->other_peer))
1855 socket->state = STATE_CLOSED;
1856 /* FIXME: Send CLOSE message and then free */
1857 GNUNET_free (socket);
1858 GNUNET_MESH_tunnel_destroy (tunnel); /* Destroy the tunnel */
1865 * Function called whenever an inbound tunnel is destroyed. Should clean up
1866 * any associated state. This function is NOT called if the client has
1867 * explicitly asked for the tunnel to be destroyed using
1868 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
1871 * @param cls closure (set from GNUNET_MESH_connect)
1872 * @param tunnel connection to the other end (henceforth invalid)
1873 * @param tunnel_ctx place where local state associated
1874 * with the tunnel is stored
1877 tunnel_cleaner (void *cls,
1878 const struct GNUNET_MESH_Tunnel *tunnel,
1881 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
1883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1884 "Peer %s has terminated connection abruptly\n",
1885 GNUNET_i2s (&socket->other_peer));
1887 socket->status = GNUNET_STREAM_SHUTDOWN;
1888 /* Clear Transmit handles */
1889 if (NULL != socket->transmit_handle)
1891 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
1892 socket->transmit_handle = NULL;
1894 socket->tunnel = NULL;
1904 * Tries to open a stream to the target peer
1906 * @param cfg configuration to use
1907 * @param target the target peer to which the stream has to be opened
1908 * @param app_port the application port number which uniquely identifies this
1910 * @param open_cb this function will be called after stream has be established
1911 * @param open_cb_cls the closure for open_cb
1912 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
1913 * @return if successful it returns the stream socket; NULL if stream cannot be
1916 struct GNUNET_STREAM_Socket *
1917 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
1918 const struct GNUNET_PeerIdentity *target,
1919 GNUNET_MESH_ApplicationType app_port,
1920 GNUNET_STREAM_OpenCallback open_cb,
1924 struct GNUNET_STREAM_Socket *socket;
1925 enum GNUNET_STREAM_Option option;
1926 va_list vargs; /* Variable arguments */
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
1932 socket->other_peer = *target;
1933 socket->open_cb = open_cb;
1934 socket->open_cls = open_cb_cls;
1937 socket->retransmit_timeout =
1938 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
1940 va_start (vargs, open_cb_cls); /* Parse variable args */
1942 option = va_arg (vargs, enum GNUNET_STREAM_Option);
1945 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
1946 /* Expect struct GNUNET_TIME_Relative */
1947 socket->retransmit_timeout = va_arg (vargs,
1948 struct GNUNET_TIME_Relative);
1950 case GNUNET_STREAM_OPTION_END:
1953 } while (GNUNET_STREAM_OPTION_END != option);
1954 va_end (vargs); /* End of variable args parsing */
1955 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
1956 10, /* QUEUE size as parameter? */
1958 NULL, /* No inbound tunnel handler */
1960 client_message_handlers,
1961 &app_port); /* We don't get inbound tunnels */
1962 if (NULL == socket->mesh) /* Fail if we cannot connect to mesh */
1964 GNUNET_free (socket);
1968 /* Now create the mesh tunnel to target */
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1970 "Creating MESH Tunnel\n");
1971 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
1972 NULL, /* Tunnel context */
1973 &mesh_peer_connect_callback,
1974 &mesh_peer_disconnect_callback,
1976 GNUNET_assert (NULL != socket->tunnel);
1977 GNUNET_MESH_peer_request_connect_add (socket->tunnel,
1980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1981 "%s() END\n", __func__);
1987 * Shutdown the stream for reading or writing (man 2 shutdown).
1989 * @param socket the stream socket
1990 * @param how SHUT_RD, SHUT_WR or SHUT_RDWR
1993 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2003 * @param socket the stream socket
2006 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
2008 struct MessageQueue *head;
2010 if (socket->read_task != GNUNET_SCHEDULER_NO_TASK)
2012 /* socket closed with read task pending!? */
2014 GNUNET_SCHEDULER_cancel (socket->read_task);
2015 socket->read_task = GNUNET_SCHEDULER_NO_TASK;
2018 /* Clear Transmit handles */
2019 if (NULL != socket->transmit_handle)
2021 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2022 socket->transmit_handle = NULL;
2025 /* Clear existing message queue */
2026 while (NULL != (head = socket->queue_head)) {
2027 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
2030 GNUNET_free (head->message);
2034 /* Close associated tunnel */
2035 if (NULL != socket->tunnel)
2037 GNUNET_MESH_tunnel_destroy (socket->tunnel);
2038 socket->tunnel = NULL;
2041 /* Close mesh connection */
2042 if (NULL != socket->mesh && GNUNET_YES != socket->derived)
2044 GNUNET_MESH_disconnect (socket->mesh);
2045 socket->mesh = NULL;
2048 /* Release receive buffer */
2049 if (NULL != socket->receive_buffer)
2051 GNUNET_free (socket->receive_buffer);
2054 GNUNET_free (socket);
2059 * Listens for stream connections for a specific application ports
2061 * @param cfg the configuration to use
2062 * @param app_port the application port for which new streams will be accepted
2063 * @param listen_cb this function will be called when a peer tries to establish
2065 * @param listen_cb_cls closure for listen_cb
2066 * @return listen socket, NULL for any error
2068 struct GNUNET_STREAM_ListenSocket *
2069 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
2070 GNUNET_MESH_ApplicationType app_port,
2071 GNUNET_STREAM_ListenCallback listen_cb,
2072 void *listen_cb_cls)
2074 /* FIXME: Add variable args for passing configration options? */
2075 struct GNUNET_STREAM_ListenSocket *lsocket;
2077 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
2078 lsocket->port = app_port;
2079 lsocket->listen_cb = listen_cb;
2080 lsocket->listen_cb_cls = listen_cb_cls;
2081 lsocket->mesh = GNUNET_MESH_connect (cfg,
2082 10, /* FIXME: QUEUE size as parameter? */
2083 lsocket, /* Closure */
2086 server_message_handlers,
2088 GNUNET_assert (NULL != lsocket->mesh);
2094 * Closes the listen socket
2096 * @param lsocket the listen socket
2099 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
2101 /* Close MESH connection */
2102 GNUNET_assert (NULL != lsocket->mesh);
2103 GNUNET_MESH_disconnect (lsocket->mesh);
2105 GNUNET_free (lsocket);
2110 * Tries to write the given data to the stream
2112 * @param socket the socket representing a stream
2113 * @param data the data buffer from where the data is written into the stream
2114 * @param size the number of bytes to be written from the data buffer
2115 * @param timeout the timeout period
2116 * @param write_cont the function to call upon writing some bytes into the stream
2117 * @param write_cont_cls the closure
2118 * @return handle to cancel the operation
2120 struct GNUNET_STREAM_IOWriteHandle *
2121 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
2124 struct GNUNET_TIME_Relative timeout,
2125 GNUNET_STREAM_CompletionContinuation write_cont,
2126 void *write_cont_cls)
2128 unsigned int num_needed_packets;
2129 unsigned int packet;
2130 struct GNUNET_STREAM_IOWriteHandle *io_handle;
2131 uint32_t packet_size;
2132 uint32_t payload_size;
2133 struct GNUNET_STREAM_DataMessage *data_msg;
2136 /* Return NULL if there is already a write request pending */
2137 if (NULL != socket->write_handle)
2142 if (!((STATE_ESTABLISHED == socket->state)
2143 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
2144 || (STATE_RECEIVE_CLOSED == socket->state)))
2146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2147 "Attempting to write on a closed (OR) not-yet-established"
2151 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
2152 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
2153 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
2154 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
2156 /* Divide the given buffer into packets for sending */
2157 for (packet=0; packet < num_needed_packets; packet++)
2159 if ((packet + 1) * max_payload_size < size)
2161 payload_size = max_payload_size;
2162 packet_size = MAX_PACKET_SIZE;
2166 payload_size = size - packet * max_payload_size;
2167 packet_size = payload_size + sizeof (struct
2168 GNUNET_STREAM_DataMessage);
2170 io_handle->messages[packet] = GNUNET_malloc (packet_size);
2171 io_handle->messages[packet]->header.header.size = htons (packet_size);
2172 io_handle->messages[packet]->header.header.type =
2173 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
2174 io_handle->messages[packet]->sequence_number =
2175 htons (socket->write_sequence_number++);
2176 io_handle->messages[packet]->offset = htons (socket->write_offset);
2178 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2179 determined from RTT */
2180 io_handle->messages[packet]->ack_deadline =
2181 GNUNET_TIME_relative_hton (GNUNET_TIME_relative_multiply
2182 (GNUNET_TIME_UNIT_SECONDS, 5));
2183 data_msg = io_handle->messages[packet];
2184 /* Copy data from given buffer to the packet */
2185 memcpy (&data_msg[1],
2188 sweep += payload_size;
2189 socket->write_offset += payload_size;
2191 socket->write_handle = io_handle;
2192 write_data (socket);
2199 * Tries to read data from the stream
2201 * @param socket the socket representing a stream
2202 * @param timeout the timeout period
2203 * @param proc function to call with data (once only)
2204 * @param proc_cls the closure for proc
2205 * @return handle to cancel the operation
2207 struct GNUNET_STREAM_IOReadHandle *
2208 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
2209 struct GNUNET_TIME_Relative timeout,
2210 GNUNET_STREAM_DataProcessor proc,
2213 struct GNUNET_STREAM_IOReadHandle *read_handle;
2215 /* Return NULL if there is already a read handle; the user has to cancel that
2216 first before continuing or has to wait until it is completed */
2217 if (NULL != socket->read_handle) return NULL;
2219 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
2220 read_handle->proc = proc;
2221 socket->read_handle = read_handle;
2223 /* Check if we have a packet at bitmap 0 */
2224 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
2227 socket->read_task = GNUNET_SCHEDULER_add_now (&call_read_processor,
2232 /* Setup the read timeout task */
2233 socket->read_io_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
2241 * Cancel pending write operation.
2243 * @param ioh handle to operation to cancel
2246 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *ioh)
2253 * Cancel pending read operation.
2255 * @param ioh handle to operation to cancel
2258 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *ioh)