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;
185 * Task scheduled to continue a read operation.
187 GNUNET_SCHEDULER_TaskIdentifier read_task;
192 struct GNUNET_MESH_Handle *mesh;
195 * The mesh tunnel handle
197 struct GNUNET_MESH_Tunnel *tunnel;
200 * Stream open closure
205 * Stream open callback
207 GNUNET_STREAM_OpenCallback open_cb;
210 * The current transmit handle (if a pending transmit request exists)
212 struct GNUNET_MESH_TransmitHandle *transmit_handle;
215 * The current message associated with the transmit handle
217 struct MessageQueue *queue_head;
220 * The queue tail, should always point to the last message in queue
222 struct MessageQueue *queue_tail;
225 * The write IO_handle associated with this socket
227 struct GNUNET_STREAM_IOWriteHandle *write_handle;
230 * The read IO_handle associated with this socket
232 struct GNUNET_STREAM_IOReadHandle *read_handle;
235 * Buffer for storing received messages
237 void *receive_buffer;
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 offset upto which user has read from the received buffer
304 uint32_t copy_offset;
309 * A socket for listening
311 struct GNUNET_STREAM_ListenSocket
317 struct GNUNET_MESH_Handle *mesh;
320 * The callback function which is called after successful opening socket
322 GNUNET_STREAM_ListenCallback listen_cb;
325 * The call back closure
332 GNUNET_MESH_ApplicationType port;
337 * The IO Write Handle
339 struct GNUNET_STREAM_IOWriteHandle
342 * The packet_buffers associated with this Handle
344 struct GNUNET_STREAM_DataMessage *messages[64];
347 * The bitmap of this IOHandle; Corresponding bit for a message is set when
348 * it has been acknowledged by the receiver
350 GNUNET_STREAM_AckBitmap ack_bitmap;
353 * Number of packets sent before waiting for an ack
355 * FIXME: Do we need this?
357 unsigned int sent_packets;
364 struct GNUNET_STREAM_IOReadHandle
367 * Callback for the read processor
369 GNUNET_STREAM_DataProcessor proc;
372 * The closure pointer for the read processor callback
379 * Default value in seconds for various timeouts
381 static unsigned int default_timeout = 300;
385 * Callback function for sending hello message
387 * @param cls closure the socket
388 * @param size number of bytes available in buf
389 * @param buf where the callee should write the message
390 * @return number of bytes written to buf
393 send_message_notify (void *cls, size_t size, void *buf)
395 struct GNUNET_STREAM_Socket *socket = cls;
396 struct MessageQueue *head;
399 socket->transmit_handle = NULL; /* Remove the transmit handle */
400 head = socket->queue_head;
402 return 0; /* just to be safe */
403 if (0 == size) /* request timed out */
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Message sending timed out. Retry %d \n",
409 socket->transmit_handle =
410 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
413 /* FIXME: exponential backoff */
414 socket->retransmit_timeout,
416 ntohs (head->message->header.size),
417 &send_message_notify,
422 ret = ntohs (head->message->header.size);
423 GNUNET_assert (size >= ret);
424 memcpy (buf, head->message, ret);
425 if (NULL != head->finish_cb)
427 head->finish_cb (socket, head->finish_cb_cls);
429 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
432 GNUNET_free (head->message);
434 head = socket->queue_head;
435 if (NULL != head) /* more pending messages to send */
438 socket->transmit_handle =
439 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
442 /* FIXME: exponential backoff */
443 socket->retransmit_timeout,
445 ntohs (head->message->header.size),
446 &send_message_notify,
454 * Queues a message for sending using the mesh connection of a socket
456 * @param socket the socket whose mesh connection is used
457 * @param message the message to be sent
458 * @param finish_cb the callback to be called when the message is sent
459 * @param finish_cb_cls the closure for the callback
462 queue_message (struct GNUNET_STREAM_Socket *socket,
463 struct GNUNET_STREAM_MessageHeader *message,
464 SendFinishCallback finish_cb,
467 struct MessageQueue *queue_entity;
469 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
470 queue_entity->message = message;
471 queue_entity->finish_cb = finish_cb;
472 queue_entity->finish_cb_cls = finish_cb_cls;
473 GNUNET_CONTAINER_DLL_insert_tail (socket->queue_head,
476 if (NULL == socket->transmit_handle)
479 socket->transmit_handle =
480 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
483 socket->retransmit_timeout,
485 ntohs (message->header.size),
486 &send_message_notify,
493 * Callback function for sending ack message
495 * @param cls closure the ACK message created in ack_task
496 * @param size number of bytes available in buffer
497 * @param buf where the callee should write the message
498 * @return number of bytes written to buf
501 send_ack_notify (void *cls, size_t size, void *buf)
503 struct GNUNET_STREAM_AckMessage *ack_msg = cls;
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "%s called with size 0\n", __func__);
511 GNUNET_assert (ack_msg->header.header.size <= size);
513 size = ack_msg->header.header.size;
514 memcpy (buf, ack_msg, size);
520 * Task for sending ACK message
522 * @param cls the socket
523 * @param tc the Task context
527 const struct GNUNET_SCHEDULER_TaskContext *tc)
529 struct GNUNET_STREAM_Socket *socket = cls;
530 struct GNUNET_STREAM_AckMessage *ack_msg;
532 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
537 socket->ack_task_id = 0;
539 /* Create the ACK Message */
540 ack_msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_AckMessage));
541 ack_msg->header.header.size = htons (sizeof (struct
542 GNUNET_STREAM_AckMessage));
543 ack_msg->header.header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_ACK);
544 ack_msg->bitmap = GNUNET_htonll (socket->ack_bitmap);
545 ack_msg->base_sequence_number = htonl (socket->read_sequence_number);
546 ack_msg->receive_window_remaining =
547 htonl (RECEIVE_BUFFER_SIZE - socket->receive_buffer_size);
549 /* Request MESH for sending ACK */
550 GNUNET_MESH_notify_transmit_ready (socket->tunnel,
553 socket->retransmit_timeout,
555 ntohs (ack_msg->header.header.size),
564 * Function to modify a bit in GNUNET_STREAM_AckBitmap
566 * @param bitmap the bitmap to modify
567 * @param bit the bit number to modify
568 * @param value GNUNET_YES to on, GNUNET_NO to off
571 ackbitmap_modify_bit (GNUNET_STREAM_AckBitmap *bitmap,
575 GNUNET_assert (bit < 64);
576 if (GNUNET_YES == value)
577 *bitmap |= (1LL << bit);
579 *bitmap &= ~(1LL << bit);
584 * Function to check if a bit is set in the GNUNET_STREAM_AckBitmap
586 * @param bitmap address of the bitmap that has to be checked
587 * @param bit the bit number to check
588 * @return GNUNET_YES if the bit is set; GNUNET_NO if not
591 ackbitmap_is_bit_set (const GNUNET_STREAM_AckBitmap *bitmap,
594 GNUNET_assert (bit < 64);
595 return 0 != (*bitmap & (1LL << bit));
601 * Function called when Data Message is sent
603 * @param cls the io_handle corresponding to the Data Message
604 * @param socket the socket which was used
607 write_data_finish_cb (void *cls,
608 struct GNUNET_STREAM_Socket *socket)
610 struct GNUNET_STREAM_IOWriteHandle *io_handle = cls;
612 io_handle->sent_packets++;
617 * Writes data using the given socket. The amount of data written is limited by
618 * the receive_window_size
620 * @param socket the socket to use
623 write_data (struct GNUNET_STREAM_Socket *socket)
625 struct GNUNET_STREAM_IOWriteHandle *io_handle = socket->write_handle;
630 /* Find the last acknowledged packet */
631 for (packet=0; packet < 64; packet++)
633 if (GNUNET_YES == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
636 else if (NULL == io_handle->messages[packet])
639 /* Resend packets which weren't ack'ed */
640 for (packet=0; packet < ack_packet; packet++)
642 if (GNUNET_NO == ackbitmap_is_bit_set (&io_handle->ack_bitmap,
645 queue_message (socket,
646 &io_handle->messages[packet]->header,
651 packet = ack_packet + 1;
652 /* Now send new packets if there is enough buffer space */
653 while ( (NULL != io_handle->messages[packet]) &&
654 (socket->receive_window_available >= ntohs (io_handle->messages[packet]->header.header.size)) )
656 socket->receive_window_available -= ntohs (io_handle->messages[packet]->header.header.size);
657 queue_message (socket,
658 &io_handle->messages[packet]->header,
659 &write_data_finish_cb,
667 * Task for calling the read processor
669 * @param cls the socket
670 * @param tc the task context
673 call_read_processor (void *cls,
674 const struct GNUNET_SCHEDULER_TaskContext *tc)
676 struct GNUNET_STREAM_Socket *socket = cls;
678 size_t valid_read_size;
680 uint32_t sequence_increase;
681 uint32_t offset_increase;
683 socket->read_task = GNUNET_SCHEDULER_NO_TASK;
684 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
687 GNUNET_assert (NULL != socket->read_handle);
688 GNUNET_assert (NULL != socket->read_handle->proc);
690 /* Check the bitmap for any holes */
691 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
693 if (GNUNET_NO == ackbitmap_is_bit_set (&socket->ack_bitmap,
697 /* We only call read processor if we have the first packet */
698 GNUNET_assert (0 < packet);
701 socket->receive_buffer_boundaries[packet-1] - socket->copy_offset;
703 GNUNET_assert (0 != valid_read_size);
705 /* Cancel the read_io_timeout_task */
706 GNUNET_SCHEDULER_cancel (socket->read_io_timeout_task);
707 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
709 /* Call the data processor */
711 socket->read_handle->proc (socket->read_handle->proc_cls,
713 socket->receive_buffer + socket->copy_offset,
715 /* Free the read handle */
716 GNUNET_free (socket->read_handle);
717 socket->read_handle = NULL;
719 GNUNET_assert (read_size <= valid_read_size);
720 socket->copy_offset += read_size;
722 /* Determine upto which packet we can remove from the buffer */
723 for (packet = 0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
724 if (socket->copy_offset < socket->receive_buffer_boundaries[packet])
727 /* If no packets can be removed we can't move the buffer */
728 if (0 == packet) return;
730 sequence_increase = packet;
732 /* Shift the data in the receive buffer */
733 memmove (socket->receive_buffer,
734 socket->receive_buffer
735 + socket->receive_buffer_boundaries[sequence_increase-1],
736 socket->receive_buffer_size - socket->receive_buffer_boundaries[sequence_increase-1]);
738 /* Shift the bitmap */
739 socket->ack_bitmap = socket->ack_bitmap >> sequence_increase;
741 /* Set read_sequence_number */
742 socket->read_sequence_number += sequence_increase;
744 /* Set read_offset */
745 offset_increase = socket->receive_buffer_boundaries[sequence_increase-1];
746 socket->read_offset += offset_increase;
748 /* Fix copy_offset */
749 GNUNET_assert (offset_increase <= socket->copy_offset);
750 socket->copy_offset -= offset_increase;
752 /* Fix relative boundaries */
753 for (packet=0; packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH; packet++)
755 if (packet < GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH - sequence_increase)
757 socket->receive_buffer_boundaries[packet] =
758 socket->receive_buffer_boundaries[packet + sequence_increase]
762 socket->receive_buffer_boundaries[packet] = 0;
768 * Cancels the existing read io handle
770 * @param cls the closure from the SCHEDULER call
771 * @param tc the task context
774 read_io_timeout (void *cls,
775 const struct GNUNET_SCHEDULER_TaskContext *tc)
777 struct GNUNET_STREAM_Socket *socket = cls;
779 socket->read_io_timeout_task = GNUNET_SCHEDULER_NO_TASK;
780 if (socket->read_task != GNUNET_SCHEDULER_NO_TASK)
782 GNUNET_SCHEDULER_cancel (socket->read_task);
783 socket->read_task = GNUNET_SCHEDULER_NO_TASK;
785 GNUNET_assert (NULL != socket->read_handle);
787 GNUNET_free (socket->read_handle);
788 socket->read_handle = NULL;
793 * Handler for DATA messages; Same for both client and server
795 * @param socket the socket through which the ack was received
796 * @param tunnel connection to the other end
797 * @param sender who sent the message
798 * @param msg the data message
799 * @param atsi performance data for the connection
800 * @return GNUNET_OK to keep the connection open,
801 * GNUNET_SYSERR to close it (signal serious error)
804 handle_data (struct GNUNET_STREAM_Socket *socket,
805 struct GNUNET_MESH_Tunnel *tunnel,
806 const struct GNUNET_PeerIdentity *sender,
807 const struct GNUNET_STREAM_DataMessage *msg,
808 const struct GNUNET_ATS_Information*atsi)
811 uint32_t bytes_needed;
812 uint32_t relative_offset;
813 uint32_t relative_sequence_number;
816 size = htons (msg->header.header.size);
817 if (size < sizeof (struct GNUNET_STREAM_DataMessage))
820 return GNUNET_SYSERR;
823 switch (socket->state)
825 case STATE_ESTABLISHED:
826 case STATE_TRANSMIT_CLOSED:
827 case STATE_TRANSMIT_CLOSE_WAIT:
829 /* check if the message's sequence number is in the range we are
831 relative_sequence_number =
832 ntohl (msg->sequence_number) - socket->read_sequence_number;
833 if ( relative_sequence_number > 64)
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "Ignoring received message with sequence number %d",
837 ntohl (msg->sequence_number));
841 /* Check if we have to allocate the buffer */
842 size -= sizeof (struct GNUNET_STREAM_DataMessage);
843 relative_offset = ntohl (msg->offset) - socket->read_offset;
844 bytes_needed = relative_offset + size;
846 if (bytes_needed > socket->receive_buffer_size)
848 if (bytes_needed <= RECEIVE_BUFFER_SIZE)
850 socket->receive_buffer = GNUNET_realloc (socket->receive_buffer,
852 socket->receive_buffer_size = bytes_needed;
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Cannot accommodate packet %d as buffer is full\n",
858 ntohl (msg->sequence_number));
863 /* Copy Data to buffer */
865 GNUNET_assert (relative_offset + size <= socket->receive_buffer_size);
866 memcpy (socket->receive_buffer + relative_offset,
869 socket->receive_buffer_boundaries[relative_sequence_number] =
870 relative_offset + size;
872 /* Modify the ACK bitmap */
873 ackbitmap_modify_bit (&socket->ack_bitmap,
874 relative_sequence_number,
877 /* Start ACK sending task if one is not already present */
878 if (0 == socket->ack_task_id)
880 socket->ack_task_id =
881 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_ntoh
887 if ((NULL != socket->read_handle) /* A read handle is waiting */
888 /* There is no current read task */
889 && (GNUNET_SCHEDULER_NO_TASK == socket->read_task)
890 /* We have the first packet */
891 && (GNUNET_YES == ackbitmap_is_bit_set(&socket->ack_bitmap,
895 GNUNET_SCHEDULER_add_now (&call_read_processor,
902 /* FIXME: call statistics */
909 * Client's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
911 * @param cls the socket (set from GNUNET_MESH_connect)
912 * @param tunnel connection to the other end
913 * @param tunnel_ctx place to store local state associated with the tunnel
914 * @param sender who sent the message
915 * @param message the actual message
916 * @param atsi performance data for the connection
917 * @return GNUNET_OK to keep the connection open,
918 * GNUNET_SYSERR to close it (signal serious error)
921 client_handle_data (void *cls,
922 struct GNUNET_MESH_Tunnel *tunnel,
924 const struct GNUNET_PeerIdentity *sender,
925 const struct GNUNET_MessageHeader *message,
926 const struct GNUNET_ATS_Information*atsi)
928 struct GNUNET_STREAM_Socket *socket = cls;
930 return handle_data (socket,
933 (const struct GNUNET_STREAM_DataMessage *) message,
939 * Callback to set state to ESTABLISHED
941 * @param cls the closure from queue_message FIXME: document
942 * @param socket the socket to requiring state change
945 set_state_established (void *cls,
946 struct GNUNET_STREAM_Socket *socket)
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attaining ESTABLISHED state\n");
949 socket->write_offset = 0;
950 socket->read_offset = 0;
951 socket->state = STATE_ESTABLISHED;
956 * Callback to set state to HELLO_WAIT
958 * @param cls the closure from queue_message
959 * @param socket the socket to requiring state change
962 set_state_hello_wait (void *cls,
963 struct GNUNET_STREAM_Socket *socket)
965 GNUNET_assert (STATE_INIT == socket->state);
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attaining HELLO_WAIT state\n");
967 socket->state = STATE_HELLO_WAIT;
972 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
974 * @param cls the socket (set from GNUNET_MESH_connect)
975 * @param tunnel connection to the other end
976 * @param tunnel_ctx this is NULL
977 * @param sender who sent the message
978 * @param message the actual message
979 * @param atsi performance data for the connection
980 * @return GNUNET_OK to keep the connection open,
981 * GNUNET_SYSERR to close it (signal serious error)
984 client_handle_hello_ack (void *cls,
985 struct GNUNET_MESH_Tunnel *tunnel,
987 const struct GNUNET_PeerIdentity *sender,
988 const struct GNUNET_MessageHeader *message,
989 const struct GNUNET_ATS_Information*atsi)
991 struct GNUNET_STREAM_Socket *socket = cls;
992 const struct GNUNET_STREAM_HelloAckMessage *ack_msg;
993 struct GNUNET_STREAM_HelloAckMessage *reply;
995 ack_msg = (const struct GNUNET_STREAM_HelloAckMessage *) message;
996 GNUNET_assert (socket->tunnel == tunnel);
997 switch (socket->state)
999 case STATE_HELLO_WAIT:
1000 socket->read_sequence_number = ntohl (ack_msg->sequence_number);
1001 socket->receive_window_available = ntohl (ack_msg->receive_window_size);
1002 /* Get the random sequence number */
1003 socket->write_sequence_number =
1004 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1006 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1007 reply->header.header.size =
1008 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1009 reply->header.header.type =
1010 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1011 reply->sequence_number = htonl (socket->write_sequence_number);
1012 reply->receive_window_size = htonl (RECEIVE_BUFFER_SIZE);
1013 queue_message (socket,
1015 &set_state_established,
1018 case STATE_ESTABLISHED:
1019 case STATE_RECEIVE_CLOSE_WAIT:
1020 // call statistics (# ACKs ignored++)
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "Server sent HELLO_ACK when in state %d\n", socket->state);
1026 socket->state = STATE_CLOSED; // introduce STATE_ERROR?
1027 return GNUNET_SYSERR;
1034 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1036 * @param cls the socket (set from GNUNET_MESH_connect)
1037 * @param tunnel connection to the other end
1038 * @param tunnel_ctx this is NULL
1039 * @param sender who sent the message
1040 * @param message the actual message
1041 * @param atsi performance data for the connection
1042 * @return GNUNET_OK to keep the connection open,
1043 * GNUNET_SYSERR to close it (signal serious error)
1046 client_handle_reset (void *cls,
1047 struct GNUNET_MESH_Tunnel *tunnel,
1049 const struct GNUNET_PeerIdentity *sender,
1050 const struct GNUNET_MessageHeader *message,
1051 const struct GNUNET_ATS_Information*atsi)
1053 struct GNUNET_STREAM_Socket *socket = cls;
1060 * Common message handler for handling TRANSMIT_CLOSE messages
1062 * @param socket the socket through which the ack was received
1063 * @param tunnel connection to the other end
1064 * @param sender who sent the message
1065 * @param msg the transmit close message
1066 * @param atsi performance data for the connection
1067 * @return GNUNET_OK to keep the connection open,
1068 * GNUNET_SYSERR to close it (signal serious error)
1071 handle_transmit_close (struct GNUNET_STREAM_Socket *socket,
1072 struct GNUNET_MESH_Tunnel *tunnel,
1073 const struct GNUNET_PeerIdentity *sender,
1074 const struct GNUNET_STREAM_MessageHeader *msg,
1075 const struct GNUNET_ATS_Information*atsi)
1077 struct GNUNET_STREAM_MessageHeader *reply;
1079 switch (socket->state)
1081 case STATE_ESTABLISHED:
1082 socket->state = STATE_RECEIVE_CLOSED;
1084 /* Send TRANSMIT_CLOSE_ACK */
1085 reply = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1086 reply->header.type =
1087 htons (GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK);
1088 reply->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1089 queue_message (socket, reply, NULL, NULL);
1093 /* FIXME: Call statistics? */
1101 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1103 * @param cls the socket (set from GNUNET_MESH_connect)
1104 * @param tunnel connection to the other end
1105 * @param tunnel_ctx this is NULL
1106 * @param sender who sent the message
1107 * @param message the actual message
1108 * @param atsi performance data for the connection
1109 * @return GNUNET_OK to keep the connection open,
1110 * GNUNET_SYSERR to close it (signal serious error)
1113 client_handle_transmit_close (void *cls,
1114 struct GNUNET_MESH_Tunnel *tunnel,
1116 const struct GNUNET_PeerIdentity *sender,
1117 const struct GNUNET_MessageHeader *message,
1118 const struct GNUNET_ATS_Information*atsi)
1120 struct GNUNET_STREAM_Socket *socket = cls;
1122 return handle_transmit_close (socket,
1125 (struct GNUNET_STREAM_MessageHeader *)message,
1131 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1133 * @param cls the socket (set from GNUNET_MESH_connect)
1134 * @param tunnel connection to the other end
1135 * @param tunnel_ctx this is NULL
1136 * @param sender who sent the message
1137 * @param message the actual message
1138 * @param atsi performance data for the connection
1139 * @return GNUNET_OK to keep the connection open,
1140 * GNUNET_SYSERR to close it (signal serious error)
1143 client_handle_transmit_close_ack (void *cls,
1144 struct GNUNET_MESH_Tunnel *tunnel,
1146 const struct GNUNET_PeerIdentity *sender,
1147 const struct GNUNET_MessageHeader *message,
1148 const struct GNUNET_ATS_Information*atsi)
1150 struct GNUNET_STREAM_Socket *socket = cls;
1157 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1159 * @param cls the socket (set from GNUNET_MESH_connect)
1160 * @param tunnel connection to the other end
1161 * @param tunnel_ctx this is NULL
1162 * @param sender who sent the message
1163 * @param message the actual message
1164 * @param atsi performance data for the connection
1165 * @return GNUNET_OK to keep the connection open,
1166 * GNUNET_SYSERR to close it (signal serious error)
1169 client_handle_receive_close (void *cls,
1170 struct GNUNET_MESH_Tunnel *tunnel,
1172 const struct GNUNET_PeerIdentity *sender,
1173 const struct GNUNET_MessageHeader *message,
1174 const struct GNUNET_ATS_Information*atsi)
1176 struct GNUNET_STREAM_Socket *socket = cls;
1183 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1185 * @param cls the socket (set from GNUNET_MESH_connect)
1186 * @param tunnel connection to the other end
1187 * @param tunnel_ctx this is NULL
1188 * @param sender who sent the message
1189 * @param message the actual message
1190 * @param atsi performance data for the connection
1191 * @return GNUNET_OK to keep the connection open,
1192 * GNUNET_SYSERR to close it (signal serious error)
1195 client_handle_receive_close_ack (void *cls,
1196 struct GNUNET_MESH_Tunnel *tunnel,
1198 const struct GNUNET_PeerIdentity *sender,
1199 const struct GNUNET_MessageHeader *message,
1200 const struct GNUNET_ATS_Information*atsi)
1202 struct GNUNET_STREAM_Socket *socket = cls;
1209 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1211 * @param cls the socket (set from GNUNET_MESH_connect)
1212 * @param tunnel connection to the other end
1213 * @param tunnel_ctx this is NULL
1214 * @param sender who sent the message
1215 * @param message the actual message
1216 * @param atsi performance data for the connection
1217 * @return GNUNET_OK to keep the connection open,
1218 * GNUNET_SYSERR to close it (signal serious error)
1221 client_handle_close (void *cls,
1222 struct GNUNET_MESH_Tunnel *tunnel,
1224 const struct GNUNET_PeerIdentity *sender,
1225 const struct GNUNET_MessageHeader *message,
1226 const struct GNUNET_ATS_Information*atsi)
1228 struct GNUNET_STREAM_Socket *socket = cls;
1235 * Client's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1237 * @param cls the socket (set from GNUNET_MESH_connect)
1238 * @param tunnel connection to the other end
1239 * @param tunnel_ctx this is NULL
1240 * @param sender who sent the message
1241 * @param message the actual message
1242 * @param atsi performance data for the connection
1243 * @return GNUNET_OK to keep the connection open,
1244 * GNUNET_SYSERR to close it (signal serious error)
1247 client_handle_close_ack (void *cls,
1248 struct GNUNET_MESH_Tunnel *tunnel,
1250 const struct GNUNET_PeerIdentity *sender,
1251 const struct GNUNET_MessageHeader *message,
1252 const struct GNUNET_ATS_Information*atsi)
1254 struct GNUNET_STREAM_Socket *socket = cls;
1259 /*****************************/
1260 /* Server's Message Handlers */
1261 /*****************************/
1264 * Server's message Handler for GNUNET_MESSAGE_TYPE_STREAM_DATA
1266 * @param cls the closure
1267 * @param tunnel connection to the other end
1268 * @param tunnel_ctx the socket
1269 * @param sender who sent the message
1270 * @param message the actual message
1271 * @param atsi performance data for the connection
1272 * @return GNUNET_OK to keep the connection open,
1273 * GNUNET_SYSERR to close it (signal serious error)
1276 server_handle_data (void *cls,
1277 struct GNUNET_MESH_Tunnel *tunnel,
1279 const struct GNUNET_PeerIdentity *sender,
1280 const struct GNUNET_MessageHeader *message,
1281 const struct GNUNET_ATS_Information*atsi)
1283 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1285 return handle_data (socket,
1288 (const struct GNUNET_STREAM_DataMessage *)message,
1294 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO
1296 * @param cls the closure
1297 * @param tunnel connection to the other end
1298 * @param tunnel_ctx the socket
1299 * @param sender who sent the message
1300 * @param message the actual message
1301 * @param atsi performance data for the connection
1302 * @return GNUNET_OK to keep the connection open,
1303 * GNUNET_SYSERR to close it (signal serious error)
1306 server_handle_hello (void *cls,
1307 struct GNUNET_MESH_Tunnel *tunnel,
1309 const struct GNUNET_PeerIdentity *sender,
1310 const struct GNUNET_MessageHeader *message,
1311 const struct GNUNET_ATS_Information*atsi)
1313 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1314 struct GNUNET_STREAM_HelloAckMessage *reply;
1316 GNUNET_assert (socket->tunnel == tunnel);
1317 if (STATE_INIT == socket->state)
1319 /* Get the random sequence number */
1320 socket->write_sequence_number =
1321 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1323 GNUNET_malloc (sizeof (struct GNUNET_STREAM_HelloAckMessage));
1324 reply->header.header.size =
1325 htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1326 reply->header.header.type =
1327 htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK);
1328 reply->sequence_number = htonl (socket->write_sequence_number);
1329 queue_message (socket,
1331 &set_state_hello_wait,
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1337 "Client sent HELLO when in state %d\n", socket->state);
1338 /* FIXME: Send RESET? */
1346 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK
1348 * @param cls the closure
1349 * @param tunnel connection to the other end
1350 * @param tunnel_ctx the socket
1351 * @param sender who sent the message
1352 * @param message the actual message
1353 * @param atsi performance data for the connection
1354 * @return GNUNET_OK to keep the connection open,
1355 * GNUNET_SYSERR to close it (signal serious error)
1358 server_handle_hello_ack (void *cls,
1359 struct GNUNET_MESH_Tunnel *tunnel,
1361 const struct GNUNET_PeerIdentity *sender,
1362 const struct GNUNET_MessageHeader *message,
1363 const struct GNUNET_ATS_Information*atsi)
1365 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1366 const struct GNUNET_STREAM_HelloAckMessage *ack_message;
1368 ack_message = (struct GNUNET_STREAM_HelloAckMessage *) message;
1369 GNUNET_assert (socket->tunnel == tunnel);
1370 if (STATE_HELLO_WAIT == socket->state)
1372 socket->read_sequence_number = ntohl (ack_message->sequence_number);
1373 socket->receive_window_available =
1374 ntohl (ack_message->receive_window_size);
1375 /* Attain ESTABLISHED state */
1376 set_state_established (NULL, socket);
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "Client sent HELLO_ACK when in state %d\n", socket->state);
1382 /* FIXME: Send RESET? */
1390 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RESET
1392 * @param cls the closure
1393 * @param tunnel connection to the other end
1394 * @param tunnel_ctx the socket
1395 * @param sender who sent the message
1396 * @param message the actual message
1397 * @param atsi performance data for the connection
1398 * @return GNUNET_OK to keep the connection open,
1399 * GNUNET_SYSERR to close it (signal serious error)
1402 server_handle_reset (void *cls,
1403 struct GNUNET_MESH_Tunnel *tunnel,
1405 const struct GNUNET_PeerIdentity *sender,
1406 const struct GNUNET_MessageHeader *message,
1407 const struct GNUNET_ATS_Information*atsi)
1409 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1416 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE
1418 * @param cls the closure
1419 * @param tunnel connection to the other end
1420 * @param tunnel_ctx the socket
1421 * @param sender who sent the message
1422 * @param message the actual message
1423 * @param atsi performance data for the connection
1424 * @return GNUNET_OK to keep the connection open,
1425 * GNUNET_SYSERR to close it (signal serious error)
1428 server_handle_transmit_close (void *cls,
1429 struct GNUNET_MESH_Tunnel *tunnel,
1431 const struct GNUNET_PeerIdentity *sender,
1432 const struct GNUNET_MessageHeader *message,
1433 const struct GNUNET_ATS_Information*atsi)
1435 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1437 return handle_transmit_close (socket,
1440 (struct GNUNET_STREAM_MessageHeader *)message,
1446 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK
1448 * @param cls the closure
1449 * @param tunnel connection to the other end
1450 * @param tunnel_ctx the socket
1451 * @param sender who sent the message
1452 * @param message the actual message
1453 * @param atsi performance data for the connection
1454 * @return GNUNET_OK to keep the connection open,
1455 * GNUNET_SYSERR to close it (signal serious error)
1458 server_handle_transmit_close_ack (void *cls,
1459 struct GNUNET_MESH_Tunnel *tunnel,
1461 const struct GNUNET_PeerIdentity *sender,
1462 const struct GNUNET_MessageHeader *message,
1463 const struct GNUNET_ATS_Information*atsi)
1465 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1472 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE
1474 * @param cls the closure
1475 * @param tunnel connection to the other end
1476 * @param tunnel_ctx the socket
1477 * @param sender who sent the message
1478 * @param message the actual message
1479 * @param atsi performance data for the connection
1480 * @return GNUNET_OK to keep the connection open,
1481 * GNUNET_SYSERR to close it (signal serious error)
1484 server_handle_receive_close (void *cls,
1485 struct GNUNET_MESH_Tunnel *tunnel,
1487 const struct GNUNET_PeerIdentity *sender,
1488 const struct GNUNET_MessageHeader *message,
1489 const struct GNUNET_ATS_Information*atsi)
1491 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1498 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK
1500 * @param cls the closure
1501 * @param tunnel connection to the other end
1502 * @param tunnel_ctx the socket
1503 * @param sender who sent the message
1504 * @param message the actual message
1505 * @param atsi performance data for the connection
1506 * @return GNUNET_OK to keep the connection open,
1507 * GNUNET_SYSERR to close it (signal serious error)
1510 server_handle_receive_close_ack (void *cls,
1511 struct GNUNET_MESH_Tunnel *tunnel,
1513 const struct GNUNET_PeerIdentity *sender,
1514 const struct GNUNET_MessageHeader *message,
1515 const struct GNUNET_ATS_Information*atsi)
1517 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1524 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE
1526 * @param cls the closure
1527 * @param tunnel connection to the other end
1528 * @param tunnel_ctx the socket
1529 * @param sender who sent the message
1530 * @param message the actual message
1531 * @param atsi performance data for the connection
1532 * @return GNUNET_OK to keep the connection open,
1533 * GNUNET_SYSERR to close it (signal serious error)
1536 server_handle_close (void *cls,
1537 struct GNUNET_MESH_Tunnel *tunnel,
1539 const struct GNUNET_PeerIdentity *sender,
1540 const struct GNUNET_MessageHeader *message,
1541 const struct GNUNET_ATS_Information*atsi)
1543 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1550 * Server's message handler for GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK
1552 * @param cls the closure
1553 * @param tunnel connection to the other end
1554 * @param tunnel_ctx the socket
1555 * @param sender who sent the message
1556 * @param message the actual message
1557 * @param atsi performance data for the connection
1558 * @return GNUNET_OK to keep the connection open,
1559 * GNUNET_SYSERR to close it (signal serious error)
1562 server_handle_close_ack (void *cls,
1563 struct GNUNET_MESH_Tunnel *tunnel,
1565 const struct GNUNET_PeerIdentity *sender,
1566 const struct GNUNET_MessageHeader *message,
1567 const struct GNUNET_ATS_Information*atsi)
1569 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1576 * Message Handler for mesh
1578 * @param socket the socket through which the ack was received
1579 * @param tunnel connection to the other end
1580 * @param sender who sent the message
1581 * @param ack the acknowledgment message
1582 * @param atsi performance data for the connection
1583 * @return GNUNET_OK to keep the connection open,
1584 * GNUNET_SYSERR to close it (signal serious error)
1587 handle_ack (struct GNUNET_STREAM_Socket *socket,
1588 struct GNUNET_MESH_Tunnel *tunnel,
1589 const struct GNUNET_PeerIdentity *sender,
1590 const struct GNUNET_STREAM_AckMessage *ack,
1591 const struct GNUNET_ATS_Information*atsi)
1593 switch (socket->state)
1595 case (STATE_ESTABLISHED):
1596 if (NULL == socket->write_handle)
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1599 "Received DATA ACK when write_handle is NULL\n");
1603 socket->write_handle->ack_bitmap = GNUNET_ntohll (ack->bitmap);
1604 socket->receive_window_available =
1605 ntohl (ack->receive_window_remaining);
1606 write_data (socket);
1616 * Message Handler for mesh
1618 * @param cls the 'struct GNUNET_STREAM_Socket'
1619 * @param tunnel connection to the other end
1620 * @param tunnel_ctx unused
1621 * @param sender who sent the message
1622 * @param message the actual message
1623 * @param atsi performance data for the connection
1624 * @return GNUNET_OK to keep the connection open,
1625 * GNUNET_SYSERR to close it (signal serious error)
1628 client_handle_ack (void *cls,
1629 struct GNUNET_MESH_Tunnel *tunnel,
1631 const struct GNUNET_PeerIdentity *sender,
1632 const struct GNUNET_MessageHeader *message,
1633 const struct GNUNET_ATS_Information*atsi)
1635 struct GNUNET_STREAM_Socket *socket = cls;
1636 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1638 return handle_ack (socket, tunnel, sender, ack, atsi);
1643 * Message Handler for mesh
1645 * @param cls the server's listen socket
1646 * @param tunnel connection to the other end
1647 * @param tunnel_ctx pointer to the 'struct GNUNET_STREAM_Socket*'
1648 * @param sender who sent the message
1649 * @param message the actual message
1650 * @param atsi performance data for the connection
1651 * @return GNUNET_OK to keep the connection open,
1652 * GNUNET_SYSERR to close it (signal serious error)
1655 server_handle_ack (void *cls,
1656 struct GNUNET_MESH_Tunnel *tunnel,
1658 const struct GNUNET_PeerIdentity *sender,
1659 const struct GNUNET_MessageHeader *message,
1660 const struct GNUNET_ATS_Information*atsi)
1662 struct GNUNET_STREAM_Socket *socket = *tunnel_ctx;
1663 const struct GNUNET_STREAM_AckMessage *ack = (const struct GNUNET_STREAM_AckMessage *) message;
1665 return handle_ack (socket, tunnel, sender, ack, atsi);
1670 * For client message handlers, the stream socket is in the
1673 static struct GNUNET_MESH_MessageHandler client_message_handlers[] = {
1674 {&client_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1675 {&client_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1676 sizeof (struct GNUNET_STREAM_AckMessage) },
1677 {&client_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1678 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1679 {&client_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1680 sizeof (struct GNUNET_STREAM_MessageHeader)},
1681 {&client_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1682 sizeof (struct GNUNET_STREAM_MessageHeader)},
1683 {&client_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1684 sizeof (struct GNUNET_STREAM_MessageHeader)},
1685 {&client_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1686 sizeof (struct GNUNET_STREAM_MessageHeader)},
1687 {&client_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1688 sizeof (struct GNUNET_STREAM_MessageHeader)},
1689 {&client_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1690 sizeof (struct GNUNET_STREAM_MessageHeader)},
1691 {&client_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1692 sizeof (struct GNUNET_STREAM_MessageHeader)},
1698 * For server message handlers, the stream socket is in the
1699 * tunnel context, and the listen socket in the closure argument.
1701 static struct GNUNET_MESH_MessageHandler server_message_handlers[] = {
1702 {&server_handle_data, GNUNET_MESSAGE_TYPE_STREAM_DATA, 0},
1703 {&server_handle_ack, GNUNET_MESSAGE_TYPE_STREAM_ACK,
1704 sizeof (struct GNUNET_STREAM_AckMessage) },
1705 {&server_handle_hello, GNUNET_MESSAGE_TYPE_STREAM_HELLO,
1706 sizeof (struct GNUNET_STREAM_MessageHeader)},
1707 {&server_handle_hello_ack, GNUNET_MESSAGE_TYPE_STREAM_HELLO_ACK,
1708 sizeof (struct GNUNET_STREAM_HelloAckMessage)},
1709 {&server_handle_reset, GNUNET_MESSAGE_TYPE_STREAM_RESET,
1710 sizeof (struct GNUNET_STREAM_MessageHeader)},
1711 {&server_handle_transmit_close, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE,
1712 sizeof (struct GNUNET_STREAM_MessageHeader)},
1713 {&server_handle_transmit_close_ack, GNUNET_MESSAGE_TYPE_STREAM_TRANSMIT_CLOSE_ACK,
1714 sizeof (struct GNUNET_STREAM_MessageHeader)},
1715 {&server_handle_receive_close, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE,
1716 sizeof (struct GNUNET_STREAM_MessageHeader)},
1717 {&server_handle_receive_close_ack, GNUNET_MESSAGE_TYPE_STREAM_RECEIVE_CLOSE_ACK,
1718 sizeof (struct GNUNET_STREAM_MessageHeader)},
1719 {&server_handle_close, GNUNET_MESSAGE_TYPE_STREAM_CLOSE,
1720 sizeof (struct GNUNET_STREAM_MessageHeader)},
1721 {&server_handle_close_ack, GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK,
1722 sizeof (struct GNUNET_STREAM_MessageHeader)},
1728 * Function called when our target peer is connected to our tunnel
1730 * @param cls the socket for which this tunnel is created
1731 * @param peer the peer identity of the target
1732 * @param atsi performance data for the connection
1735 mesh_peer_connect_callback (void *cls,
1736 const struct GNUNET_PeerIdentity *peer,
1737 const struct GNUNET_ATS_Information * atsi)
1739 struct GNUNET_STREAM_Socket *socket = cls;
1740 struct GNUNET_STREAM_MessageHeader *message;
1742 if (0 != memcmp (&socket->other_peer,
1744 sizeof (struct GNUNET_PeerIdentity)))
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1747 "A peer (%s) which is not our target has connected to our tunnel",
1752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1753 "Target peer %s connected\n", GNUNET_i2s (peer));
1755 /* Set state to INIT */
1756 socket->state = STATE_INIT;
1758 /* Send HELLO message */
1759 message = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1760 message->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_HELLO);
1761 message->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1762 queue_message (socket,
1764 &set_state_hello_wait,
1767 /* Call open callback */
1768 if (NULL == socket->open_cb)
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 "STREAM_open callback is NULL\n");
1775 socket->open_cb (socket->open_cls, socket);
1781 * Function called when our target peer is disconnected from our tunnel
1783 * @param cls the socket associated which this tunnel
1784 * @param peer the peer identity of the target
1787 mesh_peer_disconnect_callback (void *cls,
1788 const struct GNUNET_PeerIdentity *peer)
1800 * Tries to open a stream to the target peer
1802 * @param cfg configuration to use
1803 * @param target the target peer to which the stream has to be opened
1804 * @param app_port the application port number which uniquely identifies this
1806 * @param open_cb this function will be called after stream has be established
1807 * @param open_cb_cls the closure for open_cb
1808 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
1809 * @return if successful it returns the stream socket; NULL if stream cannot be
1812 struct GNUNET_STREAM_Socket *
1813 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
1814 const struct GNUNET_PeerIdentity *target,
1815 GNUNET_MESH_ApplicationType app_port,
1816 GNUNET_STREAM_OpenCallback open_cb,
1820 struct GNUNET_STREAM_Socket *socket;
1821 enum GNUNET_STREAM_Option option;
1822 va_list vargs; /* Variable arguments */
1824 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
1825 socket->other_peer = *target;
1826 socket->open_cb = open_cb;
1827 socket->open_cls = open_cb_cls;
1830 socket->retransmit_timeout =
1831 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout);
1833 va_start (vargs, open_cb_cls); /* Parse variable args */
1835 option = va_arg (vargs, enum GNUNET_STREAM_Option);
1838 case GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT:
1839 /* Expect struct GNUNET_TIME_Relative */
1840 socket->retransmit_timeout = va_arg (vargs,
1841 struct GNUNET_TIME_Relative);
1843 case GNUNET_STREAM_OPTION_END:
1846 } while (GNUNET_STREAM_OPTION_END != option);
1847 va_end (vargs); /* End of variable args parsing */
1849 socket->mesh = GNUNET_MESH_connect (cfg, /* the configuration handle */
1850 1, /* QUEUE size as parameter? */
1852 NULL, /* No inbound tunnel handler */
1853 NULL, /* No inbound tunnel cleaner */
1854 client_message_handlers,
1855 NULL); /* We don't get inbound tunnels */
1856 // FIXME: if (NULL == socket->mesh) ...
1858 /* Now create the mesh tunnel to target */
1859 socket->tunnel = GNUNET_MESH_tunnel_create (socket->mesh,
1860 NULL, /* Tunnel context */
1861 &mesh_peer_connect_callback,
1862 &mesh_peer_disconnect_callback,
1864 // FIXME: if (NULL == socket->tunnel) ...
1873 * @param socket the stream socket
1876 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket)
1878 struct MessageQueue *head;
1880 if (socket->read_task != GNUNET_SCHEDULER_NO_TASK)
1882 /* socket closed with read task pending!? */
1884 GNUNET_SCHEDULER_cancel (socket->read_task);
1885 socket->read_task = GNUNET_SCHEDULER_NO_TASK;
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;
1895 /* Clear existing message queue */
1896 while (NULL != (head = socket->queue_head)) {
1897 GNUNET_CONTAINER_DLL_remove (socket->queue_head,
1900 GNUNET_free (head->message);
1904 /* Close associated tunnel */
1905 if (NULL != socket->tunnel)
1907 GNUNET_MESH_tunnel_destroy (socket->tunnel);
1908 socket->tunnel = NULL;
1911 /* Close mesh connection */
1912 if (NULL != socket->mesh)
1914 GNUNET_MESH_disconnect (socket->mesh);
1915 socket->mesh = NULL;
1918 /* Release receive buffer */
1919 if (NULL != socket->receive_buffer)
1921 GNUNET_free (socket->receive_buffer);
1924 GNUNET_free (socket);
1929 * Method called whenever a peer creates a tunnel to us
1931 * @param cls closure
1932 * @param tunnel new handle to the tunnel
1933 * @param initiator peer that started the tunnel
1934 * @param atsi performance information for the tunnel
1935 * @return initial tunnel context for the tunnel
1936 * (can be NULL -- that's not an error)
1939 new_tunnel_notify (void *cls,
1940 struct GNUNET_MESH_Tunnel *tunnel,
1941 const struct GNUNET_PeerIdentity *initiator,
1942 const struct GNUNET_ATS_Information *atsi)
1944 struct GNUNET_STREAM_ListenSocket *lsocket = cls;
1945 struct GNUNET_STREAM_Socket *socket;
1947 socket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_Socket));
1948 socket->tunnel = tunnel;
1949 socket->session_id = 0; /* FIXME */
1950 socket->other_peer = *initiator;
1951 socket->state = STATE_INIT;
1953 if (GNUNET_SYSERR == lsocket->listen_cb (lsocket->listen_cb_cls,
1955 &socket->other_peer))
1957 socket->state = STATE_CLOSED;
1958 /* FIXME: Send CLOSE message and then free */
1959 GNUNET_free (socket);
1960 GNUNET_MESH_tunnel_destroy (tunnel); /* Destroy the tunnel */
1967 * Function called whenever an inbound tunnel is destroyed. Should clean up
1968 * any associated state. This function is NOT called if the client has
1969 * explicitly asked for the tunnel to be destroyed using
1970 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
1973 * @param cls closure (set from GNUNET_MESH_connect)
1974 * @param tunnel connection to the other end (henceforth invalid)
1975 * @param tunnel_ctx place where local state associated
1976 * with the tunnel is stored
1979 tunnel_cleaner (void *cls,
1980 const struct GNUNET_MESH_Tunnel *tunnel,
1983 struct GNUNET_STREAM_Socket *socket = tunnel_ctx;
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 "Peer %s has terminated connection abruptly\n",
1987 GNUNET_i2s (&socket->other_peer));
1989 socket->status = GNUNET_STREAM_SHUTDOWN;
1990 /* Clear Transmit handles */
1991 if (NULL != socket->transmit_handle)
1993 GNUNET_MESH_notify_transmit_ready_cancel (socket->transmit_handle);
1994 socket->transmit_handle = NULL;
1996 socket->tunnel = NULL;
2001 * Listens for stream connections for a specific application ports
2003 * @param cfg the configuration to use
2004 * @param app_port the application port for which new streams will be accepted
2005 * @param listen_cb this function will be called when a peer tries to establish
2007 * @param listen_cb_cls closure for listen_cb
2008 * @return listen socket, NULL for any error
2010 struct GNUNET_STREAM_ListenSocket *
2011 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
2012 GNUNET_MESH_ApplicationType app_port,
2013 GNUNET_STREAM_ListenCallback listen_cb,
2014 void *listen_cb_cls)
2016 /* FIXME: Add variable args for passing configration options? */
2017 struct GNUNET_STREAM_ListenSocket *lsocket;
2018 GNUNET_MESH_ApplicationType app_types[2];
2020 app_types[0] = app_port;
2022 lsocket = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ListenSocket));
2023 lsocket->port = app_port;
2024 lsocket->listen_cb = listen_cb;
2025 lsocket->listen_cb_cls = listen_cb_cls;
2026 lsocket->mesh = GNUNET_MESH_connect (cfg,
2027 10, /* FIXME: QUEUE size as parameter? */
2028 lsocket, /* Closure */
2031 server_message_handlers,
2038 * Closes the listen socket
2040 * @param lsocket the listen socket
2043 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket)
2045 /* Close MESH connection */
2046 GNUNET_MESH_disconnect (lsocket->mesh);
2048 GNUNET_free (lsocket);
2053 * Tries to write the given data to the stream
2055 * @param socket the socket representing a stream
2056 * @param data the data buffer from where the data is written into the stream
2057 * @param size the number of bytes to be written from the data buffer
2058 * @param timeout the timeout period
2059 * @param write_cont the function to call upon writing some bytes into the stream
2060 * @param write_cont_cls the closure
2061 * @return handle to cancel the operation
2063 struct GNUNET_STREAM_IOWriteHandle *
2064 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
2067 struct GNUNET_TIME_Relative timeout,
2068 GNUNET_STREAM_CompletionContinuation write_cont,
2069 void *write_cont_cls)
2071 unsigned int num_needed_packets;
2072 unsigned int packet;
2073 struct GNUNET_STREAM_IOWriteHandle *io_handle;
2074 uint32_t packet_size;
2075 uint32_t payload_size;
2076 struct GNUNET_STREAM_DataMessage *data_msg;
2079 /* Return NULL if there is already a write request pending */
2080 if (NULL != socket->write_handle)
2085 if (!((STATE_ESTABLISHED == socket->state)
2086 || (STATE_RECEIVE_CLOSE_WAIT == socket->state)
2087 || (STATE_RECEIVE_CLOSED == socket->state)))
2089 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2090 "Attempting to write on a closed (OR) not-yet-established"
2094 if (GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size < size)
2095 size = GNUNET_STREAM_ACK_BITMAP_BIT_LENGTH * max_payload_size;
2096 num_needed_packets = (size + (max_payload_size - 1)) / max_payload_size;
2097 io_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOWriteHandle));
2099 /* Divide the given buffer into packets for sending */
2100 for (packet=0; packet < num_needed_packets; packet++)
2102 if ((packet + 1) * max_payload_size < size)
2104 payload_size = max_payload_size;
2105 packet_size = MAX_PACKET_SIZE;
2109 payload_size = size - packet * max_payload_size;
2110 packet_size = payload_size + sizeof (struct
2111 GNUNET_STREAM_DataMessage);
2113 io_handle->messages[packet] = GNUNET_malloc (packet_size);
2114 io_handle->messages[packet]->header.header.size = htons (packet_size);
2115 io_handle->messages[packet]->header.header.type =
2116 htons (GNUNET_MESSAGE_TYPE_STREAM_DATA);
2117 io_handle->messages[packet]->sequence_number =
2118 htons (socket->write_sequence_number++);
2119 io_handle->messages[packet]->offset = htons (socket->write_offset);
2121 /* FIXME: Remove the fixed delay for ack deadline; Set it to the value
2122 determined from RTT */
2123 io_handle->messages[packet]->ack_deadline =
2124 GNUNET_TIME_relative_hton (GNUNET_TIME_relative_multiply
2125 (GNUNET_TIME_UNIT_SECONDS, 5));
2126 data_msg = io_handle->messages[packet];
2127 /* Copy data from given buffer to the packet */
2128 memcpy (&data_msg[1],
2131 sweep += payload_size;
2132 socket->write_offset += payload_size;
2134 socket->write_handle = io_handle;
2135 write_data (socket);
2142 * Tries to read data from the stream
2144 * @param socket the socket representing a stream
2145 * @param timeout the timeout period
2146 * @param proc function to call with data (once only)
2147 * @param proc_cls the closure for proc
2148 * @return handle to cancel the operation
2150 struct GNUNET_STREAM_IOReadHandle *
2151 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
2152 struct GNUNET_TIME_Relative timeout,
2153 GNUNET_STREAM_DataProcessor proc,
2156 struct GNUNET_STREAM_IOReadHandle *read_handle;
2158 /* Return NULL if there is already a read handle; the user has to cancel that
2159 first before continuing or has to wait until it is completed */
2160 if (NULL != socket->read_handle) return NULL;
2162 read_handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_IOReadHandle));
2163 read_handle->proc = proc;
2164 socket->read_handle = read_handle;
2166 /* Check if we have a packet at bitmap 0 */
2167 if (GNUNET_YES == ackbitmap_is_bit_set (&socket->ack_bitmap,
2170 socket->read_task = GNUNET_SCHEDULER_add_now (&call_read_processor,
2175 /* Setup the read timeout task */
2176 socket->read_io_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,