1 | /*
|
---|
2 | * Copyright (c) 2008 Lukas Mejdrech
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup tcp
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * TCP module.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef __NET_TCP_H__
|
---|
38 | #define __NET_TCP_H__
|
---|
39 |
|
---|
40 | #include <fibril_synch.h>
|
---|
41 |
|
---|
42 | #include <packet/packet.h>
|
---|
43 | #include <net_device.h>
|
---|
44 | #include <socket_core.h>
|
---|
45 | #include <tl_common.h>
|
---|
46 |
|
---|
47 | /** Type definition of the TCP global data.
|
---|
48 | * @see tcp_globals
|
---|
49 | */
|
---|
50 | typedef struct tcp_globals tcp_globals_t;
|
---|
51 |
|
---|
52 | /** Type definition of the TCP socket specific data.
|
---|
53 | * @see tcp_socket_data
|
---|
54 | */
|
---|
55 | typedef struct tcp_socket_data tcp_socket_data_t;
|
---|
56 |
|
---|
57 | /** Type definition of the TCP socket specific data pointer.
|
---|
58 | * @see tcp_socket_data
|
---|
59 | */
|
---|
60 | typedef tcp_socket_data_t * tcp_socket_data_ref;
|
---|
61 |
|
---|
62 | /** Type definition of the TCP operation data.
|
---|
63 | * @see tcp_operation
|
---|
64 | */
|
---|
65 | typedef struct tcp_operation tcp_operation_t;
|
---|
66 |
|
---|
67 | /** Type definition of the TCP operation data pointer.
|
---|
68 | * @see tcp_operation
|
---|
69 | */
|
---|
70 | typedef tcp_operation_t * tcp_operation_ref;
|
---|
71 |
|
---|
72 | /** TCP socket state type definition.
|
---|
73 | * @see tcp_socket_state
|
---|
74 | */
|
---|
75 | typedef enum tcp_socket_state tcp_socket_state_t;
|
---|
76 |
|
---|
77 | /** TCP socket state.
|
---|
78 | */
|
---|
79 | enum tcp_socket_state{
|
---|
80 | /** Initial.
|
---|
81 | * Not connected or bound.
|
---|
82 | */
|
---|
83 | TCP_SOCKET_INITIAL,
|
---|
84 | /** Listening.
|
---|
85 | * Awaiting a connection request from another TCP layer.
|
---|
86 | * When SYN is received a new bound socket in the TCP_SOCKET_SYN_RECEIVED state should be created.
|
---|
87 | */
|
---|
88 | TCP_SOCKET_LISTEN,
|
---|
89 | /** Connecting issued.
|
---|
90 | * A~SYN has been sent, and TCP is awaiting the response SYN.
|
---|
91 | * Should continue to the TCP_SOCKET_ESTABLISHED state.
|
---|
92 | */
|
---|
93 | TCP_SOCKET_SYN_SENT,
|
---|
94 | /** Connecting received.
|
---|
95 | * A~SYN has been received, a~SYN has been sent, and TCP is awaiting an ACK.
|
---|
96 | * Should continue to the TCP_SOCKET_ESTABLISHED state.
|
---|
97 | */
|
---|
98 | TCP_SOCKET_SYN_RECEIVED,
|
---|
99 | /** Connected.
|
---|
100 | * The three-way handshake has been completed.
|
---|
101 | */
|
---|
102 | TCP_SOCKET_ESTABLISHED,
|
---|
103 | /** Closing started.
|
---|
104 | * The local application has issued a~CLOSE.
|
---|
105 | * TCP has sent a~FIN, and is awaiting an ACK or a~FIN.
|
---|
106 | * Should continue to the TCP_SOCKET_FIN_WAIT_2 state when an ACK is received.
|
---|
107 | * Should continue to the TCP_SOCKET_CLOSING state when a~FIN is received.
|
---|
108 | */
|
---|
109 | TCP_SOCKET_FIN_WAIT_1,
|
---|
110 | /** Closing confirmed.
|
---|
111 | * A~FIN has been sent, and an ACK received.
|
---|
112 | * TCP is awaiting a~FIN from the remote TCP layer.
|
---|
113 | * Should continue to the TCP_SOCKET_CLOSING state.
|
---|
114 | */
|
---|
115 | TCP_SOCKET_FIN_WAIT_2,
|
---|
116 | /** Closing.
|
---|
117 | * A FIN has been sent, a FIN has been received, and an ACK has been sent.
|
---|
118 | * TCP is awaiting an ACK for the FIN that was sent.
|
---|
119 | * Should continue to the TCP_SOCKET_TIME_WAIT state.
|
---|
120 | */
|
---|
121 | TCP_SOCKET_CLOSING,
|
---|
122 | /** Closing received.
|
---|
123 | * TCP has received a~FIN, and has sent an ACK.
|
---|
124 | * It is awaiting a~close request from the local application before sending a~FIN.
|
---|
125 | * Should continue to the TCP_SOCKET_SOCKET_LAST_ACK state.
|
---|
126 | */
|
---|
127 | TCP_SOCKET_CLOSE_WAIT,
|
---|
128 | /**
|
---|
129 | * A~FIN has been received, and an ACK and a~FIN have been sent.
|
---|
130 | * TCP is awaiting an ACK.
|
---|
131 | * Should continue to the TCP_SOCKET_TIME_WAIT state.
|
---|
132 | */
|
---|
133 | TCP_SOCKET_LAST_ACK,
|
---|
134 | /** Closing finished.
|
---|
135 | * FINs have been received and ACK’d, and TCP is waiting two MSLs to remove the connection from the table.
|
---|
136 | */
|
---|
137 | TCP_SOCKET_TIME_WAIT,
|
---|
138 | /** Closed.
|
---|
139 | * Imaginary, this indicates that a~connection has been removed from the connection table.
|
---|
140 | */
|
---|
141 | TCP_SOCKET_CLOSED
|
---|
142 | };
|
---|
143 |
|
---|
144 | /** TCP operation data.
|
---|
145 | */
|
---|
146 | struct tcp_operation{
|
---|
147 | /** Operation result.
|
---|
148 | */
|
---|
149 | int result;
|
---|
150 | /** Safety lock.
|
---|
151 | */
|
---|
152 | fibril_mutex_t mutex;
|
---|
153 | /** Operation result signaling.
|
---|
154 | */
|
---|
155 | fibril_condvar_t condvar;
|
---|
156 | };
|
---|
157 |
|
---|
158 | /** TCP socket specific data.
|
---|
159 | */
|
---|
160 | struct tcp_socket_data{
|
---|
161 | /** TCP socket state.
|
---|
162 | */
|
---|
163 | tcp_socket_state_t state;
|
---|
164 | /** Data fragment size.
|
---|
165 | * Sending optimalization.
|
---|
166 | */
|
---|
167 | size_t data_fragment_size;
|
---|
168 | /** Device identifier.
|
---|
169 | */
|
---|
170 | device_id_t device_id;
|
---|
171 | /** Listening backlog.
|
---|
172 | * The maximal number of connected but not yet accepted sockets.
|
---|
173 | */
|
---|
174 | int backlog;
|
---|
175 | // /** Segment size.
|
---|
176 | // */
|
---|
177 | // size_t segment_size;
|
---|
178 | /** Parent listening socket identifier.
|
---|
179 | * Set if this socket is an accepted one.
|
---|
180 | */
|
---|
181 | int listening_socket_id;
|
---|
182 | /** Treshold size in bytes.
|
---|
183 | */
|
---|
184 | size_t treshold;
|
---|
185 | /** Window size in bytes.
|
---|
186 | */
|
---|
187 | size_t window;
|
---|
188 | /** Acknowledgement timeout.
|
---|
189 | */
|
---|
190 | suseconds_t timeout;
|
---|
191 | /** Last acknowledged byte.
|
---|
192 | */
|
---|
193 | uint32_t acknowledged;
|
---|
194 | /** Next incoming sequence number.
|
---|
195 | */
|
---|
196 | uint32_t next_incoming;
|
---|
197 | /** Incoming FIN.
|
---|
198 | */
|
---|
199 | uint32_t fin_incoming;
|
---|
200 | /** Next outgoing sequence number.
|
---|
201 | */
|
---|
202 | uint32_t next_outgoing;
|
---|
203 | /** Last outgoing sequence number.
|
---|
204 | */
|
---|
205 | uint32_t last_outgoing;
|
---|
206 | /** Outgoing FIN.
|
---|
207 | */
|
---|
208 | uint32_t fin_outgoing;
|
---|
209 | /** Expected sequence number by the remote host.
|
---|
210 | * The sequence number the other host expects.
|
---|
211 | * The notification is sent only upon a packet reecival.
|
---|
212 | */
|
---|
213 | uint32_t expected;
|
---|
214 | /** Expected sequence number counter.
|
---|
215 | * Counts the number of received notifications for the same sequence number.
|
---|
216 | */
|
---|
217 | int expected_count;
|
---|
218 | /** Incoming packet queue.
|
---|
219 | * Packets are buffered until received in the right order.
|
---|
220 | * The packets are excluded after successfully read.
|
---|
221 | * Packets are sorted by their starting byte.
|
---|
222 | * Packets metric is set as their data length.
|
---|
223 | */
|
---|
224 | packet_t incoming;
|
---|
225 | /** Outgoing packet queue.
|
---|
226 | * Packets are buffered until acknowledged by the remote host in the right order.
|
---|
227 | * The packets are excluded after acknowledged.
|
---|
228 | * Packets are sorted by their starting byte.
|
---|
229 | * Packets metric is set as their data length.
|
---|
230 | */
|
---|
231 | packet_t outgoing;
|
---|
232 | /** IP pseudo header.
|
---|
233 | */
|
---|
234 | void *pseudo_header;
|
---|
235 | /** IP pseudo header length.
|
---|
236 | */
|
---|
237 | size_t headerlen;
|
---|
238 | /** Remote host address.
|
---|
239 | */
|
---|
240 | struct sockaddr * addr;
|
---|
241 | /** Remote host address length.
|
---|
242 | */
|
---|
243 | socklen_t addrlen;
|
---|
244 | /** Remote host port.
|
---|
245 | */
|
---|
246 | uint16_t dest_port;
|
---|
247 | /** Parent local sockets.
|
---|
248 | */
|
---|
249 | socket_cores_ref local_sockets;
|
---|
250 | /** Local sockets safety lock.
|
---|
251 | * May be locked for writing while holding the global lock for reading when changing the local sockets only.
|
---|
252 | * The global lock may to be locked only before locking the local lock.
|
---|
253 | * The global lock may be locked more weakly than the local lock.
|
---|
254 | * The global lock may be released before releasing the local lock.
|
---|
255 | * @see tcp_globals:lock
|
---|
256 | */
|
---|
257 | fibril_rwlock_t * local_lock;
|
---|
258 | /** Pending operation data.
|
---|
259 | */
|
---|
260 | tcp_operation_t operation;
|
---|
261 | /** Timeouts in a row counter.
|
---|
262 | * If TCP_MAX_TIMEOUTS is reached, the connection is lost.
|
---|
263 | */
|
---|
264 | int timeout_count;
|
---|
265 | };
|
---|
266 |
|
---|
267 | /** TCP global data.
|
---|
268 | */
|
---|
269 | struct tcp_globals{
|
---|
270 | /** Networking module phone.
|
---|
271 | */
|
---|
272 | int net_phone;
|
---|
273 | /** IP module phone.
|
---|
274 | */
|
---|
275 | int ip_phone;
|
---|
276 | /** ICMP module phone.
|
---|
277 | */
|
---|
278 | int icmp_phone;
|
---|
279 | /** Last used free port.
|
---|
280 | */
|
---|
281 | int last_used_port;
|
---|
282 | /** Active sockets.
|
---|
283 | */
|
---|
284 | socket_ports_t sockets;
|
---|
285 | /** Device packet dimensions.
|
---|
286 | */
|
---|
287 | packet_dimensions_t dimensions;
|
---|
288 | /** Safety lock.
|
---|
289 | * Write lock is used only for adding or removing socket ports.
|
---|
290 | */
|
---|
291 | fibril_rwlock_t lock;
|
---|
292 | };
|
---|
293 |
|
---|
294 | #endif
|
---|
295 |
|
---|
296 | /** @}
|
---|
297 | */
|
---|
298 |
|
---|