Changeset aadf01e in mainline for uspace/srv/net/tl/tcp/tcp.c


Ignore:
Timestamp:
2010-03-07T15:13:28Z (14 years ago)
Author:
Lukas Mejdrech <lukasmejdrech@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
936835e
Parents:
aa85487
Message:

Coding style (no functional change)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/tl/tcp/tcp.c

    raa85487 raadf01e  
    123123 *  @param[in] higher_equal The last value in the interval.
    124124 */
    125 #define IS_IN_INTERVAL_OVERFLOW( lower, value, higher_equal )   (((( lower ) < ( value )) && ((( value ) <= ( higher_equal )) || (( higher_equal ) < ( lower )))) || ((( value ) <= ( higher_equal )) && (( higher_equal ) < ( lower ))))
     125#define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal)     ((((lower) < (value)) && (((value) <= (higher_equal)) || ((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && ((higher_equal) < (lower))))
    126126
    127127/** Type definition of the TCP timeout.
     
    142142        /** TCP global data are going to be read only.
    143143         */
    144         int                                     globals_read_only;
     144        int globals_read_only;
    145145        /** Socket port.
    146146         */
    147         int                                     port;
     147        int port;
    148148        /** Local sockets.
    149149         */
    150         socket_cores_ref        local_sockets;
     150        socket_cores_ref local_sockets;
    151151        /** Socket identifier.
    152152         */
    153         int                                     socket_id;
     153        int socket_id;
    154154        /** Socket state.
    155155         */
    156         tcp_socket_state_t      state;
     156        tcp_socket_state_t state;
    157157        /** Sent packet sequence number.
    158158         */
    159         int                                     sequence_number;
     159        int sequence_number;
    160160        /** Timeout in microseconds.
    161161         */
    162         suseconds_t                     timeout;
     162        suseconds_t timeout;
    163163        /** Port map key.
    164164         */
    165         char *                          key;
     165        char * key;
    166166        /** Port map key length.
    167167         */
    168         size_t                          key_length;
     168        size_t key_length;
    169169};
    170170
     
    174174 *  @return The result parameter.
    175175 */
    176 int     tcp_release_and_return( packet_t packet, int result );
    177 
    178 void    tcp_prepare_operation_header( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize );
    179 int     tcp_prepare_timeout( int ( * timeout_function )( void * tcp_timeout_t ), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only );
    180 void    tcp_free_socket_data( socket_core_ref socket );
    181 int     tcp_timeout( void * data );
    182 int     tcp_release_after_timeout( void * data );
    183 int     tcp_process_packet( device_id_t device_id, packet_t packet, services_t error );
    184 int     tcp_connect_core( socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen );
    185 int     tcp_queue_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length );
    186 int     tcp_queue_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length );
    187 packet_t        tcp_get_packets_to_send( socket_core_ref socket, tcp_socket_data_ref socket_data );
    188 void    tcp_send_packets( device_id_t device_id, packet_t packet );
    189 void    tcp_process_acknowledgement( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header );
    190 packet_t        tcp_send_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number );
    191 packet_t        tcp_prepare_copy( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number );
    192 void    tcp_retransmit_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number );
    193 int     tcp_create_notification_packet( packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize );
    194 void    tcp_refresh_socket_data( tcp_socket_data_ref socket_data );
    195 void    tcp_initialize_socket_data( tcp_socket_data_ref socket_data );
    196 int     tcp_process_listen( socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen );
    197 int     tcp_process_syn_sent( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet );
    198 int     tcp_process_syn_received( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet );
    199 int     tcp_process_established( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length );
    200 int     tcp_queue_received_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length );
    201 
    202 int     tcp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error );
    203 int     tcp_process_client_messages( ipc_callid_t callid, ipc_call_t call );
    204 int     tcp_listen_message( socket_cores_ref local_sockets, int socket_id, int backlog );
    205 int     tcp_connect_message( socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen );
    206 int     tcp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen );
    207 int     tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags );
    208 int     tcp_accept_message( socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen );
    209 int tcp_close_message( socket_cores_ref local_sockets, int socket_id );
     176int tcp_release_and_return(packet_t packet, int result);
     177
     178void tcp_prepare_operation_header(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize);
     179int 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);
     180void tcp_free_socket_data(socket_core_ref socket);
     181int tcp_timeout(void * data);
     182int tcp_release_after_timeout(void * data);
     183int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error);
     184int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen);
     185int tcp_queue_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
     186int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
     187packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data);
     188void tcp_send_packets(device_id_t device_id, packet_t packet);
     189void tcp_process_acknowledgement(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header);
     190packet_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);
     191packet_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);
     192void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number);
     193int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize);
     194void tcp_refresh_socket_data(tcp_socket_data_ref socket_data);
     195void tcp_initialize_socket_data(tcp_socket_data_ref socket_data);
     196int 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);
     197int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
     198int tcp_process_syn_received(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
     199int 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);
     200int tcp_queue_received_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length);
     201
     202int tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error);
     203int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call);
     204int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog);
     205int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen);
     206int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen);
     207int tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags);
     208int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen);
     209int tcp_close_message(socket_cores_ref local_sockets, int socket_id);
    210210
    211211/** TCP global data.
     
    213213tcp_globals_t   tcp_globals;
    214214
    215 int tcp_initialize( async_client_conn_t client_connection ){
     215int tcp_initialize(async_client_conn_t client_connection){
    216216        ERROR_DECLARE;
    217217
    218         assert( client_connection );
    219         fibril_rwlock_initialize( & tcp_globals.lock );
    220         fibril_rwlock_write_lock( & tcp_globals.lock );
    221         tcp_globals.icmp_phone = icmp_connect_module( SERVICE_ICMP, ICMP_CONNECT_TIMEOUT );
    222         tcp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_TCP, SERVICE_TCP, client_connection, tcp_received_msg );
    223         if( tcp_globals.ip_phone < 0 ){
     218        assert(client_connection);
     219        fibril_rwlock_initialize(&tcp_globals.lock);
     220        fibril_rwlock_write_lock(&tcp_globals.lock);
     221        tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT);
     222        tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP, SERVICE_TCP, client_connection, tcp_received_msg);
     223        if(tcp_globals.ip_phone < 0){
    224224                return tcp_globals.ip_phone;
    225225        }
    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 );
     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);
    229229                return ERROR_CODE;
    230230        }
    231231        tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
    232         fibril_rwlock_write_unlock( & tcp_globals.lock );
     232        fibril_rwlock_write_unlock(&tcp_globals.lock);
    233233        return EOK;
    234234}
    235235
    236 int     tcp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){
     236int tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error){
    237237        ERROR_DECLARE;
    238238
    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 );
     239        if(receiver != SERVICE_TCP){
     240                return EREFUSED;
     241        }
     242        fibril_rwlock_write_lock(&tcp_globals.lock);
     243        if(ERROR_OCCURRED(tcp_process_packet(device_id, packet, error))){
     244                fibril_rwlock_write_unlock(&tcp_globals.lock);
     245        }
     246        printf("receive %d \n", ERROR_CODE);
    245247
    246248        return ERROR_CODE;
    247249}
    248250
    249 int tcp_process_packet( device_id_t device_id, packet_t packet, services_t error ){
     251int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error){
    250252        ERROR_DECLARE;
    251253
    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 ){
     254        size_t length;
     255        size_t offset;
     256        int result;
     257        tcp_header_ref header;
     258        socket_core_ref  socket;
     259        tcp_socket_data_ref socket_data;
     260        packet_t next_packet;
     261        size_t total_length;
     262        uint32_t checksum;
     263        int fragments;
     264        icmp_type_t type;
     265        icmp_code_t code;
     266        struct sockaddr * src;
     267        struct sockaddr * dest;
     268        size_t addrlen;
     269
     270        printf("p1 \n");
     271        if(error){
     272                switch(error){
    271273                        case SERVICE_ICMP:
    272274                                // 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 );
     275                                result = icmp_client_process_packet(packet, &type, &code, NULL, NULL);
     276                                if(result < 0){
     277                                        return tcp_release_and_return(packet, result);
     278                                }
     279                                length = (size_t) result;
     280                                if(ERROR_OCCURRED(packet_trim(packet, length, 0))){
     281                                        return tcp_release_and_return(packet, ERROR_CODE);
    280282                                }
    281283                                break;
    282284                        default:
    283                                 return tcp_release_and_return( packet, ENOTSUP );
     285                                return tcp_release_and_return(packet, ENOTSUP);
    284286                }
    285287        }
    286288
    287289        // TODO process received ipopts?
    288         result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL );
    289 //      printf("ip len %d\n", result );
    290         if( result < 0 ){
    291                 return tcp_release_and_return( packet, result );
    292         }
    293         offset = ( size_t ) result;
    294 
    295         length = packet_get_data_length( packet );
    296 //      printf("packet len %d\n", length );
    297         if( length <= 0 ){
    298                 return tcp_release_and_return( packet, EINVAL );
    299         }
    300         if( length < TCP_HEADER_SIZE + offset ){
    301                 return tcp_release_and_return( packet, NO_DATA );
     290        result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
     291//      printf("ip len %d\n", result);
     292        if(result < 0){
     293                return tcp_release_and_return(packet, result);
     294        }
     295        offset = (size_t) result;
     296
     297        length = packet_get_data_length(packet);
     298//      printf("packet len %d\n", length);
     299        if(length <= 0){
     300                return tcp_release_and_return(packet, EINVAL);
     301        }
     302        if(length < TCP_HEADER_SIZE + offset){
     303                return tcp_release_and_return(packet, NO_DATA);
    302304        }
    303305
    304306        // trim all but TCP header
    305         if( ERROR_OCCURRED( packet_trim( packet, offset, 0 ))){
    306                 return tcp_release_and_return( packet, ERROR_CODE );
     307        if(ERROR_OCCURRED(packet_trim(packet, offset, 0))){
     308                return tcp_release_and_return(packet, ERROR_CODE);
    307309        }
    308310
    309311        // 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 );
     312        header = (tcp_header_ref) packet_get_data(packet);
     313        if(! header){
     314                return tcp_release_and_return(packet, NO_DATA);
     315        }
     316//      printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header), ntohs(header->destination_port));
     317
     318        result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
     319        if(result <= 0){
     320                return tcp_release_and_return(packet, result);
     321        }
     322        addrlen = (size_t) result;
     323
     324        if(ERROR_OCCURRED(tl_set_address_port(src, addrlen, ntohs(header->source_port)))){
     325                return tcp_release_and_return(packet, ERROR_CODE);
    324326        }
    325327
    326328        // find the destination socket
    327         socket = socket_port_find( & tcp_globals.sockets, ntohs( header->destination_port ), ( const char * ) src, addrlen );
    328         if( ! socket ){
     329        socket = socket_port_find(&tcp_globals.sockets, ntohs(header->destination_port), (const char *) src, addrlen);
     330        if(! socket){
    329331//              printf("listening?\n");
    330332                // 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 );
     333                socket = socket_port_find(&tcp_globals.sockets, ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
     334                if(! socket){
     335                        if(tl_prepare_icmp_packet(tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error) == EOK){
     336                                icmp_destination_unreachable_msg(tcp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet);
    335337                        }
    336338                        return EADDRNOTAVAIL;
    337339                }
    338340        }
    339         printf("socket id %d\n", socket->socket_id );
    340         socket_data = ( tcp_socket_data_ref ) socket->specific_data;
    341         assert( socket_data );
     341        printf("socket id %d\n", socket->socket_id);
     342        socket_data = (tcp_socket_data_ref) socket->specific_data;
     343        assert(socket_data);
    342344
    343345        // some data received, clear the timeout counter
     
    351353        do{
    352354                ++ fragments;
    353                 length = packet_get_data_length( next_packet );
    354                 if( length <= 0 ){
    355                         return tcp_release_and_return( packet, NO_DATA );
     355                length = packet_get_data_length(next_packet);
     356                if(length <= 0){
     357                        return tcp_release_and_return(packet, NO_DATA);
    356358                }
    357359                total_length += length;
    358360                // 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 );
     361                if(! error){
     362                        checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet));
     363                }
     364        }while((next_packet = pq_next(next_packet)));
     365//      printf("fragments %d of %d bytes\n", fragments, total_length);
    364366
    365367//      printf("lock?\n");
    366         fibril_rwlock_write_lock( socket_data->local_lock );
     368        fibril_rwlock_write_lock(socket_data->local_lock);
    367369//      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 );
     370        if(! error){
     371                if(socket_data->state == TCP_SOCKET_LISTEN){
     372                        if(socket_data->pseudo_header){
     373                                free(socket_data->pseudo_header);
    372374                                socket_data->pseudo_header = NULL;
    373375                                socket_data->headerlen = 0;
    374376                        }
    375                         if( ERROR_OCCURRED( ip_client_get_pseudo_header( IPPROTO_TCP, src, addrlen, dest, addrlen, total_length, & socket_data->pseudo_header, & socket_data->headerlen ))){
    376                                 fibril_rwlock_write_unlock( socket_data->local_lock );
    377                                 return tcp_release_and_return( packet, ERROR_CODE );
    378                         }
    379                 }else if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( socket_data->pseudo_header, socket_data->headerlen, total_length ))){
    380                         fibril_rwlock_write_unlock( socket_data->local_lock );
    381                         return tcp_release_and_return( packet, ERROR_CODE );
    382                 }
    383                 checksum = compute_checksum( checksum, socket_data->pseudo_header, socket_data->headerlen );
    384                 if( flip_checksum( compact_checksum( checksum )) != IP_CHECKSUM_ZERO ){
    385                         printf( "checksum err %x -> %x\n", header->checksum, flip_checksum( compact_checksum( checksum )));
    386                         fibril_rwlock_write_unlock( socket_data->local_lock );
    387                         if( ! ERROR_OCCURRED( tl_prepare_icmp_packet( tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error ))){
     377                        if(ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen, dest, addrlen, total_length, &socket_data->pseudo_header, &socket_data->headerlen))){
     378                                fibril_rwlock_write_unlock(socket_data->local_lock);
     379                                return tcp_release_and_return(packet, ERROR_CODE);
     380                        }
     381                }else if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(socket_data->pseudo_header, socket_data->headerlen, total_length))){
     382                        fibril_rwlock_write_unlock(socket_data->local_lock);
     383                        return tcp_release_and_return(packet, ERROR_CODE);
     384                }
     385                checksum = compute_checksum(checksum, socket_data->pseudo_header, socket_data->headerlen);
     386                if(flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO){
     387                        printf("checksum err %x -> %x\n", header->checksum, flip_checksum(compact_checksum(checksum)));
     388                        fibril_rwlock_write_unlock(socket_data->local_lock);
     389                        if(! ERROR_OCCURRED(tl_prepare_icmp_packet(tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error))){
    388390                                // 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 );
     391                                icmp_parameter_problem_msg(tcp_globals.icmp_phone, ICMP_PARAM_POINTER, ((size_t) ((void *) &header->checksum)) - ((size_t) ((void *) header)), packet);
    390392                        }
    391393                        return EINVAL;
     
    393395        }
    394396
    395         fibril_rwlock_read_unlock( & tcp_globals.lock );
     397        fibril_rwlock_read_unlock(&tcp_globals.lock);
    396398
    397399        // TODO error reporting/handling
    398 //      printf( "st %d\n", socket_data->state );
    399         switch( socket_data->state ){
     400//      printf("st %d\n", socket_data->state);
     401        switch(socket_data->state){
    400402                case TCP_SOCKET_LISTEN:
    401                         ERROR_CODE = tcp_process_listen( socket, socket_data, header, packet, src, dest, addrlen );
     403                        ERROR_CODE = tcp_process_listen(socket, socket_data, header, packet, src, dest, addrlen);
    402404                        break;
    403405                case TCP_SOCKET_SYN_RECEIVED:
    404                         ERROR_CODE = tcp_process_syn_received( socket, socket_data, header, packet );
     406                        ERROR_CODE = tcp_process_syn_received(socket, socket_data, header, packet);
    405407                        break;
    406408                case TCP_SOCKET_SYN_SENT:
    407                         ERROR_CODE = tcp_process_syn_sent( socket, socket_data, header, packet );
     409                        ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header, packet);
    408410                        break;
    409411                case TCP_SOCKET_FIN_WAIT_1:
     
    416418                        // ack releasing the socket gets processed later
    417419                case TCP_SOCKET_ESTABLISHED:
    418                         ERROR_CODE = tcp_process_established( socket, socket_data, header, packet, fragments, total_length );
     420                        ERROR_CODE = tcp_process_established(socket, socket_data, header, packet, fragments, total_length);
    419421                        break;
    420422                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 );
     423                        pq_release(tcp_globals.net_phone, packet_get_id(packet));
     424        }
     425
     426        if(ERROR_CODE != EOK){
     427                printf("process %d\n", ERROR_CODE);
     428                fibril_rwlock_write_unlock(socket_data->local_lock);
    427429        }
    428430        return EOK;
    429431}
    430432
    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 ){
     433int 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){
    432434        ERROR_DECLARE;
    433435
    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 );
     436        packet_t next_packet;
     437        packet_t tmp_packet;
     438        uint32_t old_incoming;
     439        size_t order;
     440        uint32_t sequence_number;
     441        size_t length;
     442        size_t offset;
     443        uint32_t new_sequence_number;
     444
     445        assert(socket);
     446        assert(socket_data);
     447        assert(socket->specific_data == socket_data);
     448        assert(header);
     449        assert(packet);
     450
     451        new_sequence_number = ntohl(header->sequence_number);
    450452        old_incoming = socket_data->next_incoming;
    451453
    452         if( header->finalize ){
     454        if(header->finalize){
    453455                socket_data->fin_incoming = new_sequence_number;
    454456        }
    455457
    456 //      printf( "pe %d < %d <= %d\n", new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length );
     458//      printf("pe %d < %d <= %d\n", new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length);
    457459        // trim begining if containing expected data
    458         if( IS_IN_INTERVAL_OVERFLOW( new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length )){
     460        if(IS_IN_INTERVAL_OVERFLOW(new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length)){
    459461                // get the acknowledged offset
    460                 if( socket_data->next_incoming < new_sequence_number ){
     462                if(socket_data->next_incoming < new_sequence_number){
    461463                        offset = new_sequence_number - socket_data->next_incoming;
    462464                }else{
    463465                        offset = socket_data->next_incoming - new_sequence_number;
    464466                }
    465 //              printf( "offset %d\n", offset );
     467//              printf("offset %d\n", offset);
    466468                new_sequence_number += offset;
    467469                total_length -= offset;
    468                 length = packet_get_data_length( packet );
     470                length = packet_get_data_length(packet);
    469471                // trim the acknowledged data
    470                 while( length <= offset ){
     472                while(length <= offset){
    471473                        // release the acknowledged packets
    472                         next_packet = pq_next( packet );
    473                         pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     474                        next_packet = pq_next(packet);
     475                        pq_release(tcp_globals.net_phone, packet_get_id(packet));
    474476                        packet = next_packet;
    475477                        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 );
     478                        length = packet_get_data_length(packet);
     479                }
     480                if((offset > 0)
     481                        && (ERROR_OCCURRED(packet_trim(packet, offset, 0)))){
     482                        return tcp_release_and_return(packet, ERROR_CODE);
     483                }
     484                assert(new_sequence_number == socket_data->next_incoming);
    483485        }
    484486
    485487        // 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//      if(IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming + socket_data->window, new_sequence_number, new_sequence_number + total_length)){
     489//              return tcp_release_and_return(packet, EOVERFLOW);
    488490//      }
    489491
    490492/*
    491493        // 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 )){
     494        if(IS_IN_INTERVAL_OVERFLOW(new_sequence_number, socket_data->next_incoming + socket_data->window, new_sequence_number + total_length)){
    493495                // get the allowed data length
    494                 if( socket_data->next_incoming + socket_data->window < new_sequence_number ){
     496                if(socket_data->next_incoming + socket_data->window < new_sequence_number){
    495497                        offset = new_sequence_number - socket_data->next_incoming + socket_data->window;
    496498                }else{
     
    499501                next_packet = packet;
    500502                // 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 );
     503                while(next_packet && (offset > 0)){
     504                        length = packet_get_data_length(packet);
     505                        if(length <= offset){
     506                                next_packet = pq_next(next_packet);
     507                        }else if(ERROR_OCCURRED(packet_trim(next_packet, 0, length - offset))){
     508                                return tcp_release_and_return(packet, ERROR_CODE);
    507509                        }
    508510                        offset -= length;
     
    510512                }
    511513                // release the overflowing packets
    512                 next_packet = pq_next( next_packet );
    513                 if( next_packet ){
     514                next_packet = pq_next(next_packet);
     515                if(next_packet){
    514516                        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 );
     517                        next_packet = pq_next(next_packet);
     518                        pq_insert_after(tmp_packet, next_packet);
     519                        pq_release(tcp_globals.net_phone, packet_get_id(tmp_packet));
     520                }
     521                assert(new_sequence_number + total_length == socket_data->next_incoming + socket_data->window);
    520522        }
    521523*/
    522524        // the expected one arrived?
    523         if( new_sequence_number == socket_data->next_incoming ){
     525        if(new_sequence_number == socket_data->next_incoming){
    524526                printf("expected\n");
    525527                // process acknowledgement
    526                 tcp_process_acknowledgement( socket, socket_data, header );
     528                tcp_process_acknowledgement(socket, socket_data, header);
    527529
    528530                // 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 ));
     531                total_length -= TCP_HEADER_LENGTH(header);
     532                if(ERROR_OCCURRED(packet_trim(packet, TCP_HEADER_LENGTH(header), 0))){
     533                        return tcp_release_and_return(packet, ERROR_CODE);
     534                }
     535
     536                if(total_length){
     537                        ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, fragments, total_length));
    536538                }else{
    537539                        total_length = 1;
     
    539541                socket_data->next_incoming = old_incoming + total_length;
    540542                packet = socket_data->incoming;
    541                 while( packet ){
    542                         if( ERROR_OCCURRED( pq_get_order( socket_data->incoming, & order, NULL ))){
     543                while(packet){
     544                        if(ERROR_OCCURRED(pq_get_order(socket_data->incoming, &order, NULL))){
    543545                                // remove the corrupted packet
    544                                 next_packet = pq_detach( packet );
    545                                 if( packet == socket_data->incoming ){
     546                                next_packet = pq_detach(packet);
     547                                if(packet == socket_data->incoming){
    546548                                        socket_data->incoming = next_packet;
    547549                                }
    548                                 pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     550                                pq_release(tcp_globals.net_phone, packet_get_id(packet));
    549551                                packet = next_packet;
    550552                                continue;
    551553                        }
    552                         sequence_number = ( uint32_t ) order;
    553                         if( IS_IN_INTERVAL_OVERFLOW( sequence_number, old_incoming, socket_data->next_incoming )){
     554                        sequence_number = (uint32_t) order;
     555                        if(IS_IN_INTERVAL_OVERFLOW(sequence_number, old_incoming, socket_data->next_incoming)){
    554556                                // move to the next
    555                                 packet = pq_next( packet );
     557                                packet = pq_next(packet);
    556558                        // coninual data?
    557                         }else if( IS_IN_INTERVAL_OVERFLOW( old_incoming, sequence_number, socket_data->next_incoming )){
     559                        }else if(IS_IN_INTERVAL_OVERFLOW(old_incoming, sequence_number, socket_data->next_incoming)){
    558560                                // detach the packet
    559                                 next_packet = pq_detach( packet );
    560                                 if( packet == socket_data->incoming ){
     561                                next_packet = pq_detach(packet);
     562                                if(packet == socket_data->incoming){
    561563                                        socket_data->incoming = next_packet;
    562564                                }
    563565                                // get data length
    564                                 length = packet_get_data_length( packet );
     566                                length = packet_get_data_length(packet);
    565567                                new_sequence_number = sequence_number + length;
    566                                 if( length <= 0 ){
     568                                if(length <= 0){
    567569                                        // remove the empty packet
    568                                         pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     570                                        pq_release(tcp_globals.net_phone, packet_get_id(packet));
    569571                                        packet = next_packet;
    570572                                        continue;
    571573                                }
    572574                                // exactly following
    573                                 if( sequence_number == socket_data->next_incoming ){
     575                                if(sequence_number == socket_data->next_incoming){
    574576                                        // queue received data
    575                                         ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, 1, packet_get_data_length( packet )));
     577                                        ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, 1, packet_get_data_length(packet)));
    576578                                        socket_data->next_incoming = new_sequence_number;
    577579                                        packet = next_packet;
    578580                                        continue;
    579581                                // 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                                }else if(IS_IN_INTERVAL_OVERFLOW(sequence_number, socket_data->next_incoming, new_sequence_number)){
     583                                        if(socket_data->next_incoming < new_sequence_number){
    582584                                                length = new_sequence_number - socket_data->next_incoming;
    583585                                        }else{
    584586                                                length = socket_data->next_incoming - new_sequence_number;
    585587                                        }
    586                                         if( ! ERROR_OCCURRED( packet_trim( packet, length, 0 ))){
     588                                        if(! ERROR_OCCURRED(packet_trim(packet, length, 0))){
    587589                                                // queue received data
    588                                                 ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, 1, packet_get_data_length( packet )));
     590                                                ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, 1, packet_get_data_length(packet)));
    589591                                                socket_data->next_incoming = new_sequence_number;
    590592                                                packet = next_packet;
     
    593595                                }
    594596                                // remove the duplicit or corrupted packet
    595                                 pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     597                                pq_release(tcp_globals.net_phone, packet_get_id(packet));
    596598                                packet = next_packet;
    597599                                continue;
     
    600602                        }
    601603                }
    602         }else if( IS_IN_INTERVAL( socket_data->next_incoming, new_sequence_number, socket_data->next_incoming + socket_data->window )){
     604        }else if(IS_IN_INTERVAL(socket_data->next_incoming, new_sequence_number, socket_data->next_incoming + socket_data->window)){
    603605                printf("in window\n");
    604606                // process acknowledgement
    605                 tcp_process_acknowledgement( socket, socket_data, header );
     607                tcp_process_acknowledgement(socket, socket_data, header);
    606608
    607609                // remove the header
    608                 total_length -= TCP_HEADER_LENGTH( header );
    609                 if( ERROR_OCCURRED( packet_trim( packet, TCP_HEADER_LENGTH( header ), 0 ))){
    610                         return tcp_release_and_return( packet, ERROR_CODE );
    611                 }
    612 
    613                 next_packet = pq_detach( packet );
    614                 length = packet_get_data_length( packet );
    615                 if( ERROR_OCCURRED( pq_add( & socket_data->incoming, packet, new_sequence_number, length ))){
     610                total_length -= TCP_HEADER_LENGTH(header);
     611                if(ERROR_OCCURRED(packet_trim(packet, TCP_HEADER_LENGTH(header), 0))){
     612                        return tcp_release_and_return(packet, ERROR_CODE);
     613                }
     614
     615                next_packet = pq_detach(packet);
     616                length = packet_get_data_length(packet);
     617                if(ERROR_OCCURRED(pq_add(&socket_data->incoming, packet, new_sequence_number, length))){
    616618                        // remove the corrupted packets
    617                         pq_release( tcp_globals.net_phone, packet_get_id( packet ));
    618                         pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
     619                        pq_release(tcp_globals.net_phone, packet_get_id(packet));
     620                        pq_release(tcp_globals.net_phone, packet_get_id(next_packet));
    619621                }else{
    620                         while( next_packet ){
     622                        while(next_packet){
    621623                                new_sequence_number += length;
    622                                 tmp_packet = pq_detach( next_packet );
    623                                 length = packet_get_data_length( next_packet );
    624                                 if( ERROR_OCCURRED( pq_set_order( next_packet, new_sequence_number, length ))
    625                                 || ERROR_OCCURRED( pq_insert_after( packet, next_packet ))){
    626                                         pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
     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));
    627629                                }
    628630                                next_packet = tmp_packet;
     
    632634                printf("unexpected\n");
    633635                // release duplicite or restricted
    634                 pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     636                pq_release(tcp_globals.net_phone, packet_get_id(packet));
    635637        }
    636638
    637639        // change state according to the acknowledging incoming fin
    638         if( IS_IN_INTERVAL_OVERFLOW( old_incoming, socket_data->fin_incoming, socket_data->next_incoming )){
    639                 switch( socket_data->state ){
     640        if(IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming, socket_data->next_incoming)){
     641                switch(socket_data->state){
    640642                        case TCP_SOCKET_FIN_WAIT_1:
    641643                        case TCP_SOCKET_FIN_WAIT_2:
     
    650652        }
    651653
    652         packet = tcp_get_packets_to_send( socket, socket_data );
    653         if( ! packet ){
     654        packet = tcp_get_packets_to_send(socket, socket_data);
     655        if(! packet){
    654656                // create the notification packet
    655                 ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 0 ));
    656                 ERROR_PROPAGATE( tcp_queue_prepare_packet( socket, socket_data, packet, 1 ));
    657                 packet = tcp_send_prepare_packet( socket, socket_data, packet, 1, socket_data->last_outgoing + 1 );
    658         }
    659         fibril_rwlock_write_unlock( socket_data->local_lock );
     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);
    660662        // send the packet
    661         tcp_send_packets( socket_data->device_id, packet );
     663        tcp_send_packets(socket_data->device_id, packet);
    662664        return EOK;
    663665}
    664666
    665 int tcp_queue_received_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length ){
     667int tcp_queue_received_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length){
    666668        ERROR_DECLARE;
    667669
    668         packet_dimension_ref    packet_dimension;
    669 
    670         assert( socket );
    671         assert( socket_data );
    672         assert( socket->specific_data == socket_data );
    673         assert( packet );
    674         assert( fragments >= 1 );
    675         assert( socket_data->window > total_length );
     670        packet_dimension_ref packet_dimension;
     671
     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);
    676678
    677679        // queue the received packet
    678         if( ERROR_OCCURRED( dyn_fifo_push( & socket->received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))
    679         || ERROR_OCCURRED( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ))){
    680                 return tcp_release_and_return( packet, ERROR_CODE );
     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))){
     682                return tcp_release_and_return(packet, ERROR_CODE);
    681683        }
    682684
     
    685687
    686688        // notify the destination socket
    687         async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size ), 0, 0, ( ipcarg_t ) fragments );
     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);
    688690        return EOK;
    689691}
    690692
    691 int tcp_process_syn_sent( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ){
     693int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet){
    692694        ERROR_DECLARE;
    693695
    694         packet_t        next_packet;
    695 
    696         assert( socket );
    697         assert( socket_data );
    698         assert( socket->specific_data == socket_data );
    699         assert( header );
    700         assert( packet );
    701 
    702         if( header->synchronize ){
     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){
    703705                // process acknowledgement
    704                 tcp_process_acknowledgement( socket, socket_data, header );
    705 
    706                 socket_data->next_incoming = ntohl( header->sequence_number ) + 1;
     706                tcp_process_acknowledgement(socket, socket_data, header);
     707
     708                socket_data->next_incoming = ntohl(header->sequence_number) + 1;
    707709                // release additional packets
    708                 next_packet = pq_detach( packet );
    709                 if( next_packet ){
    710                         pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
     710                next_packet = pq_detach(packet);
     711                if(next_packet){
     712                        pq_release(tcp_globals.net_phone, packet_get_id(next_packet));
    711713                }
    712714                // trim if longer than the header
    713                 if(( packet_get_data_length( packet ) > sizeof( * header ))
    714                 && ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - sizeof( * header )))){
    715                         return tcp_release_and_return( packet, ERROR_CODE );
    716                 }
    717                 tcp_prepare_operation_header( socket, socket_data, header, 0, 0 );
    718                 fibril_mutex_lock( & socket_data->operation.mutex );
    719                 socket_data->operation.result = tcp_queue_packet( socket, socket_data, packet, 1 );
    720                 if( socket_data->operation.result == EOK ){
     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){
    721723                        socket_data->state = TCP_SOCKET_ESTABLISHED;
    722                         packet = tcp_get_packets_to_send( socket, socket_data );
    723                         if( packet ){
    724                                 fibril_rwlock_write_unlock( socket_data->local_lock );
     724                        packet = tcp_get_packets_to_send(socket, socket_data);
     725                        if(packet){
     726                                fibril_rwlock_write_unlock(socket_data->local_lock);
    725727                                // send the packet
    726                                 tcp_send_packets( socket_data->device_id, packet );
     728                                tcp_send_packets(socket_data->device_id, packet);
    727729                                // signal the result
    728                                 fibril_condvar_signal( & socket_data->operation.condvar );
    729                                 fibril_mutex_unlock( & socket_data->operation.mutex );
     730                                fibril_condvar_signal(&socket_data->operation.condvar);
     731                                fibril_mutex_unlock(&socket_data->operation.mutex);
    730732                                return EOK;
    731733                        }
    732734                }
    733                 fibril_mutex_unlock( & socket_data->operation.mutex );
    734         }
    735         return tcp_release_and_return( packet, EINVAL );
    736 }
    737 
    738 int tcp_process_listen( socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen ){
     735                fibril_mutex_unlock(&socket_data->operation.mutex);
     736        }
     737        return tcp_release_and_return(packet, EINVAL);
     738}
     739
     740int 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){
    739741        ERROR_DECLARE;
    740742
    741         packet_t                        next_packet;
    742         socket_core_ref         socket;
    743         tcp_socket_data_ref     socket_data;
    744         int                                     socket_id;
    745         int                                     listening_socket_id = listening_socket->socket_id;
    746         int                                     listening_port = listening_socket->port;
    747 
    748         assert( listening_socket );
    749         assert( listening_socket_data );
    750         assert( listening_socket->specific_data == listening_socket_data );
    751         assert( header );
    752         assert( packet );
    753 
    754 //      printf( "syn %d\n", header->synchronize );
    755         if( header->synchronize ){
    756                 socket_data = ( tcp_socket_data_ref ) malloc( sizeof( * socket_data ));
    757                 if( ! socket_data ){
    758                         return tcp_release_and_return( packet, ENOMEM );
     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);
    759761                }else{
    760                         tcp_initialize_socket_data( socket_data );
     762                        tcp_initialize_socket_data(socket_data);
    761763                        socket_data->local_lock = listening_socket_data->local_lock;
    762764                        socket_data->local_sockets = listening_socket_data->local_sockets;
    763765                        socket_data->listening_socket_id = listening_socket->socket_id;
    764766
    765                         socket_data->next_incoming = ntohl( header->sequence_number );
    766                         socket_data->treshold = socket_data->next_incoming + ntohs( header->window );
     767                        socket_data->next_incoming = ntohl(header->sequence_number);
     768                        socket_data->treshold = socket_data->next_incoming + ntohs(header->window);
    767769
    768770                        socket_data->addrlen = addrlen;
    769                         socket_data->addr = malloc( socket_data->addrlen );
    770                         if( ! socket_data->addr ){
    771                                 free( socket_data );
    772                                 return tcp_release_and_return( packet, ENOMEM );
    773                         }
    774                         memcpy( socket_data->addr, src, socket_data->addrlen );
    775 
    776                         socket_data->dest_port = ntohs( header->source_port );
    777                         if( ERROR_OCCURRED( tl_set_address_port( socket_data->addr, socket_data->addrlen, socket_data->dest_port ))){
    778                                 free( socket_data->addr );
    779                                 free( socket_data );
    780                                 pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     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));
    781783                                return ERROR_CODE;
    782784                        }
    783785
    784 //                      printf( "addr %p\n", socket_data->addr, socket_data->addrlen );
     786//                      printf("addr %p\n", socket_data->addr, socket_data->addrlen);
    785787                        // create a socket
    786788                        socket_id = -1;
    787                         if( ERROR_OCCURRED( socket_create( socket_data->local_sockets, listening_socket->phone, socket_data, & socket_id ))){
    788                                 free( socket_data->addr );
    789                                 free( socket_data );
    790                                 return tcp_release_and_return( packet, ERROR_CODE );
     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);
    791793                        }
    792794
     
    797799                        listening_socket_data->headerlen = 0;
    798800
    799                         fibril_rwlock_write_unlock( socket_data->local_lock );
     801                        fibril_rwlock_write_unlock(socket_data->local_lock);
    800802//                      printf("list lg\n");
    801                         fibril_rwlock_write_lock( & tcp_globals.lock );
     803                        fibril_rwlock_write_lock(&tcp_globals.lock);
    802804//                      printf("list locked\n");
    803805                        // find the destination socket
    804                         listening_socket = socket_port_find( & tcp_globals.sockets, listening_port, SOCKET_MAP_KEY_LISTENING, 0 );
    805                         if(( ! listening_socket ) || ( listening_socket->socket_id != listening_socket_id )){
    806                                 fibril_rwlock_write_unlock( & tcp_globals.lock );
     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);
    807809                                // a shadow may remain until app hangs up
    808                                 return tcp_release_and_return( packet, EOK/*ENOTSOCK*/ );
    809                         }
    810 //                      printf("port %d\n", listening_socket->port );
    811                         listening_socket_data = ( tcp_socket_data_ref ) listening_socket->specific_data;
    812                         assert( listening_socket_data );
     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);
    813815
    814816//                      printf("list ll\n");
    815                         fibril_rwlock_write_lock( listening_socket_data->local_lock );
     817                        fibril_rwlock_write_lock(listening_socket_data->local_lock);
    816818//                      printf("list locked\n");
    817819
    818                         socket = socket_cores_find( listening_socket_data->local_sockets, socket_id );
    819                         if( ! socket ){
     820                        socket = socket_cores_find(listening_socket_data->local_sockets, socket_id);
     821                        if(! socket){
    820822                                // where is the socket?!?
    821                                 fibril_rwlock_write_unlock( & tcp_globals.lock );
     823                                fibril_rwlock_write_unlock(&tcp_globals.lock);
    822824                                return ENOTSOCK;
    823825                        }
    824                         socket_data = ( tcp_socket_data_ref ) socket->specific_data;
    825                         assert( socket_data );
     826                        socket_data = (tcp_socket_data_ref) socket->specific_data;
     827                        assert(socket_data);
    826828
    827829//                      uint8_t * data = socket_data->addr;
    828 //                      printf( "addr %d of %x %x %x %x-%x %x %x %x-%x %x %x %x-%x %x %x %x\n", socket_data->addrlen, data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ] );
    829 
    830                         ERROR_CODE = socket_port_add( & tcp_globals.sockets, listening_port, socket, ( const char * ) socket_data->addr, socket_data->addrlen );
    831                         assert( socket == socket_port_find( & tcp_globals.sockets, listening_port, ( const char * ) socket_data->addr, socket_data->addrlen ));
    832                         //ERROR_CODE = socket_bind_free_port( & tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port );
     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);
    833835                        //tcp_globals.last_used_port = socket->port;
    834 //                      printf("bound %d\n", socket->port );
    835                         fibril_rwlock_write_unlock( & tcp_globals.lock );
    836                         if( ERROR_CODE != EOK ){
    837                                 socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
    838                                 return tcp_release_and_return( packet, ERROR_CODE );
     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);
    839841                        }
    840842
    841843                        socket_data->state = TCP_SOCKET_LISTEN;
    842                         socket_data->next_incoming = ntohl( header->sequence_number ) + 1;
     844                        socket_data->next_incoming = ntohl(header->sequence_number) + 1;
    843845                        // release additional packets
    844                         next_packet = pq_detach( packet );
    845                         if( next_packet ){
    846                                 pq_release( tcp_globals.net_phone, packet_get_id( next_packet ));
     846                        next_packet = pq_detach(packet);
     847                        if(next_packet){
     848                                pq_release(tcp_globals.net_phone, packet_get_id(next_packet));
    847849                        }
    848850                        // trim if longer than the header
    849                         if(( packet_get_data_length( packet ) > sizeof( * header ))
    850                         && ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - sizeof( * header )))){
    851                                 socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
    852                                 return tcp_release_and_return( packet, ERROR_CODE );
    853                         }
    854                         tcp_prepare_operation_header( socket, socket_data, header, 1, 0 );
    855                         if( ERROR_OCCURRED( tcp_queue_packet( socket, socket_data, packet, 1 ))){
    856                                 socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
     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);
    857859                                return ERROR_CODE;
    858860                        }
    859                         packet = tcp_get_packets_to_send( socket, socket_data );
    860 //                      printf("send %d\n", packet_get_id( packet ));
    861                         if( ! packet ){
    862                                 socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
     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);
    863865                                return EINVAL;
    864866                        }else{
    865867                                socket_data->state = TCP_SOCKET_SYN_RECEIVED;
    866868//                              printf("unlock\n");
    867                                 fibril_rwlock_write_unlock( socket_data->local_lock );
     869                                fibril_rwlock_write_unlock(socket_data->local_lock);
    868870                                // send the packet
    869                                 tcp_send_packets( socket_data->device_id, packet );
     871                                tcp_send_packets(socket_data->device_id, packet);
    870872                                return EOK;
    871873                        }
    872874                }
    873875        }
    874         return tcp_release_and_return( packet, EINVAL );
    875 }
    876 
    877 int     tcp_process_syn_received( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ){
     876        return tcp_release_and_return(packet, EINVAL);
     877}
     878
     879int tcp_process_syn_received(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet){
    878880        ERROR_DECLARE;
    879881
    880         socket_core_ref         listening_socket;
    881         tcp_socket_data_ref     listening_socket_data;
    882 
    883         assert( socket );
    884         assert( socket_data );
    885         assert( socket->specific_data == socket_data );
    886         assert( header );
    887         assert( packet );
     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);
    888890
    889891        printf("syn_rec\n");
    890         if( header->acknowledge ){
     892        if(header->acknowledge){
    891893                // process acknowledgement
    892                 tcp_process_acknowledgement( socket, socket_data, header );
    893 
    894                 socket_data->next_incoming = ntohl( header->sequence_number );// + 1;
    895                 pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     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));
    896898                socket_data->state = TCP_SOCKET_ESTABLISHED;
    897                 listening_socket = socket_cores_find( socket_data->local_sockets, socket_data->listening_socket_id );
    898                 if( listening_socket ){
    899                         listening_socket_data = ( tcp_socket_data_ref ) listening_socket->specific_data;
    900                         assert( listening_socket_data );
     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);
    901903
    902904                        // queue the received packet
    903                         if( ! ERROR_OCCURRED( dyn_fifo_push( & listening_socket->accepted, ( -1 * socket->socket_id ), listening_socket_data->backlog ))){
     905                        if(! ERROR_OCCURRED(dyn_fifo_push(&listening_socket->accepted, (-1 * socket->socket_id), listening_socket_data->backlog))){
    904906                                // notify the destination socket
    905                                 async_msg_5( socket->phone, NET_SOCKET_ACCEPTED, ( ipcarg_t ) listening_socket->socket_id, socket_data->data_fragment_size, TCP_HEADER_SIZE, 0, ( ipcarg_t ) socket->socket_id );
    906                                 fibril_rwlock_write_unlock( socket_data->local_lock );
     907                                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);
     908                                fibril_rwlock_write_unlock(socket_data->local_lock);
    907909                                return EOK;
    908910                        }
     
    912914
    913915                // create the notification packet
    914                 ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 1 ));
     916                ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 1));
    915917
    916918                // send the packet
    917                 ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 1 ));
     919                ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
    918920
    919921                // flush packets
    920                 packet = tcp_get_packets_to_send( socket, socket_data );
    921                 fibril_rwlock_write_unlock( socket_data->local_lock );
    922                 if( packet ){
     922                packet = tcp_get_packets_to_send(socket, socket_data);
     923                fibril_rwlock_write_unlock(socket_data->local_lock);
     924                if(packet){
    923925                        // send the packet
    924                         tcp_send_packets( socket_data->device_id, packet );
     926                        tcp_send_packets(socket_data->device_id, packet);
    925927                }
    926928                return EOK;
    927929        }else{
    928                 return tcp_release_and_return( packet, EINVAL );
     930                return tcp_release_and_return(packet, EINVAL);
    929931        }
    930932        return EINVAL;
    931933}
    932934
    933 void tcp_process_acknowledgement( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header ){
    934         size_t          number;
    935         size_t          length;
    936         packet_t        packet;
    937         packet_t        next;
    938         packet_t        acknowledged = NULL;
    939         uint32_t        old;
    940 
    941         assert( socket );
    942         assert( socket_data );
    943         assert( socket->specific_data == socket_data );
    944         assert( header );
    945 
    946         if( header->acknowledge ){
    947                 number = ntohl( header->acknowledgement_number );
     935void 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        uint32_t old;
     942
     943        assert(socket);
     944        assert(socket_data);
     945        assert(socket->specific_data == socket_data);
     946        assert(header);
     947
     948        if(header->acknowledge){
     949                number = ntohl(header->acknowledgement_number);
    948950                // if more data acknowledged
    949                 if( number != socket_data->expected ){
     951                if(number != socket_data->expected){
    950952                        old = socket_data->expected;
    951                         if( IS_IN_INTERVAL_OVERFLOW( old, socket_data->fin_outgoing, number )){
    952                                 switch( socket_data->state ){
     953                        if(IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing, number)){
     954                                switch(socket_data->state){
    953955                                        case TCP_SOCKET_FIN_WAIT_1:
    954956                                                socket_data->state = TCP_SOCKET_FIN_WAIT_2;
     
    957959                                        case TCP_SOCKET_CLOSING:
    958960                                                // fin acknowledged - release the socket in another fibril
    959                                                 tcp_prepare_timeout( tcp_release_after_timeout, socket, socket_data, 0, TCP_SOCKET_TIME_WAIT, NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true );
     961                                                tcp_prepare_timeout(tcp_release_after_timeout, socket, socket_data, 0, TCP_SOCKET_TIME_WAIT, NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true);
    960962                                                break;
    961963                                        default:
     
    964966                        }
    965967                        // update the treshold if higher than set
    966                         if( number + ntohs( header->window ) > socket_data->expected + socket_data->treshold ){
    967                                 socket_data->treshold = number + ntohs( header->window ) - socket_data->expected;
     968                        if(number + ntohs(header->window) > socket_data->expected + socket_data->treshold){
     969                                socket_data->treshold = number + ntohs(header->window) - socket_data->expected;
    968970                        }
    969971                        // set new expected sequence number
     
    971973                        socket_data->expected_count = 1;
    972974                        packet = socket_data->outgoing;
    973                         while( pq_get_order( packet, & number, & length ) == EOK ){
    974                                 if( IS_IN_INTERVAL_OVERFLOW(( uint32_t ) old, ( uint32_t )( number + length ), ( uint32_t ) socket_data->expected )){
    975                                         next = pq_detach( packet );
    976                                         if( packet == socket_data->outgoing ){
     975                        while(pq_get_order(packet, &number, &length) == EOK){
     976                                if(IS_IN_INTERVAL_OVERFLOW((uint32_t) old, (uint32_t)(number + length), (uint32_t) socket_data->expected)){
     977                                        next = pq_detach(packet);
     978                                        if(packet == socket_data->outgoing){
    977979                                                socket_data->outgoing = next;
    978980                                        }
    979981                                        // add to acknowledged or release
    980                                         if( pq_add( & acknowledged, packet, 0, 0 ) != EOK ){
    981                                                 pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     982                                        if(pq_add(&acknowledged, packet, 0, 0) != EOK){
     983                                                pq_release(tcp_globals.net_phone, packet_get_id(packet));
    982984                                        }
    983985                                        packet = next;
    984                                 }else if( old < socket_data->expected ){
     986                                }else if(old < socket_data->expected){
    985987                                        break;
    986988                                }
    987989                        }
    988990                        // release acknowledged
    989                         if( acknowledged ){
    990                                 pq_release( tcp_globals.net_phone, packet_get_id( acknowledged ));
     991                        if(acknowledged){
     992                                pq_release(tcp_globals.net_phone, packet_get_id(acknowledged));
    991993                        }
    992994                        return;
    993995                // if the same as the previous time
    994                 }else if( number == socket_data->expected ){
     996                }else if(number == socket_data->expected){
    995997                        // increase the counter
    996998                        ++ socket_data->expected_count;
    997                         if( socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT ){
     999                        if(socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT){
    9981000                                socket_data->expected_count = 1;
    9991001                                // TODO retransmit lock
    1000                                 //tcp_retransmit_packet( socket, socket_data, number );
    1001                         }
    1002                 }
    1003         }
    1004 }
    1005 
    1006 int tcp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
     1002                                //tcp_retransmit_packet(socket, socket_data, number);
     1003                        }
     1004                }
     1005        }
     1006}
     1007
     1008int tcp_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
    10071009        ERROR_DECLARE;
    10081010
    1009         packet_t        packet;
    1010 
    1011         assert( call );
    1012         assert( answer );
    1013         assert( answer_count );
    1014 
    1015         * answer_count = 0;
    1016         switch( IPC_GET_METHOD( * call )){
     1011        packet_t packet;
     1012
     1013        assert(call);
     1014        assert(answer);
     1015        assert(answer_count);
     1016
     1017        *answer_count = 0;
     1018        switch(IPC_GET_METHOD(*call)){
    10171019                case NET_TL_RECEIVED:
    1018                         //fibril_rwlock_read_lock( & tcp_globals.lock );
    1019                         if( ! ERROR_OCCURRED( packet_translate( tcp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
    1020                                 ERROR_CODE = tcp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_TCP, IPC_GET_ERROR( call ));
    1021                         }
    1022                         //fibril_rwlock_read_unlock( & tcp_globals.lock );
     1020                        //fibril_rwlock_read_lock(&tcp_globals.lock);
     1021                        if(! ERROR_OCCURRED(packet_translate(tcp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
     1022                                ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP, IPC_GET_ERROR(call));
     1023                        }
     1024                        //fibril_rwlock_read_unlock(&tcp_globals.lock);
    10231025                        return ERROR_CODE;
    10241026                case IPC_M_CONNECT_TO_ME:
    1025                         return tcp_process_client_messages( callid, * call );
     1027                        return tcp_process_client_messages(callid, * call);
    10261028        }
    10271029        return ENOTSUP;
    10281030}
    10291031
    1030 void tcp_refresh_socket_data( tcp_socket_data_ref socket_data ){
    1031         assert( socket_data );
    1032 
    1033         bzero( socket_data, sizeof( * socket_data ));
     1032void tcp_refresh_socket_data(tcp_socket_data_ref socket_data){
     1033        assert(socket_data);
     1034
     1035        bzero(socket_data, sizeof(*socket_data));
    10341036        socket_data->state = TCP_SOCKET_INITIAL;
    10351037        socket_data->device_id = DEVICE_INVALID_ID;
     
    10431045}
    10441046
    1045 void tcp_initialize_socket_data( tcp_socket_data_ref socket_data ){
    1046         assert( socket_data );
    1047 
    1048         tcp_refresh_socket_data( socket_data );
    1049         fibril_mutex_initialize( & socket_data->operation.mutex );
    1050         fibril_condvar_initialize( & socket_data->operation.condvar );
     1047void tcp_initialize_socket_data(tcp_socket_data_ref socket_data){
     1048        assert(socket_data);
     1049
     1050        tcp_refresh_socket_data(socket_data);
     1051        fibril_mutex_initialize(&socket_data->operation.mutex);
     1052        fibril_condvar_initialize(&socket_data->operation.condvar);
    10511053        socket_data->data_fragment_size = MAX_TCP_FRAGMENT_SIZE;
    10521054}
    10531055
    1054 int tcp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){
    1055         int                                             res;
    1056         bool                                    keep_on_going = true;
    1057         socket_cores_t                  local_sockets;
    1058         int                                             app_phone = IPC_GET_PHONE( & call );
    1059         struct sockaddr *               addr;
    1060         size_t                                  addrlen;
    1061         fibril_rwlock_t                 lock;
    1062         ipc_call_t                              answer;
    1063         int                                             answer_count;
    1064         tcp_socket_data_ref             socket_data;
    1065         socket_core_ref                 socket;
    1066         packet_dimension_ref    packet_dimension;
     1056int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call){
     1057        int res;
     1058        bool keep_on_going = true;
     1059        socket_cores_t local_sockets;
     1060        int app_phone = IPC_GET_PHONE(&call);
     1061        struct sockaddr * addr;
     1062        size_t addrlen;
     1063        fibril_rwlock_t lock;
     1064        ipc_call_t answer;
     1065        int answer_count;
     1066        tcp_socket_data_ref socket_data;
     1067        socket_core_ref socket;
     1068        packet_dimension_ref packet_dimension;
    10671069
    10681070        /*
     
    10731075        answer_count = 0;
    10741076
    1075         socket_cores_initialize( & local_sockets );
    1076         fibril_rwlock_initialize( & lock );
    1077 
    1078         while( keep_on_going ){
     1077        socket_cores_initialize(&local_sockets);
     1078        fibril_rwlock_initialize(&lock);
     1079
     1080        while(keep_on_going){
    10791081
    10801082                // answer the call
    1081                 answer_call( callid, res, & answer, answer_count );
     1083                answer_call(callid, res, &answer, answer_count);
    10821084
    10831085                // refresh data
    1084                 refresh_answer( & answer, & answer_count );
     1086                refresh_answer(&answer, &answer_count);
    10851087
    10861088                // get the next call
    1087                 callid = async_get_call( & call );
     1089                callid = async_get_call(&call);
    10881090
    10891091                // process the call
    1090                 switch( IPC_GET_METHOD( call )){
     1092                switch(IPC_GET_METHOD(call)){
    10911093                        case IPC_M_PHONE_HUNGUP:
    10921094                                keep_on_going = false;
     
    10941096                                break;
    10951097                        case NET_SOCKET:
    1096                                 socket_data = ( tcp_socket_data_ref ) malloc( sizeof( * socket_data ));
    1097                                 if( ! socket_data ){
     1098                                socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
     1099                                if(! socket_data){
    10981100                                        res = ENOMEM;
    10991101                                }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 );
    1104                                         * SOCKET_SET_SOCKET_ID( answer ) = SOCKET_GET_SOCKET_ID( call );
    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 ){
    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 );
     1102                                        tcp_initialize_socket_data(socket_data);
     1103                                        socket_data->local_lock = &lock;
     1104                                        socket_data->local_sockets = &local_sockets;
     1105                                        fibril_rwlock_write_lock(&lock);
     1106                                        *SOCKET_SET_SOCKET_ID(answer) = SOCKET_GET_SOCKET_ID(call);
     1107                                        res = socket_create(&local_sockets, app_phone, socket_data, SOCKET_SET_SOCKET_ID(answer));
     1108                                        fibril_rwlock_write_unlock(&lock);
     1109                                        if(res == EOK){
     1110                                                if(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, DEVICE_INVALID_ID, &packet_dimension) == EOK){
     1111                                                        *SOCKET_SET_DATA_FRAGMENT_SIZE(answer) = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size);
    11101112                                                }
    1111 //                                              * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_TCP_FRAGMENT_SIZE;
    1112                                                 * SOCKET_SET_HEADER_SIZE( answer ) = TCP_HEADER_SIZE;
     1113//                                              *SOCKET_SET_DATA_FRAGMENT_SIZE(answer) = MAX_TCP_FRAGMENT_SIZE;
     1114                                                *SOCKET_SET_HEADER_SIZE(answer) = TCP_HEADER_SIZE;
    11131115                                                answer_count = 3;
    11141116                                        }else{
    1115                                                 free( socket_data );
     1117                                                free(socket_data);
    11161118                                        }
    11171119                                }
    11181120                                break;
    11191121                        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 );
     1122                                res = data_receive((void **) &addr, &addrlen);
     1123                                if(res == EOK){
     1124                                        fibril_rwlock_write_lock(&tcp_globals.lock);
     1125                                        fibril_rwlock_write_lock(&lock);
     1126                                        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);
     1127                                        if(res == EOK){
     1128                                                socket = socket_cores_find(&local_sockets, SOCKET_GET_SOCKET_ID(call));
     1129                                                if(socket){
     1130                                                        socket_data = (tcp_socket_data_ref) socket->specific_data;
     1131                                                        assert(socket_data);
    11301132                                                        socket_data->state = TCP_SOCKET_LISTEN;
    11311133                                                }
    11321134                                        }
    1133                                         fibril_rwlock_write_unlock( & lock );
    1134                                         fibril_rwlock_write_unlock( & tcp_globals.lock );
    1135                                         free( addr );
     1135                                        fibril_rwlock_write_unlock(&lock);
     1136                                        fibril_rwlock_write_unlock(&tcp_globals.lock);
     1137                                        free(addr);
    11361138                                }
    11371139                                break;
    11381140                        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 );
     1141                                fibril_rwlock_read_lock(&tcp_globals.lock);
     1142//                              fibril_rwlock_write_lock(&tcp_globals.lock);
     1143                                fibril_rwlock_write_lock(&lock);
     1144                                res = tcp_listen_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_BACKLOG(call));
     1145                                fibril_rwlock_write_unlock(&lock);
     1146//                              fibril_rwlock_write_unlock(&tcp_globals.lock);
     1147                                fibril_rwlock_read_unlock(&tcp_globals.lock);
    11461148                                break;
    11471149                        case NET_SOCKET_CONNECT:
    1148                                 res = data_receive(( void ** ) & addr, & addrlen );
    1149                                 if( res == EOK ){
     1150                                res = data_receive((void **) &addr, &addrlen);
     1151                                if(res == EOK){
    11501152                                        // the global lock may be released in the tcp_connect_message() function
    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 );
     1153                                        fibril_rwlock_write_lock(&tcp_globals.lock);
     1154                                        fibril_rwlock_write_lock(&lock);
     1155                                        res = tcp_connect_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen);
     1156                                        if(res != EOK){
     1157                                                fibril_rwlock_write_unlock(&lock);
     1158                                                fibril_rwlock_write_unlock(&tcp_globals.lock);
     1159                                                free(addr);
    11581160                                        }
    11591161                                }
    11601162                                break;
    11611163                        case NET_SOCKET_ACCEPT:
    1162                                 fibril_rwlock_read_lock( & tcp_globals.lock );
    1163                                 fibril_rwlock_write_lock( & lock );
    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 );
    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;
     1164                                fibril_rwlock_read_lock(&tcp_globals.lock);
     1165                                fibril_rwlock_write_lock(&lock);
     1166                                res = tcp_accept_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_NEW_SOCKET_ID(call), SOCKET_SET_DATA_FRAGMENT_SIZE(answer), &addrlen);
     1167                                fibril_rwlock_write_unlock(&lock);
     1168                                fibril_rwlock_read_unlock(&tcp_globals.lock);
     1169                                if(res > 0){
     1170                                        *SOCKET_SET_SOCKET_ID(answer) = res;
     1171                                        *SOCKET_SET_ADDRESS_LENGTH(answer) = addrlen;
    11701172                                        answer_count = 3;
    11711173                                }
    11721174                                break;
    11731175                        case NET_SOCKET_SEND:
    1174                                 fibril_rwlock_read_lock( & tcp_globals.lock );
    1175                                 fibril_rwlock_write_lock( & lock );
    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 ));
    1177                                 if( res != EOK ){
    1178                                         fibril_rwlock_write_unlock( & lock );
    1179                                         fibril_rwlock_read_unlock( & tcp_globals.lock );
     1176                                fibril_rwlock_read_lock(&tcp_globals.lock);
     1177                                fibril_rwlock_write_lock(&lock);
     1178                                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));
     1179                                if(res != EOK){
     1180                                        fibril_rwlock_write_unlock(&lock);
     1181                                        fibril_rwlock_read_unlock(&tcp_globals.lock);
    11801182                                }else{
    11811183                                        answer_count = 2;
     
    11831185                                break;
    11841186                        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 );
    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 ));
    1190                                         if( res != EOK ){
    1191                                                 fibril_rwlock_write_unlock( & lock );
    1192                                                 fibril_rwlock_read_unlock( & tcp_globals.lock );
     1187                                res = data_receive((void **) &addr, &addrlen);
     1188                                if(res == EOK){
     1189                                        fibril_rwlock_read_lock(&tcp_globals.lock);
     1190                                        fibril_rwlock_write_lock(&lock);
     1191                                        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));
     1192                                        if(res != EOK){
     1193                                                fibril_rwlock_write_unlock(&lock);
     1194                                                fibril_rwlock_read_unlock(&tcp_globals.lock);
    11931195                                        }else{
    11941196                                                answer_count = 2;
    11951197                                        }
    1196                                         free( addr );
     1198                                        free(addr);
    11971199                                }
    11981200                                break;
    11991201                        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;
     1202                                fibril_rwlock_read_lock(&tcp_globals.lock);
     1203                                fibril_rwlock_write_lock(&lock);
     1204                                res = tcp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), NULL);
     1205                                fibril_rwlock_write_unlock(&lock);
     1206                                fibril_rwlock_read_unlock(&tcp_globals.lock);
     1207                                if(res > 0){
     1208                                        *SOCKET_SET_READ_DATA_LENGTH(answer) = res;
    12071209                                        answer_count = 1;
    12081210                                        res = EOK;
     
    12101212                                break;
    12111213                        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;
     1214                                fibril_rwlock_read_lock(&tcp_globals.lock);
     1215                                fibril_rwlock_write_lock(&lock);
     1216                                res = tcp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), &addrlen);
     1217                                fibril_rwlock_write_unlock(&lock);
     1218                                fibril_rwlock_read_unlock(&tcp_globals.lock);
     1219                                if(res > 0){
     1220                                        *SOCKET_SET_READ_DATA_LENGTH(answer) = res;
     1221                                        *SOCKET_SET_ADDRESS_LENGTH(answer) = addrlen;
    12201222                                        answer_count = 3;
    12211223                                        res = EOK;
     
    12231225                                break;
    12241226                        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 );
     1227                                fibril_rwlock_write_lock(&tcp_globals.lock);
     1228                                fibril_rwlock_write_lock(&lock);
     1229                                res = tcp_close_message(&local_sockets, SOCKET_GET_SOCKET_ID(call));
     1230                                if(res != EOK){
     1231                                        fibril_rwlock_write_unlock(&lock);
     1232                                        fibril_rwlock_write_unlock(&tcp_globals.lock);
    12311233                                }
    12321234                                break;
     
    12411243        printf("release\n");
    12421244        // release all local sockets
    1243         socket_cores_release( tcp_globals.net_phone, & local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
     1245        socket_cores_release(tcp_globals.net_phone, &local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
    12441246
    12451247        return EOK;
    12461248}
    12471249
    1248 int tcp_timeout( void * data ){
    1249         tcp_timeout_ref         timeout = data;
    1250         int                                     keep_write_lock = false;
    1251         socket_core_ref         socket;
    1252         tcp_socket_data_ref     socket_data;
    1253 
    1254         assert( timeout );
     1250int tcp_timeout(void * data){
     1251        tcp_timeout_ref timeout = data;
     1252        int keep_write_lock = false;
     1253        socket_core_ref socket;
     1254        tcp_socket_data_ref socket_data;
     1255
     1256        assert(timeout);
    12551257
    12561258        // sleep the given timeout
    1257         async_usleep( timeout->timeout );
     1259        async_usleep(timeout->timeout);
    12581260        // lock the globals
    1259         if( timeout->globals_read_only ){
    1260                 fibril_rwlock_read_lock( & tcp_globals.lock );
     1261        if(timeout->globals_read_only){
     1262                fibril_rwlock_read_lock(&tcp_globals.lock);
    12611263        }else{
    1262                 fibril_rwlock_write_lock( & tcp_globals.lock );
     1264                fibril_rwlock_write_lock(&tcp_globals.lock);
    12631265        }
    12641266        // find the pending operation socket
    1265         socket = socket_port_find( & tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length );
    1266         if( socket && ( socket->socket_id == timeout->socket_id )){
    1267                 socket_data = ( tcp_socket_data_ref ) socket->specific_data;
    1268                 assert( socket_data );
    1269                 if( socket_data->local_sockets == timeout->local_sockets ){
    1270                         fibril_rwlock_write_lock( socket_data->local_lock );
    1271                         if( timeout->sequence_number ){
     1267        socket = socket_port_find(&tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length);
     1268        if(socket && (socket->socket_id == timeout->socket_id)){
     1269                socket_data = (tcp_socket_data_ref) socket->specific_data;
     1270                assert(socket_data);
     1271                if(socket_data->local_sockets == timeout->local_sockets){
     1272                        fibril_rwlock_write_lock(socket_data->local_lock);
     1273                        if(timeout->sequence_number){
    12721274                                // increase the timeout counter;
    12731275                                ++ socket_data->timeout_count;
    1274                                 if( socket_data->timeout_count == TCP_MAX_TIMEOUTS ){
     1276                                if(socket_data->timeout_count == TCP_MAX_TIMEOUTS){
    12751277                                        // TODO release as connection lost
    1276                                         //tcp_refresh_socket_data( socket_data );
     1278                                        //tcp_refresh_socket_data(socket_data);
    12771279                                }else{
    12781280                                        // retransmit
    1279                                         tcp_retransmit_packet( socket, socket_data, timeout->sequence_number );
    1280                                 }
    1281                                 fibril_rwlock_write_unlock( socket_data->local_lock );
     1281                                        tcp_retransmit_packet(socket, socket_data, timeout->sequence_number);
     1282                                }
     1283                                fibril_rwlock_write_unlock(socket_data->local_lock);
    12821284                        }else{
    1283                                 fibril_mutex_lock( & socket_data->operation.mutex );
     1285                                fibril_mutex_lock(&socket_data->operation.mutex);
    12841286                                // set the timeout operation result if state not changed
    1285                                 if( socket_data->state == timeout->state ){
     1287                                if(socket_data->state == timeout->state){
    12861288                                        socket_data->operation.result = ETIMEOUT;
    12871289                                        // notify the main fibril
    1288                                         fibril_condvar_signal( & socket_data->operation.condvar );
     1290                                        fibril_condvar_signal(&socket_data->operation.condvar);
    12891291                                        // keep the global write lock
    12901292                                        keep_write_lock = true;
     
    12921294                                        // operation is ok, do nothing
    12931295                                        // unlocking from now on, so the unlock order does not matter...
    1294                                         fibril_rwlock_write_unlock( socket_data->local_lock );
    1295                                 }
    1296                                 fibril_mutex_unlock( & socket_data->operation.mutex );
     1296                                        fibril_rwlock_write_unlock(socket_data->local_lock);
     1297                                }
     1298                                fibril_mutex_unlock(&socket_data->operation.mutex);
    12971299                        }
    12981300                }
    12991301        }
    13001302        // unlock only if no socket
    1301         if( timeout->globals_read_only ){
    1302                 fibril_rwlock_read_unlock( & tcp_globals.lock );
    1303         }else if( ! keep_write_lock ){
     1303        if(timeout->globals_read_only){
     1304                fibril_rwlock_read_unlock(&tcp_globals.lock);
     1305        }else if(! keep_write_lock){
    13041306                // release if not desired
    1305                 fibril_rwlock_write_unlock( & tcp_globals.lock );
     1307                fibril_rwlock_write_unlock(&tcp_globals.lock);
    13061308        }
    13071309        // release the timeout structure
    1308         free( timeout );
     1310        free(timeout);
    13091311        return EOK;
    13101312}
    13111313
    1312 int tcp_release_after_timeout( void * data ){
    1313         tcp_timeout_ref         timeout = data;
    1314         socket_core_ref         socket;
    1315         tcp_socket_data_ref     socket_data;
    1316         fibril_rwlock_t *       local_lock;
    1317 
    1318         assert( timeout );
     1314int tcp_release_after_timeout(void * data){
     1315        tcp_timeout_ref timeout = data;
     1316        socket_core_ref socket;
     1317        tcp_socket_data_ref socket_data;
     1318        fibril_rwlock_t * local_lock;
     1319
     1320        assert(timeout);
    13191321
    13201322        // sleep the given timeout
    1321         async_usleep( timeout->timeout );
     1323        async_usleep(timeout->timeout);
    13221324        // lock the globals
    1323         fibril_rwlock_write_lock( & tcp_globals.lock );
     1325        fibril_rwlock_write_lock(&tcp_globals.lock);
    13241326        // find the pending operation socket
    1325         socket = socket_port_find( & tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length );
    1326         if( socket && ( socket->socket_id == timeout->socket_id )){
    1327                 socket_data = ( tcp_socket_data_ref ) socket->specific_data;
    1328                 assert( socket_data );
    1329                 if( socket_data->local_sockets == timeout->local_sockets ){
     1327        socket = socket_port_find(&tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length);
     1328        if(socket && (socket->socket_id == timeout->socket_id)){
     1329                socket_data = (tcp_socket_data_ref) socket->specific_data;
     1330                assert(socket_data);
     1331                if(socket_data->local_sockets == timeout->local_sockets){
    13301332                        local_lock = socket_data->local_lock;
    1331                         fibril_rwlock_write_lock( local_lock );
    1332                         socket_destroy( tcp_globals.net_phone, timeout->socket_id, timeout->local_sockets, & tcp_globals.sockets, tcp_free_socket_data );
    1333                         fibril_rwlock_write_unlock( local_lock );
     1333                        fibril_rwlock_write_lock(local_lock);
     1334                        socket_destroy(tcp_globals.net_phone, timeout->socket_id, timeout->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
     1335                        fibril_rwlock_write_unlock(local_lock);
    13341336                }
    13351337        }
    13361338        // unlock the globals
    1337         fibril_rwlock_write_unlock( & tcp_globals.lock );
     1339        fibril_rwlock_write_unlock(&tcp_globals.lock);
    13381340        // release the timeout structure
    1339         free( timeout );
     1341        free(timeout);
    13401342        return EOK;
    13411343}
    13421344
    1343 void tcp_retransmit_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number ){
    1344         packet_t        packet;
    1345         packet_t        copy;
    1346         size_t          data_length;
    1347 
    1348         assert( socket );
    1349         assert( socket_data );
    1350         assert( socket->specific_data == socket_data );
     1345void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number){
     1346        packet_t packet;
     1347        packet_t copy;
     1348        size_t data_length;
     1349
     1350        assert(socket);
     1351        assert(socket_data);
     1352        assert(socket->specific_data == socket_data);
    13511353
    13521354        // sent packet?
    1353         packet = pq_find( socket_data->outgoing, sequence_number );
    1354         printf("retransmit %d\n", packet_get_id( packet ));
    1355         if( packet ){
    1356                 pq_get_order( packet, NULL, & data_length );
    1357                 copy = tcp_prepare_copy( socket, socket_data, packet, data_length, sequence_number );
    1358                 fibril_rwlock_write_unlock( socket_data->local_lock );
    1359 //              printf( "r send %d\n", packet_get_id( packet ));
    1360                 if( copy ){
    1361                         tcp_send_packets( socket_data->device_id, copy );
     1355        packet = pq_find(socket_data->outgoing, sequence_number);
     1356        printf("retransmit %d\n", packet_get_id(packet));
     1357        if(packet){
     1358                pq_get_order(packet, NULL, &data_length);
     1359                copy = tcp_prepare_copy(socket, socket_data, packet, data_length, sequence_number);
     1360                fibril_rwlock_write_unlock(socket_data->local_lock);
     1361//              printf("r send %d\n", packet_get_id(packet));
     1362                if(copy){
     1363                        tcp_send_packets(socket_data->device_id, copy);
    13621364                }
    13631365        }else{
    1364                 fibril_rwlock_write_unlock( socket_data->local_lock );
    1365         }
    1366 }
    1367 
    1368 int tcp_listen_message( socket_cores_ref local_sockets, int socket_id, int backlog ){
    1369         socket_core_ref         socket;
    1370         tcp_socket_data_ref     socket_data;
    1371 
    1372         assert( local_sockets );
    1373 
    1374         if( backlog < 0 ) return EINVAL;
     1366                fibril_rwlock_write_unlock(socket_data->local_lock);
     1367        }
     1368}
     1369
     1370int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog){
     1371        socket_core_ref socket;
     1372        tcp_socket_data_ref socket_data;
     1373
     1374        assert(local_sockets);
     1375
     1376        if(backlog < 0){
     1377                return EINVAL;
     1378        }
    13751379        // find the socket
    1376         socket = socket_cores_find( local_sockets, socket_id );
    1377         if( ! socket ) return ENOTSOCK;
     1380        socket = socket_cores_find(local_sockets, socket_id);
     1381        if(! socket){
     1382                return ENOTSOCK;
     1383        }
    13781384        // get the socket specific data
    1379         socket_data = ( tcp_socket_data_ref ) socket->specific_data;
    1380         assert( socket_data );
     1385        socket_data = (tcp_socket_data_ref) socket->specific_data;
     1386        assert(socket_data);
    13811387        // set the backlog
    13821388        socket_data->backlog = backlog;
     
    13841390}
    13851391
    1386 int tcp_connect_message( socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen ){
     1392int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen){
    13871393        ERROR_DECLARE;
    13881394
    1389         socket_core_ref                 socket;
    1390 
    1391         assert( local_sockets );
    1392         assert( addr );
    1393         assert( addrlen > 0 );
     1395        socket_core_ref socket;
     1396
     1397        assert(local_sockets);
     1398        assert(addr);
     1399        assert(addrlen > 0);
    13941400
    13951401        // find the socket
    1396         socket = socket_cores_find( local_sockets, socket_id );
    1397         if( ! socket ) return ENOTSOCK;
    1398         if( ERROR_OCCURRED( tcp_connect_core( socket, local_sockets, addr, addrlen ))){
    1399                 tcp_free_socket_data( socket );
     1402        socket = socket_cores_find(local_sockets, socket_id);
     1403        if(! socket){
     1404                return ENOTSOCK;
     1405        }
     1406        if(ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr, addrlen))){
     1407                tcp_free_socket_data(socket);
    14001408                // unbind if bound
    1401                 if( socket->port > 0 ){
    1402                         socket_ports_exclude( & tcp_globals.sockets, socket->port );
     1409                if(socket->port > 0){
     1410                        socket_ports_exclude(&tcp_globals.sockets, socket->port);
    14031411                        socket->port = 0;
    14041412                }
     
    14071415}
    14081416
    1409 int tcp_connect_core( socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen ){
     1417int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen){
    14101418        ERROR_DECLARE;
    14111419
    1412         tcp_socket_data_ref             socket_data;
    1413         packet_t                                packet;
    1414 
    1415         assert( socket );
    1416         assert( addr );
    1417         assert( addrlen > 0 );
     1420        tcp_socket_data_ref socket_data;
     1421        packet_t packet;
     1422
     1423        assert(socket);
     1424        assert(addr);
     1425        assert(addrlen > 0);
    14181426
    14191427        // get the socket specific data
    1420         socket_data = ( tcp_socket_data_ref ) socket->specific_data;
    1421         assert( socket_data );
    1422         assert( socket->specific_data == socket_data );
    1423         if(( socket_data->state != TCP_SOCKET_INITIAL )
    1424         && (( socket_data->state != TCP_SOCKET_LISTEN ) || ( socket->port <= 0 ))){
     1428        socket_data = (tcp_socket_data_ref) socket->specific_data;
     1429        assert(socket_data);
     1430        assert(socket->specific_data == socket_data);
     1431        if((socket_data->state != TCP_SOCKET_INITIAL)
     1432                && ((socket_data->state != TCP_SOCKET_LISTEN) || (socket->port <= 0))){
    14251433                return EINVAL;
    14261434        }
    14271435        // get the destination port
    1428         ERROR_PROPAGATE( tl_get_address_port( addr, addrlen, & socket_data->dest_port ));
    1429         if( socket->port <= 0 ){
     1436        ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, &socket_data->dest_port));
     1437        if(socket->port <= 0){
    14301438                // try to find a free port
    1431                 ERROR_PROPAGATE( socket_bind_free_port( & tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port ));
     1439                ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port));
    14321440                // set the next port as the search starting port number
    14331441                tcp_globals.last_used_port = socket->port;
    14341442        }
    1435         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 ));
     1443        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));
    14361444
    14371445        // create the notification packet
    1438         ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 1, 0 ));
     1446        ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 1, 0));
    14391447
    14401448        // unlock the globals and wait for an operation
    1441         fibril_rwlock_write_unlock( & tcp_globals.lock );
     1449        fibril_rwlock_write_unlock(&tcp_globals.lock);
    14421450
    14431451        socket_data->addr = addr;
    14441452        socket_data->addrlen = addrlen;
    14451453        // send the packet
    1446         if( ERROR_OCCURRED( tcp_queue_packet( socket, socket_data, packet, 1 ))
    1447         || ERROR_OCCURRED( tcp_prepare_timeout( tcp_timeout, socket, socket_data, 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false ))){
     1454        if(ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))
     1455                || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))){
    14481456                socket_data->addr = NULL;
    14491457                socket_data->addrlen = 0;
    1450                 fibril_rwlock_write_lock( & tcp_globals.lock );
     1458                fibril_rwlock_write_lock(&tcp_globals.lock);
    14511459        }else{
    1452                 packet = tcp_get_packets_to_send( socket, socket_data );
    1453                 if( packet ){
    1454                         fibril_mutex_lock( & socket_data->operation.mutex );
    1455                         fibril_rwlock_write_unlock( socket_data->local_lock );
     1460                packet = tcp_get_packets_to_send(socket, socket_data);
     1461                if(packet){
     1462                        fibril_mutex_lock(&socket_data->operation.mutex);
     1463                        fibril_rwlock_write_unlock(socket_data->local_lock);
    14561464                        // send the packet
    1457                         printf( "connecting %d\n", packet_get_id( packet ));
    1458                         tcp_send_packets( socket_data->device_id, packet );
     1465                        printf("connecting %d\n", packet_get_id(packet));
     1466                        tcp_send_packets(socket_data->device_id, packet);
    14591467                        // wait for a reply
    1460                         fibril_condvar_wait( & socket_data->operation.condvar, & socket_data->operation.mutex );
     1468                        fibril_condvar_wait(&socket_data->operation.condvar, &socket_data->operation.mutex);
    14611469                        ERROR_CODE = socket_data->operation.result;
    1462                         if( ERROR_CODE != EOK ){
     1470                        if(ERROR_CODE != EOK){
    14631471                                socket_data->addr = NULL;
    14641472                                socket_data->addrlen = 0;
     
    14711479        }
    14721480
    1473         fibril_mutex_unlock( & socket_data->operation.mutex );
     1481        fibril_mutex_unlock(&socket_data->operation.mutex);
    14741482
    14751483        // return the result
     
    14771485}
    14781486
    1479 int tcp_queue_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ){
     1487int tcp_queue_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length){
    14801488        ERROR_DECLARE;
    14811489
    1482         tcp_header_ref  header;
    1483 
    1484         assert( socket );
    1485         assert( socket_data );
    1486         assert( socket->specific_data == socket_data );
     1490        tcp_header_ref header;
     1491
     1492        assert(socket);
     1493        assert(socket_data);
     1494        assert(socket->specific_data == socket_data);
    14871495
    14881496        // get tcp header
    1489         header = ( tcp_header_ref ) packet_get_data( packet );
    1490         if( ! header ) return NO_DATA;
    1491         header->destination_port = htons( socket_data->dest_port );
    1492         header->source_port = htons( socket->port );
    1493         header->sequence_number = htonl( socket_data->next_outgoing );
    1494         if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) socket_data->addr, socket_data->addrlen ))){
    1495                 return tcp_release_and_return( packet, EINVAL );
     1497        header = (tcp_header_ref) packet_get_data(packet);
     1498        if(! header){
     1499                return NO_DATA;
     1500        }
     1501        header->destination_port = htons(socket_data->dest_port);
     1502        header->source_port = htons(socket->port);
     1503        header->sequence_number = htonl(socket_data->next_outgoing);
     1504        if(ERROR_OCCURRED(packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr, socket_data->addrlen))){
     1505                return tcp_release_and_return(packet, EINVAL);
    14961506        }
    14971507        // remember the outgoing FIN
    1498         if( header->finalize ){
     1508        if(header->finalize){
    14991509                socket_data->fin_outgoing = socket_data->next_outgoing;
    15001510        }
     
    15021512}
    15031513
    1504 int     tcp_queue_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ){
     1514int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length){
    15051515        ERROR_DECLARE;
    15061516
    1507         assert( socket );
    1508         assert( socket_data );
    1509         assert( socket->specific_data == socket_data );
    1510 
    1511         ERROR_PROPAGATE( tcp_queue_prepare_packet( socket, socket_data, packet, data_length ));
    1512 
    1513         if( ERROR_OCCURRED( pq_add( & socket_data->outgoing, packet, socket_data->next_outgoing, data_length ))){
    1514                 return tcp_release_and_return( packet, ERROR_CODE );
     1517        assert(socket);
     1518        assert(socket_data);
     1519        assert(socket->specific_data == socket_data);
     1520
     1521        ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet, data_length));
     1522
     1523        if(ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing, data_length))){
     1524                return tcp_release_and_return(packet, ERROR_CODE);
    15151525        }
    15161526        socket_data->next_outgoing += data_length;
     
    15181528}
    15191529
    1520 packet_t tcp_get_packets_to_send( socket_core_ref socket, tcp_socket_data_ref socket_data ){
     1530packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data){
    15211531        ERROR_DECLARE;
    15221532
    1523         packet_t                packet;
    1524         packet_t                copy;
    1525         packet_t                sending = NULL;
    1526         packet_t                previous = NULL;
    1527         size_t                  data_length;
    1528 
    1529         assert( socket );
    1530         assert( socket_data );
    1531         assert( socket->specific_data == socket_data );
    1532 
    1533         packet = pq_find( socket_data->outgoing, socket_data->last_outgoing + 1 );
    1534         while( packet ){
    1535                 pq_get_order( packet, NULL, & data_length );
     1533        packet_t packet;
     1534        packet_t copy;
     1535        packet_t sending = NULL;
     1536        packet_t previous = NULL;
     1537        size_t data_length;
     1538
     1539        assert(socket);
     1540        assert(socket_data);
     1541        assert(socket->specific_data == socket_data);
     1542
     1543        packet = pq_find(socket_data->outgoing, socket_data->last_outgoing + 1);
     1544        while(packet){
     1545                pq_get_order(packet, NULL, &data_length);
    15361546                // send only if fits into the window
    15371547                // respecting the possible overflow
    1538                 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 ))){
    1539                         copy = tcp_prepare_copy( socket, socket_data, packet, data_length, socket_data->last_outgoing + 1 );
    1540                         if( ! copy ){
     1548                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))){
     1549                        copy = tcp_prepare_copy(socket, socket_data, packet, data_length, socket_data->last_outgoing + 1);
     1550                        if(! copy){
    15411551                                return sending;
    15421552                        }
    1543                         if( ! sending ){
     1553                        if(! sending){
    15441554                                sending = copy;
    15451555                        }else{
    1546                                 if( ERROR_OCCURRED( pq_insert_after( previous, copy ))){
    1547                                         pq_release( tcp_globals.net_phone, packet_get_id( copy ));
     1556                                if(ERROR_OCCURRED(pq_insert_after(previous, copy))){
     1557                                        pq_release(tcp_globals.net_phone, packet_get_id(copy));
    15481558                                        return sending;
    15491559                                }
    15501560                        }
    15511561                        previous = copy;
    1552                         packet = pq_next( packet );
     1562                        packet = pq_next(packet);
    15531563                        // overflow occurred ?
    1554                         if(( ! packet ) && ( socket_data->last_outgoing > socket_data->next_outgoing )){
     1564                        if((! packet) && (socket_data->last_outgoing > socket_data->next_outgoing)){
    15551565                                printf("gpts overflow\n");
    15561566                                // continue from the beginning
     
    15651575}
    15661576
    1567 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 ){
     1577packet_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){
    15681578        ERROR_DECLARE;
    15691579
    1570         tcp_header_ref  header;
    1571         uint32_t                checksum;
    1572 
    1573         assert( socket );
    1574         assert( socket_data );
    1575         assert( socket->specific_data == socket_data );
     1580        tcp_header_ref header;
     1581        uint32_t checksum;
     1582
     1583        assert(socket);
     1584        assert(socket_data);
     1585        assert(socket->specific_data == socket_data);
    15761586
    15771587        // adjust the pseudo header
    1578         if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( socket_data->pseudo_header, socket_data->headerlen, packet_get_data_length( packet )))){
    1579                 pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     1588        if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(socket_data->pseudo_header, socket_data->headerlen, packet_get_data_length(packet)))){
     1589                pq_release(tcp_globals.net_phone, packet_get_id(packet));
    15801590                return NULL;
    15811591        }
    15821592
    15831593        // get the header
    1584         header = ( tcp_header_ref ) packet_get_data( packet );
    1585         if( ! header ){
    1586                 pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     1594        header = (tcp_header_ref) packet_get_data(packet);
     1595        if(! header){
     1596                pq_release(tcp_globals.net_phone, packet_get_id(packet));
    15871597                return NULL;
    15881598        }
    1589         assert( ntohl( header->sequence_number ) == sequence_number );
     1599        assert(ntohl(header->sequence_number) == sequence_number);
    15901600
    15911601        // adjust the header
    1592         if( socket_data->next_incoming ){
    1593                 header->acknowledgement_number = htonl( socket_data->next_incoming );
     1602        if(socket_data->next_incoming){
     1603                header->acknowledgement_number = htonl(socket_data->next_incoming);
    15941604                header->acknowledge = 1;
    15951605        }
    1596         header->window = htons( socket_data->window );
     1606        header->window = htons(socket_data->window);
    15971607
    15981608        // checksum
    15991609        header->checksum = 0;
    1600         checksum = compute_checksum( 0, socket_data->pseudo_header, socket_data->headerlen );
    1601         checksum = compute_checksum( checksum, ( uint8_t * ) packet_get_data( packet ), packet_get_data_length( packet ));
    1602         header->checksum = htons( flip_checksum( compact_checksum( checksum )));
     1610        checksum = compute_checksum(0, socket_data->pseudo_header, socket_data->headerlen);
     1611        checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet), packet_get_data_length(packet));
     1612        header->checksum = htons(flip_checksum(compact_checksum(checksum)));
    16031613        // prepare the packet
    1604         if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_TCP, 0, 0, 0, 0 ))
     1614        if(ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0))
    16051615        // prepare the timeout
    1606         || ERROR_OCCURRED( tcp_prepare_timeout( tcp_timeout, socket, socket_data, sequence_number, socket_data->state, socket_data->timeout, true ))){
    1607                 pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     1616                || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, sequence_number, socket_data->state, socket_data->timeout, true))){
     1617                pq_release(tcp_globals.net_phone, packet_get_id(packet));
    16081618                return NULL;
    16091619        }
     
    16111621}
    16121622
    1613 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 ){
    1614         packet_t                copy;
    1615 
    1616         assert( socket );
    1617         assert( socket_data );
    1618         assert( socket->specific_data == socket_data );
     1623packet_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){
     1624        packet_t copy;
     1625
     1626        assert(socket);
     1627        assert(socket_data);
     1628        assert(socket->specific_data == socket_data);
    16191629
    16201630        // make a copy of the packet
    1621         copy = packet_get_copy( tcp_globals.net_phone, packet );
    1622         if( ! copy ) return NULL;
    1623 
    1624         return tcp_send_prepare_packet( socket, socket_data, copy, data_length, sequence_number );
    1625 }
    1626 
    1627 void tcp_send_packets( device_id_t device_id, packet_t packet ){
    1628         packet_t        next;
    1629 
    1630         while( packet ){
    1631                 next = pq_detach( packet );
    1632                 ip_send_msg( tcp_globals.ip_phone, device_id, packet, SERVICE_TCP, 0 );
     1631        copy = packet_get_copy(tcp_globals.net_phone, packet);
     1632        if(! copy){
     1633                return NULL;
     1634        }
     1635
     1636        return tcp_send_prepare_packet(socket, socket_data, copy, data_length, sequence_number);
     1637}
     1638
     1639void tcp_send_packets(device_id_t device_id, packet_t packet){
     1640        packet_t next;
     1641
     1642        while(packet){
     1643                next = pq_detach(packet);
     1644                ip_send_msg(tcp_globals.ip_phone, device_id, packet, SERVICE_TCP, 0);
    16331645                packet = next;
    16341646        }
    16351647}
    16361648
    1637 void tcp_prepare_operation_header( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize ){
    1638         assert( socket );
    1639         assert( socket_data );
    1640         assert( socket->specific_data == socket_data );
    1641         assert( header );
    1642 
    1643         bzero( header, sizeof( * header ));
    1644         header->source_port = htons( socket->port );
    1645         header->source_port = htons( socket_data->dest_port );
    1646         header->header_length = TCP_COMPUTE_HEADER_LENGTH( sizeof( * header ));
     1649void tcp_prepare_operation_header(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize){
     1650        assert(socket);
     1651        assert(socket_data);
     1652        assert(socket->specific_data == socket_data);
     1653        assert(header);
     1654
     1655        bzero(header, sizeof(*header));
     1656        header->source_port = htons(socket->port);
     1657        header->source_port = htons(socket_data->dest_port);
     1658        header->header_length = TCP_COMPUTE_HEADER_LENGTH(sizeof(*header));
    16471659        header->synchronize = synchronize;
    16481660        header->finalize = finalize;
    16491661}
    16501662
    1651 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 ){
    1652         tcp_timeout_ref operation_timeout;
    1653         fid_t                   fibril;
    1654 
    1655         assert( socket );
    1656         assert( socket_data );
    1657         assert( socket->specific_data == socket_data );
     1663int 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){
     1664        tcp_timeout_ref operation_timeout;
     1665        fid_t fibril;
     1666
     1667        assert(socket);
     1668        assert(socket_data);
     1669        assert(socket->specific_data == socket_data);
    16581670
    16591671        // prepare the timeout with key bundle structure
    1660         operation_timeout = malloc( sizeof( * operation_timeout ) + socket->key_length + 1 );
    1661         if( ! operation_timeout ) return ENOMEM;
    1662         bzero( operation_timeout, sizeof( * operation_timeout ));
     1672        operation_timeout = malloc(sizeof(*operation_timeout) + socket->key_length + 1);
     1673        if(! operation_timeout){
     1674                return ENOMEM;
     1675        }
     1676        bzero(operation_timeout, sizeof(*operation_timeout));
    16631677        operation_timeout->globals_read_only = globals_read_only;
    16641678        operation_timeout->port = socket->port;
     
    16701684
    16711685        // copy the key
    1672         operation_timeout->key = (( char * ) operation_timeout ) + sizeof( * operation_timeout );
     1686        operation_timeout->key = ((char *) operation_timeout) + sizeof(*operation_timeout);
    16731687        operation_timeout->key_length = socket->key_length;
    1674         memcpy( operation_timeout->key, socket->key, socket->key_length );
    1675         operation_timeout->key[ operation_timeout->key_length ] = '\0';
     1688        memcpy(operation_timeout->key, socket->key, socket->key_length);
     1689        operation_timeout->key[operation_timeout->key_length] = '\0';
    16761690
    16771691        // prepare the timeouting thread
    1678         fibril = fibril_create( timeout_function, operation_timeout );
    1679         if( ! fibril ){
    1680                 free( operation_timeout );
     1692        fibril = fibril_create(timeout_function, operation_timeout);
     1693        if(! fibril){
     1694                free(operation_timeout);
    16811695                return EPARTY;
    16821696        }
    1683 //      fibril_mutex_lock( & socket_data->operation.mutex );
     1697//      fibril_mutex_lock(&socket_data->operation.mutex);
    16841698        // start the timeouting fibril
    1685         fibril_add_ready( fibril );
     1699        fibril_add_ready(fibril);
    16861700        //socket_data->state = state;
    16871701        return EOK;
    16881702}
    16891703
    1690 int tcp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ){
     1704int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen){
    16911705        ERROR_DECLARE;
    16921706
    1693         socket_core_ref         socket;
    1694         tcp_socket_data_ref     socket_data;
    1695         int                                     packet_id;
    1696         packet_t                        packet;
    1697         size_t                          length;
    1698 
    1699         assert( local_sockets );
     1707        socket_core_ref socket;
     1708        tcp_socket_data_ref socket_data;
     1709        int packet_id;
     1710        packet_t packet;
     1711        size_t length;
     1712
     1713        assert(local_sockets);
    17001714
    17011715        // find the socket
    1702         socket = socket_cores_find( local_sockets, socket_id );
    1703         if( ! socket ) return ENOTSOCK;
     1716        socket = socket_cores_find(local_sockets, socket_id);
     1717        if(! socket){
     1718                return ENOTSOCK;
     1719        }
    17041720        // get the socket specific data
    1705         if( ! socket->specific_data ) return NO_DATA;
    1706         socket_data = ( tcp_socket_data_ref ) socket->specific_data;
     1721        if(! socket->specific_data){
     1722                return NO_DATA;
     1723        }
     1724        socket_data = (tcp_socket_data_ref) socket->specific_data;
    17071725
    17081726        // check state
    1709         if(( socket_data->state != TCP_SOCKET_ESTABLISHED ) && ( socket_data->state != TCP_SOCKET_CLOSE_WAIT )){
     1727        if((socket_data->state != TCP_SOCKET_ESTABLISHED) && (socket_data->state != TCP_SOCKET_CLOSE_WAIT)){
    17101728                return ENOTCONN;
    17111729        }
    17121730
    17131731        // send the source address if desired
    1714         if( addrlen ){
    1715                 ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen ));
    1716                 * addrlen = socket_data->addrlen;
     1732        if(addrlen){
     1733                ERROR_PROPAGATE(data_reply(socket_data->addr, socket_data->addrlen));
     1734                *addrlen = socket_data->addrlen;
    17171735        }
    17181736
    17191737        // get the next received packet
    1720         packet_id = dyn_fifo_value( & socket->received );
    1721         if( packet_id < 0 ) return NO_DATA;
    1722         ERROR_PROPAGATE( packet_translate( tcp_globals.net_phone, & packet, packet_id ));
     1738        packet_id = dyn_fifo_value(&socket->received);
     1739        if(packet_id < 0){
     1740                return NO_DATA;
     1741        }
     1742        ERROR_PROPAGATE(packet_translate(tcp_globals.net_phone, &packet, packet_id));
    17231743
    17241744        // reply the packets
    1725         ERROR_PROPAGATE( socket_reply_packets( packet, & length ));
     1745        ERROR_PROPAGATE(socket_reply_packets(packet, &length));
    17261746
    17271747        // release the packet
    1728         dyn_fifo_pop( & socket->received );
    1729         pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     1748        dyn_fifo_pop(&socket->received);
     1749        pq_release(tcp_globals.net_phone, packet_get_id(packet));
    17301750        // return the total length
    1731         return ( int ) length;
    1732 }
    1733 
    1734 int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags ){
     1751        return (int) length;
     1752}
     1753
     1754int tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags){
    17351755        ERROR_DECLARE;
    17361756
    1737         socket_core_ref                 socket;
    1738         tcp_socket_data_ref             socket_data;
    1739         packet_dimension_ref    packet_dimension;
    1740         packet_t                                packet;
    1741         size_t                                  total_length;
    1742         tcp_header_ref                  header;
    1743         int                                             index;
    1744         int                                             result;
    1745 
    1746         assert( local_sockets );
    1747         assert( data_fragment_size );
     1757        socket_core_ref socket;
     1758        tcp_socket_data_ref socket_data;
     1759        packet_dimension_ref packet_dimension;
     1760        packet_t packet;
     1761        size_t total_length;
     1762        tcp_header_ref header;
     1763        int index;
     1764        int result;
     1765
     1766        assert(local_sockets);
     1767        assert(data_fragment_size);
    17481768
    17491769        // find the socket
    1750         socket = socket_cores_find( local_sockets, socket_id );
    1751         if( ! socket ) return ENOTSOCK;
     1770        socket = socket_cores_find(local_sockets, socket_id);
     1771        if(! socket){
     1772                return ENOTSOCK;
     1773        }
    17521774        // get the socket specific data
    1753         if( ! socket->specific_data ) return NO_DATA;
    1754         socket_data = ( tcp_socket_data_ref ) socket->specific_data;
     1775        if(! socket->specific_data){
     1776                return NO_DATA;
     1777        }
     1778        socket_data = (tcp_socket_data_ref) socket->specific_data;
    17551779
    17561780        // check state
    1757         if(( socket_data->state != TCP_SOCKET_ESTABLISHED ) && ( socket_data->state != TCP_SOCKET_CLOSE_WAIT )){
     1781        if((socket_data->state != TCP_SOCKET_ESTABLISHED) && (socket_data->state != TCP_SOCKET_CLOSE_WAIT)){
    17581782                return ENOTCONN;
    17591783        }
    17601784
    1761         ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ));
    1762 
    1763         * data_fragment_size = (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size );
    1764 
    1765         for( index = 0; index < fragments; ++ index ){
     1785        ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
     1786
     1787        *data_fragment_size = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size);
     1788
     1789        for(index = 0; index < fragments; ++ index){
    17661790                // read the data fragment
    1767                 result = tl_socket_read_packet_data( tcp_globals.net_phone, & packet, TCP_HEADER_SIZE, packet_dimension, socket_data->addr, socket_data->addrlen );
    1768                 if( result < 0 ) return result;
    1769                 total_length = ( size_t ) result;
     1791                result = tl_socket_read_packet_data(tcp_globals.net_phone, &packet, TCP_HEADER_SIZE, packet_dimension, socket_data->addr, socket_data->addrlen);
     1792                if(result < 0){
     1793                        return result;
     1794                }
     1795                total_length = (size_t) result;
    17701796                // prefix the tcp header
    1771                 header = PACKET_PREFIX( packet, tcp_header_t );
    1772                 if( ! header ){
    1773                         return tcp_release_and_return( packet, ENOMEM );
    1774                 }
    1775                 tcp_prepare_operation_header( socket, socket_data, header, 0, 0 );
    1776                 ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 0 ));
     1797                header = PACKET_PREFIX(packet, tcp_header_t);
     1798                if(! header){
     1799                        return tcp_release_and_return(packet, ENOMEM);
     1800                }
     1801                tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
     1802                ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 0));
    17771803        }
    17781804
    17791805        // flush packets
    1780         packet = tcp_get_packets_to_send( socket, socket_data );
    1781         fibril_rwlock_write_unlock( socket_data->local_lock );
    1782         fibril_rwlock_read_unlock( & tcp_globals.lock );
    1783         if( packet ){
     1806        packet = tcp_get_packets_to_send(socket, socket_data);
     1807        fibril_rwlock_write_unlock(socket_data->local_lock);
     1808        fibril_rwlock_read_unlock(&tcp_globals.lock);
     1809        if(packet){
    17841810                // send the packet
    1785                 tcp_send_packets( socket_data->device_id, packet );
     1811                tcp_send_packets(socket_data->device_id, packet);
    17861812        }
    17871813
     
    17891815}
    17901816
    1791 int tcp_close_message( socket_cores_ref local_sockets, int socket_id ){
     1817int tcp_close_message(socket_cores_ref local_sockets, int socket_id){
    17921818        ERROR_DECLARE;
    17931819
    1794         socket_core_ref                 socket;
    1795         tcp_socket_data_ref             socket_data;
    1796         packet_t                                packet;
     1820        socket_core_ref socket;
     1821        tcp_socket_data_ref socket_data;
     1822        packet_t packet;
    17971823
    17981824        // find the socket
    1799         socket = socket_cores_find( local_sockets, socket_id );
    1800         if( ! socket ) return ENOTSOCK;
     1825        socket = socket_cores_find(local_sockets, socket_id);
     1826        if(! socket){
     1827                return ENOTSOCK;
     1828        }
    18011829        // get the socket specific data
    1802         socket_data = ( tcp_socket_data_ref ) socket->specific_data;
    1803         assert( socket_data );
     1830        socket_data = (tcp_socket_data_ref) socket->specific_data;
     1831        assert(socket_data);
    18041832
    18051833        // check state
    1806         switch( socket_data->state ){
     1834        switch(socket_data->state){
    18071835                case TCP_SOCKET_ESTABLISHED:
    18081836                        socket_data->state = TCP_SOCKET_FIN_WAIT_1;
     
    18141842                default:
    18151843                        // just destroy
    1816                         if( ! ERROR_OCCURRED( socket_destroy( tcp_globals.net_phone, socket_id, local_sockets, & tcp_globals.sockets, tcp_free_socket_data ))){
    1817                                 fibril_rwlock_write_unlock( socket_data->local_lock );
    1818                                 fibril_rwlock_write_unlock( & tcp_globals.lock );
     1844                        if(! ERROR_OCCURRED(socket_destroy(tcp_globals.net_phone, socket_id, local_sockets, &tcp_globals.sockets, tcp_free_socket_data))){
     1845                                fibril_rwlock_write_unlock(socket_data->local_lock);
     1846                                fibril_rwlock_write_unlock(&tcp_globals.lock);
    18191847                        }
    18201848                        return ERROR_CODE;
     
    18241852
    18251853        // create the notification packet
    1826         ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 1 ));
     1854        ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 1));
    18271855
    18281856        // send the packet
    1829         ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 1 ));
     1857        ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
    18301858
    18311859        // flush packets
    1832         packet = tcp_get_packets_to_send( socket, socket_data );
    1833         fibril_rwlock_write_unlock( socket_data->local_lock );
    1834         fibril_rwlock_write_unlock( & tcp_globals.lock );
    1835         if( packet ){
     1860        packet = tcp_get_packets_to_send(socket, socket_data);
     1861        fibril_rwlock_write_unlock(socket_data->local_lock);
     1862        fibril_rwlock_write_unlock(&tcp_globals.lock);
     1863        if(packet){
    18361864                // send the packet
    1837                 tcp_send_packets( socket_data->device_id, packet );
     1865                tcp_send_packets(socket_data->device_id, packet);
    18381866        }
    18391867        return EOK;
    18401868}
    18411869
    1842 int tcp_create_notification_packet( packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize ){
     1870int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize){
    18431871        ERROR_DECLARE;
    18441872
    1845         packet_dimension_ref    packet_dimension;
    1846         tcp_header_ref                  header;
    1847 
    1848         assert( packet );
     1873        packet_dimension_ref packet_dimension;
     1874        tcp_header_ref header;
     1875
     1876        assert(packet);
    18491877
    18501878        // get the device packet dimension
    1851         ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ));
     1879        ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
    18521880        // get a new packet
    1853         * packet = packet_get_4( tcp_globals.net_phone, TCP_HEADER_SIZE, packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix );
    1854         if( ! * packet ) return ENOMEM;
     1881        *packet = packet_get_4(tcp_globals.net_phone, TCP_HEADER_SIZE, packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix);
     1882        if(! * packet){
     1883                return ENOMEM;
     1884        }
    18551885        // allocate space in the packet
    1856         header = PACKET_SUFFIX( * packet, tcp_header_t );
    1857         if( ! header ){
    1858                 tcp_release_and_return( * packet, ENOMEM );
    1859         }
    1860 
    1861         tcp_prepare_operation_header( socket, socket_data, header, synchronize, finalize );
     1886        header = PACKET_SUFFIX(*packet, tcp_header_t);
     1887        if(! header){
     1888                tcp_release_and_return(*packet, ENOMEM);
     1889        }
     1890
     1891        tcp_prepare_operation_header(socket, socket_data, header, synchronize, finalize);
    18621892        return EOK;
    18631893}
    18641894
    1865 int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen ){
     1895int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen){
    18661896        ERROR_DECLARE;
    18671897
    1868         socket_core_ref         accepted;
    1869         socket_core_ref         socket;
    1870         tcp_socket_data_ref     socket_data;
    1871         packet_dimension_ref    packet_dimension;
    1872 
    1873         assert( local_sockets );
    1874         assert( data_fragment_size );
    1875         assert( addrlen );
     1898        socket_core_ref accepted;
     1899        socket_core_ref socket;
     1900        tcp_socket_data_ref socket_data;
     1901        packet_dimension_ref packet_dimension;
     1902
     1903        assert(local_sockets);
     1904        assert(data_fragment_size);
     1905        assert(addrlen);
    18761906
    18771907        // find the socket
    1878         socket = socket_cores_find( local_sockets, socket_id );
    1879         if( ! socket ) return ENOTSOCK;
     1908        socket = socket_cores_find(local_sockets, socket_id);
     1909        if(! socket){
     1910                return ENOTSOCK;
     1911        }
    18801912        // get the socket specific data
    1881         socket_data = ( tcp_socket_data_ref ) socket->specific_data;
    1882         assert( socket_data );
     1913        socket_data = (tcp_socket_data_ref) socket->specific_data;
     1914        assert(socket_data);
    18831915
    18841916        // check state
    1885         if( socket_data->state != TCP_SOCKET_LISTEN ){
     1917        if(socket_data->state != TCP_SOCKET_LISTEN){
    18861918                return EINVAL;
    18871919        }
    18881920
    18891921        do{
    1890                 socket_id = dyn_fifo_value( & socket->accepted );
    1891                 if( socket_id < 0 ) return ENOTSOCK;
     1922                socket_id = dyn_fifo_value(&socket->accepted);
     1923                if(socket_id < 0){
     1924                        return ENOTSOCK;
     1925                }
    18921926                socket_id *= -1;
    18931927
    1894                 accepted = socket_cores_find( local_sockets, socket_id );
    1895                 if( ! accepted ) return ENOTSOCK;
     1928                accepted = socket_cores_find(local_sockets, socket_id);
     1929                if(! accepted){
     1930                        return ENOTSOCK;
     1931                }
    18961932                // get the socket specific data
    1897                 socket_data = ( tcp_socket_data_ref ) accepted->specific_data;
    1898                 assert( socket_data );
     1933                socket_data = (tcp_socket_data_ref) accepted->specific_data;
     1934                assert(socket_data);
    18991935                // TODO can it be in another state?
    1900                 if( socket_data->state == TCP_SOCKET_ESTABLISHED ){
    1901                         ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen ));
    1902                         ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension ));
    1903                         * addrlen = socket_data->addrlen;
    1904                         * data_fragment_size = (( packet_dimension->content < socket_data->data_fragment_size ) ? packet_dimension->content : socket_data->data_fragment_size );
    1905                         if( new_socket_id > 0 ){
    1906                                 ERROR_PROPAGATE( socket_cores_update( local_sockets, accepted->socket_id, new_socket_id ));
     1936                if(socket_data->state == TCP_SOCKET_ESTABLISHED){
     1937                        ERROR_PROPAGATE(data_reply(socket_data->addr, socket_data->addrlen));
     1938                        ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
     1939                        *addrlen = socket_data->addrlen;
     1940                        *data_fragment_size = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size);
     1941                        if(new_socket_id > 0){
     1942                                ERROR_PROPAGATE(socket_cores_update(local_sockets, accepted->socket_id, new_socket_id));
    19071943                                accepted->socket_id = new_socket_id;
    19081944                        }
    19091945                }
    1910                 dyn_fifo_pop( & socket->accepted );
    1911         }while( socket_data->state != TCP_SOCKET_ESTABLISHED );
    1912         printf("ret accept %d\n", accepted->socket_id );
     1946                dyn_fifo_pop(&socket->accepted);
     1947        }while(socket_data->state != TCP_SOCKET_ESTABLISHED);
     1948        printf("ret accept %d\n", accepted->socket_id);
    19131949        return accepted->socket_id;
    19141950}
    19151951
    1916 void tcp_free_socket_data( socket_core_ref socket ){
    1917         tcp_socket_data_ref             socket_data;
    1918 
    1919         assert( socket );
    1920 
    1921         printf( "destroy_socket %d\n", socket->socket_id );
     1952void tcp_free_socket_data(socket_core_ref socket){
     1953        tcp_socket_data_ref socket_data;
     1954
     1955        assert(socket);
     1956
     1957        printf("destroy_socket %d\n", socket->socket_id);
    19221958
    19231959        // get the socket specific data
    1924         socket_data = ( tcp_socket_data_ref ) socket->specific_data;
    1925         assert( socket_data );
     1960        socket_data = (tcp_socket_data_ref) socket->specific_data;
     1961        assert(socket_data);
    19261962        //free the pseudo header
    1927         if( socket_data->pseudo_header ){
    1928                 if( socket_data->headerlen ){
     1963        if(socket_data->pseudo_header){
     1964                if(socket_data->headerlen){
    19291965                        printf("d pseudo\n");
    1930                         free( socket_data->pseudo_header );
     1966                        free(socket_data->pseudo_header);
    19311967                        socket_data->headerlen = 0;
    19321968                }
     
    19351971        socket_data->headerlen = 0;
    19361972        // free the address
    1937         if( socket_data->addr ){
    1938                 if( socket_data->addrlen ){
     1973        if(socket_data->addr){
     1974                if(socket_data->addrlen){
    19391975                        printf("d addr\n");
    1940                         free( socket_data->addr );
     1976                        free(socket_data->addr);
    19411977                        socket_data->addrlen = 0;
    19421978                }
     
    19461982}
    19471983
    1948 int     tcp_release_and_return( packet_t packet, int result ){
    1949         pq_release( tcp_globals.net_phone, packet_get_id( packet ));
     1984int tcp_release_and_return(packet_t packet, int result){
     1985        pq_release(tcp_globals.net_phone, packet_get_id(packet));
    19501986        return result;
    19511987}
Note: See TracChangeset for help on using the changeset viewer.