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