xref: /haiku/src/add-ons/kernel/network/protocols/tcp/tcp.h (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 /*
2  * Copyright 2006-2007, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  *		Andrew Galante, haiku.galante@gmail.com
8  *		Hugo Santos, hugosantos@gmail.com
9  */
10 #ifndef TCP_H
11 #define TCP_H
12 
13 
14 #include <net_buffer.h>
15 #include <net_datalink.h>
16 #include <net_socket.h>
17 #include <net_stack.h>
18 
19 #include <util/khash.h>
20 
21 #include <ByteOrder.h>
22 
23 #include <sys/socket.h>
24 
25 
26 class EndpointManager;
27 
28 typedef enum {
29 	// establishing a connection
30 	CLOSED,
31 	LISTEN,
32 	SYNCHRONIZE_SENT,
33 	SYNCHRONIZE_RECEIVED,
34 	ESTABLISHED,
35 
36 	// peer closes the connection
37 	FINISH_RECEIVED,
38 	WAIT_FOR_FINISH_ACKNOWLEDGE,
39 
40 	// we close the connection
41 	FINISH_SENT,
42 	FINISH_ACKNOWLEDGED,
43 	CLOSING,
44 	TIME_WAIT
45 } tcp_state;
46 
47 struct tcp_header {
48 	uint16 source_port;
49 	uint16 destination_port;
50 	uint32 sequence;
51 	uint32 acknowledge;
52 	struct {
53 #if B_HOST_IS_LENDIAN == 1
54 		uint8 reserved : 4;
55 		uint8 header_length : 4;
56 #else
57 		uint8 header_length : 4;
58 		uint8 reserved : 4;
59 #endif
60 	};
61 	uint8  flags;
62 	uint16 advertised_window;
63 	uint16 checksum;
64 	uint16 urgent_offset;
65 
66 	uint32 HeaderLength() const { return (uint32)header_length << 2; }
67 	uint32 Sequence() const { return ntohl(sequence); }
68 	uint32 Acknowledge() const { return ntohl(acknowledge); }
69 	uint16 AdvertisedWindow() const { return ntohs(advertised_window); }
70 	uint16 Checksum() const { return ntohs(checksum); }
71 	uint16 UrgentOffset() const { return ntohs(urgent_offset); }
72 } _PACKED;
73 
74 class tcp_sequence {
75 	public:
76 		tcp_sequence() {}
77 		tcp_sequence(uint32 sequence) : number(sequence) {}
78 
79 		operator uint32() const { return number; }
80 
81 		void operator=(uint32 sequence) { number = sequence; }
82 		bool operator>(uint32 sequence) const { return (int32)(number - sequence) > 0; }
83 		bool operator>=(uint32 sequence) const { return (int32)(number - sequence) >= 0; }
84 		bool operator<(uint32 sequence) const { return (int32)(number - sequence) < 0; }
85 		bool operator<=(uint32 sequence) const { return (int32)(number - sequence) <= 0; }
86 
87 		uint32& operator+=(uint32 sequence) { return number += sequence; }
88 		uint32& operator++() { return ++number; }
89 		uint32 operator++(int _) { return number++; }
90 
91 	private:
92 		uint32 number;
93 };
94 
95 // TCP flag constants
96 #define TCP_FLAG_FINISH					0x01
97 #define TCP_FLAG_SYNCHRONIZE			0x02
98 #define TCP_FLAG_RESET					0x04
99 #define TCP_FLAG_PUSH					0x08
100 #define TCP_FLAG_ACKNOWLEDGE			0x10
101 #define TCP_FLAG_URGENT					0x20
102 #define TCP_FLAG_CONGESTION_NOTIFICATION_ECHO	0x40
103 #define TCP_FLAG_CONGESTION_WINDOW_REDUCED		0x80
104 
105 #define TCP_CONNECTION_TIMEOUT			75000000	// 75 secs
106 #define TCP_DELAYED_ACKNOWLEDGE_TIMEOUT	100000		// 100 msecs
107 #define TCP_DEFAULT_MAX_SEGMENT_SIZE	536
108 #define TCP_MAX_WINDOW					65535
109 #define TCP_MAX_SEGMENT_LIFETIME		60000000	// 60 secs
110 
111 struct tcp_sack {
112 	uint32 left_edge;
113 	uint32 right_edge;
114 } _PACKED;
115 
116 struct tcp_option {
117 	uint8	kind;
118 	uint8	length;
119 	union {
120 		uint8		window_shift;
121 		uint16		max_segment_size;
122 		struct {
123 			uint32	value;
124 			uint32	reply;
125 		}			timestamp;
126 		tcp_sack	sack[0];
127 	};
128 } _PACKED;
129 
130 enum tcp_option_kind {
131 	TCP_OPTION_END				= 0,
132 	TCP_OPTION_NOP				= 1,
133 	TCP_OPTION_MAX_SEGMENT_SIZE	= 2,
134 	TCP_OPTION_WINDOW_SHIFT		= 3,
135 	TCP_OPTION_SACK_PERMITTED	= 4,
136 	TCP_OPTION_SACK				= 5,
137 	TCP_OPTION_TIMESTAMP		= 8,
138 };
139 
140 #define TCP_MAX_WINDOW_SHIFT	14
141 
142 enum {
143 	TCP_HAS_WINDOW_SCALE	= 1 << 0,
144 	TCP_HAS_TIMESTAMPS		= 1 << 1,
145 	TCP_SACK_PERMITTED		= 1 << 2,
146 };
147 
148 struct tcp_segment_header {
149 	tcp_segment_header(uint8 _flags)
150 		:
151 		flags(_flags),
152 		window_shift(0),
153 		max_segment_size(0),
154 		sack_count(0),
155 		options(0)
156 	{}
157 
158 	uint32	sequence;
159 	uint32	acknowledge;
160 	uint16	advertised_window;
161 	uint16	urgent_offset;
162 	uint8	flags;
163 	uint8	window_shift;
164 	uint16	max_segment_size;
165 
166 	uint32	timestamp_value;
167 	uint32	timestamp_reply;
168 
169 	tcp_sack	*sacks;
170 	int			sack_count;
171 
172 	uint32	options;
173 
174 	bool AcknowledgeOnly() const
175 	{
176 		return (flags & (TCP_FLAG_SYNCHRONIZE | TCP_FLAG_FINISH | TCP_FLAG_RESET
177 			| TCP_FLAG_URGENT | TCP_FLAG_ACKNOWLEDGE)) == TCP_FLAG_ACKNOWLEDGE;
178 	}
179 };
180 
181 enum tcp_segment_action {
182 	KEEP		= 0x00,
183 	DROP		= 0x01,
184 	RESET		= 0x02,
185 	ACKNOWLEDGE	= 0x04,
186 	IMMEDIATE_ACKNOWLEDGE = 0x08,
187 };
188 
189 
190 extern net_buffer_module_info *gBufferModule;
191 extern net_datalink_module_info *gDatalinkModule;
192 extern net_socket_module_info *gSocketModule;
193 extern net_stack_module_info *gStackModule;
194 
195 
196 status_t add_tcp_header(net_address_module_info *addressModule,
197 	tcp_segment_header &segment, net_buffer *buffer);
198 size_t tcp_options_length(tcp_segment_header &segment);
199 
200 const char *name_for_state(tcp_state state);
201 
202 EndpointManager *create_endpoint_manager(net_domain *domain);
203 void return_endpoint_manager(EndpointManager *);
204 
205 #endif	// TCP_H
206