[21580dd] | 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, 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 );
|
---|
[1a0fb3f8] | 221 | tcp_globals.icmp_phone = icmp_connect_module( SERVICE_ICMP, ICMP_CONNECT_TIMEOUT );
|
---|
[21580dd] | 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 < sizeof( tcp_header_t ) + 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 ))){
|
---|
| 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 | tmp_packet = pq_add( socket_data->incoming, packet, new_sequence_number, length );
|
---|
| 616 | if( ! tmp_packet ){
|
---|
| 617 | // remove the corrupted packets
|
---|
| 618 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
| 619 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
|
---|
| 620 | }else{
|
---|
| 621 | socket_data->incoming = tmp_packet;
|
---|
| 622 | while( next_packet ){
|
---|
| 623 | new_sequence_number += length;
|
---|
| 624 | tmp_packet = pq_detach( next_packet );
|
---|
| 625 | length = packet_get_data_length( next_packet );
|
---|
| 626 | if( ERROR_OCCURRED( pq_set_order( next_packet, new_sequence_number, length ))
|
---|
| 627 | || ERROR_OCCURRED( pq_insert_after( packet, next_packet ))){
|
---|
| 628 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
|
---|
| 629 | }
|
---|
| 630 | next_packet = tmp_packet;
|
---|
| 631 | }
|
---|
| 632 | }
|
---|
| 633 | }else{
|
---|
| 634 | printf("unexpected\n");
|
---|
| 635 | // release duplicite or restricted
|
---|
| 636 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | // change state according to the acknowledging incoming fin
|
---|
| 640 | if( IS_IN_INTERVAL_OVERFLOW( old_incoming, socket_data->fin_incoming, socket_data->next_incoming )){
|
---|
| 641 | switch( socket_data->state ){
|
---|
| 642 | case TCP_SOCKET_FIN_WAIT_1:
|
---|
| 643 | case TCP_SOCKET_FIN_WAIT_2:
|
---|
| 644 | case TCP_SOCKET_CLOSING:
|
---|
| 645 | socket_data->state = TCP_SOCKET_CLOSING;
|
---|
| 646 | break;
|
---|
| 647 | //case TCP_ESTABLISHED:
|
---|
| 648 | default:
|
---|
| 649 | socket_data->state = TCP_SOCKET_CLOSE_WAIT;
|
---|
| 650 | break;
|
---|
| 651 | }
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
| 655 | if( ! packet ){
|
---|
| 656 | // create the notification packet
|
---|
| 657 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 0 ));
|
---|
| 658 | ERROR_PROPAGATE( tcp_queue_prepare_packet( socket, socket_data, packet, 1 ));
|
---|
| 659 | packet = tcp_send_prepare_packet( socket, socket_data, packet, 1, socket_data->last_outgoing + 1 );
|
---|
| 660 | }
|
---|
| 661 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 662 | // send the packet
|
---|
| 663 | tcp_send_packets( socket_data->device_id, packet );
|
---|
| 664 | return EOK;
|
---|
| 665 | }
|
---|
| 666 |
|
---|
| 667 | int tcp_queue_received_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length ){
|
---|
| 668 | ERROR_DECLARE;
|
---|
| 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 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
| 680 | }
|
---|
| 681 |
|
---|
| 682 | // decrease the window size
|
---|
| 683 | socket_data->window -= total_length;
|
---|
| 684 |
|
---|
| 685 | // notify the destination socket
|
---|
| 686 | async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, 0, 0, 0, ( ipcarg_t ) fragments );
|
---|
| 687 | return EOK;
|
---|
| 688 | }
|
---|
| 689 |
|
---|
| 690 | int tcp_process_syn_sent( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ){
|
---|
| 691 | ERROR_DECLARE;
|
---|
| 692 |
|
---|
| 693 | packet_t next_packet;
|
---|
| 694 |
|
---|
| 695 | assert( socket );
|
---|
| 696 | assert( socket_data );
|
---|
| 697 | assert( socket->specific_data == socket_data );
|
---|
| 698 | assert( header );
|
---|
| 699 | assert( packet );
|
---|
| 700 |
|
---|
| 701 | if( header->synchronize ){
|
---|
| 702 | // process acknowledgement
|
---|
| 703 | tcp_process_acknowledgement( socket, socket_data, header );
|
---|
| 704 |
|
---|
| 705 | socket_data->next_incoming = ntohl( header->sequence_number ) + 1;
|
---|
| 706 | // release additional packets
|
---|
| 707 | next_packet = pq_detach( packet );
|
---|
| 708 | if( next_packet ){
|
---|
| 709 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
|
---|
| 710 | }
|
---|
| 711 | // trim if longer than the header
|
---|
| 712 | if(( packet_get_data_length( packet ) > sizeof( * header ))
|
---|
| 713 | && ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - sizeof( * header )))){
|
---|
| 714 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
| 715 | }
|
---|
| 716 | tcp_prepare_operation_header( socket, socket_data, header, 0, 0 );
|
---|
| 717 | fibril_mutex_lock( & socket_data->operation.mutex );
|
---|
| 718 | socket_data->operation.result = tcp_queue_packet( socket, socket_data, packet, 1 );
|
---|
| 719 | if( socket_data->operation.result == EOK ){
|
---|
| 720 | socket_data->state = TCP_SOCKET_ESTABLISHED;
|
---|
| 721 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
| 722 | if( packet ){
|
---|
| 723 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 724 | // send the packet
|
---|
| 725 | tcp_send_packets( socket_data->device_id, packet );
|
---|
| 726 | // signal the result
|
---|
| 727 | fibril_condvar_signal( & socket_data->operation.condvar );
|
---|
| 728 | fibril_mutex_unlock( & socket_data->operation.mutex );
|
---|
| 729 | return EOK;
|
---|
| 730 | }
|
---|
| 731 | }
|
---|
| 732 | fibril_mutex_unlock( & socket_data->operation.mutex );
|
---|
| 733 | }
|
---|
| 734 | return tcp_release_and_return( packet, EINVAL );
|
---|
| 735 | }
|
---|
| 736 |
|
---|
| 737 | 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 ){
|
---|
| 738 | ERROR_DECLARE;
|
---|
| 739 |
|
---|
| 740 | packet_t next_packet;
|
---|
| 741 | socket_core_ref socket;
|
---|
| 742 | tcp_socket_data_ref socket_data;
|
---|
| 743 | int socket_id;
|
---|
| 744 | int listening_socket_id = listening_socket->socket_id;
|
---|
| 745 | int listening_port = listening_socket->port;
|
---|
| 746 |
|
---|
| 747 | assert( listening_socket );
|
---|
| 748 | assert( listening_socket_data );
|
---|
| 749 | assert( listening_socket->specific_data == listening_socket_data );
|
---|
| 750 | assert( header );
|
---|
| 751 | assert( packet );
|
---|
| 752 |
|
---|
| 753 | // printf( "syn %d\n", header->synchronize );
|
---|
| 754 | if( header->synchronize ){
|
---|
| 755 | socket_data = ( tcp_socket_data_ref ) malloc( sizeof( * socket_data ));
|
---|
| 756 | if( ! socket_data ){
|
---|
| 757 | return tcp_release_and_return( packet, ENOMEM );
|
---|
| 758 | }else{
|
---|
| 759 | tcp_initialize_socket_data( socket_data );
|
---|
| 760 | socket_data->local_lock = listening_socket_data->local_lock;
|
---|
| 761 | socket_data->local_sockets = listening_socket_data->local_sockets;
|
---|
| 762 | socket_data->listening_socket_id = listening_socket->socket_id;
|
---|
| 763 |
|
---|
| 764 | socket_data->next_incoming = ntohl( header->sequence_number );
|
---|
| 765 | socket_data->treshold = socket_data->next_incoming + ntohs( header->window );
|
---|
| 766 |
|
---|
| 767 | socket_data->addrlen = addrlen;
|
---|
| 768 | socket_data->addr = malloc( socket_data->addrlen );
|
---|
| 769 | if( ! socket_data->addr ){
|
---|
| 770 | free( socket_data );
|
---|
| 771 | return tcp_release_and_return( packet, ENOMEM );
|
---|
| 772 | }
|
---|
| 773 | memcpy( socket_data->addr, src, socket_data->addrlen );
|
---|
| 774 |
|
---|
| 775 | socket_data->dest_port = ntohs( header->source_port );
|
---|
| 776 | if( ERROR_OCCURRED( tl_set_address_port( socket_data->addr, socket_data->addrlen, socket_data->dest_port ))){
|
---|
| 777 | free( socket_data->addr );
|
---|
| 778 | free( socket_data );
|
---|
| 779 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
| 780 | return ERROR_CODE;
|
---|
| 781 | }
|
---|
| 782 |
|
---|
| 783 | // printf( "addr %p\n", socket_data->addr, socket_data->addrlen );
|
---|
| 784 | // create a socket
|
---|
| 785 | if( ERROR_OCCURRED( socket_create( socket_data->local_sockets, listening_socket->phone, socket_data, & socket_id ))){
|
---|
| 786 | free( socket_data->addr );
|
---|
| 787 | free( socket_data );
|
---|
| 788 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | printf("new_sock %d\n", socket_id);
|
---|
| 792 | socket_data->pseudo_header = listening_socket_data->pseudo_header;
|
---|
| 793 | socket_data->headerlen = listening_socket_data->headerlen;
|
---|
| 794 | listening_socket_data->pseudo_header = NULL;
|
---|
| 795 | listening_socket_data->headerlen = 0;
|
---|
| 796 |
|
---|
| 797 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 798 | // printf("list lg\n");
|
---|
| 799 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
| 800 | // printf("list locked\n");
|
---|
| 801 | // find the destination socket
|
---|
| 802 | listening_socket = socket_port_find( & tcp_globals.sockets, listening_port, SOCKET_MAP_KEY_LISTENING, 0 );
|
---|
| 803 | if(( ! listening_socket ) || ( listening_socket->socket_id != listening_socket_id )){
|
---|
| 804 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 805 | // a shadow may remain until app hangs up
|
---|
| 806 | return tcp_release_and_return( packet, EOK/*ENOTSOCK*/ );
|
---|
| 807 | }
|
---|
| 808 | // printf("port %d\n", listening_socket->port );
|
---|
| 809 | listening_socket_data = ( tcp_socket_data_ref ) listening_socket->specific_data;
|
---|
| 810 | assert( listening_socket_data );
|
---|
| 811 |
|
---|
| 812 | // printf("list ll\n");
|
---|
| 813 | fibril_rwlock_write_lock( listening_socket_data->local_lock );
|
---|
| 814 | // printf("list locked\n");
|
---|
| 815 |
|
---|
| 816 | socket = socket_cores_find( listening_socket_data->local_sockets, socket_id );
|
---|
| 817 | if( ! socket ){
|
---|
| 818 | // where is the socket?!?
|
---|
| 819 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 820 | return ENOTSOCK;
|
---|
| 821 | }
|
---|
| 822 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 823 | assert( socket_data );
|
---|
| 824 |
|
---|
| 825 | // uint8_t * data = socket_data->addr;
|
---|
| 826 | // 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 ] );
|
---|
| 827 |
|
---|
| 828 | ERROR_CODE = socket_port_add( & tcp_globals.sockets, listening_port, socket, ( const char * ) socket_data->addr, socket_data->addrlen );
|
---|
| 829 | assert( socket == socket_port_find( & tcp_globals.sockets, listening_port, ( const char * ) socket_data->addr, socket_data->addrlen ));
|
---|
| 830 | //ERROR_CODE = socket_bind_free_port( & tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port );
|
---|
| 831 | //tcp_globals.last_used_port = socket->port;
|
---|
| 832 | // printf("bound %d\n", socket->port );
|
---|
| 833 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 834 | if( ERROR_CODE != EOK ){
|
---|
| 835 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
| 836 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
| 837 | }
|
---|
| 838 |
|
---|
| 839 | socket_data->state = TCP_SOCKET_LISTEN;
|
---|
| 840 | socket_data->next_incoming = ntohl( header->sequence_number ) + 1;
|
---|
| 841 | // release additional packets
|
---|
| 842 | next_packet = pq_detach( packet );
|
---|
| 843 | if( next_packet ){
|
---|
| 844 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
|
---|
| 845 | }
|
---|
| 846 | // trim if longer than the header
|
---|
| 847 | if(( packet_get_data_length( packet ) > sizeof( * header ))
|
---|
| 848 | && ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - sizeof( * header )))){
|
---|
| 849 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
| 850 | return tcp_release_and_return( packet, ERROR_CODE );
|
---|
| 851 | }
|
---|
| 852 | tcp_prepare_operation_header( socket, socket_data, header, 1, 0 );
|
---|
| 853 | if( ERROR_OCCURRED( tcp_queue_packet( socket, socket_data, packet, 1 ))){
|
---|
| 854 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
| 855 | return ERROR_CODE;
|
---|
| 856 | }
|
---|
| 857 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
| 858 | // printf("send %d\n", packet_get_id( packet ));
|
---|
| 859 | if( ! packet ){
|
---|
| 860 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
| 861 | return EINVAL;
|
---|
| 862 | }else{
|
---|
| 863 | socket_data->state = TCP_SOCKET_SYN_RECEIVED;
|
---|
| 864 | // printf("unlock\n");
|
---|
| 865 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 866 | // send the packet
|
---|
| 867 | tcp_send_packets( socket_data->device_id, packet );
|
---|
| 868 | return EOK;
|
---|
| 869 | }
|
---|
| 870 | }
|
---|
| 871 | }
|
---|
| 872 | return tcp_release_and_return( packet, EINVAL );
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 | int tcp_process_syn_received( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ){
|
---|
| 876 | ERROR_DECLARE;
|
---|
| 877 |
|
---|
| 878 | socket_core_ref listening_socket;
|
---|
| 879 | tcp_socket_data_ref listening_socket_data;
|
---|
| 880 |
|
---|
| 881 | assert( socket );
|
---|
| 882 | assert( socket_data );
|
---|
| 883 | assert( socket->specific_data == socket_data );
|
---|
| 884 | assert( header );
|
---|
| 885 | assert( packet );
|
---|
| 886 |
|
---|
| 887 | printf("syn_rec\n");
|
---|
| 888 | if( header->acknowledge ){
|
---|
| 889 | // process acknowledgement
|
---|
| 890 | tcp_process_acknowledgement( socket, socket_data, header );
|
---|
| 891 |
|
---|
| 892 | socket_data->next_incoming = ntohl( header->sequence_number );// + 1;
|
---|
| 893 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
| 894 | socket_data->state = TCP_SOCKET_ESTABLISHED;
|
---|
| 895 | listening_socket = socket_cores_find( socket_data->local_sockets, socket_data->listening_socket_id );
|
---|
| 896 | if( listening_socket ){
|
---|
| 897 | listening_socket_data = ( tcp_socket_data_ref ) listening_socket->specific_data;
|
---|
| 898 | assert( listening_socket_data );
|
---|
| 899 |
|
---|
| 900 | // queue the received packet
|
---|
| 901 | if( ! ERROR_OCCURRED( dyn_fifo_push( & listening_socket->accepted, socket->socket_id, listening_socket_data->backlog ))){
|
---|
| 902 | // notify the destination socket
|
---|
| 903 | async_msg_5( socket->phone, NET_SOCKET_ACCEPTED, ( ipcarg_t ) listening_socket->socket_id, 0, 0, 0, ( ipcarg_t ) socket->socket_id );
|
---|
| 904 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 905 | return EOK;
|
---|
| 906 | }
|
---|
| 907 | }
|
---|
| 908 | // send FIN
|
---|
| 909 | socket_data->state = TCP_SOCKET_FIN_WAIT_1;
|
---|
| 910 |
|
---|
| 911 | // create the notification packet
|
---|
| 912 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 1 ));
|
---|
| 913 |
|
---|
| 914 | // send the packet
|
---|
| 915 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 1 ));
|
---|
| 916 |
|
---|
| 917 | // flush packets
|
---|
| 918 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
| 919 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 920 | if( packet ){
|
---|
| 921 | // send the packet
|
---|
| 922 | tcp_send_packets( socket_data->device_id, packet );
|
---|
| 923 | }
|
---|
| 924 | return EOK;
|
---|
| 925 | }else{
|
---|
| 926 | return tcp_release_and_return( packet, EINVAL );
|
---|
| 927 | }
|
---|
| 928 | return EINVAL;
|
---|
| 929 | }
|
---|
| 930 |
|
---|
| 931 | void tcp_process_acknowledgement( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header ){
|
---|
| 932 | size_t number;
|
---|
| 933 | size_t length;
|
---|
| 934 | packet_t packet;
|
---|
| 935 | packet_t next;
|
---|
| 936 | packet_t acknowledged = NULL;
|
---|
| 937 | packet_t first;
|
---|
| 938 | uint32_t old;
|
---|
| 939 |
|
---|
| 940 | assert( socket );
|
---|
| 941 | assert( socket_data );
|
---|
| 942 | assert( socket->specific_data == socket_data );
|
---|
| 943 | assert( header );
|
---|
| 944 |
|
---|
| 945 | if( header->acknowledge ){
|
---|
| 946 | number = ntohl( header->acknowledgement_number );
|
---|
| 947 | // if more data acknowledged
|
---|
| 948 | if( number != socket_data->expected ){
|
---|
| 949 | old = socket_data->expected;
|
---|
| 950 | if( IS_IN_INTERVAL_OVERFLOW( old, socket_data->fin_outgoing, number )){
|
---|
| 951 | switch( socket_data->state ){
|
---|
| 952 | case TCP_SOCKET_FIN_WAIT_1:
|
---|
| 953 | socket_data->state = TCP_SOCKET_FIN_WAIT_2;
|
---|
| 954 | break;
|
---|
| 955 | case TCP_SOCKET_LAST_ACK:
|
---|
| 956 | case TCP_SOCKET_CLOSING:
|
---|
| 957 | // fin acknowledged - release the socket in another fibril
|
---|
| 958 | tcp_prepare_timeout( tcp_release_after_timeout, socket, socket_data, 0, TCP_SOCKET_TIME_WAIT, NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true );
|
---|
| 959 | break;
|
---|
| 960 | default:
|
---|
| 961 | break;
|
---|
| 962 | }
|
---|
| 963 | }
|
---|
| 964 | // update the treshold if higher than set
|
---|
| 965 | if( number + ntohs( header->window ) > socket_data->expected + socket_data->treshold ){
|
---|
| 966 | socket_data->treshold = number + ntohs( header->window ) - socket_data->expected;
|
---|
| 967 | }
|
---|
| 968 | // set new expected sequence number
|
---|
| 969 | socket_data->expected = number;
|
---|
| 970 | socket_data->expected_count = 1;
|
---|
| 971 | packet = socket_data->outgoing;
|
---|
| 972 | while( pq_get_order( packet, & number, & length ) == EOK ){
|
---|
| 973 | if( IS_IN_INTERVAL_OVERFLOW(( uint32_t ) old, ( uint32_t )( number + length ), ( uint32_t ) socket_data->expected )){
|
---|
| 974 | next = pq_detach( packet );
|
---|
| 975 | if( packet == socket_data->outgoing ){
|
---|
| 976 | socket_data->outgoing = next;
|
---|
| 977 | }
|
---|
| 978 | // add to acknowledged or release
|
---|
| 979 | first = pq_add( acknowledged, packet, 0, 0 );
|
---|
| 980 | if( first ){
|
---|
| 981 | acknowledged = first;
|
---|
| 982 | }else{
|
---|
| 983 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
| 984 | }
|
---|
| 985 | packet = next;
|
---|
| 986 | }else if( old < socket_data->expected ){
|
---|
| 987 | break;
|
---|
| 988 | }
|
---|
| 989 | }
|
---|
| 990 | // release acknowledged
|
---|
| 991 | if( acknowledged ){
|
---|
| 992 | pq_release( tcp_globals.net_phone, packet_get_id( acknowledged ));
|
---|
| 993 | }
|
---|
| 994 | return;
|
---|
| 995 | // if the same as the previous time
|
---|
| 996 | }else if( number == socket_data->expected ){
|
---|
| 997 | // increase the counter
|
---|
| 998 | ++ socket_data->expected_count;
|
---|
| 999 | if( socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT ){
|
---|
| 1000 | socket_data->expected_count = 1;
|
---|
| 1001 | // TODO retransmit lock
|
---|
| 1002 | //tcp_retransmit_packet( socket, socket_data, number );
|
---|
| 1003 | }
|
---|
| 1004 | }
|
---|
| 1005 | }
|
---|
| 1006 | }
|
---|
| 1007 |
|
---|
| 1008 | int tcp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
|
---|
| 1009 | ERROR_DECLARE;
|
---|
| 1010 |
|
---|
| 1011 | packet_t packet;
|
---|
| 1012 |
|
---|
| 1013 | assert( call );
|
---|
| 1014 | assert( answer );
|
---|
| 1015 | assert( answer_count );
|
---|
| 1016 |
|
---|
| 1017 | * answer_count = 0;
|
---|
| 1018 | switch( IPC_GET_METHOD( * call )){
|
---|
| 1019 | case NET_TL_RECEIVED:
|
---|
| 1020 | //fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
| 1021 | if( ! ERROR_OCCURRED( packet_translate( tcp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
|
---|
| 1022 | ERROR_CODE = tcp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_TCP, IPC_GET_ERROR( call ));
|
---|
| 1023 | }
|
---|
| 1024 | //fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
| 1025 | return ERROR_CODE;
|
---|
| 1026 | case IPC_M_CONNECT_TO_ME:
|
---|
| 1027 | return tcp_process_client_messages( callid, * call );
|
---|
| 1028 | }
|
---|
| 1029 | return ENOTSUP;
|
---|
| 1030 | }
|
---|
| 1031 |
|
---|
| 1032 | void tcp_refresh_socket_data( tcp_socket_data_ref socket_data ){
|
---|
| 1033 | assert( socket_data );
|
---|
| 1034 |
|
---|
| 1035 | bzero( socket_data, sizeof( * socket_data ));
|
---|
| 1036 | socket_data->state = TCP_SOCKET_INITIAL;
|
---|
| 1037 | socket_data->device_id = -1;
|
---|
| 1038 | socket_data->window = NET_DEFAULT_TCP_WINDOW;
|
---|
| 1039 | socket_data->treshold = socket_data->window;
|
---|
| 1040 | socket_data->last_outgoing = TCP_INITIAL_SEQUENCE_NUMBER;
|
---|
| 1041 | socket_data->timeout = NET_DEFAULT_TCP_INITIAL_TIMEOUT;
|
---|
| 1042 | socket_data->acknowledged = socket_data->last_outgoing;
|
---|
| 1043 | socket_data->next_outgoing = socket_data->last_outgoing + 1;
|
---|
| 1044 | socket_data->expected = socket_data->next_outgoing;
|
---|
| 1045 | }
|
---|
| 1046 |
|
---|
| 1047 | void tcp_initialize_socket_data( tcp_socket_data_ref socket_data ){
|
---|
| 1048 | assert( socket_data );
|
---|
| 1049 |
|
---|
| 1050 | tcp_refresh_socket_data( socket_data );
|
---|
| 1051 | fibril_mutex_initialize( & socket_data->operation.mutex );
|
---|
| 1052 | fibril_condvar_initialize( & socket_data->operation.condvar );
|
---|
| 1053 | }
|
---|
| 1054 |
|
---|
| 1055 | int tcp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){
|
---|
| 1056 | int res;
|
---|
| 1057 | bool keep_on_going = true;
|
---|
| 1058 | socket_cores_t local_sockets;
|
---|
| 1059 | int app_phone = IPC_GET_PHONE( & call );
|
---|
| 1060 | struct sockaddr * addr;
|
---|
| 1061 | size_t addrlen;
|
---|
| 1062 | fibril_rwlock_t lock;
|
---|
| 1063 | ipc_call_t answer;
|
---|
| 1064 | int answer_count;
|
---|
| 1065 | tcp_socket_data_ref socket_data;
|
---|
| 1066 | socket_core_ref socket;
|
---|
| 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 | res = socket_create( & local_sockets, app_phone, socket_data, SOCKET_SET_SOCKET_ID( answer ));
|
---|
| 1099 | fibril_rwlock_write_unlock( & lock );
|
---|
| 1100 | if( res == EOK ){
|
---|
| 1101 | * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( tcp_header_t );
|
---|
| 1102 | * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_TCP_FRAGMENT_SIZE;
|
---|
| 1103 | answer_count = 3;
|
---|
| 1104 | }else{
|
---|
| 1105 | free( socket_data );
|
---|
| 1106 | }
|
---|
| 1107 | }
|
---|
| 1108 | break;
|
---|
| 1109 | case NET_SOCKET_BIND:
|
---|
| 1110 | res = data_receive(( void ** ) & addr, & addrlen );
|
---|
| 1111 | if( res == EOK ){
|
---|
| 1112 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
| 1113 | fibril_rwlock_write_lock( & lock );
|
---|
| 1114 | 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 );
|
---|
| 1115 | if( res == EOK ){
|
---|
| 1116 | socket = socket_cores_find( & local_sockets, SOCKET_GET_SOCKET_ID( call ));
|
---|
| 1117 | if( socket ){
|
---|
| 1118 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 1119 | assert( socket_data );
|
---|
| 1120 | socket_data->state = TCP_SOCKET_LISTEN;
|
---|
| 1121 | }
|
---|
| 1122 | }
|
---|
| 1123 | fibril_rwlock_write_unlock( & lock );
|
---|
| 1124 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 1125 | free( addr );
|
---|
| 1126 | }
|
---|
| 1127 | break;
|
---|
| 1128 | case NET_SOCKET_LISTEN:
|
---|
| 1129 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
| 1130 | // fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
| 1131 | fibril_rwlock_write_lock( & lock );
|
---|
| 1132 | res = tcp_listen_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_BACKLOG( call ));
|
---|
| 1133 | fibril_rwlock_write_unlock( & lock );
|
---|
| 1134 | // fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 1135 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
| 1136 | break;
|
---|
| 1137 | case NET_SOCKET_CONNECT:
|
---|
| 1138 | res = data_receive(( void ** ) & addr, & addrlen );
|
---|
| 1139 | if( res == EOK ){
|
---|
| 1140 | // the global lock may released in the tcp_connect_message() function
|
---|
| 1141 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
| 1142 | fibril_rwlock_write_lock( & lock );
|
---|
| 1143 | res = tcp_connect_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen );
|
---|
| 1144 | if( res != EOK ){
|
---|
| 1145 | fibril_rwlock_write_unlock( & lock );
|
---|
| 1146 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 1147 | free( addr );
|
---|
| 1148 | }
|
---|
| 1149 | }
|
---|
| 1150 | break;
|
---|
| 1151 | case NET_SOCKET_ACCEPT:
|
---|
| 1152 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
| 1153 | fibril_rwlock_write_lock( & lock );
|
---|
| 1154 | res = tcp_accept_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), & addrlen );
|
---|
| 1155 | fibril_rwlock_write_unlock( & lock );
|
---|
| 1156 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
| 1157 | if( res > 0 ){
|
---|
| 1158 | * SOCKET_SET_SOCKET_ID( answer ) = res;
|
---|
| 1159 | * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen;
|
---|
| 1160 | answer_count = 2;
|
---|
| 1161 | }
|
---|
| 1162 | break;
|
---|
| 1163 | case NET_SOCKET_SEND:
|
---|
| 1164 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
| 1165 | fibril_rwlock_write_lock( & lock );
|
---|
| 1166 | res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_DATA_FRAGMENT_SIZE( call ), SOCKET_GET_FLAGS( call ));
|
---|
| 1167 | if( res != EOK ){
|
---|
| 1168 | fibril_rwlock_write_unlock( & lock );
|
---|
| 1169 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
| 1170 | }
|
---|
| 1171 | break;
|
---|
| 1172 | case NET_SOCKET_SENDTO:
|
---|
| 1173 | res = data_receive(( void ** ) & addr, & addrlen );
|
---|
| 1174 | if( res == EOK ){
|
---|
| 1175 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
| 1176 | fibril_rwlock_write_lock( & lock );
|
---|
| 1177 | res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_DATA_FRAGMENT_SIZE( call ), SOCKET_GET_FLAGS( call ));
|
---|
| 1178 | if( res != EOK ){
|
---|
| 1179 | fibril_rwlock_write_unlock( & lock );
|
---|
| 1180 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
| 1181 | }
|
---|
| 1182 | free( addr );
|
---|
| 1183 | }
|
---|
| 1184 | break;
|
---|
| 1185 | case NET_SOCKET_RECV:
|
---|
| 1186 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
| 1187 | fibril_rwlock_write_lock( & lock );
|
---|
| 1188 | res = tcp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), NULL );
|
---|
| 1189 | fibril_rwlock_write_unlock( & lock );
|
---|
| 1190 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
| 1191 | if( res > 0 ){
|
---|
| 1192 | * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
|
---|
| 1193 | answer_count = 1;
|
---|
| 1194 | res = EOK;
|
---|
| 1195 | }
|
---|
| 1196 | break;
|
---|
| 1197 | case NET_SOCKET_RECVFROM:
|
---|
| 1198 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
| 1199 | fibril_rwlock_write_lock( & lock );
|
---|
| 1200 | res = tcp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), & addrlen );
|
---|
| 1201 | fibril_rwlock_write_unlock( & lock );
|
---|
| 1202 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
| 1203 | if( res > 0 ){
|
---|
| 1204 | * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
|
---|
| 1205 | * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen;
|
---|
| 1206 | answer_count = 2;
|
---|
| 1207 | res = EOK;
|
---|
| 1208 | }
|
---|
| 1209 | break;
|
---|
| 1210 | case NET_SOCKET_CLOSE:
|
---|
| 1211 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
| 1212 | fibril_rwlock_write_lock( & lock );
|
---|
| 1213 | res = tcp_close_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ));
|
---|
| 1214 | if( res != EOK ){
|
---|
| 1215 | fibril_rwlock_write_unlock( & lock );
|
---|
| 1216 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 1217 | }
|
---|
| 1218 | break;
|
---|
| 1219 | case NET_SOCKET_GETSOCKOPT:
|
---|
| 1220 | case NET_SOCKET_SETSOCKOPT:
|
---|
| 1221 | default:
|
---|
| 1222 | res = ENOTSUP;
|
---|
| 1223 | break;
|
---|
| 1224 | }
|
---|
| 1225 |
|
---|
| 1226 | // printf( "res = %d\n", res );
|
---|
| 1227 |
|
---|
| 1228 | answer_call( callid, res, & answer, answer_count );
|
---|
| 1229 | }
|
---|
| 1230 |
|
---|
| 1231 | printf("release\n");
|
---|
| 1232 | // release all local sockets
|
---|
| 1233 | socket_cores_release( tcp_globals.net_phone, & local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
| 1234 |
|
---|
| 1235 | return EOK;
|
---|
| 1236 | }
|
---|
| 1237 |
|
---|
| 1238 | int tcp_timeout( void * data ){
|
---|
| 1239 | tcp_timeout_ref timeout = data;
|
---|
| 1240 | int keep_write_lock = false;
|
---|
| 1241 | socket_core_ref socket;
|
---|
| 1242 | tcp_socket_data_ref socket_data;
|
---|
| 1243 |
|
---|
| 1244 | assert( timeout );
|
---|
| 1245 |
|
---|
| 1246 | // sleep the given timeout
|
---|
| 1247 | async_usleep( timeout->timeout );
|
---|
| 1248 | // lock the globals
|
---|
| 1249 | if( timeout->globals_read_only ){
|
---|
| 1250 | fibril_rwlock_read_lock( & tcp_globals.lock );
|
---|
| 1251 | }else{
|
---|
| 1252 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
| 1253 | }
|
---|
| 1254 | // find the pending operation socket
|
---|
| 1255 | socket = socket_port_find( & tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length );
|
---|
| 1256 | if( socket && ( socket->socket_id == timeout->socket_id )){
|
---|
| 1257 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 1258 | assert( socket_data );
|
---|
| 1259 | if( socket_data->local_sockets == timeout->local_sockets ){
|
---|
| 1260 | fibril_rwlock_write_lock( socket_data->local_lock );
|
---|
| 1261 | if( timeout->sequence_number ){
|
---|
| 1262 | // increase the timeout counter;
|
---|
| 1263 | ++ socket_data->timeout_count;
|
---|
| 1264 | if( socket_data->timeout_count == TCP_MAX_TIMEOUTS ){
|
---|
| 1265 | // TODO release as connection lost
|
---|
| 1266 | //tcp_refresh_socket_data( socket_data );
|
---|
| 1267 | }
|
---|
| 1268 | // retransmit
|
---|
| 1269 | // TODO enable retransmit
|
---|
| 1270 | //tcp_retransmit_packet( socket, socket_data, timeout->sequence_number );
|
---|
| 1271 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 1272 | }else{
|
---|
| 1273 | fibril_mutex_lock( & socket_data->operation.mutex );
|
---|
| 1274 | // set the timeout operation result if state not changed
|
---|
| 1275 | if( socket_data->state == timeout->state ){
|
---|
| 1276 | socket_data->operation.result = ETIMEOUT;
|
---|
| 1277 | // notify the main fibril
|
---|
| 1278 | fibril_condvar_signal( & socket_data->operation.condvar );
|
---|
| 1279 | // keep the global write lock
|
---|
| 1280 | keep_write_lock = true;
|
---|
| 1281 | }else{
|
---|
| 1282 | // operation is ok, do nothing
|
---|
| 1283 | // unlocking from now on, so the unlock order does not matter...
|
---|
| 1284 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 1285 | }
|
---|
| 1286 | fibril_mutex_unlock( & socket_data->operation.mutex );
|
---|
| 1287 | }
|
---|
| 1288 | }
|
---|
| 1289 | }
|
---|
| 1290 | // unlock only if no socket
|
---|
| 1291 | if( timeout->globals_read_only ){
|
---|
| 1292 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
| 1293 | }else if( ! keep_write_lock ){
|
---|
| 1294 | // release if not desired
|
---|
| 1295 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 1296 | }
|
---|
| 1297 | // release the timeout structure
|
---|
| 1298 | free( timeout );
|
---|
| 1299 | return EOK;
|
---|
| 1300 | }
|
---|
| 1301 |
|
---|
| 1302 | int tcp_release_after_timeout( void * data ){
|
---|
| 1303 | tcp_timeout_ref timeout = data;
|
---|
| 1304 | socket_core_ref socket;
|
---|
| 1305 | tcp_socket_data_ref socket_data;
|
---|
| 1306 | fibril_rwlock_t * local_lock;
|
---|
| 1307 |
|
---|
| 1308 | assert( timeout );
|
---|
| 1309 |
|
---|
| 1310 | // sleep the given timeout
|
---|
| 1311 | async_usleep( timeout->timeout );
|
---|
| 1312 | // lock the globals
|
---|
| 1313 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
| 1314 | // find the pending operation socket
|
---|
| 1315 | socket = socket_port_find( & tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length );
|
---|
| 1316 | if( socket && ( socket->socket_id == timeout->socket_id )){
|
---|
| 1317 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 1318 | assert( socket_data );
|
---|
| 1319 | if( socket_data->local_sockets == timeout->local_sockets ){
|
---|
| 1320 | local_lock = socket_data->local_lock;
|
---|
| 1321 | fibril_rwlock_write_lock( local_lock );
|
---|
| 1322 | socket_destroy( tcp_globals.net_phone, timeout->socket_id, timeout->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
|
---|
| 1323 | fibril_rwlock_write_unlock( local_lock );
|
---|
| 1324 | }
|
---|
| 1325 | }
|
---|
| 1326 | // unlock the globals
|
---|
| 1327 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 1328 | // release the timeout structure
|
---|
| 1329 | free( timeout );
|
---|
| 1330 | return EOK;
|
---|
| 1331 | }
|
---|
| 1332 |
|
---|
| 1333 | void tcp_retransmit_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number ){
|
---|
| 1334 | packet_t packet;
|
---|
| 1335 | packet_t copy;
|
---|
| 1336 | size_t data_length;
|
---|
| 1337 |
|
---|
| 1338 | assert( socket );
|
---|
| 1339 | assert( socket_data );
|
---|
| 1340 | assert( socket->specific_data == socket_data );
|
---|
| 1341 |
|
---|
| 1342 | // sent packet?
|
---|
| 1343 | packet = pq_find( socket_data->outgoing, sequence_number );
|
---|
| 1344 | printf("retransmit %d\n", packet_get_id( packet ));
|
---|
| 1345 | if( packet ){
|
---|
| 1346 | pq_get_order( packet, NULL, & data_length );
|
---|
| 1347 | copy = tcp_prepare_copy( socket, socket_data, packet, data_length, sequence_number );
|
---|
| 1348 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 1349 | // printf( "r send %d\n", packet_get_id( packet ));
|
---|
| 1350 | if( copy ){
|
---|
| 1351 | tcp_send_packets( socket_data->device_id, copy );
|
---|
| 1352 | }
|
---|
| 1353 | }else{
|
---|
| 1354 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 1355 | }
|
---|
| 1356 | }
|
---|
| 1357 |
|
---|
| 1358 | int tcp_listen_message( socket_cores_ref local_sockets, int socket_id, int backlog ){
|
---|
| 1359 | socket_core_ref socket;
|
---|
| 1360 | tcp_socket_data_ref socket_data;
|
---|
| 1361 |
|
---|
| 1362 | assert( local_sockets );
|
---|
| 1363 |
|
---|
| 1364 | if( backlog < 0 ) return EINVAL;
|
---|
| 1365 | // find the socket
|
---|
| 1366 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
| 1367 | if( ! socket ) return ENOTSOCK;
|
---|
| 1368 | // get the socket specific data
|
---|
| 1369 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 1370 | assert( socket_data );
|
---|
| 1371 | // set the backlog
|
---|
| 1372 | socket_data->backlog = backlog;
|
---|
| 1373 | return EOK;
|
---|
| 1374 | }
|
---|
| 1375 |
|
---|
| 1376 | int tcp_connect_message( socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen ){
|
---|
| 1377 | ERROR_DECLARE;
|
---|
| 1378 |
|
---|
| 1379 | socket_core_ref socket;
|
---|
| 1380 |
|
---|
| 1381 | assert( local_sockets );
|
---|
| 1382 | assert( addr );
|
---|
| 1383 | assert( addrlen > 0 );
|
---|
| 1384 |
|
---|
| 1385 | // find the socket
|
---|
| 1386 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
| 1387 | if( ! socket ) return ENOTSOCK;
|
---|
| 1388 | if( ERROR_OCCURRED( tcp_connect_core( socket, local_sockets, addr, addrlen ))){
|
---|
| 1389 | tcp_free_socket_data( socket );
|
---|
| 1390 | // unbind if bound
|
---|
| 1391 | if( socket->port > 0 ){
|
---|
| 1392 | socket_ports_exclude( & tcp_globals.sockets, socket->port );
|
---|
| 1393 | socket->port = 0;
|
---|
| 1394 | }
|
---|
| 1395 | }
|
---|
| 1396 | return ERROR_CODE;
|
---|
| 1397 | }
|
---|
| 1398 |
|
---|
| 1399 | int tcp_connect_core( socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen ){
|
---|
| 1400 | ERROR_DECLARE;
|
---|
| 1401 |
|
---|
| 1402 | tcp_socket_data_ref socket_data;
|
---|
| 1403 | packet_t packet;
|
---|
| 1404 |
|
---|
| 1405 | assert( socket );
|
---|
| 1406 | assert( addr );
|
---|
| 1407 | assert( addrlen > 0 );
|
---|
| 1408 |
|
---|
| 1409 | // get the socket specific data
|
---|
| 1410 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 1411 | assert( socket_data );
|
---|
| 1412 | assert( socket->specific_data == socket_data );
|
---|
| 1413 | if(( socket_data->state != TCP_SOCKET_INITIAL )
|
---|
| 1414 | && (( socket_data->state != TCP_SOCKET_LISTEN ) || ( socket->port <= 0 ))){
|
---|
| 1415 | return EINVAL;
|
---|
| 1416 | }
|
---|
| 1417 | // get the destination port
|
---|
| 1418 | ERROR_PROPAGATE( tl_get_address_port( addr, addrlen, & socket_data->dest_port ));
|
---|
| 1419 | if( socket->port <= 0 ){
|
---|
| 1420 | // try to find a free port
|
---|
| 1421 | ERROR_PROPAGATE( socket_bind_free_port( & tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port ));
|
---|
| 1422 | // set the next port as the search starting port number
|
---|
| 1423 | tcp_globals.last_used_port = socket->port;
|
---|
| 1424 | }
|
---|
| 1425 | 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 ));
|
---|
| 1426 |
|
---|
| 1427 | // create the notification packet
|
---|
| 1428 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 1, 0 ));
|
---|
| 1429 |
|
---|
| 1430 | // unlock the globals and wait for an operation
|
---|
| 1431 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 1432 |
|
---|
| 1433 | socket_data->addr = addr;
|
---|
| 1434 | socket_data->addrlen = addrlen;
|
---|
| 1435 | // send the packet
|
---|
| 1436 | if( ERROR_OCCURRED( tcp_queue_packet( socket, socket_data, packet, 1 ))
|
---|
| 1437 | || ERROR_OCCURRED( tcp_prepare_timeout( tcp_timeout, socket, socket_data, 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false ))){
|
---|
| 1438 | socket_data->addr = NULL;
|
---|
| 1439 | socket_data->addrlen = 0;
|
---|
| 1440 | fibril_rwlock_write_lock( & tcp_globals.lock );
|
---|
| 1441 | }else{
|
---|
| 1442 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
| 1443 | if( packet ){
|
---|
| 1444 | fibril_mutex_lock( & socket_data->operation.mutex );
|
---|
| 1445 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 1446 | // send the packet
|
---|
| 1447 | printf( "connecting %d\n", packet_get_id( packet ));
|
---|
| 1448 | tcp_send_packets( socket_data->device_id, packet );
|
---|
| 1449 | // wait for a reply
|
---|
| 1450 | fibril_condvar_wait( & socket_data->operation.condvar, & socket_data->operation.mutex );
|
---|
| 1451 | ERROR_CODE = socket_data->operation.result;
|
---|
| 1452 | if( ERROR_CODE != EOK ){
|
---|
| 1453 | socket_data->addr = NULL;
|
---|
| 1454 | socket_data->addrlen = 0;
|
---|
| 1455 | }
|
---|
| 1456 | }else{
|
---|
| 1457 | socket_data->addr = NULL;
|
---|
| 1458 | socket_data->addrlen = 0;
|
---|
| 1459 | ERROR_CODE = EINTR;
|
---|
| 1460 | }
|
---|
| 1461 | }
|
---|
| 1462 |
|
---|
| 1463 | fibril_mutex_unlock( & socket_data->operation.mutex );
|
---|
| 1464 |
|
---|
| 1465 | // return the result
|
---|
| 1466 | return ERROR_CODE;
|
---|
| 1467 | }
|
---|
| 1468 |
|
---|
| 1469 | int tcp_queue_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ){
|
---|
| 1470 | ERROR_DECLARE;
|
---|
| 1471 |
|
---|
| 1472 | tcp_header_ref header;
|
---|
| 1473 |
|
---|
| 1474 | assert( socket );
|
---|
| 1475 | assert( socket_data );
|
---|
| 1476 | assert( socket->specific_data == socket_data );
|
---|
| 1477 |
|
---|
| 1478 | // get tcp header
|
---|
| 1479 | header = ( tcp_header_ref ) packet_get_data( packet );
|
---|
| 1480 | if( ! header ) return NO_DATA;
|
---|
| 1481 | header->destination_port = htons( socket_data->dest_port );
|
---|
| 1482 | header->source_port = htons( socket->port );
|
---|
| 1483 | header->sequence_number = htonl( socket_data->next_outgoing );
|
---|
| 1484 | if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) socket_data->addr, socket_data->addrlen ))){
|
---|
| 1485 | return tcp_release_and_return( packet, EINVAL );
|
---|
| 1486 | }
|
---|
| 1487 | // remember the outgoing FIN
|
---|
| 1488 | if( header->finalize ){
|
---|
| 1489 | socket_data->fin_outgoing = socket_data->next_outgoing;
|
---|
| 1490 | }
|
---|
| 1491 | return EOK;
|
---|
| 1492 | }
|
---|
| 1493 |
|
---|
| 1494 | int tcp_queue_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ){
|
---|
| 1495 | ERROR_DECLARE;
|
---|
| 1496 | packet_t first;
|
---|
| 1497 |
|
---|
| 1498 | assert( socket );
|
---|
| 1499 | assert( socket_data );
|
---|
| 1500 | assert( socket->specific_data == socket_data );
|
---|
| 1501 |
|
---|
| 1502 | ERROR_PROPAGATE( tcp_queue_prepare_packet( socket, socket_data, packet, data_length ));
|
---|
| 1503 |
|
---|
| 1504 | first = pq_add( socket_data->outgoing, packet, socket_data->next_outgoing, data_length );
|
---|
| 1505 | if( ! first ){
|
---|
| 1506 | return tcp_release_and_return( packet, EINVAL );
|
---|
| 1507 | }
|
---|
| 1508 | socket_data->outgoing = first;
|
---|
| 1509 | socket_data->next_outgoing += data_length;
|
---|
| 1510 | return EOK;
|
---|
| 1511 | }
|
---|
| 1512 |
|
---|
| 1513 | packet_t tcp_get_packets_to_send( socket_core_ref socket, tcp_socket_data_ref socket_data ){
|
---|
| 1514 | ERROR_DECLARE;
|
---|
| 1515 |
|
---|
| 1516 | packet_t packet;
|
---|
| 1517 | packet_t copy;
|
---|
| 1518 | packet_t sending = NULL;
|
---|
| 1519 | packet_t previous = NULL;
|
---|
| 1520 | size_t data_length;
|
---|
| 1521 |
|
---|
| 1522 | assert( socket );
|
---|
| 1523 | assert( socket_data );
|
---|
| 1524 | assert( socket->specific_data == socket_data );
|
---|
| 1525 |
|
---|
| 1526 | packet = pq_find( socket_data->outgoing, socket_data->last_outgoing + 1 );
|
---|
| 1527 | while( packet ){
|
---|
| 1528 | pq_get_order( packet, NULL, & data_length );
|
---|
| 1529 | // send only if fits into the window
|
---|
| 1530 | // respecting the possible overflow
|
---|
| 1531 | 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 ))){
|
---|
| 1532 | copy = tcp_prepare_copy( socket, socket_data, packet, data_length, socket_data->last_outgoing + 1 );
|
---|
| 1533 | if( ! copy ){
|
---|
| 1534 | return sending;
|
---|
| 1535 | }
|
---|
| 1536 | if( ! sending ){
|
---|
| 1537 | sending = copy;
|
---|
| 1538 | }else{
|
---|
| 1539 | if( ERROR_OCCURRED( pq_insert_after( previous, copy ))){
|
---|
| 1540 | pq_release( tcp_globals.net_phone, packet_get_id( copy ));
|
---|
| 1541 | return sending;
|
---|
| 1542 | }
|
---|
| 1543 | }
|
---|
| 1544 | previous = copy;
|
---|
| 1545 | packet = pq_next( packet );
|
---|
| 1546 | // overflow occurred ?
|
---|
| 1547 | if(( ! packet ) && ( socket_data->last_outgoing > socket_data->next_outgoing )){
|
---|
| 1548 | printf("gpts overflow\n");
|
---|
| 1549 | // continue from the beginning
|
---|
| 1550 | packet = socket_data->outgoing;
|
---|
| 1551 | }
|
---|
| 1552 | socket_data->last_outgoing += data_length;
|
---|
| 1553 | }else{
|
---|
| 1554 | break;
|
---|
| 1555 | }
|
---|
| 1556 | }
|
---|
| 1557 | return sending;
|
---|
| 1558 | }
|
---|
| 1559 |
|
---|
| 1560 | 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 ){
|
---|
| 1561 | ERROR_DECLARE;
|
---|
| 1562 |
|
---|
| 1563 | tcp_header_ref header;
|
---|
| 1564 | uint32_t checksum;
|
---|
| 1565 |
|
---|
| 1566 | assert( socket );
|
---|
| 1567 | assert( socket_data );
|
---|
| 1568 | assert( socket->specific_data == socket_data );
|
---|
| 1569 |
|
---|
| 1570 | // adjust the pseudo header
|
---|
| 1571 | if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( socket_data->pseudo_header, socket_data->headerlen, packet_get_data_length( packet )))){
|
---|
| 1572 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
| 1573 | return NULL;
|
---|
| 1574 | }
|
---|
| 1575 |
|
---|
| 1576 | // get the header
|
---|
| 1577 | header = ( tcp_header_ref ) packet_get_data( packet );
|
---|
| 1578 | if( ! header ){
|
---|
| 1579 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
| 1580 | return NULL;
|
---|
| 1581 | }
|
---|
| 1582 | assert( ntohl( header->sequence_number ) == sequence_number );
|
---|
| 1583 |
|
---|
| 1584 | // adjust the header
|
---|
| 1585 | if( socket_data->next_incoming ){
|
---|
| 1586 | header->acknowledgement_number = htonl( socket_data->next_incoming );
|
---|
| 1587 | header->acknowledge = 1;
|
---|
| 1588 | }
|
---|
| 1589 | header->window = htons( socket_data->window );
|
---|
| 1590 |
|
---|
| 1591 | // checksum
|
---|
| 1592 | header->checksum = 0;
|
---|
| 1593 | checksum = compute_checksum( 0, socket_data->pseudo_header, socket_data->headerlen );
|
---|
| 1594 | checksum = compute_checksum( checksum, ( uint8_t * ) packet_get_data( packet ), packet_get_data_length( packet ));
|
---|
| 1595 | header->checksum = htons( flip_checksum( compact_checksum( checksum )));
|
---|
| 1596 | // prepare the packet
|
---|
| 1597 | if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_TCP, 0, 0, 0, 0 ))
|
---|
| 1598 | // prepare the timeout
|
---|
| 1599 | || ERROR_OCCURRED( tcp_prepare_timeout( tcp_timeout, socket, socket_data, sequence_number, socket_data->state, socket_data->timeout, true ))){
|
---|
| 1600 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
| 1601 | return NULL;
|
---|
| 1602 | }
|
---|
| 1603 | return packet;
|
---|
| 1604 | }
|
---|
| 1605 |
|
---|
| 1606 | 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 ){
|
---|
| 1607 | packet_t copy;
|
---|
| 1608 |
|
---|
| 1609 | assert( socket );
|
---|
| 1610 | assert( socket_data );
|
---|
| 1611 | assert( socket->specific_data == socket_data );
|
---|
| 1612 |
|
---|
| 1613 | // make a copy of the packet
|
---|
| 1614 | copy = packet_get_copy( tcp_globals.net_phone, packet );
|
---|
| 1615 | if( ! copy ) return NULL;
|
---|
| 1616 |
|
---|
| 1617 | return tcp_send_prepare_packet( socket, socket_data, copy, data_length, sequence_number );
|
---|
| 1618 | }
|
---|
| 1619 |
|
---|
| 1620 | void tcp_send_packets( device_id_t device_id, packet_t packet ){
|
---|
| 1621 | packet_t next;
|
---|
| 1622 |
|
---|
| 1623 | while( packet ){
|
---|
| 1624 | next = pq_detach( packet );
|
---|
| 1625 | ip_send_msg( tcp_globals.ip_phone, device_id, packet, SERVICE_TCP, 0 );
|
---|
| 1626 | packet = next;
|
---|
| 1627 | }
|
---|
| 1628 | }
|
---|
| 1629 |
|
---|
| 1630 | void tcp_prepare_operation_header( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize ){
|
---|
| 1631 | assert( socket );
|
---|
| 1632 | assert( socket_data );
|
---|
| 1633 | assert( socket->specific_data == socket_data );
|
---|
| 1634 | assert( header );
|
---|
| 1635 |
|
---|
| 1636 | bzero( header, sizeof( * header ));
|
---|
| 1637 | header->source_port = htons( socket->port );
|
---|
| 1638 | header->source_port = htons( socket_data->dest_port );
|
---|
| 1639 | header->header_length = TCP_COMPUTE_HEADER_LENGTH( sizeof( * header ));
|
---|
| 1640 | header->synchronize = synchronize;
|
---|
| 1641 | header->finalize = finalize;
|
---|
| 1642 | }
|
---|
| 1643 |
|
---|
| 1644 | 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 ){
|
---|
| 1645 | tcp_timeout_ref operation_timeout;
|
---|
| 1646 | fid_t fibril;
|
---|
| 1647 |
|
---|
| 1648 | assert( socket );
|
---|
| 1649 | assert( socket_data );
|
---|
| 1650 | assert( socket->specific_data == socket_data );
|
---|
| 1651 |
|
---|
| 1652 | // prepare the timeout with key bundle structure
|
---|
| 1653 | operation_timeout = malloc( sizeof( * operation_timeout ) + socket->key_length + 1 );
|
---|
| 1654 | if( ! operation_timeout ) return ENOMEM;
|
---|
| 1655 | bzero( operation_timeout, sizeof( * operation_timeout ));
|
---|
| 1656 | operation_timeout->globals_read_only = globals_read_only;
|
---|
| 1657 | operation_timeout->port = socket->port;
|
---|
| 1658 | operation_timeout->local_sockets = socket_data->local_sockets;
|
---|
| 1659 | operation_timeout->socket_id = socket->socket_id;
|
---|
| 1660 | operation_timeout->timeout = timeout;
|
---|
| 1661 | operation_timeout->sequence_number = sequence_number;
|
---|
| 1662 | operation_timeout->state = state;
|
---|
| 1663 |
|
---|
| 1664 | // copy the key
|
---|
| 1665 | operation_timeout->key = (( char * ) operation_timeout ) + sizeof( * operation_timeout );
|
---|
| 1666 | operation_timeout->key_length = socket->key_length;
|
---|
| 1667 | memcpy( operation_timeout->key, socket->key, socket->key_length );
|
---|
| 1668 | operation_timeout->key[ operation_timeout->key_length ] = '\0';
|
---|
| 1669 |
|
---|
| 1670 | // prepare the timeouting thread
|
---|
| 1671 | fibril = fibril_create( timeout_function, operation_timeout );
|
---|
| 1672 | if( ! fibril ){
|
---|
| 1673 | free( operation_timeout );
|
---|
| 1674 | return EPARTY;
|
---|
| 1675 | }
|
---|
| 1676 | // fibril_mutex_lock( & socket_data->operation.mutex );
|
---|
| 1677 | // start the timeouting fibril
|
---|
| 1678 | fibril_add_ready( fibril );
|
---|
| 1679 | //socket_data->state = state;
|
---|
| 1680 | return EOK;
|
---|
| 1681 | }
|
---|
| 1682 |
|
---|
| 1683 | int tcp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ){
|
---|
| 1684 | ERROR_DECLARE;
|
---|
| 1685 |
|
---|
| 1686 | socket_core_ref socket;
|
---|
| 1687 | tcp_socket_data_ref socket_data;
|
---|
| 1688 | int packet_id;
|
---|
| 1689 | packet_t packet;
|
---|
| 1690 | size_t length;
|
---|
| 1691 |
|
---|
| 1692 | assert( local_sockets );
|
---|
| 1693 |
|
---|
| 1694 | // find the socket
|
---|
| 1695 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
| 1696 | if( ! socket ) return ENOTSOCK;
|
---|
| 1697 | // get the socket specific data
|
---|
| 1698 | if( ! socket->specific_data ) return NO_DATA;
|
---|
| 1699 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 1700 |
|
---|
| 1701 | // check state
|
---|
| 1702 | if(( socket_data->state != TCP_SOCKET_ESTABLISHED ) && ( socket_data->state != TCP_SOCKET_CLOSE_WAIT )){
|
---|
| 1703 | return ENOTCONN;
|
---|
| 1704 | }
|
---|
| 1705 |
|
---|
| 1706 | // send the source address if desired
|
---|
| 1707 | if( addrlen ){
|
---|
| 1708 | ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen ));
|
---|
| 1709 | * addrlen = socket_data->addrlen;
|
---|
| 1710 | }
|
---|
| 1711 |
|
---|
| 1712 | // get the next received packet
|
---|
| 1713 | packet_id = dyn_fifo_value( & socket->received );
|
---|
| 1714 | if( packet_id < 0 ) return NO_DATA;
|
---|
| 1715 | ERROR_PROPAGATE( packet_translate( tcp_globals.net_phone, & packet, packet_id ));
|
---|
| 1716 |
|
---|
| 1717 | // reply the packets
|
---|
| 1718 | ERROR_PROPAGATE( socket_reply_packets( packet, & length ));
|
---|
| 1719 |
|
---|
| 1720 | // release the packet
|
---|
| 1721 | dyn_fifo_pop( & socket->received );
|
---|
| 1722 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
| 1723 | // return the total length
|
---|
| 1724 | return ( int ) length;
|
---|
| 1725 | }
|
---|
| 1726 |
|
---|
| 1727 | int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t data_fragment_size, int flags ){
|
---|
| 1728 | ERROR_DECLARE;
|
---|
| 1729 |
|
---|
| 1730 | socket_core_ref socket;
|
---|
| 1731 | tcp_socket_data_ref socket_data;
|
---|
| 1732 | packet_dimension_ref packet_dimension;
|
---|
| 1733 | packet_t packet;
|
---|
| 1734 | size_t total_length;
|
---|
| 1735 | tcp_header_ref header;
|
---|
| 1736 | int index;
|
---|
| 1737 | int result;
|
---|
| 1738 |
|
---|
| 1739 | assert( local_sockets );
|
---|
| 1740 |
|
---|
| 1741 | // find the socket
|
---|
| 1742 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
| 1743 | if( ! socket ) return ENOTSOCK;
|
---|
| 1744 | // get the socket specific data
|
---|
| 1745 | if( ! socket->specific_data ) return NO_DATA;
|
---|
| 1746 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 1747 |
|
---|
| 1748 | // check state
|
---|
| 1749 | if(( socket_data->state != TCP_SOCKET_ESTABLISHED ) && ( socket_data->state != TCP_SOCKET_CLOSE_WAIT )){
|
---|
| 1750 | return ENOTCONN;
|
---|
| 1751 | }
|
---|
| 1752 |
|
---|
| 1753 | ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ));
|
---|
| 1754 |
|
---|
| 1755 | // TODO return the device_id + data_fragment_size if different - the client should send it again
|
---|
| 1756 | // ( two messages are better than ip fragmentation )
|
---|
| 1757 |
|
---|
| 1758 | for( index = 0; index < fragments; ++ index ){
|
---|
| 1759 | // read the data fragment
|
---|
| 1760 | result = tl_socket_read_packet_data( tcp_globals.net_phone, & packet, sizeof( tcp_header_t ), packet_dimension, socket_data->addr, socket_data->addrlen );
|
---|
| 1761 | if( result < 0 ) return result;
|
---|
| 1762 | total_length = ( size_t ) result;
|
---|
| 1763 | // prefix the tcp header
|
---|
| 1764 | header = PACKET_PREFIX( packet, tcp_header_t );
|
---|
| 1765 | if( ! header ){
|
---|
| 1766 | return tcp_release_and_return( packet, ENOMEM );
|
---|
| 1767 | }
|
---|
| 1768 | tcp_prepare_operation_header( socket, socket_data, header, 0, 0 );
|
---|
| 1769 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 0 ));
|
---|
| 1770 | }
|
---|
| 1771 |
|
---|
| 1772 | // flush packets
|
---|
| 1773 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
| 1774 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 1775 | fibril_rwlock_read_unlock( & tcp_globals.lock );
|
---|
| 1776 | if( packet ){
|
---|
| 1777 | // send the packet
|
---|
| 1778 | tcp_send_packets( socket_data->device_id, packet );
|
---|
| 1779 | }
|
---|
| 1780 |
|
---|
| 1781 | return EOK;
|
---|
| 1782 | }
|
---|
| 1783 |
|
---|
| 1784 | int tcp_close_message( socket_cores_ref local_sockets, int socket_id ){
|
---|
| 1785 | ERROR_DECLARE;
|
---|
| 1786 |
|
---|
| 1787 | socket_core_ref socket;
|
---|
| 1788 | tcp_socket_data_ref socket_data;
|
---|
| 1789 | packet_t packet;
|
---|
| 1790 |
|
---|
| 1791 | // find the socket
|
---|
| 1792 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
| 1793 | if( ! socket ) return ENOTSOCK;
|
---|
| 1794 | // get the socket specific data
|
---|
| 1795 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 1796 | assert( socket_data );
|
---|
| 1797 |
|
---|
| 1798 | // check state
|
---|
| 1799 | switch( socket_data->state ){
|
---|
| 1800 | case TCP_SOCKET_ESTABLISHED:
|
---|
| 1801 | socket_data->state = TCP_SOCKET_FIN_WAIT_1;
|
---|
| 1802 | break;
|
---|
| 1803 | case TCP_SOCKET_CLOSE_WAIT:
|
---|
| 1804 | socket_data->state = TCP_SOCKET_LAST_ACK;
|
---|
| 1805 | break;
|
---|
| 1806 | // case TCP_SOCKET_LISTEN:
|
---|
| 1807 | default:
|
---|
| 1808 | // just destroy
|
---|
| 1809 | if( ! ERROR_OCCURRED( socket_destroy( tcp_globals.net_phone, socket_id, local_sockets, & tcp_globals.sockets, tcp_free_socket_data ))){
|
---|
| 1810 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 1811 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 1812 | }
|
---|
| 1813 | return ERROR_CODE;
|
---|
| 1814 | }
|
---|
| 1815 | // send FIN
|
---|
| 1816 | // TODO should I wait to complete?
|
---|
| 1817 |
|
---|
| 1818 | // create the notification packet
|
---|
| 1819 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 1 ));
|
---|
| 1820 |
|
---|
| 1821 | // send the packet
|
---|
| 1822 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 1 ));
|
---|
| 1823 |
|
---|
| 1824 | // flush packets
|
---|
| 1825 | packet = tcp_get_packets_to_send( socket, socket_data );
|
---|
| 1826 | fibril_rwlock_write_unlock( socket_data->local_lock );
|
---|
| 1827 | fibril_rwlock_write_unlock( & tcp_globals.lock );
|
---|
| 1828 | if( packet ){
|
---|
| 1829 | // send the packet
|
---|
| 1830 | tcp_send_packets( socket_data->device_id, packet );
|
---|
| 1831 | }
|
---|
| 1832 | return EOK;
|
---|
| 1833 | }
|
---|
| 1834 |
|
---|
| 1835 | int tcp_create_notification_packet( packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize ){
|
---|
| 1836 | ERROR_DECLARE;
|
---|
| 1837 |
|
---|
| 1838 | packet_dimension_ref packet_dimension;
|
---|
| 1839 | tcp_header_ref header;
|
---|
| 1840 |
|
---|
| 1841 | assert( packet );
|
---|
| 1842 |
|
---|
| 1843 | // get the device packet dimension
|
---|
| 1844 | ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ));
|
---|
| 1845 | // get a new packet
|
---|
| 1846 | * packet = packet_get_4( tcp_globals.net_phone, sizeof( tcp_header_t ), packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix );
|
---|
| 1847 | if( ! * packet ) return ENOMEM;
|
---|
| 1848 | // allocate space in the packet
|
---|
| 1849 | header = PACKET_SUFFIX( * packet, tcp_header_t );
|
---|
| 1850 | if( ! header ){
|
---|
| 1851 | tcp_release_and_return( * packet, ENOMEM );
|
---|
| 1852 | }
|
---|
| 1853 |
|
---|
| 1854 | tcp_prepare_operation_header( socket, socket_data, header, synchronize, finalize );
|
---|
| 1855 | return EOK;
|
---|
| 1856 | }
|
---|
| 1857 |
|
---|
| 1858 | int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, size_t * addrlen ){
|
---|
| 1859 | ERROR_DECLARE;
|
---|
| 1860 |
|
---|
| 1861 | socket_core_ref accepted;
|
---|
| 1862 | socket_core_ref socket;
|
---|
| 1863 | tcp_socket_data_ref socket_data;
|
---|
| 1864 |
|
---|
| 1865 | assert( local_sockets );
|
---|
| 1866 | assert( addrlen );
|
---|
| 1867 |
|
---|
| 1868 | // find the socket
|
---|
| 1869 | socket = socket_cores_find( local_sockets, socket_id );
|
---|
| 1870 | if( ! socket ) return ENOTSOCK;
|
---|
| 1871 | // get the socket specific data
|
---|
| 1872 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 1873 | assert( socket_data );
|
---|
| 1874 |
|
---|
| 1875 | // check state
|
---|
| 1876 | if( socket_data->state != TCP_SOCKET_LISTEN ){
|
---|
| 1877 | return EINVAL;
|
---|
| 1878 | }
|
---|
| 1879 |
|
---|
| 1880 | do{
|
---|
| 1881 | socket_id = dyn_fifo_value( & socket->accepted );
|
---|
| 1882 | if( socket_id < 0 ) return ENOTSOCK;
|
---|
| 1883 |
|
---|
| 1884 | accepted = socket_cores_find( local_sockets, socket_id );
|
---|
| 1885 | if( ! accepted ) return ENOTSOCK;
|
---|
| 1886 | // get the socket specific data
|
---|
| 1887 | socket_data = ( tcp_socket_data_ref ) accepted->specific_data;
|
---|
| 1888 | assert( socket_data );
|
---|
| 1889 | if( socket_data->state == TCP_SOCKET_ESTABLISHED ){
|
---|
| 1890 | ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen ));
|
---|
| 1891 | * addrlen = socket_data->addrlen;
|
---|
| 1892 | }
|
---|
| 1893 | dyn_fifo_pop( & socket->accepted );
|
---|
| 1894 | }while( socket_data->state != TCP_SOCKET_ESTABLISHED );
|
---|
| 1895 | printf("ret accept %d\n", accepted->socket_id );
|
---|
| 1896 | return accepted->socket_id;
|
---|
| 1897 | }
|
---|
| 1898 |
|
---|
| 1899 | void tcp_free_socket_data( socket_core_ref socket ){
|
---|
| 1900 | tcp_socket_data_ref socket_data;
|
---|
| 1901 |
|
---|
| 1902 | assert( socket );
|
---|
| 1903 |
|
---|
| 1904 | printf( "destroy_socket %d\n", socket->socket_id );
|
---|
| 1905 |
|
---|
| 1906 | // get the socket specific data
|
---|
| 1907 | socket_data = ( tcp_socket_data_ref ) socket->specific_data;
|
---|
| 1908 | assert( socket_data );
|
---|
| 1909 | //free the pseudo header
|
---|
| 1910 | if( socket_data->pseudo_header ){
|
---|
| 1911 | if( socket_data->headerlen ){
|
---|
| 1912 | printf("d pseudo\n");
|
---|
| 1913 | free( socket_data->pseudo_header );
|
---|
| 1914 | socket_data->headerlen = 0;
|
---|
| 1915 | }
|
---|
| 1916 | socket_data->pseudo_header = NULL;
|
---|
| 1917 | }
|
---|
| 1918 | socket_data->headerlen = 0;
|
---|
| 1919 | // free the address
|
---|
| 1920 | if( socket_data->addr ){
|
---|
| 1921 | if( socket_data->addrlen ){
|
---|
| 1922 | printf("d addr\n");
|
---|
| 1923 | free( socket_data->addr );
|
---|
| 1924 | socket_data->addrlen = 0;
|
---|
| 1925 | }
|
---|
| 1926 | socket_data->addr = NULL;
|
---|
| 1927 | }
|
---|
| 1928 | socket_data->addrlen = 0;
|
---|
| 1929 | }
|
---|
| 1930 |
|
---|
| 1931 | int tcp_release_and_return( packet_t packet, int result ){
|
---|
| 1932 | pq_release( tcp_globals.net_phone, packet_get_id( packet ));
|
---|
| 1933 | return result;
|
---|
| 1934 | }
|
---|
| 1935 |
|
---|
| 1936 | /** @}
|
---|
| 1937 | */
|
---|