xref: /haiku/src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.h (revision cfc3fa87da824bdf593eb8b817a83b6376e77935)
1 /*
2  * Copyright 2006-2008, 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 WaitList {
29 public:
30 	WaitList(const char *name);
31 	~WaitList();
32 
33 	status_t InitCheck() const;
34 
35 	status_t Wait(MutexLocker &, bigtime_t timeout, bool wakeNext = true);
36 	void Signal();
37 
38 private:
39 	int32 fCondition;
40 	sem_id fSem;
41 };
42 
43 
44 class TCPEndpoint : public net_protocol, public ProtocolSocket {
45 public:
46 	TCPEndpoint(net_socket *socket);
47 	~TCPEndpoint();
48 
49 	status_t InitCheck() const;
50 
51 	status_t Open();
52 	status_t Close();
53 	status_t Free();
54 	status_t Connect(const struct sockaddr *address);
55 	status_t Accept(struct net_socket **_acceptedSocket);
56 	status_t Bind(const sockaddr *address);
57 	status_t Unbind(struct sockaddr *address);
58 	status_t Listen(int count);
59 	status_t Shutdown(int direction);
60 	status_t SendData(net_buffer *buffer);
61 	ssize_t SendAvailable();
62 	status_t ReadData(size_t numBytes, uint32 flags, net_buffer **_buffer);
63 	ssize_t ReadAvailable();
64 
65 	status_t FillStat(struct net_stat *stat);
66 
67 	status_t SetSendBufferSize(size_t length);
68 	status_t SetReceiveBufferSize(size_t length);
69 
70 	status_t SetOption(int option, const void *value, int length);
71 
72 	tcp_state State() const { return fState; }
73 	bool IsBound() const;
74 
75 	status_t DelayedAcknowledge();
76 	status_t SendAcknowledge(bool force);
77 	status_t UpdateTimeWait();
78 
79 	int32 SegmentReceived(tcp_segment_header& segment, net_buffer *buffer);
80 	int32 Spawn(TCPEndpoint *parent, tcp_segment_header& segment,
81 		net_buffer *buffer);
82 
83 	void DumpInternalState() const;
84 
85 private:
86 	friend class EndpointManager;
87 
88 	void _StartPersistTimer();
89 	void _EnterTimeWait();
90 	uint8 _CurrentFlags();
91 	bool _ShouldSendSegment(tcp_segment_header &segment, uint32 length,
92 		uint32 segmentMaxSize, uint32 flightSize);
93 	status_t _SendQueued(bool force = false);
94 	status_t _SendQueued(bool force, uint32 sendWindow);
95 	int _MaxSegmentSize(const struct sockaddr *) const;
96 	status_t _ShutdownEgress(bool closing);
97 	ssize_t _AvailableData() const;
98 	void _NotifyReader();
99 	bool _ShouldReceive() const;
100 	void _HandleReset(status_t error);
101 	int32 _ListenReceive(tcp_segment_header& segment, net_buffer *buffer);
102 	int32 _SynchronizeSentReceive(tcp_segment_header& segment,
103 		net_buffer *buffer);
104 	int32 _SegmentReceived(tcp_segment_header& segment, net_buffer *buffer);
105 	int32 _Receive(tcp_segment_header& segment, net_buffer *buffer);
106 	void _UpdateTimestamps(tcp_segment_header& segment,
107 		size_t segmentLength);
108 	void _MarkEstablished();
109 	status_t _WaitForEstablished(MutexLocker &lock, bigtime_t timeout);
110 	bool _AddData(tcp_segment_header &segment, net_buffer *buffer);
111 	void _PrepareReceivePath(tcp_segment_header &segment);
112 	status_t _PrepareSendPath(const sockaddr *peer);
113 	void _Acknowledged(tcp_segment_header &segment);
114 	void _Retransmit();
115 	void _UpdateSRTT(int32 roundTripTime);
116 	void _ResetSlowStart();
117 	void _DuplicateAcknowledge(tcp_segment_header &segment);
118 
119 	static void _TimeWaitTimer(net_timer *timer, void *data);
120 	static void _RetransmitTimer(net_timer *timer, void *data);
121 	static void _PersistTimer(net_timer *timer, void *data);
122 	static void _DelayedAcknowledgeTimer(net_timer *timer, void *data);
123 
124 	EndpointManager *fManager;
125 
126 	HashTableLink<TCPEndpoint> fConnectionHashLink;
127 	HashTableLink<TCPEndpoint> fEndpointHashLink;
128 
129 	friend class ConnectionHashDefinition;
130 	friend class EndpointHashDefinition;
131 
132 	mutex			fLock;
133 	WaitList		fReceiveList;
134 	WaitList		fSendList;
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 	uint32			fSendWindow;
145 	uint32			fSendMaxWindow;
146 	uint32			fSendMaxSegmentSize;
147 	BufferQueue		fSendQueue;
148 	tcp_sequence	fLastAcknowledgeSent;
149 	tcp_sequence	fInitialSendSequence;
150 	uint32			fDuplicateAcknowledgeCount;
151 
152 	net_route 		*fRoute;
153 		// TODO: don't use a net_route, but a net_route_info!!!
154 
155 	tcp_sequence	fReceiveNext;
156 	tcp_sequence	fReceiveMaxAdvertised;
157 	uint32			fReceiveWindow;
158 	uint32			fReceiveMaxSegmentSize;
159 	BufferQueue		fReceiveQueue;
160 	tcp_sequence	fInitialReceiveSequence;
161 
162 	// round trip time and retransmit timeout computation
163 	int32			fRoundTripTime;
164 	int32			fRoundTripDeviation;
165 	bigtime_t		fRetransmitTimeout;
166 
167 	uint32			fReceivedTimestamp;
168 
169 	uint32			fCongestionWindow;
170 	uint32			fSlowStartThreshold;
171 
172 	tcp_state		fState;
173 	uint32			fFlags;
174 
175 	// timer
176 	net_timer		fRetransmitTimer;
177 	net_timer		fPersistTimer;
178 	net_timer		fDelayedAcknowledgeTimer;
179 	net_timer		fTimeWaitTimer;
180 };
181 
182 #endif	// TCP_ENDPOINT_H
183