1 /* 2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Andrew Galante, haiku.galante@gmail.com 7 * Axel Dörfler, axeld@pinc-software.de 8 * Hugo Santos, hugosantos@gmail.com 9 */ 10 #ifndef TCP_ENDPOINT_H 11 #define TCP_ENDPOINT_H 12 13 14 #include "BufferQueue.h" 15 #include "EndpointManager.h" 16 #include "tcp.h" 17 18 #include <ProtocolUtilities.h> 19 #include <net_protocol.h> 20 #include <net_stack.h> 21 #include <condition_variable.h> 22 #include <util/AutoLock.h> 23 #include <util/DoublyLinkedList.h> 24 #include <util/OpenHashTable.h> 25 26 #include <stddef.h> 27 28 29 class TCPEndpoint : public net_protocol, public ProtocolSocket { 30 public: 31 TCPEndpoint(net_socket* socket); 32 ~TCPEndpoint(); 33 34 status_t InitCheck() const; 35 36 status_t Open(); 37 status_t Close(); 38 void Free(); 39 status_t Connect(const struct sockaddr* address); 40 status_t Accept(struct net_socket** _acceptedSocket); 41 status_t Bind(const sockaddr* address); 42 status_t Unbind(struct sockaddr* address); 43 status_t Listen(int count); 44 status_t Shutdown(int direction); 45 status_t SendData(net_buffer* buffer); 46 ssize_t SendAvailable(); 47 status_t ReadData(size_t numBytes, uint32 flags, 48 net_buffer** _buffer); 49 ssize_t ReadAvailable(); 50 51 status_t FillStat(struct net_stat* stat); 52 53 status_t SetSendBufferSize(size_t length); 54 status_t SetReceiveBufferSize(size_t length); 55 56 status_t GetOption(int option, void* value, int* _length); 57 status_t SetOption(int option, const void* value, int length); 58 59 tcp_state State() const { return fState; } 60 bool IsBound() const; 61 bool IsLocal() const; 62 63 status_t DelayedAcknowledge(); 64 status_t SendAcknowledge(bool force); 65 66 int32 SegmentReceived(tcp_segment_header& segment, 67 net_buffer* buffer); 68 69 void Dump() const; 70 71 private: 72 void _StartPersistTimer(); 73 void _EnterTimeWait(); 74 void _UpdateTimeWait(); 75 void _Close(); 76 void _CancelConnectionTimers(); 77 uint8 _CurrentFlags(); 78 bool _ShouldSendSegment(tcp_segment_header& segment, 79 uint32 length, uint32 segmentMaxSize, 80 uint32 flightSize); 81 status_t _SendQueued(bool force = false); 82 status_t _SendQueued(bool force, uint32 sendWindow); 83 int _MaxSegmentSize(const struct sockaddr* address) const; 84 status_t _Disconnect(bool closing); 85 ssize_t _AvailableData() const; 86 void _NotifyReader(); 87 bool _ShouldReceive() const; 88 void _HandleReset(status_t error); 89 int32 _Spawn(TCPEndpoint* parent, tcp_segment_header& segment, 90 net_buffer* buffer); 91 int32 _ListenReceive(tcp_segment_header& segment, 92 net_buffer* buffer); 93 int32 _SynchronizeSentReceive(tcp_segment_header& segment, 94 net_buffer* buffer); 95 int32 _SegmentReceived(tcp_segment_header& segment, 96 net_buffer* buffer); 97 int32 _Receive(tcp_segment_header& segment, 98 net_buffer* buffer); 99 void _UpdateTimestamps(tcp_segment_header& segment, 100 size_t segmentLength); 101 void _MarkEstablished(); 102 status_t _WaitForEstablished(MutexLocker& lock, 103 bigtime_t timeout); 104 bool _AddData(tcp_segment_header& segment, 105 net_buffer* buffer); 106 void _PrepareReceivePath(tcp_segment_header& segment); 107 status_t _PrepareSendPath(const sockaddr* peer); 108 void _Acknowledged(tcp_segment_header& segment); 109 void _Retransmit(); 110 void _UpdateRoundTripTime(int32 roundTripTime, int32 expectedSamples); 111 void _ResetSlowStart(); 112 void _DuplicateAcknowledge(tcp_segment_header& segment); 113 114 static void _TimeWaitTimer(net_timer* timer, void* _endpoint); 115 static void _RetransmitTimer(net_timer* timer, void* _endpoint); 116 static void _PersistTimer(net_timer* timer, void* _endpoint); 117 static void _DelayedAcknowledgeTimer(net_timer* timer, 118 void* _endpoint); 119 120 static status_t _WaitForCondition(ConditionVariable& condition, 121 MutexLocker& locker, bigtime_t timeout); 122 123 private: 124 TCPEndpoint* fConnectionHashLink; 125 TCPEndpoint* fEndpointHashLink; 126 friend class EndpointManager; 127 friend struct ConnectionHashDefinition; 128 friend class EndpointHashDefinition; 129 130 mutex fLock; 131 EndpointManager* fManager; 132 ConditionVariable 133 fReceiveCondition; 134 ConditionVariable 135 fSendCondition; 136 sem_id fAcceptSemaphore; 137 uint8 fOptions; 138 139 uint8 fSendWindowShift; 140 uint8 fReceiveWindowShift; 141 142 tcp_sequence fSendUnacknowledged; 143 tcp_sequence fSendNext; 144 tcp_sequence fSendMax; 145 tcp_sequence fSendUrgentOffset; 146 uint32 fSendWindow; 147 uint32 fSendMaxWindow; 148 uint32 fSendMaxSegmentSize; 149 uint32 fSendMaxSegments; 150 BufferQueue fSendQueue; 151 tcp_sequence fLastAcknowledgeSent; 152 tcp_sequence fInitialSendSequence; 153 tcp_sequence fPreviousHighestAcknowledge; 154 uint32 fDuplicateAcknowledgeCount; 155 uint32 fPreviousFlightSize; 156 uint32 fRecover; 157 158 net_route *fRoute; 159 // TODO: don't use a net_route, but a net_route_info!!! 160 // (the latter will automatically adapt to routing changes) 161 162 tcp_sequence fReceiveNext; 163 tcp_sequence fReceiveMaxAdvertised; 164 uint32 fReceiveWindow; 165 uint32 fReceiveMaxSegmentSize; 166 BufferQueue fReceiveQueue; 167 bool fFinishReceived; 168 tcp_sequence fFinishReceivedAt; 169 tcp_sequence fInitialReceiveSequence; 170 171 // round trip time and retransmit timeout computation 172 int32 fSmoothedRoundTripTime; 173 int32 fRoundTripVariation; 174 uint32 fSendTime; 175 tcp_sequence fRoundTripStartSequence; 176 bigtime_t fRetransmitTimeout; 177 178 uint32 fReceivedTimestamp; 179 180 uint32 fCongestionWindow; 181 uint32 fSlowStartThreshold; 182 183 tcp_state fState; 184 uint32 fFlags; 185 186 // timer 187 net_timer fRetransmitTimer; 188 net_timer fPersistTimer; 189 net_timer fDelayedAcknowledgeTimer; 190 net_timer fTimeWaitTimer; 191 }; 192 193 #endif // TCP_ENDPOINT_H 194