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