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 implementation.
|
---|
35 | * @see tcp.h
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <assert.h>
|
---|
39 | #include <async.h>
|
---|
40 | #include <fibril_synch.h>
|
---|
41 | #include <malloc.h>
|
---|
42 | //TODO remove stdio
|
---|
43 | #include <stdio.h>
|
---|
44 |
|
---|
45 | #include <ipc/ipc.h>
|
---|
46 | #include <ipc/services.h>
|
---|
47 |
|
---|
48 | #include "../../err.h"
|
---|
49 | #include "../../messages.h"
|
---|
50 | #include "../../modules.h"
|
---|
51 |
|
---|
52 | #include "../../structures/dynamic_fifo.h"
|
---|
53 | #include "../../structures/packet/packet_client.h"
|
---|
54 |
|
---|
55 | #include "../../include/checksum.h"
|
---|
56 | #include "../../include/in.h"
|
---|
57 | #include "../../include/in6.h"
|
---|
58 | #include "../../include/inet.h"
|
---|
59 | #include "../../include/ip_client.h"
|
---|
60 | #include "../../include/ip_interface.h"
|
---|
61 | #include "../../include/ip_protocols.h"
|
---|
62 | #include "../../include/icmp_client.h"
|
---|
63 | #include "../../include/icmp_interface.h"
|
---|
64 | #include "../../include/net_interface.h"
|
---|
65 | #include "../../include/socket_codes.h"
|
---|
66 | #include "../../include/socket_errno.h"
|
---|
67 | #include "../../include/tcp_codes.h"
|
---|
68 |
|
---|
69 | #include "../../socket/socket_core.h"
|
---|
70 | #include "../../socket/socket_messages.h"
|
---|
71 |
|
---|
72 | #include "../tl_common.h"
|
---|
73 | #include "../tl_messages.h"
|
---|
74 |
|
---|
75 | #include "tcp.h"
|
---|
76 | #include "tcp_header.h"
|
---|
77 | #include "tcp_module.h"
|
---|
78 |
|
---|
79 | /** The TCP window default value.
|
---|
80 | */
|
---|
81 | #define NET_DEFAULT_TCP_WINDOW 10240
|
---|
82 |
|
---|
83 | /** Initial timeout for new connections.
|
---|
84 | */
|
---|
85 | #define NET_DEFAULT_TCP_INITIAL_TIMEOUT 3000000L
|
---|
86 |
|
---|
87 | /** Default timeout for closing.
|
---|
88 | */
|
---|
89 | #define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L
|
---|
90 |
|
---|
91 | /** The initial outgoing sequence number.
|
---|
92 | */
|
---|
93 | #define TCP_INITIAL_SEQUENCE_NUMBER 2999
|
---|
94 |
|
---|
95 | /** Maximum TCP fragment size.
|
---|
96 | */
|
---|
97 | #define MAX_TCP_FRAGMENT_SIZE 65535
|
---|
98 |
|
---|
99 | /** Free ports pool start.
|
---|
100 | */
|
---|
101 | #define TCP_FREE_PORTS_START 1025
|
---|
102 |
|
---|
103 | /** Free ports pool end.
|
---|
104 | */
|
---|
105 | #define TCP_FREE_PORTS_END 65535
|
---|
106 |
|
---|
107 | /** Timeout for connection initialization, SYN sent.
|
---|
108 | */
|
---|
109 | #define TCP_SYN_SENT_TIMEOUT 1000000L
|
---|
110 |
|
---|
111 | /** The maximum number of timeouts in a row before singaling connection lost.
|
---|
112 | */
|
---|
113 | #define TCP_MAX_TIMEOUTS 8
|
---|
114 |
|
---|
115 | /** The number of acknowledgements before retransmit.
|
---|
116 | */
|
---|
117 | #define TCP_FAST_RETRANSMIT_COUNT 3
|
---|
118 |
|
---|
119 | /** Returns a value indicating whether the value is in the interval respecting the possible overflow.
|
---|
120 | * The high end and/or the value may overflow, be lower than the low value.
|
---|
121 | * @param[in] lower The last value before the interval.
|
---|
122 | * @param[in] value The value to be checked.
|
---|
123 | * @param[in] higher_equal The last value in the interval.
|
---|
124 | */
|
---|
125 | #define IS_IN_INTERVAL_OVERFLOW( lower, value, higher_equal ) (((( lower ) < ( value )) && ((( value ) <= ( higher_equal )) || (( higher_equal ) < ( lower )))) || ((( value ) <= ( higher_equal )) && (( higher_equal ) < ( lower ))))
|
---|
126 |
|
---|
127 | /** Type definition of the TCP timeout.
|
---|
128 | * @see tcp_timeout
|
---|
129 | */
|
---|
130 | typedef struct tcp_timeout tcp_timeout_t;
|
---|
131 |
|
---|
132 | /** Type definition of the TCP timeout pointer.
|
---|
133 | * @see tcp_timeout
|
---|
134 | */
|
---|
135 | typedef tcp_timeout_t * tcp_timeout_ref;
|
---|
136 |
|
---|
137 | /** TCP reply timeout data.
|
---|
138 | * Used as a timeouting fibril argument.
|
---|
139 | * @see tcp_timeout()
|
---|
140 | */
|
---|
141 | struct tcp_timeout{
|
---|
142 | /** TCP global data are going to be read only.
|
---|
143 | */
|
---|
144 | int globals_read_only;
|
---|
145 | /** Socket port.
|
---|
146 | */
|
---|
147 | int port;
|
---|
148 | /** Local sockets.
|
---|
149 | */
|
---|
150 | socket_cores_ref local_sockets;
|
---|
151 | /** Socket identifier.
|
---|
152 | */
|
---|
153 | int socket_id;
|
---|
154 | /** Socket state.
|
---|
155 | */
|
---|
156 | tcp_socket_state_t state;
|
---|
157 | /** Sent packet sequence number.
|
---|
158 | */
|
---|
159 | int sequence_number;
|
---|
160 | /** Timeout in microseconds.
|
---|
161 | */
|
---|
162 | suseconds_t timeout;
|
---|
163 | /** Port map key.
|
---|
164 | */
|
---|
165 | char * key;
|
---|
166 | /** Port map key length.
|
---|
167 | */
|
---|
168 | size_t key_length;
|
---|
169 | };
|
---|
170 |
|
---|
171 | /** Releases the packet and returns the result.
|
---|
172 | * @param[in] packet The packet queue to be released.
|
---|
173 | * @param[in] result The result to be returned.
|
---|
174 | * @return The result parameter.
|
---|
175 | */
|
---|
176 | int tcp_release_and_return( packet_t packet, int result );
|
---|
177 |
|
---|
178 | void tcp_prepare_operation_header( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize );
|
---|
179 | int tcp_prepare_timeout( int ( * timeout_function )( void * tcp_timeout_t ), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only );
|
---|
180 | void tcp_free_socket_data( socket_core_ref socket );
|
---|
181 | int tcp_timeout( void * data );
|
---|
182 | int tcp_release_after_timeout( void * data );
|
---|
183 | int tcp_process_packet( device_id_t device_id, packet_t packet, services_t error );
|
---|
184 | int tcp_connect_core( socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen );
|
---|
185 | int tcp_queue_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length );
|
---|
186 | int tcp_queue_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length );
|
---|
187 | packet_t tcp_get_packets_to_send( socket_core_ref socket, tcp_socket_data_ref socket_data );
|
---|
188 | void tcp_send_packets( device_id_t device_id, packet_t packet );
|
---|
189 | void tcp_process_acknowledgement( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header );
|
---|
190 | packet_t tcp_send_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number );
|
---|
191 | packet_t tcp_prepare_copy( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number );
|
---|
192 | void tcp_retransmit_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number );
|
---|
193 | int tcp_create_notification_packet( packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize );
|
---|
194 | void tcp_refresh_socket_data( tcp_socket_data_ref socket_data );
|
---|
195 | void tcp_initialize_socket_data( tcp_socket_data_ref socket_data );
|
---|
196 | int tcp_process_listen( socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen );
|
---|
197 | int tcp_process_syn_sent( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet );
|
---|
198 | int tcp_process_syn_received( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet );
|
---|
199 | int tcp_process_established( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length );
|
---|
200 | int tcp_queue_received_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length );
|
---|
201 |
|
---|
202 | int tcp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error );
|
---|
203 | int tcp_process_client_messages( ipc_callid_t callid, ipc_call_t call );
|
---|
204 | int tcp_listen_message( socket_cores_ref local_sockets, int socket_id, int backlog );
|
---|
205 | int tcp_connect_message( socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen );
|
---|
206 | int tcp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen );
|
---|
207 | int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags );
|
---|
208 | int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen );
|
---|
209 | int tcp_close_message( socket_cores_ref local_sockets, int socket_id );
|
---|
210 |
|
---|
211 | /** TCP global data.
|
---|
212 | */
|
---|
213 | tcp_globals_t tcp_globals;
|
---|
214 |
|
---|
215 | int tcp_initialize( async_client_conn_t client_connection ){
|
---|
216 | ERROR_DECLARE;
|
---|
217 |
|
---|
218 | assert( client_connection );
|
---|
219 | fibril_rwlock_initialize( & tcp_globals.lock );
|
---|
220 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
221 | tcp_globals.icmp_phone = icmp_connect_module( SERVICE_ICMP, ICMP_CONNECT_TIMEOUT );
|
---|
222 | tcp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_TCP, SERVICE_TCP, client_connection, tcp_received_msg );
|
---|
223 | if( tcp_globals.ip_phone < 0 ){
|
---|
224 | return tcp_globals.ip_phone;
|
---|
225 | }
|
---|
226 | ERROR_PROPAGATE( socket_ports_initialize( & tcp_globals.sockets ));
|
---|
227 | if( ERROR_OCCURRED( packet_dimensions_initialize( & tcp_globals.dimensions ))){
|
---|
228 | socket_ports_destroy( & tcp_globals.sockets );
|
---|
229 | return ERROR_CODE;
|
---|
230 | }
|
---|
231 | tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
|
---|
232 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
233 | return EOK;
|
---|
234 | }
|
---|
235 |
|
---|
236 | int tcp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){
|
---|
237 | ERROR_DECLARE;
|
---|
238 |
|
---|
239 | if( receiver != SERVICE_TCP ) return EREFUSED;
|
---|
240 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
241 | if( ERROR_OCCURRED( tcp_process_packet( device_id, packet, error ))){
|
---|
242 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
243 | }
|
---|
244 | printf( "receive %d \n", ERROR_CODE );
|
---|
245 |
|
---|
246 | return ERROR_CODE;
|
---|
247 | }
|
---|
248 |
|
---|
249 | int tcp_process_packet( device_id_t device_id, packet_t packet, services_t error ){
|
---|
250 | ERROR_DECLARE;
|
---|
251 |
|
---|
252 | size_t length;
|
---|
253 | size_t offset;
|
---|
254 | int result;
|
---|
255 | tcp_header_ref header;
|
---|
256 | socket_core_ref socket;
|
---|
257 | tcp_socket_data_ref socket_data;
|
---|
258 | packet_t next_packet;
|
---|
259 | size_t total_length;
|
---|
260 | uint32_t checksum;
|
---|
261 | int fragments;
|
---|
262 | icmp_type_t type;
|
---|
263 | icmp_code_t code;
|
---|
264 | struct sockaddr * src;
|
---|
265 | struct sockaddr * dest;
|
---|
266 | size_t addrlen;
|
---|
267 |
|
---|
268 | printf( "p1 \n" );
|
---|
269 | if( error ){
|
---|
270 | switch( error ){
|
---|
271 | case SERVICE_ICMP:
|
---|
272 | // process error
|
---|
273 | result = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
|
---|
274 | if( result < 0 ){
|
---|
275 | return tcp_release_and_return( packet, result );
|
---|
276 | }
|
---|
277 | length = ( size_t ) result;
|
---|
278 | if( ERROR_OCCURRED( packet_trim( packet, length, 0 ))){
|
---|
279 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
280 | }
|
---|
281 | break;
|
---|
282 | default:
|
---|
283 | return tcp_release_and_return( packet, ENOTSUP );
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | // TODO process received ipopts?
|
---|
288 | result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL );
|
---|
289 | // printf("ip len %d\n", result );
|
---|
290 | if( result < 0 ){
|
---|
291 | return tcp_release_and_return( packet, result );
|
---|
292 | }
|
---|
293 | offset = ( size_t ) result;
|
---|
294 |
|
---|
295 | length = packet_get_data_length( packet );
|
---|
296 | // printf("packet len %d\n", length );
|
---|
297 | if( length <= 0 ){
|
---|
298 | return tcp_release_and_return( packet, EINVAL );
|
---|
299 | }
|
---|
300 | if( length < TCP_HEADER_SIZE + offset ){
|
---|
301 | return tcp_release_and_return( packet, NO_DATA );
|
---|
302 | }
|
---|
303 |
|
---|
304 | // trim all but TCP header
|
---|
305 | if( ERROR_OCCURRED( packet_trim( packet, offset, 0 ))){
|
---|
306 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
307 | }
|
---|
308 |
|
---|
309 | // get tcp header
|
---|
310 | header = ( tcp_header_ref ) packet_get_data( packet );
|
---|
311 | if( ! header ){
|
---|
312 | return tcp_release_and_return( packet, NO_DATA );
|
---|
313 | }
|
---|
314 | // printf( "header len %d, port %d \n", TCP_HEADER_LENGTH( header ), ntohs( header->destination_port ));
|
---|
315 |
|
---|
316 | result = packet_get_addr( packet, ( uint8_t ** ) & src, ( uint8_t ** ) & dest );
|
---|
317 | if( result <= 0 ){
|
---|
318 | return tcp_release_and_return( packet, result );
|
---|
319 | }
|
---|
320 | addrlen = ( size_t ) result;
|
---|
321 |
|
---|
322 | if( ERROR_OCCURRED( tl_set_address_port( src, addrlen, ntohs( header->source_port )))){
|
---|
323 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
324 | }
|
---|
325 |
|
---|
326 | // find the destination socket
|
---|
327 | socket = socket_port_find( & tcp_globals.sockets, ntohs( header->destination_port ), ( const char * ) src, addrlen );
|
---|
328 | if( ! socket ){
|
---|
329 | // printf("listening?\n");
|
---|
330 | // find the listening destination socket
|
---|
331 | socket = socket_port_find( & tcp_globals.sockets, ntohs( header->destination_port ), SOCKET_MAP_KEY_LISTENING, 0 );
|
---|
332 | if( ! socket ){
|
---|
333 | if( tl_prepare_icmp_packet( tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error ) == EOK ){
|
---|
334 | icmp_destination_unreachable_msg( tcp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet );
|
---|
335 | }
|
---|
336 | return EADDRNOTAVAIL;
|
---|
337 | }
|
---|
338 | }
|
---|
339 | printf("socket id %d\n", socket->socket_id );
|
---|
340 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
341 | assert( socket_data );
|
---|
342 |
|
---|
343 | // some data received, clear the timeout counter
|
---|
344 | socket_data->timeout_count = 0;
|
---|
345 |
|
---|
346 | // count the received packet fragments
|
---|
347 | next_packet = packet;
|
---|
348 | fragments = 0;
|
---|
349 | checksum = 0;
|
---|
350 | total_length = 0;
|
---|
351 | do{
|
---|
352 | ++ fragments;
|
---|
353 | length = packet_get_data_length( next_packet );
|
---|
354 | if( length <= 0 ){
|
---|
355 | return tcp_release_and_return( packet, NO_DATA );
|
---|
356 | }
|
---|
357 | total_length += length;
|
---|
358 | // add partial checksum if set
|
---|
359 | if( ! error ){
|
---|
360 | checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet ));
|
---|
361 | }
|
---|
362 | }while(( next_packet = pq_next( next_packet )));
|
---|
363 | // printf( "fragments %d of %d bytes\n", fragments, total_length );
|
---|
364 |
|
---|
365 | // printf("lock?\n");
|
---|
366 | fibril_rwlock_write_lock( socket_data->local_lock );
|
---|
367 | // printf("locked\n");
|
---|
368 | if( ! error ){
|
---|
369 | if( socket_data->state == TCP_SOCKET_LISTEN ){
|
---|
370 | if( socket_data->pseudo_header ){
|
---|
371 | free( socket_data->pseudo_header );
|
---|
372 | socket_data->pseudo_header = NULL;
|
---|
373 | socket_data->headerlen = 0;
|
---|
374 | }
|
---|
375 | if( ERROR_OCCURRED( ip_client_get_pseudo_header( IPPROTO_TCP, src, addrlen, dest, addrlen, total_length, & socket_data->pseudo_header, & socket_data->headerlen ))){
|
---|
376 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
377 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
378 | }
|
---|
379 | }else if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( socket_data->pseudo_header, socket_data->headerlen, total_length ))){
|
---|
380 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
381 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
382 | }
|
---|
383 | checksum = compute_checksum( checksum, socket_data->pseudo_header, socket_data->headerlen );
|
---|
384 | if( flip_checksum( compact_checksum( checksum )) != IP_CHECKSUM_ZERO ){
|
---|
385 | printf( "checksum err %x -> %x\n", header->checksum, flip_checksum( compact_checksum( checksum )));
|
---|
386 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
387 | if( ! ERROR_OCCURRED( tl_prepare_icmp_packet( tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error ))){
|
---|
388 | // checksum error ICMP
|
---|
389 | icmp_parameter_problem_msg( tcp_globals.icmp_phone, ICMP_PARAM_POINTER, (( size_t ) (( void * ) & header->checksum )) - (( size_t ) (( void * ) header )), packet );
|
---|
390 | }
|
---|
391 | return EINVAL;
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
396 |
|
---|
397 | // TODO error reporting/handling
|
---|
398 | // printf( "st %d\n", socket_data->state );
|
---|
399 | switch( socket_data->state ){
|
---|
400 | case TCP_SOCKET_LISTEN:
|
---|
401 | ERROR_CODE = tcp_process_listen( socket, socket_data, header, packet, src, dest, addrlen );
|
---|
402 | break;
|
---|
403 | case TCP_SOCKET_SYN_RECEIVED:
|
---|
404 | ERROR_CODE = tcp_process_syn_received( socket, socket_data, header, packet );
|
---|
405 | break;
|
---|
406 | case TCP_SOCKET_SYN_SENT:
|
---|
407 | ERROR_CODE = tcp_process_syn_sent( socket, socket_data, header, packet );
|
---|
408 | break;
|
---|
409 | case TCP_SOCKET_FIN_WAIT_1:
|
---|
410 | // ack changing the state to FIN_WAIT_2 gets processed later
|
---|
411 | case TCP_SOCKET_FIN_WAIT_2:
|
---|
412 | // fin changing state to LAST_ACK gets processed later
|
---|
413 | case TCP_SOCKET_LAST_ACK:
|
---|
414 | // ack releasing the socket get processed later
|
---|
415 | case TCP_SOCKET_CLOSING:
|
---|
416 | // ack releasing the socket gets processed later
|
---|
417 | case TCP_SOCKET_ESTABLISHED:
|
---|
418 | ERROR_CODE = tcp_process_established( socket, socket_data, header, packet, fragments, total_length );
|
---|
419 | break;
|
---|
420 | default:
|
---|
421 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
422 | }
|
---|
423 |
|
---|
424 | if( ERROR_CODE != EOK ){
|
---|
425 | printf( "process %d\n", ERROR_CODE );
|
---|
426 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
427 | }
|
---|
428 | return EOK;
|
---|
429 | }
|
---|
430 |
|
---|
431 | int tcp_process_established( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length ){
|
---|
432 | ERROR_DECLARE;
|
---|
433 |
|
---|
434 | packet_t next_packet;
|
---|
435 | packet_t tmp_packet;
|
---|
436 | uint32_t old_incoming;
|
---|
437 | size_t order;
|
---|
438 | uint32_t sequence_number;
|
---|
439 | size_t length;
|
---|
440 | size_t offset;
|
---|
441 | uint32_t new_sequence_number;
|
---|
442 |
|
---|
443 | assert( socket );
|
---|
444 | assert( socket_data );
|
---|
445 | assert( socket->specific_data == socket_data );
|
---|
446 | assert( header );
|
---|
447 | assert( packet );
|
---|
448 |
|
---|
449 | new_sequence_number = ntohl( header->sequence_number );
|
---|
450 | old_incoming = socket_data->next_incoming;
|
---|
451 |
|
---|
452 | if( header->finalize ){
|
---|
453 | socket_data->fin_incoming = new_sequence_number;
|
---|
454 | }
|
---|
455 |
|
---|
456 | // printf( "pe %d < %d <= %d\n", new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length );
|
---|
457 | // trim begining if containing expected data
|
---|
458 | if( IS_IN_INTERVAL_OVERFLOW( new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length )){
|
---|
459 | // get the acknowledged offset
|
---|
460 | if( socket_data->next_incoming < new_sequence_number ){
|
---|
461 | offset = new_sequence_number - socket_data->next_incoming;
|
---|
462 | }else{
|
---|
463 | offset = socket_data->next_incoming - new_sequence_number;
|
---|
464 | }
|
---|
465 | // printf( "offset %d\n", offset );
|
---|
466 | new_sequence_number += offset;
|
---|
467 | total_length -= offset;
|
---|
468 | length = packet_get_data_length( packet );
|
---|
469 | // trim the acknowledged data
|
---|
470 | while( length <= offset ){
|
---|
471 | // release the acknowledged packets
|
---|
472 | next_packet = pq_next( packet );
|
---|
473 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
474 | packet = next_packet;
|
---|
475 | offset -= length;
|
---|
476 | length = packet_get_data_length( packet );
|
---|
477 | }
|
---|
478 | if(( offset > 0 )
|
---|
479 | && ( ERROR_OCCURRED( packet_trim( packet, offset, 0 )))){
|
---|
480 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
481 | }
|
---|
482 | assert( new_sequence_number == socket_data->next_incoming );
|
---|
483 | }
|
---|
484 |
|
---|
485 | // release if overflowing the window
|
---|
486 | // if( IS_IN_INTERVAL_OVERFLOW( socket_data->next_incoming + socket_data->window, new_sequence_number, new_sequence_number + total_length )){
|
---|
487 | // return tcp_release_and_return( packet, EOVERFLOW );
|
---|
488 | // }
|
---|
489 |
|
---|
490 | /*
|
---|
491 | // trim end if overflowing the window
|
---|
492 | if( IS_IN_INTERVAL_OVERFLOW( new_sequence_number, socket_data->next_incoming + socket_data->window, new_sequence_number + total_length )){
|
---|
493 | // get the allowed data length
|
---|
494 | if( socket_data->next_incoming + socket_data->window < new_sequence_number ){
|
---|
495 | offset = new_sequence_number - socket_data->next_incoming + socket_data->window;
|
---|
496 | }else{
|
---|
497 | offset = socket_data->next_incoming + socket_data->window - new_sequence_number;
|
---|
498 | }
|
---|
499 | next_packet = packet;
|
---|
500 | // trim the overflowing data
|
---|
501 | while( next_packet && ( offset > 0 )){
|
---|
502 | length = packet_get_data_length( packet );
|
---|
503 | if( length <= offset ){
|
---|
504 | next_packet = pq_next( next_packet );
|
---|
505 | }else if( ERROR_OCCURRED( packet_trim( next_packet, 0, length - offset ))){
|
---|
506 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
507 | }
|
---|
508 | offset -= length;
|
---|
509 | total_length -= length - offset;
|
---|
510 | }
|
---|
511 | // release the overflowing packets
|
---|
512 | next_packet = pq_next( next_packet );
|
---|
513 | if( next_packet ){
|
---|
514 | tmp_packet = next_packet;
|
---|
515 | next_packet = pq_next( next_packet );
|
---|
516 | pq_insert_after( tmp_packet, next_packet );
|
---|
517 | pq_release( tcp_globals.net_phone, packet_get_id( tmp_packet ));
|
---|
518 | }
|
---|
519 | assert( new_sequence_number + total_length == socket_data->next_incoming + socket_data->window );
|
---|
520 | }
|
---|
521 | */
|
---|
522 | // the expected one arrived?
|
---|
523 | if( new_sequence_number == socket_data->next_incoming ){
|
---|
524 | printf("expected\n");
|
---|
525 | // process acknowledgement
|
---|
526 | tcp_process_acknowledgement( socket, socket_data, header );
|
---|
527 |
|
---|
528 | // remove the header
|
---|
529 | total_length -= TCP_HEADER_LENGTH( header );
|
---|
530 | if( ERROR_OCCURRED( packet_trim( packet, TCP_HEADER_LENGTH( header ), 0 ))){
|
---|
531 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
532 | }
|
---|
533 |
|
---|
534 | if( total_length ){
|
---|
535 | ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, fragments, total_length ));
|
---|
536 | }else{
|
---|
537 | total_length = 1;
|
---|
538 | }
|
---|
539 | socket_data->next_incoming = old_incoming + total_length;
|
---|
540 | packet = socket_data->incoming;
|
---|
541 | while( packet ){
|
---|
542 | if( ERROR_OCCURRED( pq_get_order( socket_data->incoming, & order, NULL ))){
|
---|
543 | // remove the corrupted packet
|
---|
544 | next_packet = pq_detach( packet );
|
---|
545 | if( packet == socket_data->incoming ){
|
---|
546 | socket_data->incoming = next_packet;
|
---|
547 | }
|
---|
548 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
549 | packet = next_packet;
|
---|
550 | continue;
|
---|
551 | }
|
---|
552 | sequence_number = ( uint32_t ) order;
|
---|
553 | if( IS_IN_INTERVAL_OVERFLOW( sequence_number, old_incoming, socket_data->next_incoming )){
|
---|
554 | // move to the next
|
---|
555 | packet = pq_next( packet );
|
---|
556 | // coninual data?
|
---|
557 | }else if( IS_IN_INTERVAL_OVERFLOW( old_incoming, sequence_number, socket_data->next_incoming )){
|
---|
558 | // detach the packet
|
---|
559 | next_packet = pq_detach( packet );
|
---|
560 | if( packet == socket_data->incoming ){
|
---|
561 | socket_data->incoming = next_packet;
|
---|
562 | }
|
---|
563 | // get data length
|
---|
564 | length = packet_get_data_length( packet );
|
---|
565 | new_sequence_number = sequence_number + length;
|
---|
566 | if( length <= 0 ){
|
---|
567 | // remove the empty packet
|
---|
568 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
569 | packet = next_packet;
|
---|
570 | continue;
|
---|
571 | }
|
---|
572 | // exactly following
|
---|
573 | if( sequence_number == socket_data->next_incoming ){
|
---|
574 | // queue received data
|
---|
575 | ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, 1, packet_get_data_length( packet )));
|
---|
576 | socket_data->next_incoming = new_sequence_number;
|
---|
577 | packet = next_packet;
|
---|
578 | continue;
|
---|
579 | // at least partly following data?
|
---|
580 | }else if( IS_IN_INTERVAL_OVERFLOW( sequence_number, socket_data->next_incoming, new_sequence_number )){
|
---|
581 | if( socket_data->next_incoming < new_sequence_number ){
|
---|
582 | length = new_sequence_number - socket_data->next_incoming;
|
---|
583 | }else{
|
---|
584 | length = socket_data->next_incoming - new_sequence_number;
|
---|
585 | }
|
---|
586 | if( ! ERROR_OCCURRED( packet_trim( packet, length, 0 ))){
|
---|
587 | // queue received data
|
---|
588 | ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, 1, packet_get_data_length( packet )));
|
---|
589 | socket_data->next_incoming = new_sequence_number;
|
---|
590 | packet = next_packet;
|
---|
591 | continue;
|
---|
592 | }
|
---|
593 | }
|
---|
594 | // remove the duplicit or corrupted packet
|
---|
595 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
596 | packet = next_packet;
|
---|
597 | continue;
|
---|
598 | }else{
|
---|
599 | break;
|
---|
600 | }
|
---|
601 | }
|
---|
602 | }else if( IS_IN_INTERVAL( socket_data->next_incoming, new_sequence_number, socket_data->next_incoming + socket_data->window )){
|
---|
603 | printf("in window\n");
|
---|
604 | // process acknowledgement
|
---|
605 | tcp_process_acknowledgement( socket, socket_data, header );
|
---|
606 |
|
---|
607 | // remove the header
|
---|
608 | total_length -= TCP_HEADER_LENGTH( header );
|
---|
609 | if( ERROR_OCCURRED( packet_trim( packet, TCP_HEADER_LENGTH( header ), 0 ))){
|
---|
610 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
611 | }
|
---|
612 |
|
---|
613 | next_packet = pq_detach( packet );
|
---|
614 | length = packet_get_data_length( packet );
|
---|
615 | if( ERROR_OCCURRED( pq_add( & socket_data->incoming, packet, new_sequence_number, length ))){
|
---|
616 | // remove the corrupted packets
|
---|
617 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
618 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
|
---|
619 | }else{
|
---|
620 | while( next_packet ){
|
---|
621 | new_sequence_number += length;
|
---|
622 | tmp_packet = pq_detach( next_packet );
|
---|
623 | length = packet_get_data_length( next_packet );
|
---|
624 | if( ERROR_OCCURRED( pq_set_order( next_packet, new_sequence_number, length ))
|
---|
625 | || ERROR_OCCURRED( pq_insert_after( packet, next_packet ))){
|
---|
626 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
|
---|
627 | }
|
---|
628 | next_packet = tmp_packet;
|
---|
629 | }
|
---|
630 | }
|
---|
631 | }else{
|
---|
632 | printf("unexpected\n");
|
---|
633 | // release duplicite or restricted
|
---|
634 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
635 | }
|
---|
636 |
|
---|
637 | // change state according to the acknowledging incoming fin
|
---|
638 | if( IS_IN_INTERVAL_OVERFLOW( old_incoming, socket_data->fin_incoming, socket_data->next_incoming )){
|
---|
639 | switch( socket_data->state ){
|
---|
640 | case TCP_SOCKET_FIN_WAIT_1:
|
---|
641 | case TCP_SOCKET_FIN_WAIT_2:
|
---|
642 | case TCP_SOCKET_CLOSING:
|
---|
643 | socket_data->state = TCP_SOCKET_CLOSING;
|
---|
644 | break;
|
---|
645 | //case TCP_ESTABLISHED:
|
---|
646 | default:
|
---|
647 | socket_data->state = TCP_SOCKET_CLOSE_WAIT;
|
---|
648 | break;
|
---|
649 | }
|
---|
650 | }
|
---|
651 |
|
---|
652 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
653 | if( ! packet ){
|
---|
654 | // create the notification packet
|
---|
655 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 0 ));
|
---|
656 | ERROR_PROPAGATE( tcp_queue_prepare_packet( socket, socket_data, packet, 1 ));
|
---|
657 | packet = tcp_send_prepare_packet( socket, socket_data, packet, 1, socket_data->last_outgoing + 1 );
|
---|
658 | }
|
---|
659 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
660 | // send the packet
|
---|
661 | tcp_send_packets( socket_data->device_id, packet );
|
---|
662 | return EOK;
|
---|
663 | }
|
---|
664 |
|
---|
665 | int tcp_queue_received_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length ){
|
---|
666 | ERROR_DECLARE;
|
---|
667 |
|
---|
668 | packet_dimension_ref packet_dimension;
|
---|
669 |
|
---|
670 | assert( socket );
|
---|
671 | assert( socket_data );
|
---|
672 | assert( socket->specific_data == socket_data );
|
---|
673 | assert( packet );
|
---|
674 | assert( fragments >= 1 );
|
---|
675 | assert( socket_data->window > total_length );
|
---|
676 |
|
---|
677 | // queue the received packet
|
---|
678 | if( ERROR_OCCURRED( dyn_fifo_push( & socket->received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))
|
---|
679 | || ERROR_OCCURRED( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ))){
|
---|
680 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
681 | }
|
---|
682 |
|
---|
683 | // decrease the window size
|
---|
684 | socket_data->window -= total_length;
|
---|
685 |
|
---|
686 | // notify the destination socket
|
---|
687 | async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size ), 0, 0, ( ipcarg_t ) fragments );
|
---|
688 | return EOK;
|
---|
689 | }
|
---|
690 |
|
---|
691 | int tcp_process_syn_sent( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ){
|
---|
692 | ERROR_DECLARE;
|
---|
693 |
|
---|
694 | packet_t next_packet;
|
---|
695 |
|
---|
696 | assert( socket );
|
---|
697 | assert( socket_data );
|
---|
698 | assert( socket->specific_data == socket_data );
|
---|
699 | assert( header );
|
---|
700 | assert( packet );
|
---|
701 |
|
---|
702 | if( header->synchronize ){
|
---|
703 | // process acknowledgement
|
---|
704 | tcp_process_acknowledgement( socket, socket_data, header );
|
---|
705 |
|
---|
706 | socket_data->next_incoming = ntohl( header->sequence_number ) + 1;
|
---|
707 | // release additional packets
|
---|
708 | next_packet = pq_detach( packet );
|
---|
709 | if( next_packet ){
|
---|
710 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
|
---|
711 | }
|
---|
712 | // trim if longer than the header
|
---|
713 | if(( packet_get_data_length( packet ) > sizeof( * header ))
|
---|
714 | && ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - sizeof( * header )))){
|
---|
715 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
716 | }
|
---|
717 | tcp_prepare_operation_header( socket, socket_data, header, 0, 0 );
|
---|
718 | fibril_mutex_lock( & socket_data->operation.mutex );
|
---|
719 | socket_data->operation.result = tcp_queue_packet( socket, socket_data, packet, 1 );
|
---|
720 | if( socket_data->operation.result == EOK ){
|
---|
721 | socket_data->state = TCP_SOCKET_ESTABLISHED;
|
---|
722 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
723 | if( packet ){
|
---|
724 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
725 | // send the packet
|
---|
726 | tcp_send_packets( socket_data->device_id, packet );
|
---|
727 | // signal the result
|
---|
728 | fibril_condvar_signal( & socket_data->operation.condvar );
|
---|
729 | fibril_mutex_unlock( & socket_data->operation.mutex );
|
---|
730 | return EOK;
|
---|
731 | }
|
---|
732 | }
|
---|
733 | fibril_mutex_unlock( & socket_data->operation.mutex );
|
---|
734 | }
|
---|
735 | return tcp_release_and_return( packet, EINVAL );
|
---|
736 | }
|
---|
737 |
|
---|
738 | int tcp_process_listen( socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen ){
|
---|
739 | ERROR_DECLARE;
|
---|
740 |
|
---|
741 | packet_t next_packet;
|
---|
742 | socket_core_ref socket;
|
---|
743 | tcp_socket_data_ref socket_data;
|
---|
744 | int socket_id;
|
---|
745 | int listening_socket_id = listening_socket->socket_id;
|
---|
746 | int listening_port = listening_socket->port;
|
---|
747 |
|
---|
748 | assert( listening_socket );
|
---|
749 | assert( listening_socket_data );
|
---|
750 | assert( listening_socket->specific_data == listening_socket_data );
|
---|
751 | assert( header );
|
---|
752 | assert( packet );
|
---|
753 |
|
---|
754 | // printf( "syn %d\n", header->synchronize );
|
---|
755 | if( header->synchronize ){
|
---|
756 | socket_data = ( tcp_socket_data_ref ) malloc( sizeof( * socket_data ));
|
---|
757 | if( ! socket_data ){
|
---|
758 | return tcp_release_and_return( packet, ENOMEM );
|
---|
759 | }else{
|
---|
760 | tcp_initialize_socket_data( socket_data );
|
---|
761 | socket_data->local_lock = listening_socket_data->local_lock;
|
---|
762 | socket_data->local_sockets = listening_socket_data->local_sockets;
|
---|
763 | socket_data->listening_socket_id = listening_socket->socket_id;
|
---|
764 |
|
---|
765 | socket_data->next_incoming = ntohl( header->sequence_number );
|
---|
766 | socket_data->treshold = socket_data->next_incoming + ntohs( header->window );
|
---|
767 |
|
---|
768 | socket_data->addrlen = addrlen;
|
---|
769 | socket_data->addr = malloc( socket_data->addrlen );
|
---|
770 | if( ! socket_data->addr ){
|
---|
771 | free( socket_data );
|
---|
772 | return tcp_release_and_return( packet, ENOMEM );
|
---|
773 | }
|
---|
774 | memcpy( socket_data->addr, src, socket_data->addrlen );
|
---|
775 |
|
---|
776 | socket_data->dest_port = ntohs( header->source_port );
|
---|
777 | if( ERROR_OCCURRED( tl_set_address_port( socket_data->addr, socket_data->addrlen, socket_data->dest_port ))){
|
---|
778 | free( socket_data->addr );
|
---|
779 | free( socket_data );
|
---|
780 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
781 | return ERROR_CODE;
|
---|
782 | }
|
---|
783 |
|
---|
784 | // printf( "addr %p\n", socket_data->addr, socket_data->addrlen );
|
---|
785 | // create a socket
|
---|
786 | socket_id = -1;
|
---|
787 | if( ERROR_OCCURRED( socket_create( socket_data->local_sockets, listening_socket->phone, socket_data, & socket_id ))){
|
---|
788 | free( socket_data->addr );
|
---|
789 | free( socket_data );
|
---|
790 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
791 | }
|
---|
792 |
|
---|
793 | printf("new_sock %d\n", socket_id);
|
---|
794 | socket_data->pseudo_header = listening_socket_data->pseudo_header;
|
---|
795 | socket_data->headerlen = listening_socket_data->headerlen;
|
---|
796 | listening_socket_data->pseudo_header = NULL;
|
---|
797 | listening_socket_data->headerlen = 0;
|
---|
798 |
|
---|
799 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
800 | // printf("list lg\n");
|
---|
801 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
802 | // printf("list locked\n");
|
---|
803 | // find the destination socket
|
---|
804 | listening_socket = socket_port_find( & tcp_globals.sockets, listening_port, SOCKET_MAP_KEY_LISTENING, 0 );
|
---|
805 | if(( ! listening_socket ) || ( listening_socket->socket_id != listening_socket_id )){
|
---|
806 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
807 | // a shadow may remain until app hangs up
|
---|
808 | return tcp_release_and_return( packet, EOK/*ENOTSOCK*/ );
|
---|
809 | }
|
---|
810 | // printf("port %d\n", listening_socket->port );
|
---|
811 | listening_socket_data = ( tcp_socket_data_ref ) listening_socket->specific_data;
|
---|
812 | assert( listening_socket_data );
|
---|
813 |
|
---|
814 | // printf("list ll\n");
|
---|
815 | fibril_rwlock_write_lock( listening_socket_data->local_lock );
|
---|
816 | // printf("list locked\n");
|
---|
817 |
|
---|
818 | socket = socket_cores_find( listening_socket_data->local_sockets, socket_id );
|
---|
819 | if( ! socket ){
|
---|
820 | // where is the socket?!?
|
---|
821 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
822 | return ENOTSOCK;
|
---|
823 | }
|
---|
824 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
825 | assert( socket_data );
|
---|
826 |
|
---|
827 | // uint8_t * data = socket_data->addr;
|
---|
828 | // printf( "addr %d of %x %x %x %x-%x %x %x %x-%x %x %x %x-%x %x %x %x\n", socket_data->addrlen, data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ] );
|
---|
829 |
|
---|
830 | ERROR_CODE = socket_port_add( & tcp_globals.sockets, listening_port, socket, ( const char * ) socket_data->addr, socket_data->addrlen );
|
---|
831 | assert( socket == socket_port_find( & tcp_globals.sockets, listening_port, ( const char * ) socket_data->addr, socket_data->addrlen ));
|
---|
832 | //ERROR_CODE = socket_bind_free_port( & tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port );
|
---|
833 | //tcp_globals.last_used_port = socket->port;
|
---|
834 | // printf("bound %d\n", socket->port );
|
---|
835 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
836 | if( ERROR_CODE != EOK ){
|
---|
837 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
838 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
839 | }
|
---|
840 |
|
---|
841 | socket_data->state = TCP_SOCKET_LISTEN;
|
---|
842 | socket_data->next_incoming = ntohl( header->sequence_number ) + 1;
|
---|
843 | // release additional packets
|
---|
844 | next_packet = pq_detach( packet );
|
---|
845 | if( next_packet ){
|
---|
846 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
|
---|
847 | }
|
---|
848 | // trim if longer than the header
|
---|
849 | if(( packet_get_data_length( packet ) > sizeof( * header ))
|
---|
850 | && ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - sizeof( * header )))){
|
---|
851 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
852 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
853 | }
|
---|
854 | tcp_prepare_operation_header( socket, socket_data, header, 1, 0 );
|
---|
855 | if( ERROR_OCCURRED( tcp_queue_packet( socket, socket_data, packet, 1 ))){
|
---|
856 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
857 | return ERROR_CODE;
|
---|
858 | }
|
---|
859 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
860 | // printf("send %d\n", packet_get_id( packet ));
|
---|
861 | if( ! packet ){
|
---|
862 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
863 | return EINVAL;
|
---|
864 | }else{
|
---|
865 | socket_data->state = TCP_SOCKET_SYN_RECEIVED;
|
---|
866 | // printf("unlock\n");
|
---|
867 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
868 | // send the packet
|
---|
869 | tcp_send_packets( socket_data->device_id, packet );
|
---|
870 | return EOK;
|
---|
871 | }
|
---|
872 | }
|
---|
873 | }
|
---|
874 | return tcp_release_and_return( packet, EINVAL );
|
---|
875 | }
|
---|
876 |
|
---|
877 | int tcp_process_syn_received( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ){
|
---|
878 | ERROR_DECLARE;
|
---|
879 |
|
---|
880 | socket_core_ref listening_socket;
|
---|
881 | tcp_socket_data_ref listening_socket_data;
|
---|
882 |
|
---|
883 | assert( socket );
|
---|
884 | assert( socket_data );
|
---|
885 | assert( socket->specific_data == socket_data );
|
---|
886 | assert( header );
|
---|
887 | assert( packet );
|
---|
888 |
|
---|
889 | printf("syn_rec\n");
|
---|
890 | if( header->acknowledge ){
|
---|
891 | // process acknowledgement
|
---|
892 | tcp_process_acknowledgement( socket, socket_data, header );
|
---|
893 |
|
---|
894 | socket_data->next_incoming = ntohl( header->sequence_number );// + 1;
|
---|
895 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
896 | socket_data->state = TCP_SOCKET_ESTABLISHED;
|
---|
897 | listening_socket = socket_cores_find( socket_data->local_sockets, socket_data->listening_socket_id );
|
---|
898 | if( listening_socket ){
|
---|
899 | listening_socket_data = ( tcp_socket_data_ref ) listening_socket->specific_data;
|
---|
900 | assert( listening_socket_data );
|
---|
901 |
|
---|
902 | // queue the received packet
|
---|
903 | if( ! ERROR_OCCURRED( dyn_fifo_push( & listening_socket->accepted, ( -1 * socket->socket_id ), listening_socket_data->backlog ))){
|
---|
904 | // notify the destination socket
|
---|
905 | async_msg_5( socket->phone, NET_SOCKET_ACCEPTED, ( ipcarg_t ) listening_socket->socket_id, socket_data->data_fragment_size, TCP_HEADER_SIZE, 0, ( ipcarg_t ) socket->socket_id );
|
---|
906 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
907 | return EOK;
|
---|
908 | }
|
---|
909 | }
|
---|
910 | // send FIN
|
---|
911 | socket_data->state = TCP_SOCKET_FIN_WAIT_1;
|
---|
912 |
|
---|
913 | // create the notification packet
|
---|
914 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 1 ));
|
---|
915 |
|
---|
916 | // send the packet
|
---|
917 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 1 ));
|
---|
918 |
|
---|
919 | // flush packets
|
---|
920 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
921 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
922 | if( packet ){
|
---|
923 | // send the packet
|
---|
924 | tcp_send_packets( socket_data->device_id, packet );
|
---|
925 | }
|
---|
926 | return EOK;
|
---|
927 | }else{
|
---|
928 | return tcp_release_and_return( packet, EINVAL );
|
---|
929 | }
|
---|
930 | return EINVAL;
|
---|
931 | }
|
---|
932 |
|
---|
933 | void tcp_process_acknowledgement( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header ){
|
---|
934 | size_t number;
|
---|
935 | size_t length;
|
---|
936 | packet_t packet;
|
---|
937 | packet_t next;
|
---|
938 | packet_t acknowledged = NULL;
|
---|
939 | uint32_t old;
|
---|
940 |
|
---|
941 | assert( socket );
|
---|
942 | assert( socket_data );
|
---|
943 | assert( socket->specific_data == socket_data );
|
---|
944 | assert( header );
|
---|
945 |
|
---|
946 | if( header->acknowledge ){
|
---|
947 | number = ntohl( header->acknowledgement_number );
|
---|
948 | // if more data acknowledged
|
---|
949 | if( number != socket_data->expected ){
|
---|
950 | old = socket_data->expected;
|
---|
951 | if( IS_IN_INTERVAL_OVERFLOW( old, socket_data->fin_outgoing, number )){
|
---|
952 | switch( socket_data->state ){
|
---|
953 | case TCP_SOCKET_FIN_WAIT_1:
|
---|
954 | socket_data->state = TCP_SOCKET_FIN_WAIT_2;
|
---|
955 | break;
|
---|
956 | case TCP_SOCKET_LAST_ACK:
|
---|
957 | case TCP_SOCKET_CLOSING:
|
---|
958 | // fin acknowledged - release the socket in another fibril
|
---|
959 | tcp_prepare_timeout( tcp_release_after_timeout, socket, socket_data, 0, TCP_SOCKET_TIME_WAIT, NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true );
|
---|
960 | break;
|
---|
961 | default:
|
---|
962 | break;
|
---|
963 | }
|
---|
964 | }
|
---|
965 | // update the treshold if higher than set
|
---|
966 | if( number + ntohs( header->window ) > socket_data->expected + socket_data->treshold ){
|
---|
967 | socket_data->treshold = number + ntohs( header->window ) - socket_data->expected;
|
---|
968 | }
|
---|
969 | // set new expected sequence number
|
---|
970 | socket_data->expected = number;
|
---|
971 | socket_data->expected_count = 1;
|
---|
972 | packet = socket_data->outgoing;
|
---|
973 | while( pq_get_order( packet, & number, & length ) == EOK ){
|
---|
974 | if( IS_IN_INTERVAL_OVERFLOW(( uint32_t ) old, ( uint32_t )( number + length ), ( uint32_t ) socket_data->expected )){
|
---|
975 | next = pq_detach( packet );
|
---|
976 | if( packet == socket_data->outgoing ){
|
---|
977 | socket_data->outgoing = next;
|
---|
978 | }
|
---|
979 | // add to acknowledged or release
|
---|
980 | if( pq_add( & acknowledged, packet, 0, 0 ) != EOK ){
|
---|
981 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
982 | }
|
---|
983 | packet = next;
|
---|
984 | }else if( old < socket_data->expected ){
|
---|
985 | break;
|
---|
986 | }
|
---|
987 | }
|
---|
988 | // release acknowledged
|
---|
989 | if( acknowledged ){
|
---|
990 | pq_release( tcp_globals.net_phone, packet_get_id( acknowledged ));
|
---|
991 | }
|
---|
992 | return;
|
---|
993 | // if the same as the previous time
|
---|
994 | }else if( number == socket_data->expected ){
|
---|
995 | // increase the counter
|
---|
996 | ++ socket_data->expected_count;
|
---|
997 | if( socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT ){
|
---|
998 | socket_data->expected_count = 1;
|
---|
999 | // TODO retransmit lock
|
---|
1000 | //tcp_retransmit_packet( socket, socket_data, number );
|
---|
1001 | }
|
---|
1002 | }
|
---|
1003 | }
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | int tcp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
|
---|
1007 | ERROR_DECLARE;
|
---|
1008 |
|
---|
1009 | packet_t packet;
|
---|
1010 |
|
---|
1011 | assert( call );
|
---|
1012 | assert( answer );
|
---|
1013 | assert( answer_count );
|
---|
1014 |
|
---|
1015 | * answer_count = 0;
|
---|
1016 | switch( IPC_GET_METHOD( * call )){
|
---|
1017 | case NET_TL_RECEIVED:
|
---|
1018 | //fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
1019 | if( ! ERROR_OCCURRED( packet_translate( tcp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
|
---|
1020 | ERROR_CODE = tcp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_TCP, IPC_GET_ERROR( call ));
|
---|
1021 | }
|
---|
1022 | //fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
1023 | return ERROR_CODE;
|
---|
1024 | case IPC_M_CONNECT_TO_ME:
|
---|
1025 | return tcp_process_client_messages( callid, * call );
|
---|
1026 | }
|
---|
1027 | return ENOTSUP;
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | void tcp_refresh_socket_data( tcp_socket_data_ref socket_data ){
|
---|
1031 | assert( socket_data );
|
---|
1032 |
|
---|
1033 | bzero( socket_data, sizeof( * socket_data ));
|
---|
1034 | socket_data->state = TCP_SOCKET_INITIAL;
|
---|
1035 | socket_data->device_id = DEVICE_INVALID_ID;
|
---|
1036 | socket_data->window = NET_DEFAULT_TCP_WINDOW;
|
---|
1037 | socket_data->treshold = socket_data->window;
|
---|
1038 | socket_data->last_outgoing = TCP_INITIAL_SEQUENCE_NUMBER;
|
---|
1039 | socket_data->timeout = NET_DEFAULT_TCP_INITIAL_TIMEOUT;
|
---|
1040 | socket_data->acknowledged = socket_data->last_outgoing;
|
---|
1041 | socket_data->next_outgoing = socket_data->last_outgoing + 1;
|
---|
1042 | socket_data->expected = socket_data->next_outgoing;
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | void tcp_initialize_socket_data( tcp_socket_data_ref socket_data ){
|
---|
1046 | assert( socket_data );
|
---|
1047 |
|
---|
1048 | tcp_refresh_socket_data( socket_data );
|
---|
1049 | fibril_mutex_initialize( & socket_data->operation.mutex );
|
---|
1050 | fibril_condvar_initialize( & socket_data->operation.condvar );
|
---|
1051 | socket_data->data_fragment_size = MAX_TCP_FRAGMENT_SIZE;
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | int tcp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){
|
---|
1055 | int res;
|
---|
1056 | bool keep_on_going = true;
|
---|
1057 | socket_cores_t local_sockets;
|
---|
1058 | int app_phone = IPC_GET_PHONE( & call );
|
---|
1059 | struct sockaddr * addr;
|
---|
1060 | size_t addrlen;
|
---|
1061 | fibril_rwlock_t lock;
|
---|
1062 | ipc_call_t answer;
|
---|
1063 | int answer_count;
|
---|
1064 | tcp_socket_data_ref socket_data;
|
---|
1065 | socket_core_ref socket;
|
---|
1066 | packet_dimension_ref packet_dimension;
|
---|
1067 |
|
---|
1068 | /*
|
---|
1069 | * Accept the connection
|
---|
1070 | * - Answer the first IPC_M_CONNECT_ME_TO call.
|
---|
1071 | */
|
---|
1072 | ipc_answer_0( callid, EOK );
|
---|
1073 |
|
---|
1074 | socket_cores_initialize( & local_sockets );
|
---|
1075 | fibril_rwlock_initialize( & lock );
|
---|
1076 |
|
---|
1077 | while( keep_on_going ){
|
---|
1078 | // refresh data
|
---|
1079 | refresh_answer( & answer, & answer_count );
|
---|
1080 |
|
---|
1081 | callid = async_get_call( & call );
|
---|
1082 | // printf( "message %d\n", IPC_GET_METHOD( * call ));
|
---|
1083 |
|
---|
1084 | switch( IPC_GET_METHOD( call )){
|
---|
1085 | case IPC_M_PHONE_HUNGUP:
|
---|
1086 | keep_on_going = false;
|
---|
1087 | res = EOK;
|
---|
1088 | break;
|
---|
1089 | case NET_SOCKET:
|
---|
1090 | socket_data = ( tcp_socket_data_ref ) malloc( sizeof( * socket_data ));
|
---|
1091 | if( ! socket_data ){
|
---|
1092 | res = ENOMEM;
|
---|
1093 | }else{
|
---|
1094 | tcp_initialize_socket_data( socket_data );
|
---|
1095 | socket_data->local_lock = & lock;
|
---|
1096 | socket_data->local_sockets = & local_sockets;
|
---|
1097 | fibril_rwlock_write_lock( & lock );
|
---|
1098 | * SOCKET_SET_SOCKET_ID( answer ) = SOCKET_GET_SOCKET_ID( call );
|
---|
1099 | res = socket_create( & local_sockets, app_phone, socket_data, SOCKET_SET_SOCKET_ID( answer ));
|
---|
1100 | fibril_rwlock_write_unlock( & lock );
|
---|
1101 | if( res == EOK ){
|
---|
1102 | if( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, DEVICE_INVALID_ID, & packet_dimension ) == EOK ){
|
---|
1103 | * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size );
|
---|
1104 | }
|
---|
1105 | // * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_TCP_FRAGMENT_SIZE;
|
---|
1106 | * SOCKET_SET_HEADER_SIZE( answer ) = TCP_HEADER_SIZE;
|
---|
1107 | answer_count = 3;
|
---|
1108 | }else{
|
---|
1109 | free( socket_data );
|
---|
1110 | }
|
---|
1111 | }
|
---|
1112 | break;
|
---|
1113 | case NET_SOCKET_BIND:
|
---|
1114 | res = data_receive(( void ** ) & addr, & addrlen );
|
---|
1115 | if( res == EOK ){
|
---|
1116 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
1117 | fibril_rwlock_write_lock( & lock );
|
---|
1118 | res = socket_bind( & local_sockets, & tcp_globals.sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port );
|
---|
1119 | if( res == EOK ){
|
---|
1120 | socket = socket_cores_find( & local_sockets, SOCKET_GET_SOCKET_ID( call ));
|
---|
1121 | if( socket ){
|
---|
1122 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
1123 | assert( socket_data );
|
---|
1124 | socket_data->state = TCP_SOCKET_LISTEN;
|
---|
1125 | }
|
---|
1126 | }
|
---|
1127 | fibril_rwlock_write_unlock( & lock );
|
---|
1128 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
1129 | free( addr );
|
---|
1130 | }
|
---|
1131 | break;
|
---|
1132 | case NET_SOCKET_LISTEN:
|
---|
1133 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
1134 | // fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
1135 | fibril_rwlock_write_lock( & lock );
|
---|
1136 | res = tcp_listen_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_BACKLOG( call ));
|
---|
1137 | fibril_rwlock_write_unlock( & lock );
|
---|
1138 | // fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
1139 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
1140 | break;
|
---|
1141 | case NET_SOCKET_CONNECT:
|
---|
1142 | res = data_receive(( void ** ) & addr, & addrlen );
|
---|
1143 | if( res == EOK ){
|
---|
1144 | // the global lock may be released in the tcp_connect_message() function
|
---|
1145 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
1146 | fibril_rwlock_write_lock( & lock );
|
---|
1147 | res = tcp_connect_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen );
|
---|
1148 | if( res != EOK ){
|
---|
1149 | fibril_rwlock_write_unlock( & lock );
|
---|
1150 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
1151 | free( addr );
|
---|
1152 | }
|
---|
1153 | }
|
---|
1154 | break;
|
---|
1155 | case NET_SOCKET_ACCEPT:
|
---|
1156 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
1157 | fibril_rwlock_write_lock( & lock );
|
---|
1158 | res = tcp_accept_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_NEW_SOCKET_ID( call ), SOCKET_SET_DATA_FRAGMENT_SIZE( answer ), & addrlen );
|
---|
1159 | fibril_rwlock_write_unlock( & lock );
|
---|
1160 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
1161 | if( res > 0 ){
|
---|
1162 | * SOCKET_SET_SOCKET_ID( answer ) = res;
|
---|
1163 | * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen;
|
---|
1164 | answer_count = 3;
|
---|
1165 | }
|
---|
1166 | break;
|
---|
1167 | case NET_SOCKET_SEND:
|
---|
1168 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
1169 | fibril_rwlock_write_lock( & lock );
|
---|
1170 | res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_SET_DATA_FRAGMENT_SIZE( answer ), SOCKET_GET_FLAGS( call ));
|
---|
1171 | if( res != EOK ){
|
---|
1172 | fibril_rwlock_write_unlock( & lock );
|
---|
1173 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
1174 | }else{
|
---|
1175 | answer_count = 2;
|
---|
1176 | }
|
---|
1177 | break;
|
---|
1178 | case NET_SOCKET_SENDTO:
|
---|
1179 | res = data_receive(( void ** ) & addr, & addrlen );
|
---|
1180 | if( res == EOK ){
|
---|
1181 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
1182 | fibril_rwlock_write_lock( & lock );
|
---|
1183 | res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_SET_DATA_FRAGMENT_SIZE( answer ), SOCKET_GET_FLAGS( call ));
|
---|
1184 | if( res != EOK ){
|
---|
1185 | fibril_rwlock_write_unlock( & lock );
|
---|
1186 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
1187 | }else{
|
---|
1188 | answer_count = 2;
|
---|
1189 | }
|
---|
1190 | free( addr );
|
---|
1191 | }
|
---|
1192 | break;
|
---|
1193 | case NET_SOCKET_RECV:
|
---|
1194 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
1195 | fibril_rwlock_write_lock( & lock );
|
---|
1196 | res = tcp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), NULL );
|
---|
1197 | fibril_rwlock_write_unlock( & lock );
|
---|
1198 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
1199 | if( res > 0 ){
|
---|
1200 | * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
|
---|
1201 | answer_count = 1;
|
---|
1202 | res = EOK;
|
---|
1203 | }
|
---|
1204 | break;
|
---|
1205 | case NET_SOCKET_RECVFROM:
|
---|
1206 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
1207 | fibril_rwlock_write_lock( & lock );
|
---|
1208 | res = tcp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), & addrlen );
|
---|
1209 | fibril_rwlock_write_unlock( & lock );
|
---|
1210 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
1211 | if( res > 0 ){
|
---|
1212 | * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
|
---|
1213 | * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen;
|
---|
1214 | answer_count = 3;
|
---|
1215 | res = EOK;
|
---|
1216 | }
|
---|
1217 | break;
|
---|
1218 | case NET_SOCKET_CLOSE:
|
---|
1219 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
1220 | fibril_rwlock_write_lock( & lock );
|
---|
1221 | res = tcp_close_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ));
|
---|
1222 | if( res != EOK ){
|
---|
1223 | fibril_rwlock_write_unlock( & lock );
|
---|
1224 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
1225 | }
|
---|
1226 | break;
|
---|
1227 | case NET_SOCKET_GETSOCKOPT:
|
---|
1228 | case NET_SOCKET_SETSOCKOPT:
|
---|
1229 | default:
|
---|
1230 | res = ENOTSUP;
|
---|
1231 | break;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | // printf( "res = %d\n", res );
|
---|
1235 |
|
---|
1236 | answer_call( callid, res, & answer, answer_count );
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | printf("release\n");
|
---|
1240 | // release all local sockets
|
---|
1241 | socket_cores_release( tcp_globals.net_phone, & local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
1242 |
|
---|
1243 | return EOK;
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | int tcp_timeout( void * data ){
|
---|
1247 | tcp_timeout_ref timeout = data;
|
---|
1248 | int keep_write_lock = false;
|
---|
1249 | socket_core_ref socket;
|
---|
1250 | tcp_socket_data_ref socket_data;
|
---|
1251 |
|
---|
1252 | assert( timeout );
|
---|
1253 |
|
---|
1254 | // sleep the given timeout
|
---|
1255 | async_usleep( timeout->timeout );
|
---|
1256 | // lock the globals
|
---|
1257 | if( timeout->globals_read_only ){
|
---|
1258 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
1259 | }else{
|
---|
1260 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
1261 | }
|
---|
1262 | // find the pending operation socket
|
---|
1263 | socket = socket_port_find( & tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length );
|
---|
1264 | if( socket && ( socket->socket_id == timeout->socket_id )){
|
---|
1265 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
1266 | assert( socket_data );
|
---|
1267 | if( socket_data->local_sockets == timeout->local_sockets ){
|
---|
1268 | fibril_rwlock_write_lock( socket_data->local_lock );
|
---|
1269 | if( timeout->sequence_number ){
|
---|
1270 | // increase the timeout counter;
|
---|
1271 | ++ socket_data->timeout_count;
|
---|
1272 | if( socket_data->timeout_count == TCP_MAX_TIMEOUTS ){
|
---|
1273 | // TODO release as connection lost
|
---|
1274 | //tcp_refresh_socket_data( socket_data );
|
---|
1275 | }
|
---|
1276 | // retransmit
|
---|
1277 | // TODO enable retransmit
|
---|
1278 | //tcp_retransmit_packet( socket, socket_data, timeout->sequence_number );
|
---|
1279 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
1280 | }else{
|
---|
1281 | fibril_mutex_lock( & socket_data->operation.mutex );
|
---|
1282 | // set the timeout operation result if state not changed
|
---|
1283 | if( socket_data->state == timeout->state ){
|
---|
1284 | socket_data->operation.result = ETIMEOUT;
|
---|
1285 | // notify the main fibril
|
---|
1286 | fibril_condvar_signal( & socket_data->operation.condvar );
|
---|
1287 | // keep the global write lock
|
---|
1288 | keep_write_lock = true;
|
---|
1289 | }else{
|
---|
1290 | // operation is ok, do nothing
|
---|
1291 | // unlocking from now on, so the unlock order does not matter...
|
---|
1292 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
1293 | }
|
---|
1294 | fibril_mutex_unlock( & socket_data->operation.mutex );
|
---|
1295 | }
|
---|
1296 | }
|
---|
1297 | }
|
---|
1298 | // unlock only if no socket
|
---|
1299 | if( timeout->globals_read_only ){
|
---|
1300 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
1301 | }else if( ! keep_write_lock ){
|
---|
1302 | // release if not desired
|
---|
1303 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
1304 | }
|
---|
1305 | // release the timeout structure
|
---|
1306 | free( timeout );
|
---|
1307 | return EOK;
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | int tcp_release_after_timeout( void * data ){
|
---|
1311 | tcp_timeout_ref timeout = data;
|
---|
1312 | socket_core_ref socket;
|
---|
1313 | tcp_socket_data_ref socket_data;
|
---|
1314 | fibril_rwlock_t * local_lock;
|
---|
1315 |
|
---|
1316 | assert( timeout );
|
---|
1317 |
|
---|
1318 | // sleep the given timeout
|
---|
1319 | async_usleep( timeout->timeout );
|
---|
1320 | // lock the globals
|
---|
1321 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
1322 | // find the pending operation socket
|
---|
1323 | socket = socket_port_find( & tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length );
|
---|
1324 | if( socket && ( socket->socket_id == timeout->socket_id )){
|
---|
1325 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
1326 | assert( socket_data );
|
---|
1327 | if( socket_data->local_sockets == timeout->local_sockets ){
|
---|
1328 | local_lock = socket_data->local_lock;
|
---|
1329 | fibril_rwlock_write_lock( local_lock );
|
---|
1330 | socket_destroy( tcp_globals.net_phone, timeout->socket_id, timeout->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
1331 | fibril_rwlock_write_unlock( local_lock );
|
---|
1332 | }
|
---|
1333 | }
|
---|
1334 | // unlock the globals
|
---|
1335 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
1336 | // release the timeout structure
|
---|
1337 | free( timeout );
|
---|
1338 | return EOK;
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | void tcp_retransmit_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number ){
|
---|
1342 | packet_t packet;
|
---|
1343 | packet_t copy;
|
---|
1344 | size_t data_length;
|
---|
1345 |
|
---|
1346 | assert( socket );
|
---|
1347 | assert( socket_data );
|
---|
1348 | assert( socket->specific_data == socket_data );
|
---|
1349 |
|
---|
1350 | // sent packet?
|
---|
1351 | packet = pq_find( socket_data->outgoing, sequence_number );
|
---|
1352 | printf("retransmit %d\n", packet_get_id( packet ));
|
---|
1353 | if( packet ){
|
---|
1354 | pq_get_order( packet, NULL, & data_length );
|
---|
1355 | copy = tcp_prepare_copy( socket, socket_data, packet, data_length, sequence_number );
|
---|
1356 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
1357 | // printf( "r send %d\n", packet_get_id( packet ));
|
---|
1358 | if( copy ){
|
---|
1359 | tcp_send_packets( socket_data->device_id, copy );
|
---|
1360 | }
|
---|
1361 | }else{
|
---|
1362 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
1363 | }
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | int tcp_listen_message( socket_cores_ref local_sockets, int socket_id, int backlog ){
|
---|
1367 | socket_core_ref socket;
|
---|
1368 | tcp_socket_data_ref socket_data;
|
---|
1369 |
|
---|
1370 | assert( local_sockets );
|
---|
1371 |
|
---|
1372 | if( backlog < 0 ) return EINVAL;
|
---|
1373 | // find the socket
|
---|
1374 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
1375 | if( ! socket ) return ENOTSOCK;
|
---|
1376 | // get the socket specific data
|
---|
1377 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
1378 | assert( socket_data );
|
---|
1379 | // set the backlog
|
---|
1380 | socket_data->backlog = backlog;
|
---|
1381 | return EOK;
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 | int tcp_connect_message( socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen ){
|
---|
1385 | ERROR_DECLARE;
|
---|
1386 |
|
---|
1387 | socket_core_ref socket;
|
---|
1388 |
|
---|
1389 | assert( local_sockets );
|
---|
1390 | assert( addr );
|
---|
1391 | assert( addrlen > 0 );
|
---|
1392 |
|
---|
1393 | // find the socket
|
---|
1394 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
1395 | if( ! socket ) return ENOTSOCK;
|
---|
1396 | if( ERROR_OCCURRED( tcp_connect_core( socket, local_sockets, addr, addrlen ))){
|
---|
1397 | tcp_free_socket_data( socket );
|
---|
1398 | // unbind if bound
|
---|
1399 | if( socket->port > 0 ){
|
---|
1400 | socket_ports_exclude( & tcp_globals.sockets, socket->port );
|
---|
1401 | socket->port = 0;
|
---|
1402 | }
|
---|
1403 | }
|
---|
1404 | return ERROR_CODE;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | int tcp_connect_core( socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen ){
|
---|
1408 | ERROR_DECLARE;
|
---|
1409 |
|
---|
1410 | tcp_socket_data_ref socket_data;
|
---|
1411 | packet_t packet;
|
---|
1412 |
|
---|
1413 | assert( socket );
|
---|
1414 | assert( addr );
|
---|
1415 | assert( addrlen > 0 );
|
---|
1416 |
|
---|
1417 | // get the socket specific data
|
---|
1418 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
1419 | assert( socket_data );
|
---|
1420 | assert( socket->specific_data == socket_data );
|
---|
1421 | if(( socket_data->state != TCP_SOCKET_INITIAL )
|
---|
1422 | && (( socket_data->state != TCP_SOCKET_LISTEN ) || ( socket->port <= 0 ))){
|
---|
1423 | return EINVAL;
|
---|
1424 | }
|
---|
1425 | // get the destination port
|
---|
1426 | ERROR_PROPAGATE( tl_get_address_port( addr, addrlen, & socket_data->dest_port ));
|
---|
1427 | if( socket->port <= 0 ){
|
---|
1428 | // try to find a free port
|
---|
1429 | ERROR_PROPAGATE( socket_bind_free_port( & tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port ));
|
---|
1430 | // set the next port as the search starting port number
|
---|
1431 | tcp_globals.last_used_port = socket->port;
|
---|
1432 | }
|
---|
1433 | ERROR_PROPAGATE( ip_get_route_req( tcp_globals.ip_phone, IPPROTO_TCP, addr, addrlen, & socket_data->device_id, & socket_data->pseudo_header, & socket_data->headerlen ));
|
---|
1434 |
|
---|
1435 | // create the notification packet
|
---|
1436 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 1, 0 ));
|
---|
1437 |
|
---|
1438 | // unlock the globals and wait for an operation
|
---|
1439 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
1440 |
|
---|
1441 | socket_data->addr = addr;
|
---|
1442 | socket_data->addrlen = addrlen;
|
---|
1443 | // send the packet
|
---|
1444 | if( ERROR_OCCURRED( tcp_queue_packet( socket, socket_data, packet, 1 ))
|
---|
1445 | || ERROR_OCCURRED( tcp_prepare_timeout( tcp_timeout, socket, socket_data, 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false ))){
|
---|
1446 | socket_data->addr = NULL;
|
---|
1447 | socket_data->addrlen = 0;
|
---|
1448 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
1449 | }else{
|
---|
1450 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
1451 | if( packet ){
|
---|
1452 | fibril_mutex_lock( & socket_data->operation.mutex );
|
---|
1453 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
1454 | // send the packet
|
---|
1455 | printf( "connecting %d\n", packet_get_id( packet ));
|
---|
1456 | tcp_send_packets( socket_data->device_id, packet );
|
---|
1457 | // wait for a reply
|
---|
1458 | fibril_condvar_wait( & socket_data->operation.condvar, & socket_data->operation.mutex );
|
---|
1459 | ERROR_CODE = socket_data->operation.result;
|
---|
1460 | if( ERROR_CODE != EOK ){
|
---|
1461 | socket_data->addr = NULL;
|
---|
1462 | socket_data->addrlen = 0;
|
---|
1463 | }
|
---|
1464 | }else{
|
---|
1465 | socket_data->addr = NULL;
|
---|
1466 | socket_data->addrlen = 0;
|
---|
1467 | ERROR_CODE = EINTR;
|
---|
1468 | }
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | fibril_mutex_unlock( & socket_data->operation.mutex );
|
---|
1472 |
|
---|
1473 | // return the result
|
---|
1474 | return ERROR_CODE;
|
---|
1475 | }
|
---|
1476 |
|
---|
1477 | int tcp_queue_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ){
|
---|
1478 | ERROR_DECLARE;
|
---|
1479 |
|
---|
1480 | tcp_header_ref header;
|
---|
1481 |
|
---|
1482 | assert( socket );
|
---|
1483 | assert( socket_data );
|
---|
1484 | assert( socket->specific_data == socket_data );
|
---|
1485 |
|
---|
1486 | // get tcp header
|
---|
1487 | header = ( tcp_header_ref ) packet_get_data( packet );
|
---|
1488 | if( ! header ) return NO_DATA;
|
---|
1489 | header->destination_port = htons( socket_data->dest_port );
|
---|
1490 | header->source_port = htons( socket->port );
|
---|
1491 | header->sequence_number = htonl( socket_data->next_outgoing );
|
---|
1492 | if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) socket_data->addr, socket_data->addrlen ))){
|
---|
1493 | return tcp_release_and_return( packet, EINVAL );
|
---|
1494 | }
|
---|
1495 | // remember the outgoing FIN
|
---|
1496 | if( header->finalize ){
|
---|
1497 | socket_data->fin_outgoing = socket_data->next_outgoing;
|
---|
1498 | }
|
---|
1499 | return EOK;
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 | int tcp_queue_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ){
|
---|
1503 | ERROR_DECLARE;
|
---|
1504 |
|
---|
1505 | assert( socket );
|
---|
1506 | assert( socket_data );
|
---|
1507 | assert( socket->specific_data == socket_data );
|
---|
1508 |
|
---|
1509 | ERROR_PROPAGATE( tcp_queue_prepare_packet( socket, socket_data, packet, data_length ));
|
---|
1510 |
|
---|
1511 | if( ERROR_OCCURRED( pq_add( & socket_data->outgoing, packet, socket_data->next_outgoing, data_length ))){
|
---|
1512 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
1513 | }
|
---|
1514 | socket_data->next_outgoing += data_length;
|
---|
1515 | return EOK;
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | packet_t tcp_get_packets_to_send( socket_core_ref socket, tcp_socket_data_ref socket_data ){
|
---|
1519 | ERROR_DECLARE;
|
---|
1520 |
|
---|
1521 | packet_t packet;
|
---|
1522 | packet_t copy;
|
---|
1523 | packet_t sending = NULL;
|
---|
1524 | packet_t previous = NULL;
|
---|
1525 | size_t data_length;
|
---|
1526 |
|
---|
1527 | assert( socket );
|
---|
1528 | assert( socket_data );
|
---|
1529 | assert( socket->specific_data == socket_data );
|
---|
1530 |
|
---|
1531 | packet = pq_find( socket_data->outgoing, socket_data->last_outgoing + 1 );
|
---|
1532 | while( packet ){
|
---|
1533 | pq_get_order( packet, NULL, & data_length );
|
---|
1534 | // send only if fits into the window
|
---|
1535 | // respecting the possible overflow
|
---|
1536 | if( IS_IN_INTERVAL_OVERFLOW(( uint32_t ) socket_data->last_outgoing, ( uint32_t )( socket_data->last_outgoing + data_length ), ( uint32_t )( socket_data->expected + socket_data->treshold ))){
|
---|
1537 | copy = tcp_prepare_copy( socket, socket_data, packet, data_length, socket_data->last_outgoing + 1 );
|
---|
1538 | if( ! copy ){
|
---|
1539 | return sending;
|
---|
1540 | }
|
---|
1541 | if( ! sending ){
|
---|
1542 | sending = copy;
|
---|
1543 | }else{
|
---|
1544 | if( ERROR_OCCURRED( pq_insert_after( previous, copy ))){
|
---|
1545 | pq_release( tcp_globals.net_phone, packet_get_id( copy ));
|
---|
1546 | return sending;
|
---|
1547 | }
|
---|
1548 | }
|
---|
1549 | previous = copy;
|
---|
1550 | packet = pq_next( packet );
|
---|
1551 | // overflow occurred ?
|
---|
1552 | if(( ! packet ) && ( socket_data->last_outgoing > socket_data->next_outgoing )){
|
---|
1553 | printf("gpts overflow\n");
|
---|
1554 | // continue from the beginning
|
---|
1555 | packet = socket_data->outgoing;
|
---|
1556 | }
|
---|
1557 | socket_data->last_outgoing += data_length;
|
---|
1558 | }else{
|
---|
1559 | break;
|
---|
1560 | }
|
---|
1561 | }
|
---|
1562 | return sending;
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 | packet_t tcp_send_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number ){
|
---|
1566 | ERROR_DECLARE;
|
---|
1567 |
|
---|
1568 | tcp_header_ref header;
|
---|
1569 | uint32_t checksum;
|
---|
1570 |
|
---|
1571 | assert( socket );
|
---|
1572 | assert( socket_data );
|
---|
1573 | assert( socket->specific_data == socket_data );
|
---|
1574 |
|
---|
1575 | // adjust the pseudo header
|
---|
1576 | if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( socket_data->pseudo_header, socket_data->headerlen, packet_get_data_length( packet )))){
|
---|
1577 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
1578 | return NULL;
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | // get the header
|
---|
1582 | header = ( tcp_header_ref ) packet_get_data( packet );
|
---|
1583 | if( ! header ){
|
---|
1584 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
1585 | return NULL;
|
---|
1586 | }
|
---|
1587 | assert( ntohl( header->sequence_number ) == sequence_number );
|
---|
1588 |
|
---|
1589 | // adjust the header
|
---|
1590 | if( socket_data->next_incoming ){
|
---|
1591 | header->acknowledgement_number = htonl( socket_data->next_incoming );
|
---|
1592 | header->acknowledge = 1;
|
---|
1593 | }
|
---|
1594 | header->window = htons( socket_data->window );
|
---|
1595 |
|
---|
1596 | // checksum
|
---|
1597 | header->checksum = 0;
|
---|
1598 | checksum = compute_checksum( 0, socket_data->pseudo_header, socket_data->headerlen );
|
---|
1599 | checksum = compute_checksum( checksum, ( uint8_t * ) packet_get_data( packet ), packet_get_data_length( packet ));
|
---|
1600 | header->checksum = htons( flip_checksum( compact_checksum( checksum )));
|
---|
1601 | // prepare the packet
|
---|
1602 | if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_TCP, 0, 0, 0, 0 ))
|
---|
1603 | // prepare the timeout
|
---|
1604 | || ERROR_OCCURRED( tcp_prepare_timeout( tcp_timeout, socket, socket_data, sequence_number, socket_data->state, socket_data->timeout, true ))){
|
---|
1605 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
1606 | return NULL;
|
---|
1607 | }
|
---|
1608 | return packet;
|
---|
1609 | }
|
---|
1610 |
|
---|
1611 | packet_t tcp_prepare_copy( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number ){
|
---|
1612 | packet_t copy;
|
---|
1613 |
|
---|
1614 | assert( socket );
|
---|
1615 | assert( socket_data );
|
---|
1616 | assert( socket->specific_data == socket_data );
|
---|
1617 |
|
---|
1618 | // make a copy of the packet
|
---|
1619 | copy = packet_get_copy( tcp_globals.net_phone, packet );
|
---|
1620 | if( ! copy ) return NULL;
|
---|
1621 |
|
---|
1622 | return tcp_send_prepare_packet( socket, socket_data, copy, data_length, sequence_number );
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | void tcp_send_packets( device_id_t device_id, packet_t packet ){
|
---|
1626 | packet_t next;
|
---|
1627 |
|
---|
1628 | while( packet ){
|
---|
1629 | next = pq_detach( packet );
|
---|
1630 | ip_send_msg( tcp_globals.ip_phone, device_id, packet, SERVICE_TCP, 0 );
|
---|
1631 | packet = next;
|
---|
1632 | }
|
---|
1633 | }
|
---|
1634 |
|
---|
1635 | void tcp_prepare_operation_header( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize ){
|
---|
1636 | assert( socket );
|
---|
1637 | assert( socket_data );
|
---|
1638 | assert( socket->specific_data == socket_data );
|
---|
1639 | assert( header );
|
---|
1640 |
|
---|
1641 | bzero( header, sizeof( * header ));
|
---|
1642 | header->source_port = htons( socket->port );
|
---|
1643 | header->source_port = htons( socket_data->dest_port );
|
---|
1644 | header->header_length = TCP_COMPUTE_HEADER_LENGTH( sizeof( * header ));
|
---|
1645 | header->synchronize = synchronize;
|
---|
1646 | header->finalize = finalize;
|
---|
1647 | }
|
---|
1648 |
|
---|
1649 | int tcp_prepare_timeout( int ( * timeout_function )( void * tcp_timeout_t ), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only ){
|
---|
1650 | tcp_timeout_ref operation_timeout;
|
---|
1651 | fid_t fibril;
|
---|
1652 |
|
---|
1653 | assert( socket );
|
---|
1654 | assert( socket_data );
|
---|
1655 | assert( socket->specific_data == socket_data );
|
---|
1656 |
|
---|
1657 | // prepare the timeout with key bundle structure
|
---|
1658 | operation_timeout = malloc( sizeof( * operation_timeout ) + socket->key_length + 1 );
|
---|
1659 | if( ! operation_timeout ) return ENOMEM;
|
---|
1660 | bzero( operation_timeout, sizeof( * operation_timeout ));
|
---|
1661 | operation_timeout->globals_read_only = globals_read_only;
|
---|
1662 | operation_timeout->port = socket->port;
|
---|
1663 | operation_timeout->local_sockets = socket_data->local_sockets;
|
---|
1664 | operation_timeout->socket_id = socket->socket_id;
|
---|
1665 | operation_timeout->timeout = timeout;
|
---|
1666 | operation_timeout->sequence_number = sequence_number;
|
---|
1667 | operation_timeout->state = state;
|
---|
1668 |
|
---|
1669 | // copy the key
|
---|
1670 | operation_timeout->key = (( char * ) operation_timeout ) + sizeof( * operation_timeout );
|
---|
1671 | operation_timeout->key_length = socket->key_length;
|
---|
1672 | memcpy( operation_timeout->key, socket->key, socket->key_length );
|
---|
1673 | operation_timeout->key[ operation_timeout->key_length ] = '\0';
|
---|
1674 |
|
---|
1675 | // prepare the timeouting thread
|
---|
1676 | fibril = fibril_create( timeout_function, operation_timeout );
|
---|
1677 | if( ! fibril ){
|
---|
1678 | free( operation_timeout );
|
---|
1679 | return EPARTY;
|
---|
1680 | }
|
---|
1681 | // fibril_mutex_lock( & socket_data->operation.mutex );
|
---|
1682 | // start the timeouting fibril
|
---|
1683 | fibril_add_ready( fibril );
|
---|
1684 | //socket_data->state = state;
|
---|
1685 | return EOK;
|
---|
1686 | }
|
---|
1687 |
|
---|
1688 | int tcp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ){
|
---|
1689 | ERROR_DECLARE;
|
---|
1690 |
|
---|
1691 | socket_core_ref socket;
|
---|
1692 | tcp_socket_data_ref socket_data;
|
---|
1693 | int packet_id;
|
---|
1694 | packet_t packet;
|
---|
1695 | size_t length;
|
---|
1696 |
|
---|
1697 | assert( local_sockets );
|
---|
1698 |
|
---|
1699 | // find the socket
|
---|
1700 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
1701 | if( ! socket ) return ENOTSOCK;
|
---|
1702 | // get the socket specific data
|
---|
1703 | if( ! socket->specific_data ) return NO_DATA;
|
---|
1704 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
1705 |
|
---|
1706 | // check state
|
---|
1707 | if(( socket_data->state != TCP_SOCKET_ESTABLISHED ) && ( socket_data->state != TCP_SOCKET_CLOSE_WAIT )){
|
---|
1708 | return ENOTCONN;
|
---|
1709 | }
|
---|
1710 |
|
---|
1711 | // send the source address if desired
|
---|
1712 | if( addrlen ){
|
---|
1713 | ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen ));
|
---|
1714 | * addrlen = socket_data->addrlen;
|
---|
1715 | }
|
---|
1716 |
|
---|
1717 | // get the next received packet
|
---|
1718 | packet_id = dyn_fifo_value( & socket->received );
|
---|
1719 | if( packet_id < 0 ) return NO_DATA;
|
---|
1720 | ERROR_PROPAGATE( packet_translate( tcp_globals.net_phone, & packet, packet_id ));
|
---|
1721 |
|
---|
1722 | // reply the packets
|
---|
1723 | ERROR_PROPAGATE( socket_reply_packets( packet, & length ));
|
---|
1724 |
|
---|
1725 | // release the packet
|
---|
1726 | dyn_fifo_pop( & socket->received );
|
---|
1727 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
1728 | // return the total length
|
---|
1729 | return ( int ) length;
|
---|
1730 | }
|
---|
1731 |
|
---|
1732 | int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags ){
|
---|
1733 | ERROR_DECLARE;
|
---|
1734 |
|
---|
1735 | socket_core_ref socket;
|
---|
1736 | tcp_socket_data_ref socket_data;
|
---|
1737 | packet_dimension_ref packet_dimension;
|
---|
1738 | packet_t packet;
|
---|
1739 | size_t total_length;
|
---|
1740 | tcp_header_ref header;
|
---|
1741 | int index;
|
---|
1742 | int result;
|
---|
1743 |
|
---|
1744 | assert( local_sockets );
|
---|
1745 | assert( data_fragment_size );
|
---|
1746 |
|
---|
1747 | // find the socket
|
---|
1748 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
1749 | if( ! socket ) return ENOTSOCK;
|
---|
1750 | // get the socket specific data
|
---|
1751 | if( ! socket->specific_data ) return NO_DATA;
|
---|
1752 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
1753 |
|
---|
1754 | // check state
|
---|
1755 | if(( socket_data->state != TCP_SOCKET_ESTABLISHED ) && ( socket_data->state != TCP_SOCKET_CLOSE_WAIT )){
|
---|
1756 | return ENOTCONN;
|
---|
1757 | }
|
---|
1758 |
|
---|
1759 | ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ));
|
---|
1760 |
|
---|
1761 | * data_fragment_size = (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size );
|
---|
1762 |
|
---|
1763 | for( index = 0; index < fragments; ++ index ){
|
---|
1764 | // read the data fragment
|
---|
1765 | result = tl_socket_read_packet_data( tcp_globals.net_phone, & packet, TCP_HEADER_SIZE, packet_dimension, socket_data->addr, socket_data->addrlen );
|
---|
1766 | if( result < 0 ) return result;
|
---|
1767 | total_length = ( size_t ) result;
|
---|
1768 | // prefix the tcp header
|
---|
1769 | header = PACKET_PREFIX( packet, tcp_header_t );
|
---|
1770 | if( ! header ){
|
---|
1771 | return tcp_release_and_return( packet, ENOMEM );
|
---|
1772 | }
|
---|
1773 | tcp_prepare_operation_header( socket, socket_data, header, 0, 0 );
|
---|
1774 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 0 ));
|
---|
1775 | }
|
---|
1776 |
|
---|
1777 | // flush packets
|
---|
1778 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
1779 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
1780 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
1781 | if( packet ){
|
---|
1782 | // send the packet
|
---|
1783 | tcp_send_packets( socket_data->device_id, packet );
|
---|
1784 | }
|
---|
1785 |
|
---|
1786 | return EOK;
|
---|
1787 | }
|
---|
1788 |
|
---|
1789 | int tcp_close_message( socket_cores_ref local_sockets, int socket_id ){
|
---|
1790 | ERROR_DECLARE;
|
---|
1791 |
|
---|
1792 | socket_core_ref socket;
|
---|
1793 | tcp_socket_data_ref socket_data;
|
---|
1794 | packet_t packet;
|
---|
1795 |
|
---|
1796 | // find the socket
|
---|
1797 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
1798 | if( ! socket ) return ENOTSOCK;
|
---|
1799 | // get the socket specific data
|
---|
1800 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
1801 | assert( socket_data );
|
---|
1802 |
|
---|
1803 | // check state
|
---|
1804 | switch( socket_data->state ){
|
---|
1805 | case TCP_SOCKET_ESTABLISHED:
|
---|
1806 | socket_data->state = TCP_SOCKET_FIN_WAIT_1;
|
---|
1807 | break;
|
---|
1808 | case TCP_SOCKET_CLOSE_WAIT:
|
---|
1809 | socket_data->state = TCP_SOCKET_LAST_ACK;
|
---|
1810 | break;
|
---|
1811 | // case TCP_SOCKET_LISTEN:
|
---|
1812 | default:
|
---|
1813 | // just destroy
|
---|
1814 | if( ! ERROR_OCCURRED( socket_destroy( tcp_globals.net_phone, socket_id, local_sockets, & tcp_globals.sockets, tcp_free_socket_data ))){
|
---|
1815 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
1816 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
1817 | }
|
---|
1818 | return ERROR_CODE;
|
---|
1819 | }
|
---|
1820 | // send FIN
|
---|
1821 | // TODO should I wait to complete?
|
---|
1822 |
|
---|
1823 | // create the notification packet
|
---|
1824 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 1 ));
|
---|
1825 |
|
---|
1826 | // send the packet
|
---|
1827 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 1 ));
|
---|
1828 |
|
---|
1829 | // flush packets
|
---|
1830 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
1831 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
1832 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
1833 | if( packet ){
|
---|
1834 | // send the packet
|
---|
1835 | tcp_send_packets( socket_data->device_id, packet );
|
---|
1836 | }
|
---|
1837 | return EOK;
|
---|
1838 | }
|
---|
1839 |
|
---|
1840 | int tcp_create_notification_packet( packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize ){
|
---|
1841 | ERROR_DECLARE;
|
---|
1842 |
|
---|
1843 | packet_dimension_ref packet_dimension;
|
---|
1844 | tcp_header_ref header;
|
---|
1845 |
|
---|
1846 | assert( packet );
|
---|
1847 |
|
---|
1848 | // get the device packet dimension
|
---|
1849 | ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ));
|
---|
1850 | // get a new packet
|
---|
1851 | * packet = packet_get_4( tcp_globals.net_phone, TCP_HEADER_SIZE, packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix );
|
---|
1852 | if( ! * packet ) return ENOMEM;
|
---|
1853 | // allocate space in the packet
|
---|
1854 | header = PACKET_SUFFIX( * packet, tcp_header_t );
|
---|
1855 | if( ! header ){
|
---|
1856 | tcp_release_and_return( * packet, ENOMEM );
|
---|
1857 | }
|
---|
1858 |
|
---|
1859 | tcp_prepare_operation_header( socket, socket_data, header, synchronize, finalize );
|
---|
1860 | return EOK;
|
---|
1861 | }
|
---|
1862 |
|
---|
1863 | int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen ){
|
---|
1864 | ERROR_DECLARE;
|
---|
1865 |
|
---|
1866 | socket_core_ref accepted;
|
---|
1867 | socket_core_ref socket;
|
---|
1868 | tcp_socket_data_ref socket_data;
|
---|
1869 | packet_dimension_ref packet_dimension;
|
---|
1870 |
|
---|
1871 | assert( local_sockets );
|
---|
1872 | assert( data_fragment_size );
|
---|
1873 | assert( addrlen );
|
---|
1874 |
|
---|
1875 | // find the socket
|
---|
1876 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
1877 | if( ! socket ) return ENOTSOCK;
|
---|
1878 | // get the socket specific data
|
---|
1879 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
1880 | assert( socket_data );
|
---|
1881 |
|
---|
1882 | // check state
|
---|
1883 | if( socket_data->state != TCP_SOCKET_LISTEN ){
|
---|
1884 | return EINVAL;
|
---|
1885 | }
|
---|
1886 |
|
---|
1887 | do{
|
---|
1888 | socket_id = dyn_fifo_value( & socket->accepted );
|
---|
1889 | if( socket_id < 0 ) return ENOTSOCK;
|
---|
1890 | socket_id *= -1;
|
---|
1891 |
|
---|
1892 | accepted = socket_cores_find( local_sockets, socket_id );
|
---|
1893 | if( ! accepted ) return ENOTSOCK;
|
---|
1894 | // get the socket specific data
|
---|
1895 | socket_data = ( tcp_socket_data_ref ) accepted->specific_data;
|
---|
1896 | assert( socket_data );
|
---|
1897 | // TODO can it be in another state?
|
---|
1898 | if( socket_data->state == TCP_SOCKET_ESTABLISHED ){
|
---|
1899 | ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen ));
|
---|
1900 | ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ));
|
---|
1901 | * addrlen = socket_data->addrlen;
|
---|
1902 | * data_fragment_size = (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size );
|
---|
1903 | if( new_socket_id > 0 ){
|
---|
1904 | ERROR_PROPAGATE( socket_cores_update( local_sockets, accepted->socket_id, new_socket_id ));
|
---|
1905 | accepted->socket_id = new_socket_id;
|
---|
1906 | }
|
---|
1907 | }
|
---|
1908 | dyn_fifo_pop( & socket->accepted );
|
---|
1909 | }while( socket_data->state != TCP_SOCKET_ESTABLISHED );
|
---|
1910 | printf("ret accept %d\n", accepted->socket_id );
|
---|
1911 | return accepted->socket_id;
|
---|
1912 | }
|
---|
1913 |
|
---|
1914 | void tcp_free_socket_data( socket_core_ref socket ){
|
---|
1915 | tcp_socket_data_ref socket_data;
|
---|
1916 |
|
---|
1917 | assert( socket );
|
---|
1918 |
|
---|
1919 | printf( "destroy_socket %d\n", socket->socket_id );
|
---|
1920 |
|
---|
1921 | // get the socket specific data
|
---|
1922 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
1923 | assert( socket_data );
|
---|
1924 | //free the pseudo header
|
---|
1925 | if( socket_data->pseudo_header ){
|
---|
1926 | if( socket_data->headerlen ){
|
---|
1927 | printf("d pseudo\n");
|
---|
1928 | free( socket_data->pseudo_header );
|
---|
1929 | socket_data->headerlen = 0;
|
---|
1930 | }
|
---|
1931 | socket_data->pseudo_header = NULL;
|
---|
1932 | }
|
---|
1933 | socket_data->headerlen = 0;
|
---|
1934 | // free the address
|
---|
1935 | if( socket_data->addr ){
|
---|
1936 | if( socket_data->addrlen ){
|
---|
1937 | printf("d addr\n");
|
---|
1938 | free( socket_data->addr );
|
---|
1939 | socket_data->addrlen = 0;
|
---|
1940 | }
|
---|
1941 | socket_data->addr = NULL;
|
---|
1942 | }
|
---|
1943 | socket_data->addrlen = 0;
|
---|
1944 | }
|
---|
1945 |
|
---|
1946 | int tcp_release_and_return( packet_t packet, int result ){
|
---|
1947 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
1948 | return result;
|
---|
1949 | }
|
---|
1950 |
|
---|
1951 | /** @}
|
---|
1952 | */
|
---|