2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file stream/stream_api.c
23 * @brief Implementation of the stream library
24 * @author Sree Harsha Totakura
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_stream_lib.h"
30 #include "stream_protocol.h"
34 * The maximum packet size of a stream packet
36 #define MAX_PACKET_SIZE 64000
39 * The maximum payload a data message packet can carry
41 static size_t max_payload_size =
42 MAX_PACKET_SIZE - sizeof (struct GNUNET_STREAM_DataMessage);
47 #define RECEIVE_BUFFER_SIZE 4096000
50 * states in the Protocol
55 * Client initialization state
60 * Listener initialization state
65 * Pre-connection establishment state
70 * State where a connection has been established
75 * State where the socket is closed on our side and waiting to be ACK'ed
77 STATE_RECEIVE_CLOSE_WAIT,
80 * State where the socket is closed for reading
85 * State where the socket is closed on our side and waiting to be ACK'ed
87 STATE_TRANSMIT_CLOSE_WAIT,
90 * State where the socket is closed for writing
92 STATE_TRANSMIT_CLOSED,
95 * State where the socket is closed on our side and waiting to be ACK'ed
100 * State where the socket is closed
107 * Functions of this type are called when a message is written
109 * @param cls the closure from queue_message
110 * @param socket the socket the written message was bound to
112 typedef void (*SendFinishCallback) (void *cls,
113 struct GNUNET_STREAM_Socket *socket);
117 * The send message queue
124 struct GNUNET_STREAM_MessageHeader *message;
127 * Callback to be called when the message is sent
129 SendFinishCallback finish_cb;
132 * The closure for finish_cb
137 * The next message in queue. Should be NULL in the last message
139 struct MessageQueue *next;
142 * The next message in queue. Should be NULL in the first message
144 struct MessageQueue *prev;
149 * The STREAM Socket Handler
151 struct GNUNET_STREAM_Socket
155 * The peer identity of the peer at the other end of the stream
157 struct GNUNET_PeerIdentity other_peer;
160 * Retransmission timeout
162 struct GNUNET_TIME_Relative retransmit_timeout;
165 * The Acknowledgement Bitmap
167 GNUNET_STREAM_AckBitmap ack_bitmap;
170 * Time when the Acknowledgement was queued
172 struct GNUNET_TIME_Absolute ack_time_registered;
175 * Queued Acknowledgement deadline
177 struct GNUNET_TIME_Relative ack_time_deadline;
180 * The task for sending timely Acks
182 GNUNET_SCHEDULER_TaskIdentifier ack_task_id;
187 struct GNUNET_MESH_Handle *mesh;
190 * The mesh tunnel handle
192 struct GNUNET_MESH_Tunnel *tunnel;
195 * Stream open closure
200 * Stream open callback
202 GNUNET_STREAM_OpenCallback open_cb;
205 * The current transmit handle (if a pending transmit request exists)
207 struct GNUNET_MESH_TransmitHandle *transmit_handle;
210 * The current message associated with the transmit handle
212 struct MessageQueue *queue_head;
215 * The queue tail, should always point to the last message in queue
217 struct MessageQueue *queue_tail;
220 * The write IO_handle associated with this socket
222 struct GNUNET_STREAM_IOWriteHandle *write_handle;
225 * The read IO_handle associated with this socket
227 struct GNUNET_STREAM_IOReadHandle *read_handle;
230 * Buffer for storing received messages
232 void *receive_buffer;
235 * Copy buffer pointer; Used during read operations
240 * Task identifier for the read io timeout task
242 GNUNET_SCHEDULER_TaskIdentifier read_io_timeout_task;
245 * The state of the protocol associated with this socket
250 * The status of the socket
252 enum GNUNET_STREAM_Status status;
255 * The number of previous timeouts; FIXME: currently not used
257 unsigned int retries;
260 * The session id associated with this stream connection
261 * FIXME: Not used currently, may be removed
266 * Write sequence number. Set to random when sending HELLO(client) and
269 uint32_t write_sequence_number;
272 * Read sequence number. This number's value is determined during handshake
274 uint32_t read_sequence_number;
277 * The receiver buffer size
279 uint32_t receive_buffer_size;
282 * The receiver buffer boundaries
284 uint32_t receive_buffer_boundaries[GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH];
287 * receiver's available buffer after the last acknowledged packet
289 uint32_t receive_window_available;
292 * The offset pointer used during write operation
294 uint32_t write_offset;
297 * The offset after which we are expecting data
299 uint32_t read_offset;
302 * The size of the copy buffer
304 uint32_t copy_buffer_size;
307 * The read offset of copy buffer
309 uint32_t copy_buffer_read_offset;
314 * A socket for listening
316 struct GNUNET_STREAM_ListenSocket
322 struct GNUNET_MESH_Handle *mesh;
325 * The callback function which is called after successful opening socket
327 GNUNET_STREAM_ListenCallback listen_cb;
330 * The call back closure
337 GNUNET_MESH_ApplicationType port;
342 * The IO Write Handle
344 struct GNUNET_STREAM_IOWriteHandle
347 * The packet_buffers associated with this Handle
349 struct GNUNET_STREAM_DataMessage *messages[64];
352 * The bitmap of this IOHandle; Corresponding bit for a message is set when
353 * it has been acknowledged by the receiver
355 GNUNET_STREAM_AckBitmap ack_bitmap;
358 * Number of packets sent before waiting for an ack
360 * FIXME: Do we need this?
362 unsigned int sent_packets;
369 struct GNUNET_STREAM_IOReadHandle
372 * Callback for the read processor
374 GNUNET_STREAM_DataProcessor proc;
377 * The closure pointer for the read processor callback
384 * Default value in seconds for various timeouts
386 static unsigned int default_timeout = 300;
390 * Callback function for sending hello message
392 * @param cls closure the socket
393 * @param size number of bytes available in buf
394 * @param buf where the callee should write the message
395 * @return number of bytes written to buf
398 send_message_notify (void *cls, size_t size, void *buf)
400 struct GNUNET_STREAM_Socket *socket = cls;
401 struct MessageQueue *head;
404 socket->transmit_handle = NULL; /* Remove the transmit handle */
405 head = socket->queue_head;
407 return 0; /* just to be safe */
408 if (0 == size) /* request timed out */
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Message sending timed out. Retry %d \n",
414 socket->transmit_handle =
415 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
418 /* FIXME: exponential backoff */
419 socket->retransmit_timeout,
421 ntohs (head->message->header.size),
422 &send_message_notify,
427 ret = ntohs (head->message->header.size);
428 GNUNET_assert (size >= ret);
429 memcpy (buf, head->message, ret);
430 if (NULL != head->finish_cb)
432 head->finish_cb (socket, head->finish_cb_cls);
434 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
437 GNUNET_free (head->message);
439 head = socket->queue_head;
440 if (NULL != head) /* more pending messages to send */
443 socket->transmit_handle =
444 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
447 /* FIXME: exponential backoff */
448 socket->retransmit_timeout,
450 ntohs (head->message->header.size),
451 &send_message_notify,
459 * Queues a message for sending using the mesh connection of a socket
461 * @param socket the socket whose mesh connection is used
462 * @param message the message to be sent
463 * @param finish_cb the callback to be called when the message is sent
464 * @param finish_cb_cls the closure for the callback
467 queue_message (struct GNUNET_STREAM_Socket *socket,
468 struct GNUNET_STREAM_MessageHeader *message,
469 SendFinishCallback finish_cb,
472 struct MessageQueue *queue_entity;
474 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
475 queue_entity->message = message;
476 queue_entity->finish_cb = finish_cb;
477 queue_entity->finish_cb_cls = finish_cb_cls;
478 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
481 if (NULL == socket->transmit_handle)
484 socket->transmit_handle =
485 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
488 socket->retransmit_timeout,
490 ntohs (message->header.size),
491 &send_message_notify,
498 * Callback function for sending ack message
500 * @param cls closure the ACK message created in ack_task
501 * @param size number of bytes available in buffer
502 * @param buf where the callee should write the message
503 * @return number of bytes written to buf
506 send_ack_notify (void *cls, size_t size, void *buf)
508 struct GNUNET_STREAM_AckMessage *ack_msg = cls;
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "%s called with size 0\n", __func__);
516 GNUNET_assert (ack_msg->header.header.size <= size);
518 size = ack_msg->header.header.size;
519 memcpy (buf, ack_msg, size);
525 * Task for sending ACK message
527 * @param cls the socket
528 * @param tc the Task context
532 const struct GNUNET_SCHEDULER_TaskContext *tc)
534 struct GNUNET_STREAM_Socket *socket = cls;
535 struct GNUNET_STREAM_AckMessage *ack_msg;
537 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
542 socket->ack_task_id = 0;
544 /* Create the ACK Message */
545 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
546 ack_msg->header.header.size = htons (sizeof (struct
547 GNUNET_STREAM_AckMessage));
548 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
549 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
550 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
551 ack_msg->receive_window_remaining =
552 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
554 /* Request MESH for sending ACK */
555 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
558 socket->retransmit_timeout,
560 ntohs (ack_msg->header.header.size),
569 * Function to modify a bit in GNUNET_STREAM_AckBitmap
571 * @param bitmap the bitmap to modify
572 * @param bit the bit number to modify
573 * @param value GNUNET_YES to on, GNUNET_NO to off
576 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
580 GNUNET_assert (bit < 64);
581 if (GNUNET_YES == value)
582 *bitmap |= (1LL << bit);
584 *bitmap &= ~(1LL << bit);
589 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
591 * @param bitmap address of the bitmap that has to be checked
592 * @param bit the bit number to check
593 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
596 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
599 GNUNET_assert (bit < 64);
600 return 0 != (*bitmap & (1LL << bit));
606 * Function called when Data Message is sent
608 * @param cls the io_handle corresponding to the Data Message
609 * @param socket the socket which was used
612 write_data_finish_cb (void *cls,
613 struct GNUNET_STREAM_Socket *socket)
615 struct GNUNET_STREAM_IOWriteHandle *io_handle = cls;
617 io_handle->sent_packets++;
622 * Writes data using the given socket. The amount of data written is limited by
623 * the receive_window_size
625 * @param socket the socket to use
628 write_data (struct GNUNET_STREAM_Socket *socket)
630 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
635 /* Find the last acknowledged packet */
636 for (packet=0; packet < 64; packet++)
638 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
641 else if (NULL == io_handle->messages[packet])
644 /* Resend packets which weren't ack'ed */
645 for (packet=0; packet < ack_packet; packet++)
647 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
650 queue_message (socket,
651 &io_handle->messages[packet]->header,
656 packet = ack_packet + 1;
657 /* Now send new packets if there is enough buffer space */
658 while ( (NULL != io_handle->messages[packet]) &&
659 (socket->receive_window_available >= ntohs (io_handle->messages[packet]->header.header.size)) )
661 socket->receive_window_available -= ntohs (io_handle->messages[packet]->header.header.size);
662 queue_message (socket,
663 &io_handle->messages[packet]->header,
664 &write_data_finish_cb,
672 * Task for calling the read processor
674 * @param cls the socket
675 * @param tc the task context
678 call_read_processor_task (void *cls,
679 const struct GNUNET_SCHEDULER_TaskContext *tc)
681 struct GNUNET_STREAM_Socket *socket = cls;
683 size_t valid_read_size;
685 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) return;
687 GNUNET_assert (NULL != socket->read_handle);
688 GNUNET_assert (NULL != socket->read_handle->proc);
689 GNUNET_assert (NULL != socket->copy_buffer);
690 GNUNET_assert (0 != socket->copy_buffer_size);
692 valid_read_size = socket->copy_buffer_size - socket->copy_buffer_read_offset;
693 GNUNET_assert (0 != valid_read_size);
695 /* Cancel the read_io_timeout_task */
696 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task);
697 /* Call the data processor */
698 read_size = socket->read_handle->proc (socket->read_handle->proc_cls,
701 + socket->copy_buffer_read_offset,
704 GNUNET_assert (read_size <= valid_read_size);
705 socket->copy_buffer_read_offset += read_size;
707 /* Free the copy buffer once it has been read entirely */
708 if (socket->copy_buffer_read_offset == socket->copy_buffer_size)
710 GNUNET_free (socket->copy_buffer);
711 socket->copy_buffer = NULL;
712 socket->copy_buffer_size = 0;
713 socket->copy_buffer_read_offset = 0;
716 /* Free the read handle */
717 GNUNET_free (socket->read_handle);
718 socket->read_handle = NULL;
723 * Prepares the receive buffer for possible reads; Should only be called when
724 * there is a valid READ io request pending and socket->copy_buffer is empty
726 * @param socket the socket pointer
729 prepare_buffer_for_read (struct GNUNET_STREAM_Socket *socket)
732 uint32_t offset_increase;
733 uint32_t sequence_increase;
735 GNUNET_assert (NULL == socket->copy_buffer);
736 GNUNET_assert (NULL != socket->read_handle);
738 /* Check the bitmap for any holes */
739 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
741 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
746 sequence_increase = packet;
748 if (0 == sequence_increase) /* The first packet is still missing */
753 /* Copy data to copy buffer */
754 GNUNET_assert (0 < socket->receive_buffer_boundaries[sequence_increase-1]);
755 socket->copy_buffer =
756 GNUNET_malloc (socket->receive_buffer_boundaries[sequence_increase-1]);
757 memcpy (socket->copy_buffer,
758 socket->receive_buffer,
759 socket->receive_buffer_boundaries[sequence_increase-1]);
761 /* Shift the data in the receive buffer */
762 memmove (socket->receive_buffer,
763 socket->receive_buffer
764 + socket->receive_buffer_boundaries[sequence_increase-1],
765 socket->receive_buffer_size - socket->receive_buffer_boundaries[sequence_increase-1]);
767 /* Shift the bitmap */
768 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
770 /* Set read_sequence_number */
771 socket->read_sequence_number += sequence_increase;
773 /* Set read_offset */
774 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
775 socket->read_offset += offset_increase;
777 /* Fix relative boundaries */
778 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
780 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
782 socket->receive_buffer_boundaries[packet] =
783 socket->receive_buffer_boundaries[packet + sequence_increase]
787 socket->receive_buffer_boundaries[packet] = 0;
790 GNUNET_SCHEDULER_add_continuation (&call_read_processor_task,
792 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
798 * Cancels the existing read io handle
800 * @param cls the closure from the SCHEDULER call
801 * @param tc the task context
804 cancel_read_io (void *cls,
805 const struct GNUNET_SCHEDULER_TaskContext *tc)
807 struct GNUNET_STREAM_Socket *socket = cls;
809 GNUNET_assert (NULL != socket->read_handle);
811 GNUNET_free (socket->read_handle);
812 socket->read_handle = NULL;
817 * Handler for DATA messages; Same for both client and server
819 * @param socket the socket through which the ack was received
820 * @param tunnel connection to the other end
821 * @param sender who sent the message
822 * @param msg the data message
823 * @param atsi performance data for the connection
824 * @return GNUNET_OK to keep the connection open,
825 * GNUNET_SYSERR to close it (signal serious error)
828 handle_data (struct GNUNET_STREAM_Socket *socket,
829 struct GNUNET_MESH_Tunnel *tunnel,
830 const struct GNUNET_PeerIdentity *sender,
831 const struct GNUNET_STREAM_DataMessage *msg,
832 const struct GNUNET_ATS_Information*atsi)
835 uint32_t bytes_needed;
836 uint32_t relative_offset;
837 uint32_t relative_sequence_number;
840 size = htons (msg->header.header.size);
841 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
844 return GNUNET_SYSERR;
847 switch (socket->state)
849 case STATE_ESTABLISHED:
850 case STATE_TRANSMIT_CLOSED:
851 case STATE_TRANSMIT_CLOSE_WAIT:
853 /* check if the message's sequence number is in the range we are
855 relative_sequence_number =
856 ntohl (msg->sequence_number) - socket->read_sequence_number;
857 if ( relative_sequence_number > 64)
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Ignoring received message with sequence number %d",
861 ntohl (msg->sequence_number));
865 /* Check if we have to allocate the buffer */
866 size -= sizeof (struct GNUNET_STREAM_DataMessage);
867 relative_offset = ntohl (msg->offset) - socket->read_offset;
868 bytes_needed = relative_offset + size;
870 if (bytes_needed > socket->receive_buffer_size)
872 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
874 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
876 socket->receive_buffer_size = bytes_needed;
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Cannot accommodate packet %d as buffer is full\n",
882 ntohl (msg->sequence_number));
887 /* Copy Data to buffer */
889 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
890 memcpy (socket->receive_buffer + relative_offset,
893 socket->receive_buffer_boundaries[relative_sequence_number] =
894 relative_offset + size;
896 /* Modify the ACK bitmap */
897 ackbitmap_modify_bit (&socket->ack_bitmap,
898 relative_sequence_number,
901 /* Start ACK sending task if one is not already present */
902 if (0 == socket->ack_task_id)
904 socket->ack_task_id =
905 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
911 if ((NULL != socket->read_handle) /* A read handle is waiting */
912 && (NULL == socket->copy_buffer)) /* And the copy buffer is empty */
914 prepare_buffer_for_read (socket);
920 /* FIXME: call statistics */
927 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
929 * @param cls the socket (set from GNUNET_MESH_connect)
930 * @param tunnel connection to the other end
931 * @param tunnel_ctx place to store local state associated with the tunnel
932 * @param sender who sent the message
933 * @param message the actual message
934 * @param atsi performance data for the connection
935 * @return GNUNET_OK to keep the connection open,
936 * GNUNET_SYSERR to close it (signal serious error)
939 client_handle_data (void *cls,
940 struct GNUNET_MESH_Tunnel *tunnel,
942 const struct GNUNET_PeerIdentity *sender,
943 const struct GNUNET_MessageHeader *message,
944 const struct GNUNET_ATS_Information*atsi)
946 struct GNUNET_STREAM_Socket *socket = cls;
948 return handle_data (socket,
951 (const struct GNUNET_STREAM_DataMessage *) message,
957 * Callback to set state to ESTABLISHED
959 * @param cls the closure from queue_message FIXME: document
960 * @param socket the socket to requiring state change
963 set_state_established (void *cls,
964 struct GNUNET_STREAM_Socket *socket)
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attaining ESTABLISHED state\n");
967 socket->write_offset = 0;
968 socket->read_offset = 0;
969 socket->state = STATE_ESTABLISHED;
974 * Callback to set state to HELLO_WAIT
976 * @param cls the closure from queue_message
977 * @param socket the socket to requiring state change
980 set_state_hello_wait (void *cls,
981 struct GNUNET_STREAM_Socket *socket)
983 GNUNET_assert (STATE_INIT == socket->state);
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attaining HELLO_WAIT state\n");
985 socket->state = STATE_HELLO_WAIT;
990 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
992 * @param cls the socket (set from GNUNET_MESH_connect)
993 * @param tunnel connection to the other end
994 * @param tunnel_ctx this is NULL
995 * @param sender who sent the message
996 * @param message the actual message
997 * @param atsi performance data for the connection
998 * @return GNUNET_OK to keep the connection open,
999 * GNUNET_SYSERR to close it (signal serious error)
1002 client_handle_hello_ack (void *cls,
1003 struct GNUNET_MESH_Tunnel *tunnel,
1005 const struct GNUNET_PeerIdentity *sender,
1006 const struct GNUNET_MessageHeader *message,
1007 const struct GNUNET_ATS_Information*atsi)
1009 struct GNUNET_STREAM_Socket *socket = cls;
1010 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
1011 struct GNUNET_STREAM_HelloAckMessage *reply;
1013 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
1014 GNUNET_assert (socket->tunnel == tunnel);
1015 switch (socket->state)
1017 case STATE_HELLO_WAIT:
1018 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1019 socket->receive_window_available = ntohl (ack_msg->receive_window_size);
1020 /* Get the random sequence number */
1021 socket->write_sequence_number =
1022 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1024 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1025 reply->header.header.size =
1026 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1027 reply->header.header.type =
1028 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1029 reply->sequence_number = htonl (socket->write_sequence_number);
1030 reply->receive_window_size = htonl (RECEIVE_BUFFER_SIZE);
1031 queue_message (socket,
1033 &set_state_established,
1036 case STATE_ESTABLISHED:
1037 case STATE_RECEIVE_CLOSE_WAIT:
1038 // call statistics (# ACKs ignored++)
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "Server sent HELLO_ACK when in state %d\n", socket->state);
1044 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1045 return GNUNET_SYSERR;
1052 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1054 * @param cls the socket (set from GNUNET_MESH_connect)
1055 * @param tunnel connection to the other end
1056 * @param tunnel_ctx this is NULL
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 client_handle_reset (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 = cls;
1078 * Common message handler for handling TRANSMIT_CLOSE messages
1080 * @param socket the socket through which the ack was received
1081 * @param tunnel connection to the other end
1082 * @param sender who sent the message
1083 * @param msg the transmit close message
1084 * @param atsi performance data for the connection
1085 * @return GNUNET_OK to keep the connection open,
1086 * GNUNET_SYSERR to close it (signal serious error)
1089 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1090 struct GNUNET_MESH_Tunnel *tunnel,
1091 const struct GNUNET_PeerIdentity *sender,
1092 const struct GNUNET_STREAM_MessageHeader *msg,
1093 const struct GNUNET_ATS_Information*atsi)
1095 struct GNUNET_STREAM_MessageHeader *reply;
1097 switch (socket->state)
1099 case STATE_ESTABLISHED:
1100 socket->state = STATE_RECEIVE_CLOSED;
1102 /* Send TRANSMIT_CLOSE_ACK */
1103 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1104 reply->header.type =
1105 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1106 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1107 queue_message (socket, reply, NULL, NULL);
1111 /* FIXME: Call statistics? */
1119 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1121 * @param cls the socket (set from GNUNET_MESH_connect)
1122 * @param tunnel connection to the other end
1123 * @param tunnel_ctx this is NULL
1124 * @param sender who sent the message
1125 * @param message the actual message
1126 * @param atsi performance data for the connection
1127 * @return GNUNET_OK to keep the connection open,
1128 * GNUNET_SYSERR to close it (signal serious error)
1131 client_handle_transmit_close (void *cls,
1132 struct GNUNET_MESH_Tunnel *tunnel,
1134 const struct GNUNET_PeerIdentity *sender,
1135 const struct GNUNET_MessageHeader *message,
1136 const struct GNUNET_ATS_Information*atsi)
1138 struct GNUNET_STREAM_Socket *socket = cls;
1140 return handle_transmit_close (socket,
1143 (struct GNUNET_STREAM_MessageHeader *)message,
1149 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1151 * @param cls the socket (set from GNUNET_MESH_connect)
1152 * @param tunnel connection to the other end
1153 * @param tunnel_ctx this is NULL
1154 * @param sender who sent the message
1155 * @param message the actual message
1156 * @param atsi performance data for the connection
1157 * @return GNUNET_OK to keep the connection open,
1158 * GNUNET_SYSERR to close it (signal serious error)
1161 client_handle_transmit_close_ack (void *cls,
1162 struct GNUNET_MESH_Tunnel *tunnel,
1164 const struct GNUNET_PeerIdentity *sender,
1165 const struct GNUNET_MessageHeader *message,
1166 const struct GNUNET_ATS_Information*atsi)
1168 struct GNUNET_STREAM_Socket *socket = cls;
1175 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1177 * @param cls the socket (set from GNUNET_MESH_connect)
1178 * @param tunnel connection to the other end
1179 * @param tunnel_ctx this is NULL
1180 * @param sender who sent the message
1181 * @param message the actual message
1182 * @param atsi performance data for the connection
1183 * @return GNUNET_OK to keep the connection open,
1184 * GNUNET_SYSERR to close it (signal serious error)
1187 client_handle_receive_close (void *cls,
1188 struct GNUNET_MESH_Tunnel *tunnel,
1190 const struct GNUNET_PeerIdentity *sender,
1191 const struct GNUNET_MessageHeader *message,
1192 const struct GNUNET_ATS_Information*atsi)
1194 struct GNUNET_STREAM_Socket *socket = cls;
1201 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1203 * @param cls the socket (set from GNUNET_MESH_connect)
1204 * @param tunnel connection to the other end
1205 * @param tunnel_ctx this is NULL
1206 * @param sender who sent the message
1207 * @param message the actual message
1208 * @param atsi performance data for the connection
1209 * @return GNUNET_OK to keep the connection open,
1210 * GNUNET_SYSERR to close it (signal serious error)
1213 client_handle_receive_close_ack (void *cls,
1214 struct GNUNET_MESH_Tunnel *tunnel,
1216 const struct GNUNET_PeerIdentity *sender,
1217 const struct GNUNET_MessageHeader *message,
1218 const struct GNUNET_ATS_Information*atsi)
1220 struct GNUNET_STREAM_Socket *socket = cls;
1227 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1229 * @param cls the socket (set from GNUNET_MESH_connect)
1230 * @param tunnel connection to the other end
1231 * @param tunnel_ctx this is NULL
1232 * @param sender who sent the message
1233 * @param message the actual message
1234 * @param atsi performance data for the connection
1235 * @return GNUNET_OK to keep the connection open,
1236 * GNUNET_SYSERR to close it (signal serious error)
1239 client_handle_close (void *cls,
1240 struct GNUNET_MESH_Tunnel *tunnel,
1242 const struct GNUNET_PeerIdentity *sender,
1243 const struct GNUNET_MessageHeader *message,
1244 const struct GNUNET_ATS_Information*atsi)
1246 struct GNUNET_STREAM_Socket *socket = cls;
1253 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1255 * @param cls the socket (set from GNUNET_MESH_connect)
1256 * @param tunnel connection to the other end
1257 * @param tunnel_ctx this is NULL
1258 * @param sender who sent the message
1259 * @param message the actual message
1260 * @param atsi performance data for the connection
1261 * @return GNUNET_OK to keep the connection open,
1262 * GNUNET_SYSERR to close it (signal serious error)
1265 client_handle_close_ack (void *cls,
1266 struct GNUNET_MESH_Tunnel *tunnel,
1268 const struct GNUNET_PeerIdentity *sender,
1269 const struct GNUNET_MessageHeader *message,
1270 const struct GNUNET_ATS_Information*atsi)
1272 struct GNUNET_STREAM_Socket *socket = cls;
1277 /*****************************/
1278 /* Server's Message Handlers */
1279 /*****************************/
1282 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1284 * @param cls the closure
1285 * @param tunnel connection to the other end
1286 * @param tunnel_ctx the socket
1287 * @param sender who sent the message
1288 * @param message the actual message
1289 * @param atsi performance data for the connection
1290 * @return GNUNET_OK to keep the connection open,
1291 * GNUNET_SYSERR to close it (signal serious error)
1294 server_handle_data (void *cls,
1295 struct GNUNET_MESH_Tunnel *tunnel,
1297 const struct GNUNET_PeerIdentity *sender,
1298 const struct GNUNET_MessageHeader *message,
1299 const struct GNUNET_ATS_Information*atsi)
1301 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1303 return handle_data (socket,
1306 (const struct GNUNET_STREAM_DataMessage *)message,
1312 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1314 * @param cls the closure
1315 * @param tunnel connection to the other end
1316 * @param tunnel_ctx the socket
1317 * @param sender who sent the message
1318 * @param message the actual message
1319 * @param atsi performance data for the connection
1320 * @return GNUNET_OK to keep the connection open,
1321 * GNUNET_SYSERR to close it (signal serious error)
1324 server_handle_hello (void *cls,
1325 struct GNUNET_MESH_Tunnel *tunnel,
1327 const struct GNUNET_PeerIdentity *sender,
1328 const struct GNUNET_MessageHeader *message,
1329 const struct GNUNET_ATS_Information*atsi)
1331 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1332 struct GNUNET_STREAM_HelloAckMessage *reply;
1334 GNUNET_assert (socket->tunnel == tunnel);
1335 if (STATE_INIT == socket->state)
1337 /* Get the random sequence number */
1338 socket->write_sequence_number =
1339 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1341 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1342 reply->header.header.size =
1343 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1344 reply->header.header.type =
1345 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1346 reply->sequence_number = htonl (socket->write_sequence_number);
1347 queue_message (socket,
1349 &set_state_hello_wait,
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 "Client sent HELLO when in state %d\n", socket->state);
1356 /* FIXME: Send RESET? */
1364 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1366 * @param cls the closure
1367 * @param tunnel connection to the other end
1368 * @param tunnel_ctx the socket
1369 * @param sender who sent the message
1370 * @param message the actual message
1371 * @param atsi performance data for the connection
1372 * @return GNUNET_OK to keep the connection open,
1373 * GNUNET_SYSERR to close it (signal serious error)
1376 server_handle_hello_ack (void *cls,
1377 struct GNUNET_MESH_Tunnel *tunnel,
1379 const struct GNUNET_PeerIdentity *sender,
1380 const struct GNUNET_MessageHeader *message,
1381 const struct GNUNET_ATS_Information*atsi)
1383 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1384 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1386 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1387 GNUNET_assert (socket->tunnel == tunnel);
1388 if (STATE_HELLO_WAIT == socket->state)
1390 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1391 socket->receive_window_available =
1392 ntohl (ack_message->receive_window_size);
1393 /* Attain ESTABLISHED state */
1394 set_state_established (NULL, socket);
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399 "Client sent HELLO_ACK when in state %d\n", socket->state);
1400 /* FIXME: Send RESET? */
1408 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1410 * @param cls the closure
1411 * @param tunnel connection to the other end
1412 * @param tunnel_ctx the socket
1413 * @param sender who sent the message
1414 * @param message the actual message
1415 * @param atsi performance data for the connection
1416 * @return GNUNET_OK to keep the connection open,
1417 * GNUNET_SYSERR to close it (signal serious error)
1420 server_handle_reset (void *cls,
1421 struct GNUNET_MESH_Tunnel *tunnel,
1423 const struct GNUNET_PeerIdentity *sender,
1424 const struct GNUNET_MessageHeader *message,
1425 const struct GNUNET_ATS_Information*atsi)
1427 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1434 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1436 * @param cls the closure
1437 * @param tunnel connection to the other end
1438 * @param tunnel_ctx the socket
1439 * @param sender who sent the message
1440 * @param message the actual message
1441 * @param atsi performance data for the connection
1442 * @return GNUNET_OK to keep the connection open,
1443 * GNUNET_SYSERR to close it (signal serious error)
1446 server_handle_transmit_close (void *cls,
1447 struct GNUNET_MESH_Tunnel *tunnel,
1449 const struct GNUNET_PeerIdentity *sender,
1450 const struct GNUNET_MessageHeader *message,
1451 const struct GNUNET_ATS_Information*atsi)
1453 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1455 return handle_transmit_close (socket,
1458 (struct GNUNET_STREAM_MessageHeader *)message,
1464 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1466 * @param cls the closure
1467 * @param tunnel connection to the other end
1468 * @param tunnel_ctx the socket
1469 * @param sender who sent the message
1470 * @param message the actual message
1471 * @param atsi performance data for the connection
1472 * @return GNUNET_OK to keep the connection open,
1473 * GNUNET_SYSERR to close it (signal serious error)
1476 server_handle_transmit_close_ack (void *cls,
1477 struct GNUNET_MESH_Tunnel *tunnel,
1479 const struct GNUNET_PeerIdentity *sender,
1480 const struct GNUNET_MessageHeader *message,
1481 const struct GNUNET_ATS_Information*atsi)
1483 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1490 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1492 * @param cls the closure
1493 * @param tunnel connection to the other end
1494 * @param tunnel_ctx the socket
1495 * @param sender who sent the message
1496 * @param message the actual message
1497 * @param atsi performance data for the connection
1498 * @return GNUNET_OK to keep the connection open,
1499 * GNUNET_SYSERR to close it (signal serious error)
1502 server_handle_receive_close (void *cls,
1503 struct GNUNET_MESH_Tunnel *tunnel,
1505 const struct GNUNET_PeerIdentity *sender,
1506 const struct GNUNET_MessageHeader *message,
1507 const struct GNUNET_ATS_Information*atsi)
1509 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1516 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1518 * @param cls the closure
1519 * @param tunnel connection to the other end
1520 * @param tunnel_ctx the socket
1521 * @param sender who sent the message
1522 * @param message the actual message
1523 * @param atsi performance data for the connection
1524 * @return GNUNET_OK to keep the connection open,
1525 * GNUNET_SYSERR to close it (signal serious error)
1528 server_handle_receive_close_ack (void *cls,
1529 struct GNUNET_MESH_Tunnel *tunnel,
1531 const struct GNUNET_PeerIdentity *sender,
1532 const struct GNUNET_MessageHeader *message,
1533 const struct GNUNET_ATS_Information*atsi)
1535 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1542 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1544 * @param cls the closure
1545 * @param tunnel connection to the other end
1546 * @param tunnel_ctx the socket
1547 * @param sender who sent the message
1548 * @param message the actual message
1549 * @param atsi performance data for the connection
1550 * @return GNUNET_OK to keep the connection open,
1551 * GNUNET_SYSERR to close it (signal serious error)
1554 server_handle_close (void *cls,
1555 struct GNUNET_MESH_Tunnel *tunnel,
1557 const struct GNUNET_PeerIdentity *sender,
1558 const struct GNUNET_MessageHeader *message,
1559 const struct GNUNET_ATS_Information*atsi)
1561 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1568 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1570 * @param cls the closure
1571 * @param tunnel connection to the other end
1572 * @param tunnel_ctx the socket
1573 * @param sender who sent the message
1574 * @param message the actual message
1575 * @param atsi performance data for the connection
1576 * @return GNUNET_OK to keep the connection open,
1577 * GNUNET_SYSERR to close it (signal serious error)
1580 server_handle_close_ack (void *cls,
1581 struct GNUNET_MESH_Tunnel *tunnel,
1583 const struct GNUNET_PeerIdentity *sender,
1584 const struct GNUNET_MessageHeader *message,
1585 const struct GNUNET_ATS_Information*atsi)
1587 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1594 * Message Handler for mesh
1596 * @param socket the socket through which the ack was received
1597 * @param tunnel connection to the other end
1598 * @param sender who sent the message
1599 * @param ack the acknowledgment message
1600 * @param atsi performance data for the connection
1601 * @return GNUNET_OK to keep the connection open,
1602 * GNUNET_SYSERR to close it (signal serious error)
1605 handle_ack (struct GNUNET_STREAM_Socket *socket,
1606 struct GNUNET_MESH_Tunnel *tunnel,
1607 const struct GNUNET_PeerIdentity *sender,
1608 const struct GNUNET_STREAM_AckMessage *ack,
1609 const struct GNUNET_ATS_Information*atsi)
1611 switch (socket->state)
1613 case (STATE_ESTABLISHED):
1614 if (NULL == socket->write_handle)
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1617 "Received DATA ACK when write_handle is NULL\n");
1621 socket->write_handle->ack_bitmap = GNUNET_ntohll (ack->bitmap);
1622 socket->receive_window_available =
1623 ntohl (ack->receive_window_remaining);
1624 write_data (socket);
1634 * Message Handler for mesh
1636 * @param cls the 'struct GNUNET_STREAM_Socket'
1637 * @param tunnel connection to the other end
1638 * @param tunnel_ctx unused
1639 * @param sender who sent the message
1640 * @param message the actual message
1641 * @param atsi performance data for the connection
1642 * @return GNUNET_OK to keep the connection open,
1643 * GNUNET_SYSERR to close it (signal serious error)
1646 client_handle_ack (void *cls,
1647 struct GNUNET_MESH_Tunnel *tunnel,
1649 const struct GNUNET_PeerIdentity *sender,
1650 const struct GNUNET_MessageHeader *message,
1651 const struct GNUNET_ATS_Information*atsi)
1653 struct GNUNET_STREAM_Socket *socket = cls;
1654 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1656 return handle_ack (socket, tunnel, sender, ack, atsi);
1661 * Message Handler for mesh
1663 * @param cls the server's listen socket
1664 * @param tunnel connection to the other end
1665 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
1666 * @param sender who sent the message
1667 * @param message the actual message
1668 * @param atsi performance data for the connection
1669 * @return GNUNET_OK to keep the connection open,
1670 * GNUNET_SYSERR to close it (signal serious error)
1673 server_handle_ack (void *cls,
1674 struct GNUNET_MESH_Tunnel *tunnel,
1676 const struct GNUNET_PeerIdentity *sender,
1677 const struct GNUNET_MessageHeader *message,
1678 const struct GNUNET_ATS_Information*atsi)
1680 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1681 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1683 return handle_ack (socket, tunnel, sender, ack, atsi);
1688 * For client message handlers, the stream socket is in the
1691 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
1692 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1693 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1694 sizeof (struct GNUNET_STREAM_AckMessage) },
1695 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1696 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1697 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1698 sizeof (struct GNUNET_STREAM_MessageHeader)},
1699 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1700 sizeof (struct GNUNET_STREAM_MessageHeader)},
1701 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1702 sizeof (struct GNUNET_STREAM_MessageHeader)},
1703 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1704 sizeof (struct GNUNET_STREAM_MessageHeader)},
1705 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1706 sizeof (struct GNUNET_STREAM_MessageHeader)},
1707 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1708 sizeof (struct GNUNET_STREAM_MessageHeader)},
1709 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1710 sizeof (struct GNUNET_STREAM_MessageHeader)},
1716 * For server message handlers, the stream socket is in the
1717 * tunnel context, and the listen socket in the closure argument.
1719 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
1720 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1721 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1722 sizeof (struct GNUNET_STREAM_AckMessage) },
1723 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
1724 sizeof (struct GNUNET_STREAM_MessageHeader)},
1725 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1726 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1727 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1728 sizeof (struct GNUNET_STREAM_MessageHeader)},
1729 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1730 sizeof (struct GNUNET_STREAM_MessageHeader)},
1731 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1732 sizeof (struct GNUNET_STREAM_MessageHeader)},
1733 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1734 sizeof (struct GNUNET_STREAM_MessageHeader)},
1735 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1736 sizeof (struct GNUNET_STREAM_MessageHeader)},
1737 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1738 sizeof (struct GNUNET_STREAM_MessageHeader)},
1739 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1740 sizeof (struct GNUNET_STREAM_MessageHeader)},
1746 * Function called when our target peer is connected to our tunnel
1748 * @param cls the socket for which this tunnel is created
1749 * @param peer the peer identity of the target
1750 * @param atsi performance data for the connection
1753 mesh_peer_connect_callback (void *cls,
1754 const struct GNUNET_PeerIdentity *peer,
1755 const struct GNUNET_ATS_Information * atsi)
1757 struct GNUNET_STREAM_Socket *socket = cls;
1758 struct GNUNET_STREAM_MessageHeader *message;
1760 if (0 != memcmp (&socket->other_peer,
1762 sizeof (struct GNUNET_PeerIdentity)))
1764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1765 "A peer (%s) which is not our target has connected to our tunnel",
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 "Target peer %s connected\n", GNUNET_i2s (peer));
1773 /* Set state to INIT */
1774 socket->state = STATE_INIT;
1776 /* Send HELLO message */
1777 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1778 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
1779 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1780 queue_message (socket,
1782 &set_state_hello_wait,
1785 /* Call open callback */
1786 if (NULL == socket->open_cb)
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 "STREAM_open callback is NULL\n");
1793 socket->open_cb (socket->open_cls, socket);
1799 * Function called when our target peer is disconnected from our tunnel
1801 * @param cls the socket associated which this tunnel
1802 * @param peer the peer identity of the target
1805 mesh_peer_disconnect_callback (void *cls,
1806 const struct GNUNET_PeerIdentity *peer)
1818 * Tries to open a stream to the target peer
1820 * @param cfg configuration to use
1821 * @param target the target peer to which the stream has to be opened
1822 * @param app_port the application port number which uniquely identifies this
1824 * @param open_cb this function will be called after stream has be established
1825 * @param open_cb_cls the closure for open_cb
1826 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
1827 * @return if successful it returns the stream socket; NULL if stream cannot be
1830 struct GNUNET_STREAM_Socket *
1831 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
1832 const struct GNUNET_PeerIdentity *target,
1833 GNUNET_MESH_ApplicationType app_port,
1834 GNUNET_STREAM_OpenCallback open_cb,
1838 struct GNUNET_STREAM_Socket *socket;
1839 enum GNUNET_STREAM_Option option;
1840 va_list vargs; /* Variable arguments */
1842 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
1843 socket->other_peer = *target;
1844 socket->open_cb = open_cb;
1845 socket->open_cls = open_cb_cls;
1848 socket->retransmit_timeout =
1849 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
1851 va_start (vargs, open_cb_cls); /* Parse variable args */
1853 option = va_arg (vargs, enum GNUNET_STREAM_Option);
1856 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
1857 /* Expect struct GNUNET_TIME_Relative */
1858 socket->retransmit_timeout = va_arg (vargs,
1859 struct GNUNET_TIME_Relative);
1861 case GNUNET_STREAM_OPTION_END:
1864 } while (GNUNET_STREAM_OPTION_END != option);
1865 va_end (vargs); /* End of variable args parsing */
1867 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
1868 1, /* QUEUE size as parameter? */
1870 NULL, /* No inbound tunnel handler */
1871 NULL, /* No inbound tunnel cleaner */
1872 client_message_handlers,
1873 NULL); /* We don't get inbound tunnels */
1874 // FIXME: if (NULL == socket->mesh) ...
1876 /* Now create the mesh tunnel to target */
1877 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
1878 NULL, /* Tunnel context */
1879 &mesh_peer_connect_callback,
1880 &mesh_peer_disconnect_callback,
1882 // FIXME: if (NULL == socket->tunnel) ...
1891 * @param socket the stream socket
1894 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
1896 struct MessageQueue *head;
1898 /* Clear Transmit handles */
1899 if (NULL != socket->transmit_handle)
1901 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
1902 socket->transmit_handle = NULL;
1905 /* Clear existing message queue */
1906 while (NULL != (head = socket->queue_head)) {
1907 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
1910 GNUNET_free (head->message);
1914 /* Close associated tunnel */
1915 if (NULL != socket->tunnel)
1917 GNUNET_MESH_tunnel_destroy (socket->tunnel);
1918 socket->tunnel = NULL;
1921 /* Close mesh connection */
1922 if (NULL != socket->mesh)
1924 GNUNET_MESH_disconnect (socket->mesh);
1925 socket->mesh = NULL;
1928 /* Release receive buffer */
1929 if (NULL != socket->receive_buffer)
1931 GNUNET_free (socket->receive_buffer);
1934 GNUNET_free (socket);
1939 * Method called whenever a peer creates a tunnel to us
1941 * @param cls closure
1942 * @param tunnel new handle to the tunnel
1943 * @param initiator peer that started the tunnel
1944 * @param atsi performance information for the tunnel
1945 * @return initial tunnel context for the tunnel
1946 * (can be NULL -- that's not an error)
1949 new_tunnel_notify (void *cls,
1950 struct GNUNET_MESH_Tunnel *tunnel,
1951 const struct GNUNET_PeerIdentity *initiator,
1952 const struct GNUNET_ATS_Information *atsi)
1954 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
1955 struct GNUNET_STREAM_Socket *socket;
1957 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
1958 socket->tunnel = tunnel;
1959 socket->session_id = 0; /* FIXME */
1960 socket->other_peer = *initiator;
1961 socket->state = STATE_INIT;
1963 if (GNUNET_SYSERR == lsocket->listen_cb (lsocket->listen_cb_cls,
1965 &socket->other_peer))
1967 socket->state = STATE_CLOSED;
1968 /* FIXME: Send CLOSE message and then free */
1969 GNUNET_free (socket);
1970 GNUNET_MESH_tunnel_destroy (tunnel); /* Destroy the tunnel */
1977 * Function called whenever an inbound tunnel is destroyed. Should clean up
1978 * any associated state. This function is NOT called if the client has
1979 * explicitly asked for the tunnel to be destroyed using
1980 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
1983 * @param cls closure (set from GNUNET_MESH_connect)
1984 * @param tunnel connection to the other end (henceforth invalid)
1985 * @param tunnel_ctx place where local state associated
1986 * with the tunnel is stored
1989 tunnel_cleaner (void *cls,
1990 const struct GNUNET_MESH_Tunnel *tunnel,
1993 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
1995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1996 "Peer %s has terminated connection abruptly\n",
1997 GNUNET_i2s (&socket->other_peer));
1999 socket->status = GNUNET_STREAM_SHUTDOWN;
2000 /* Clear Transmit handles */
2001 if (NULL != socket->transmit_handle)
2003 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
2004 socket->transmit_handle = NULL;
2006 socket->tunnel = NULL;
2011 * Listens for stream connections for a specific application ports
2013 * @param cfg the configuration to use
2014 * @param app_port the application port for which new streams will be accepted
2015 * @param listen_cb this function will be called when a peer tries to establish
2017 * @param listen_cb_cls closure for listen_cb
2018 * @return listen socket, NULL for any error
2020 struct GNUNET_STREAM_ListenSocket *
2021 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
2022 GNUNET_MESH_ApplicationType app_port,
2023 GNUNET_STREAM_ListenCallback listen_cb,
2024 void *listen_cb_cls)
2026 /* FIXME: Add variable args for passing configration options? */
2027 struct GNUNET_STREAM_ListenSocket *lsocket;
2028 GNUNET_MESH_ApplicationType app_types[2];
2030 app_types[0] = app_port;
2032 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
2033 lsocket->port = app_port;
2034 lsocket->listen_cb = listen_cb;
2035 lsocket->listen_cb_cls = listen_cb_cls;
2036 lsocket->mesh = GNUNET_MESH_connect (cfg,
2037 10, /* FIXME: QUEUE size as parameter? */
2038 lsocket, /* Closure */
2041 server_message_handlers,
2048 * Closes the listen socket
2050 * @param lsocket the listen socket
2053 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
2055 /* Close MESH connection */
2056 GNUNET_MESH_disconnect (lsocket->mesh);
2058 GNUNET_free (lsocket);
2063 * Tries to write the given data to the stream
2065 * @param socket the socket representing a stream
2066 * @param data the data buffer from where the data is written into the stream
2067 * @param size the number of bytes to be written from the data buffer
2068 * @param timeout the timeout period
2069 * @param write_cont the function to call upon writing some bytes into the stream
2070 * @param write_cont_cls the closure
2071 * @return handle to cancel the operation
2073 struct GNUNET_STREAM_IOWriteHandle *
2074 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
2077 struct GNUNET_TIME_Relative timeout,
2078 GNUNET_STREAM_CompletionContinuation write_cont,
2079 void *write_cont_cls)
2081 unsigned int num_needed_packets;
2082 unsigned int packet;
2083 struct GNUNET_STREAM_IOWriteHandle *io_handle;
2084 uint32_t packet_size;
2085 uint32_t payload_size;
2086 struct GNUNET_STREAM_DataMessage *data_msg;
2089 /* Return NULL if there is already a write request pending */
2090 if (NULL != socket->write_handle)
2095 if (!((STATE_ESTABLISHED == socket->state)
2096 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
2097 || (STATE_RECEIVE_CLOSED == socket->state)))
2099 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2100 "Attempting to write on a closed (OR) not-yet-established"
2104 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
2105 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
2106 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
2107 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
2109 /* Divide the given buffer into packets for sending */
2110 for (packet=0; packet < num_needed_packets; packet++)
2112 if ((packet + 1) * max_payload_size < size)
2114 payload_size = max_payload_size;
2115 packet_size = MAX_PACKET_SIZE;
2119 payload_size = size - packet * max_payload_size;
2120 packet_size = payload_size + sizeof (struct
2121 GNUNET_STREAM_DataMessage);
2123 io_handle->messages[packet] = GNUNET_malloc (packet_size);
2124 io_handle->messages[packet]->header.header.size = htons (packet_size);
2125 io_handle->messages[packet]->header.header.type =
2126 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
2127 io_handle->messages[packet]->sequence_number =
2128 htons (socket->write_sequence_number++);
2129 io_handle->messages[packet]->offset = htons (socket->write_offset);
2131 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2132 determined from RTT */
2133 io_handle->messages[packet]->ack_deadline =
2134 GNUNET_TIME_relative_hton (GNUNET_TIME_relative_multiply
2135 (GNUNET_TIME_UNIT_SECONDS, 5));
2136 data_msg = io_handle->messages[packet];
2137 /* Copy data from given buffer to the packet */
2138 memcpy (&data_msg[1],
2141 sweep += payload_size;
2142 socket->write_offset += payload_size;
2144 socket->write_handle = io_handle;
2145 write_data (socket);
2152 * Tries to read data from the stream
2154 * @param socket the socket representing a stream
2155 * @param timeout the timeout period
2156 * @param proc function to call with data (once only)
2157 * @param proc_cls the closure for proc
2158 * @return handle to cancel the operation
2160 struct GNUNET_STREAM_IOReadHandle *
2161 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
2162 struct GNUNET_TIME_Relative timeout,
2163 GNUNET_STREAM_DataProcessor proc,
2166 struct GNUNET_STREAM_IOReadHandle *read_handle;
2168 /* Return NULL if there is already a read handle; the user has to cancel that
2169 first before continuing or has to wait until it is completed */
2170 if (NULL != socket->read_handle) return NULL;
2172 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
2173 read_handle->proc = proc;
2174 socket->read_handle = read_handle;
2176 /* if previous copy buffer is still not read call the data processor on it */
2177 if (NULL != socket->copy_buffer)
2178 GNUNET_SCHEDULER_add_continuation (&call_read_processor_task,
2180 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
2182 prepare_buffer_for_read (socket);
2184 socket->read_io_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,