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