Ignore:
File:
1 edited

Legend:

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

    r14f1db0 r46d4d9f  
    2828
    2929/** @addtogroup tcp
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  TCP module implementation.
    35  *  @see tcp.h
     34 * TCP module implementation.
     35 * @see tcp.h
    3636 */
     37
     38#include "tcp.h"
     39#include "tcp_header.h"
     40#include "tcp_module.h"
    3741
    3842#include <assert.h>
     
    4044#include <fibril_synch.h>
    4145#include <malloc.h>
    42 //TODO remove stdio
     46/* TODO remove stdio */
    4347#include <stdio.h>
     48#include <errno.h>
    4449
    4550#include <ipc/ipc.h>
    4651#include <ipc/services.h>
    47 
    48 #include <net_err.h>
    49 #include <net_messages.h>
    50 #include <net_modules.h>
     52#include <ipc/net.h>
     53#include <ipc/tl.h>
     54#include <ipc/socket.h>
     55
     56#include <net/socket_codes.h>
     57#include <net/ip_protocols.h>
     58#include <net/in.h>
     59#include <net/in6.h>
     60#include <net/inet.h>
     61#include <net/modules.h>
     62
    5163#include <adt/dynamic_fifo.h>
    52 #include <packet/packet_client.h>
     64#include <packet_client.h>
    5365#include <packet_remote.h>
    5466#include <net_checksum.h>
    55 #include <in.h>
    56 #include <in6.h>
    57 #include <inet.h>
    5867#include <ip_client.h>
    5968#include <ip_interface.h>
    60 #include <ip_protocols.h>
    6169#include <icmp_client.h>
    6270#include <icmp_interface.h>
    6371#include <net_interface.h>
    64 #include <socket_codes.h>
    65 #include <socket_errno.h>
    66 #include <tcp_codes.h>
    6772#include <socket_core.h>
    68 #include <socket_messages.h>
    6973#include <tl_common.h>
    70 #include <tl_messages.h>
    7174#include <tl_local.h>
    7275#include <tl_interface.h>
    7376
    74 #include "tcp.h"
    75 #include "tcp_header.h"
    76 #include "tcp_module.h"
    77 
    78 /** TCP module name.
     77/** TCP module name. */
     78#define NAME    "TCP protocol"
     79
     80/** The TCP window default value. */
     81#define NET_DEFAULT_TCP_WINDOW          10240
     82
     83/** Initial timeout for new connections. */
     84#define NET_DEFAULT_TCP_INITIAL_TIMEOUT 3000000L
     85
     86/** Default timeout for closing. */
     87#define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L
     88
     89/** The initial outgoing sequence number. */
     90#define TCP_INITIAL_SEQUENCE_NUMBER     2999
     91
     92/** Maximum TCP fragment size. */
     93#define MAX_TCP_FRAGMENT_SIZE           65535
     94
     95/** Free ports pool start. */
     96#define TCP_FREE_PORTS_START            1025
     97
     98/** Free ports pool end. */
     99#define TCP_FREE_PORTS_END              65535
     100
     101/** Timeout for connection initialization, SYN sent. */
     102#define TCP_SYN_SENT_TIMEOUT            1000000L
     103
     104/** The maximum number of timeouts in a row before singaling connection lost. */
     105#define TCP_MAX_TIMEOUTS                8
     106
     107/** The number of acknowledgements before retransmit. */
     108#define TCP_FAST_RETRANSMIT_COUNT       3
     109
     110/** Returns a value indicating whether the value is in the interval respecting
     111 * the possible overflow.
     112 *
     113 * The high end and/or the value may overflow, be lower than the low value.
     114 *
     115 * @param[in] lower     The last value before the interval.
     116 * @param[in] value     The value to be checked.
     117 * @param[in] higher_equal The last value in the interval.
    79118 */
    80 #define NAME    "TCP protocol"
    81 
    82 /** The TCP window default value.
    83  */
    84 #define NET_DEFAULT_TCP_WINDOW  10240
    85 
    86 /** Initial timeout for new connections.
    87  */
    88 #define NET_DEFAULT_TCP_INITIAL_TIMEOUT 3000000L
    89 
    90 /** Default timeout for closing.
    91  */
    92 #define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT       2000L
    93 
    94 /** The initial outgoing sequence number.
    95  */
    96 #define TCP_INITIAL_SEQUENCE_NUMBER             2999
    97 
    98 /** Maximum TCP fragment size.
    99  */
    100 #define MAX_TCP_FRAGMENT_SIZE   65535
    101 
    102 /** Free ports pool start.
    103  */
    104 #define TCP_FREE_PORTS_START    1025
    105 
    106 /** Free ports pool end.
    107  */
    108 #define TCP_FREE_PORTS_END              65535
    109 
    110 /** Timeout for connection initialization, SYN sent.
    111  */
    112 #define TCP_SYN_SENT_TIMEOUT    1000000L
    113 
    114 /** The maximum number of timeouts in a row before singaling connection lost.
    115  */
    116 #define TCP_MAX_TIMEOUTS                8
    117 
    118 /** The number of acknowledgements before retransmit.
    119  */
    120 #define TCP_FAST_RETRANSMIT_COUNT       3
    121 
    122 /** Returns a value indicating whether the value is in the interval respecting the possible overflow.
    123  *  The high end and/or the value may overflow, be lower than the low value.
    124  *  @param[in] lower The last value before the interval.
    125  *  @param[in] value The value to be checked.
    126  *  @param[in] higher_equal The last value in the interval.
    127  */
    128 #define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal)     ((((lower) < (value)) && (((value) <= (higher_equal)) || ((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && ((higher_equal) < (lower))))
     119#define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal) \
     120        ((((lower) < (value)) && (((value) <= (higher_equal)) || \
     121        ((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && \
     122        ((higher_equal) < (lower))))
    129123
    130124/** Type definition of the TCP timeout.
    131125 *  @see tcp_timeout
    132126 */
    133 typedef struct tcp_timeout      tcp_timeout_t;
    134 
    135 /** Type definition of the TCP timeout pointer.
    136  *  @see tcp_timeout
    137  */
    138 typedef tcp_timeout_t * tcp_timeout_ref;
     127typedef struct tcp_timeout tcp_timeout_t;
    139128
    140129/** TCP reply timeout data.
     
    142131 *  @see tcp_timeout()
    143132 */
    144 struct tcp_timeout{
    145         /** TCP global data are going to be read only.
    146          */
     133struct tcp_timeout {
     134        /** TCP global data are going to be read only. */
    147135        int globals_read_only;
    148         /** Socket port.
    149         */
     136
     137        /** Socket port. */
    150138        int port;
    151         /** Local sockets.
    152         */
    153         socket_cores_ref local_sockets;
    154         /** Socket identifier.
    155         */
     139
     140        /** Local sockets. */
     141        socket_cores_t *local_sockets;
     142
     143        /** Socket identifier. */
    156144        int socket_id;
    157         /** Socket state.
    158         */
     145
     146        /** Socket state. */
    159147        tcp_socket_state_t state;
    160         /** Sent packet sequence number.
    161         */
     148
     149        /** Sent packet sequence number. */
    162150        int sequence_number;
    163         /** Timeout in microseconds.
    164         */
     151
     152        /** Timeout in microseconds. */
    165153        suseconds_t timeout;
    166         /** Port map key.
    167         */
    168         char * key;
    169         /** Port map key length.
    170         */
     154
     155        /** Port map key. */
     156        char *key;
     157
     158        /** Port map key length. */
    171159        size_t key_length;
    172160};
    173161
    174 /** Releases the packet and returns the result.
    175  *  @param[in] packet The packet queue to be released.
    176  *  @param[in] result The result to be returned.
    177  *  @return The result parameter.
     162static int tcp_release_and_return(packet_t *, int);
     163static void tcp_prepare_operation_header(socket_core_t *, tcp_socket_data_t *,
     164    tcp_header_t *, int synchronize, int);
     165static int tcp_prepare_timeout(int (*)(void *), socket_core_t *,
     166    tcp_socket_data_t *, size_t, tcp_socket_state_t, suseconds_t, int);
     167static void tcp_free_socket_data(socket_core_t *);
     168
     169static int tcp_timeout(void *);
     170
     171static int tcp_release_after_timeout(void *);
     172
     173static int tcp_process_packet(device_id_t, packet_t *, services_t);
     174static int tcp_connect_core(socket_core_t *, socket_cores_t *,
     175    struct sockaddr *, socklen_t);
     176static int tcp_queue_prepare_packet(socket_core_t *, tcp_socket_data_t *,
     177    packet_t *, size_t);
     178static int tcp_queue_packet(socket_core_t *, tcp_socket_data_t *, packet_t *,
     179    size_t);
     180static packet_t *tcp_get_packets_to_send(socket_core_t *, tcp_socket_data_t *);
     181static void tcp_send_packets(device_id_t, packet_t *);
     182
     183static void tcp_process_acknowledgement(socket_core_t *, tcp_socket_data_t *,
     184    tcp_header_t *);
     185static packet_t *tcp_send_prepare_packet(socket_core_t *, tcp_socket_data_t *,
     186    packet_t *, size_t, size_t);
     187static packet_t *tcp_prepare_copy(socket_core_t *, tcp_socket_data_t *,
     188    packet_t *, size_t, size_t);
     189/* static */ void tcp_retransmit_packet(socket_core_t *, tcp_socket_data_t *,
     190    size_t);
     191static int tcp_create_notification_packet(packet_t **, socket_core_t *,
     192    tcp_socket_data_t *, int, int);
     193static void tcp_refresh_socket_data(tcp_socket_data_t *);
     194
     195static void tcp_initialize_socket_data(tcp_socket_data_t *);
     196
     197static int tcp_process_listen(socket_core_t *, tcp_socket_data_t *,
     198    tcp_header_t *, packet_t *, struct sockaddr *, struct sockaddr *, size_t);
     199static int tcp_process_syn_sent(socket_core_t *, tcp_socket_data_t *,
     200    tcp_header_t *, packet_t *);
     201static int tcp_process_syn_received(socket_core_t *, tcp_socket_data_t *,
     202    tcp_header_t *, packet_t *);
     203static int tcp_process_established(socket_core_t *, tcp_socket_data_t *,
     204    tcp_header_t *, packet_t *, int, size_t);
     205static int tcp_queue_received_packet(socket_core_t *, tcp_socket_data_t *,
     206    packet_t *, int, size_t);
     207
     208static int tcp_received_msg(device_id_t, packet_t *, services_t, services_t);
     209static int tcp_process_client_messages(ipc_callid_t, ipc_call_t);
     210
     211static int tcp_listen_message(socket_cores_t *, int, int);
     212static int tcp_connect_message(socket_cores_t *, int, struct sockaddr *,
     213    socklen_t);
     214static int tcp_recvfrom_message(socket_cores_t *, int, int, size_t *);
     215static int tcp_send_message(socket_cores_t *, int, int, size_t *, int);
     216static int tcp_accept_message(socket_cores_t *, int, int, size_t *, size_t *);
     217static int tcp_close_message(socket_cores_t *, int);
     218
     219/** TCP global data. */
     220tcp_globals_t tcp_globals;
     221
     222/** Initializes the TCP module.
     223 *
     224 * @param[in] client_connection The client connection processing function. The
     225 *                      module skeleton propagates its own one.
     226 * @return              EOK on success.
     227 * @return              ENOMEM if there is not enough memory left.
    178228 */
    179 int tcp_release_and_return(packet_t packet, int result);
    180 
    181 void tcp_prepare_operation_header(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize);
    182 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);
    183 void tcp_free_socket_data(socket_core_ref socket);
    184 int tcp_timeout(void * data);
    185 int tcp_release_after_timeout(void * data);
    186 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error);
    187 int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen);
    188 int tcp_queue_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
    189 int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
    190 packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data);
    191 void tcp_send_packets(device_id_t device_id, packet_t packet);
    192 void tcp_process_acknowledgement(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header);
    193 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);
    194 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);
    195 void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number);
    196 int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize);
    197 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data);
    198 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data);
    199 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);
    200 int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
    201 int tcp_process_syn_received(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
    202 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);
    203 int tcp_queue_received_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length);
    204 
    205 int tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error);
    206 int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call);
    207 int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog);
    208 int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen);
    209 int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen);
    210 int tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags);
    211 int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen);
    212 int tcp_close_message(socket_cores_ref local_sockets, int socket_id);
    213 
    214 /** TCP global data.
    215  */
    216 tcp_globals_t   tcp_globals;
    217 
    218 int tcp_initialize(async_client_conn_t client_connection){
    219         ERROR_DECLARE;
     229int tcp_initialize(async_client_conn_t client_connection)
     230{
     231        int rc;
    220232
    221233        assert(client_connection);
     234
    222235        fibril_rwlock_initialize(&tcp_globals.lock);
    223236        fibril_rwlock_write_lock(&tcp_globals.lock);
    224         tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT);
    225         tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP, SERVICE_TCP, client_connection, tcp_received_msg);
    226         if(tcp_globals.ip_phone < 0){
     237
     238        tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
     239            ICMP_CONNECT_TIMEOUT);
     240        tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
     241            SERVICE_TCP, client_connection);
     242        if (tcp_globals.ip_phone < 0) {
     243                fibril_rwlock_write_unlock(&tcp_globals.lock);
    227244                return tcp_globals.ip_phone;
    228245        }
    229         ERROR_PROPAGATE(socket_ports_initialize(&tcp_globals.sockets));
    230         if(ERROR_OCCURRED(packet_dimensions_initialize(&tcp_globals.dimensions))){
     246       
     247        rc = socket_ports_initialize(&tcp_globals.sockets);
     248        if (rc != EOK)
     249                goto out;
     250
     251        rc = packet_dimensions_initialize(&tcp_globals.dimensions);
     252        if (rc != EOK) {
    231253                socket_ports_destroy(&tcp_globals.sockets);
    232                 return ERROR_CODE;
    233         }
     254                goto out;
     255        }
     256
    234257        tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
     258
     259out:
    235260        fibril_rwlock_write_unlock(&tcp_globals.lock);
    236         return EOK;
    237 }
    238 
    239 int tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error){
    240         ERROR_DECLARE;
    241 
    242         if(receiver != SERVICE_TCP){
     261        return rc;
     262}
     263
     264int tcp_received_msg(device_id_t device_id, packet_t *packet,
     265    services_t receiver, services_t error)
     266{
     267        int rc;
     268
     269        if (receiver != SERVICE_TCP)
    243270                return EREFUSED;
    244         }
     271
    245272        fibril_rwlock_write_lock(&tcp_globals.lock);
    246         if(ERROR_OCCURRED(tcp_process_packet(device_id, packet, error))){
     273        rc = tcp_process_packet(device_id, packet, error);
     274        if (rc != EOK)
    247275                fibril_rwlock_write_unlock(&tcp_globals.lock);
    248         }
    249         printf("receive %d \n", ERROR_CODE);
    250 
    251         return ERROR_CODE;
    252 }
    253 
    254 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error){
    255         ERROR_DECLARE;
    256 
     276
     277        printf("receive %d \n", rc);
     278
     279        return rc;
     280}
     281
     282int tcp_process_packet(device_id_t device_id, packet_t *packet, services_t error)
     283{
    257284        size_t length;
    258285        size_t offset;
    259286        int result;
    260         tcp_header_ref header;
    261         socket_core_ref  socket;
    262         tcp_socket_data_ref socket_data;
    263         packet_t next_packet;
     287        tcp_header_t *header;
     288        socket_core_t *socket;
     289        tcp_socket_data_t *socket_data;
     290        packet_t *next_packet;
    264291        size_t total_length;
    265292        uint32_t checksum;
     
    267294        icmp_type_t type;
    268295        icmp_code_t code;
    269         struct sockaddr * src;
    270         struct sockaddr * dest;
     296        struct sockaddr *src;
     297        struct sockaddr *dest;
    271298        size_t addrlen;
    272 
    273         printf("p1 \n");
    274         if(error){
    275                 switch(error){
    276                         case SERVICE_ICMP:
    277                                 // process error
    278                                 result = icmp_client_process_packet(packet, &type, &code, NULL, NULL);
    279                                 if(result < 0){
    280                                         return tcp_release_and_return(packet, result);
    281                                 }
    282                                 length = (size_t) result;
    283                                 if(ERROR_OCCURRED(packet_trim(packet, length, 0))){
    284                                         return tcp_release_and_return(packet, ERROR_CODE);
    285                                 }
    286                                 break;
    287                         default:
    288                                 return tcp_release_and_return(packet, ENOTSUP);
    289                 }
    290         }
    291 
    292         // TODO process received ipopts?
     299        int rc;
     300
     301        switch (error) {
     302        case SERVICE_NONE:
     303                break;
     304        case SERVICE_ICMP:
     305                /* Process error */
     306                result = icmp_client_process_packet(packet, &type, &code, NULL,
     307                    NULL);
     308                if (result < 0)
     309                        return tcp_release_and_return(packet, result);
     310
     311                length = (size_t) result;
     312                rc = packet_trim(packet, length, 0);
     313                if (rc != EOK)
     314                        return tcp_release_and_return(packet, rc);
     315                break;
     316        default:
     317                return tcp_release_and_return(packet, ENOTSUP);
     318        }
     319
     320        /* TODO process received ipopts? */
    293321        result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
    294 //      printf("ip len %d\n", result);
    295         if(result < 0){
     322        if (result < 0)
    296323                return tcp_release_and_return(packet, result);
    297         }
     324
    298325        offset = (size_t) result;
    299326
    300327        length = packet_get_data_length(packet);
    301 //      printf("packet len %d\n", length);
    302         if(length <= 0){
     328        if (length <= 0)
    303329                return tcp_release_and_return(packet, EINVAL);
    304         }
    305         if(length < TCP_HEADER_SIZE + offset){
     330
     331        if (length < TCP_HEADER_SIZE + offset)
    306332                return tcp_release_and_return(packet, NO_DATA);
    307         }
    308 
    309         // trim all but TCP header
    310         if(ERROR_OCCURRED(packet_trim(packet, offset, 0))){
    311                 return tcp_release_and_return(packet, ERROR_CODE);
    312         }
    313 
    314         // get tcp header
    315         header = (tcp_header_ref) packet_get_data(packet);
    316         if(! header){
     333
     334        /* Trim all but TCP header */
     335        rc = packet_trim(packet, offset, 0);
     336        if (rc != EOK)
     337                return tcp_release_and_return(packet, rc);
     338
     339        /* Get tcp header */
     340        header = (tcp_header_t *) packet_get_data(packet);
     341        if (!header)
    317342                return tcp_release_and_return(packet, NO_DATA);
    318         }
    319 //      printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header), ntohs(header->destination_port));
     343
     344//      printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header),
     345//          ntohs(header->destination_port));
    320346
    321347        result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
    322         if(result <= 0){
     348        if (result <= 0)
    323349                return tcp_release_and_return(packet, result);
    324         }
     350
    325351        addrlen = (size_t) result;
    326352
    327         if(ERROR_OCCURRED(tl_set_address_port(src, addrlen, ntohs(header->source_port)))){
    328                 return tcp_release_and_return(packet, ERROR_CODE);
    329         }
    330 
    331         // find the destination socket
    332         socket = socket_port_find(&tcp_globals.sockets, ntohs(header->destination_port), (const char *) src, addrlen);
    333         if(! socket){
    334 //              printf("listening?\n");
    335                 // find the listening destination socket
    336                 socket = socket_port_find(&tcp_globals.sockets, ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
    337                 if(! socket){
    338                         if(tl_prepare_icmp_packet(tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error) == EOK){
    339                                 icmp_destination_unreachable_msg(tcp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet);
    340                         }
    341                         return EADDRNOTAVAIL;
    342                 }
    343         }
     353        rc = tl_set_address_port(src, addrlen, ntohs(header->source_port));
     354        if (rc != EOK)
     355                return tcp_release_and_return(packet, rc);
     356       
     357        /* Find the destination socket */
     358        socket = socket_port_find(&tcp_globals.sockets,
     359            ntohs(header->destination_port), (const char *) src, addrlen);
     360        if (!socket) {
     361                /* Find the listening destination socket */
     362                socket = socket_port_find(&tcp_globals.sockets,
     363                    ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,
     364                    0);
     365        }
     366
     367        if (!socket) {
     368                if (tl_prepare_icmp_packet(tcp_globals.net_phone,
     369                    tcp_globals.icmp_phone, packet, error) == EOK) {
     370                        icmp_destination_unreachable_msg(tcp_globals.icmp_phone,
     371                            ICMP_PORT_UNREACH, 0, packet);
     372                }
     373                return EADDRNOTAVAIL;
     374        }
     375
    344376        printf("socket id %d\n", socket->socket_id);
    345         socket_data = (tcp_socket_data_ref) socket->specific_data;
     377        socket_data = (tcp_socket_data_t *) socket->specific_data;
    346378        assert(socket_data);
    347379
    348         // some data received, clear the timeout counter
     380        /* Some data received, clear the timeout counter */
    349381        socket_data->timeout_count = 0;
    350382
    351         // count the received packet fragments
     383        /* Count the received packet fragments */
    352384        next_packet = packet;
    353385        fragments = 0;
    354386        checksum = 0;
    355387        total_length = 0;
    356         do{
    357                 ++ fragments;
     388        do {
     389                fragments++;
    358390                length = packet_get_data_length(next_packet);
    359                 if(length <= 0){
     391                if (length <= 0)
    360392                        return tcp_release_and_return(packet, NO_DATA);
    361                 }
     393
    362394                total_length += length;
    363                 // add partial checksum if set
    364                 if(! error){
    365                         checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet));
    366                 }
    367         }while((next_packet = pq_next(next_packet)));
    368 //      printf("fragments %d of %d bytes\n", fragments, total_length);
    369 
    370 //      printf("lock?\n");
     395
     396                /* Add partial checksum if set */
     397                if (!error) {
     398                        checksum = compute_checksum(checksum,
     399                            packet_get_data(packet),
     400                            packet_get_data_length(packet));
     401                }
     402
     403        } while ((next_packet = pq_next(next_packet)));
     404
    371405        fibril_rwlock_write_lock(socket_data->local_lock);
    372 //      printf("locked\n");
    373         if(! error){
    374                 if(socket_data->state == TCP_SOCKET_LISTEN){
    375                         if(socket_data->pseudo_header){
    376                                 free(socket_data->pseudo_header);
    377                                 socket_data->pseudo_header = NULL;
    378                                 socket_data->headerlen = 0;
    379                         }
    380                         if(ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen, dest, addrlen, total_length, &socket_data->pseudo_header, &socket_data->headerlen))){
    381                                 fibril_rwlock_write_unlock(socket_data->local_lock);
    382                                 return tcp_release_and_return(packet, ERROR_CODE);
    383                         }
    384                 }else if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(socket_data->pseudo_header, socket_data->headerlen, total_length))){
     406
     407        if (error)
     408                goto has_error_service;
     409       
     410        if (socket_data->state == TCP_SOCKET_LISTEN) {
     411                if (socket_data->pseudo_header) {
     412                        free(socket_data->pseudo_header);
     413                        socket_data->pseudo_header = NULL;
     414                        socket_data->headerlen = 0;
     415                }
     416
     417                rc = ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen,
     418                    dest, addrlen, total_length, &socket_data->pseudo_header,
     419                    &socket_data->headerlen);
     420                if (rc != EOK) {
    385421                        fibril_rwlock_write_unlock(socket_data->local_lock);
    386                         return tcp_release_and_return(packet, ERROR_CODE);
    387                 }
    388                 checksum = compute_checksum(checksum, socket_data->pseudo_header, socket_data->headerlen);
    389                 if(flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO){
    390                         printf("checksum err %x -> %x\n", header->checksum, flip_checksum(compact_checksum(checksum)));
     422                        return tcp_release_and_return(packet, rc);
     423                }
     424        } else {
     425                rc = ip_client_set_pseudo_header_data_length(
     426                    socket_data->pseudo_header, socket_data->headerlen,
     427                    total_length);
     428                if (rc != EOK) {
    391429                        fibril_rwlock_write_unlock(socket_data->local_lock);
    392                         if(! ERROR_OCCURRED(tl_prepare_icmp_packet(tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error))){
    393                                 // checksum error ICMP
    394                                 icmp_parameter_problem_msg(tcp_globals.icmp_phone, ICMP_PARAM_POINTER, ((size_t) ((void *) &header->checksum)) - ((size_t) ((void *) header)), packet);
    395                         }
    396                         return EINVAL;
    397                 }
    398         }
    399 
     430                        return tcp_release_and_return(packet, rc);
     431                }
     432        }
     433       
     434        checksum = compute_checksum(checksum, socket_data->pseudo_header,
     435            socket_data->headerlen);
     436        if (flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO) {
     437                printf("checksum err %x -> %x\n", header->checksum,
     438                    flip_checksum(compact_checksum(checksum)));
     439                fibril_rwlock_write_unlock(socket_data->local_lock);
     440
     441                rc = tl_prepare_icmp_packet(tcp_globals.net_phone,
     442                    tcp_globals.icmp_phone, packet, error);
     443                if (rc == EOK) {
     444                        /* Checksum error ICMP */
     445                        icmp_parameter_problem_msg(tcp_globals.icmp_phone,
     446                            ICMP_PARAM_POINTER,
     447                            ((size_t) ((void *) &header->checksum)) -
     448                            ((size_t) ((void *) header)), packet);
     449                }
     450
     451                return EINVAL;
     452        }
     453
     454has_error_service:
    400455        fibril_rwlock_read_unlock(&tcp_globals.lock);
    401456
    402         // TODO error reporting/handling
    403 //      printf("st %d\n", socket_data->state);
    404         switch(socket_data->state){
    405                 case TCP_SOCKET_LISTEN:
    406                         ERROR_CODE = tcp_process_listen(socket, socket_data, header, packet, src, dest, addrlen);
    407                         break;
    408                 case TCP_SOCKET_SYN_RECEIVED:
    409                         ERROR_CODE = tcp_process_syn_received(socket, socket_data, header, packet);
    410                         break;
    411                 case TCP_SOCKET_SYN_SENT:
    412                         ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header, packet);
    413                         break;
    414                 case TCP_SOCKET_FIN_WAIT_1:
    415                         // ack changing the state to FIN_WAIT_2 gets processed later
    416                 case TCP_SOCKET_FIN_WAIT_2:
    417                         // fin changing state to LAST_ACK gets processed later
    418                 case TCP_SOCKET_LAST_ACK:
    419                         // ack releasing the socket get processed later
    420                 case TCP_SOCKET_CLOSING:
    421                         // ack releasing the socket gets processed later
    422                 case TCP_SOCKET_ESTABLISHED:
    423                         ERROR_CODE = tcp_process_established(socket, socket_data, header, packet, fragments, total_length);
    424                         break;
    425                 default:
    426                         pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    427         }
    428 
    429         if(ERROR_CODE != EOK){
    430                 printf("process %d\n", ERROR_CODE);
     457        /* TODO error reporting/handling */
     458        switch (socket_data->state) {
     459        case TCP_SOCKET_LISTEN:
     460                rc = tcp_process_listen(socket, socket_data, header, packet,
     461                    src, dest, addrlen);
     462                break;
     463        case TCP_SOCKET_SYN_RECEIVED:
     464                rc = tcp_process_syn_received(socket, socket_data, header,
     465                    packet);
     466                break;
     467        case TCP_SOCKET_SYN_SENT:
     468                rc = tcp_process_syn_sent(socket, socket_data, header, packet);
     469                break;
     470        case TCP_SOCKET_FIN_WAIT_1:
     471                /* ack changing the state to FIN_WAIT_2 gets processed later */
     472        case TCP_SOCKET_FIN_WAIT_2:
     473                /* fin changing state to LAST_ACK gets processed later */
     474        case TCP_SOCKET_LAST_ACK:
     475                /* ack releasing the socket get processed later */
     476        case TCP_SOCKET_CLOSING:
     477                /* ack releasing the socket gets processed later */
     478        case TCP_SOCKET_ESTABLISHED:
     479                rc = tcp_process_established(socket, socket_data, header,
     480                    packet, fragments, total_length);
     481                break;
     482        default:
     483                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     484        }
     485
     486        if (rc != EOK) {
    431487                fibril_rwlock_write_unlock(socket_data->local_lock);
    432         }
     488                printf("process %d\n", rc);
     489        }
     490
    433491        return EOK;
    434492}
    435493
    436 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){
    437         ERROR_DECLARE;
    438 
    439         packet_t next_packet;
    440         packet_t tmp_packet;
     494int tcp_process_established(socket_core_t *socket, tcp_socket_data_t *
     495    socket_data, tcp_header_t *header, packet_t *packet, int fragments,
     496    size_t total_length)
     497{
     498        packet_t *next_packet;
     499        packet_t *tmp_packet;
    441500        uint32_t old_incoming;
    442501        size_t order;
     
    445504        size_t offset;
    446505        uint32_t new_sequence_number;
     506        int rc;
    447507
    448508        assert(socket);
     
    455515        old_incoming = socket_data->next_incoming;
    456516
    457         if(header->finalize){
     517        if (header->finalize)
    458518                socket_data->fin_incoming = new_sequence_number;
    459         }
    460 
    461 //      printf("pe %d < %d <= %d\n", new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length);
    462         // trim begining if containing expected data
    463         if(IS_IN_INTERVAL_OVERFLOW(new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length)){
    464                 // get the acknowledged offset
    465                 if(socket_data->next_incoming < new_sequence_number){
    466                         offset = new_sequence_number - socket_data->next_incoming;
    467                 }else{
    468                         offset = socket_data->next_incoming - new_sequence_number;
    469                 }
    470 //              printf("offset %d\n", offset);
     519
     520        /* Trim begining if containing expected data */
     521        if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
     522            socket_data->next_incoming, new_sequence_number + total_length)) {
     523
     524                /* Get the acknowledged offset */
     525                if (socket_data->next_incoming < new_sequence_number) {
     526                        offset = new_sequence_number -
     527                            socket_data->next_incoming;
     528                } else {
     529                        offset = socket_data->next_incoming -
     530                            new_sequence_number;
     531                }
     532
    471533                new_sequence_number += offset;
    472534                total_length -= offset;
    473535                length = packet_get_data_length(packet);
    474                 // trim the acknowledged data
    475                 while(length <= offset){
    476                         // release the acknowledged packets
     536
     537                /* Trim the acknowledged data */
     538                while (length <= offset) {
     539                        /* Release the acknowledged packets */
    477540                        next_packet = pq_next(packet);
    478                         pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     541                        pq_release_remote(tcp_globals.net_phone,
     542                            packet_get_id(packet));
    479543                        packet = next_packet;
    480544                        offset -= length;
    481545                        length = packet_get_data_length(packet);
    482546                }
    483                 if((offset > 0)
    484                         && (ERROR_OCCURRED(packet_trim(packet, offset, 0)))){
    485                         return tcp_release_and_return(packet, ERROR_CODE);
    486                 }
     547
     548                if (offset > 0) {
     549                        rc = packet_trim(packet, offset, 0);
     550                        if (rc != EOK)
     551                                return tcp_release_and_return(packet, rc);
     552                }
     553
    487554                assert(new_sequence_number == socket_data->next_incoming);
    488555        }
    489556
    490         // release if overflowing the window
    491 //      if(IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming + socket_data->window, new_sequence_number, new_sequence_number + total_length)){
    492 //              return tcp_release_and_return(packet, EOVERFLOW);
    493 //      }
    494 
     557        /* Release if overflowing the window */
    495558/*
     559        if (IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming +
     560            socket_data->window, new_sequence_number, new_sequence_number +
     561            total_length)) {
     562                return tcp_release_and_return(packet, EOVERFLOW);
     563        }
     564
    496565        // trim end if overflowing the window
    497         if(IS_IN_INTERVAL_OVERFLOW(new_sequence_number, socket_data->next_incoming + socket_data->window, new_sequence_number + total_length)){
     566        if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
     567            socket_data->next_incoming + socket_data->window,
     568            new_sequence_number + total_length)) {
    498569                // get the allowed data length
    499                 if(socket_data->next_incoming + socket_data->window < new_sequence_number){
    500                         offset = new_sequence_number - socket_data->next_incoming + socket_data->window;
    501                 }else{
    502                         offset = socket_data->next_incoming + socket_data->window - new_sequence_number;
     570                if (socket_data->next_incoming + socket_data->window <
     571                    new_sequence_number) {
     572                        offset = new_sequence_number -
     573                            socket_data->next_incoming + socket_data->window;
     574                } else {
     575                        offset = socket_data->next_incoming +
     576                            socket_data->window - new_sequence_number;
    503577                }
    504578                next_packet = packet;
    505579                // trim the overflowing data
    506                 while(next_packet && (offset > 0)){
     580                while (next_packet && (offset > 0)) {
    507581                        length = packet_get_data_length(packet);
    508                         if(length <= offset){
     582                        if (length <= offset)
    509583                                next_packet = pq_next(next_packet);
    510                         }else if(ERROR_OCCURRED(packet_trim(next_packet, 0, length - offset))){
    511                                 return tcp_release_and_return(packet, ERROR_CODE);
     584                        else {
     585                                rc = packet_trim(next_packet, 0,
     586                                    length - offset));
     587                                if (rc != EOK)
     588                                        return tcp_release_and_return(packet,
     589                                            rc);
    512590                        }
    513591                        offset -= length;
     
    516594                // release the overflowing packets
    517595                next_packet = pq_next(next_packet);
    518                 if(next_packet){
     596                if (next_packet) {
    519597                        tmp_packet = next_packet;
    520598                        next_packet = pq_next(next_packet);
    521599                        pq_insert_after(tmp_packet, next_packet);
    522                         pq_release_remote(tcp_globals.net_phone, packet_get_id(tmp_packet));
    523                 }
    524                 assert(new_sequence_number + total_length == socket_data->next_incoming + socket_data->window);
     600                        pq_release_remote(tcp_globals.net_phone,
     601                            packet_get_id(tmp_packet));
     602                }
     603                assert(new_sequence_number + total_length ==
     604                    socket_data->next_incoming + socket_data->window);
    525605        }
    526606*/
    527         // the expected one arrived?
    528         if(new_sequence_number == socket_data->next_incoming){
     607        /* The expected one arrived? */
     608        if (new_sequence_number == socket_data->next_incoming) {
    529609                printf("expected\n");
    530                 // process acknowledgement
     610                /* Process acknowledgement */
    531611                tcp_process_acknowledgement(socket, socket_data, header);
    532612
    533                 // remove the header
     613                /* Remove the header */
    534614                total_length -= TCP_HEADER_LENGTH(header);
    535                 if(ERROR_OCCURRED(packet_trim(packet, TCP_HEADER_LENGTH(header), 0))){
    536                         return tcp_release_and_return(packet, ERROR_CODE);
    537                 }
    538 
    539                 if(total_length){
    540                         ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, fragments, total_length));
    541                 }else{
     615                rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
     616                if (rc != EOK)
     617                        return tcp_release_and_return(packet, rc);
     618
     619                if (total_length) {
     620                        rc = tcp_queue_received_packet(socket, socket_data,
     621                            packet, fragments, total_length);
     622                        if (rc != EOK)
     623                                return rc;
     624                } else {
    542625                        total_length = 1;
    543626                }
     627
    544628                socket_data->next_incoming = old_incoming + total_length;
    545629                packet = socket_data->incoming;
    546                 while(packet){
    547                         if(ERROR_OCCURRED(pq_get_order(socket_data->incoming, &order, NULL))){
    548                                 // remove the corrupted packet
     630                while (packet) {
     631                        rc = pq_get_order(socket_data->incoming, &order, NULL);
     632                        if (rc != EOK) {
     633                                /* Remove the corrupted packet */
    549634                                next_packet = pq_detach(packet);
    550                                 if(packet == socket_data->incoming){
     635                                if (packet == socket_data->incoming)
    551636                                        socket_data->incoming = next_packet;
    552                                 }
    553                                 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     637                                pq_release_remote(tcp_globals.net_phone,
     638                                    packet_get_id(packet));
    554639                                packet = next_packet;
    555640                                continue;
    556641                        }
     642
    557643                        sequence_number = (uint32_t) order;
    558                         if(IS_IN_INTERVAL_OVERFLOW(sequence_number, old_incoming, socket_data->next_incoming)){
    559                                 // move to the next
     644                        if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
     645                            old_incoming, socket_data->next_incoming)) {
     646                                /* Move to the next */
    560647                                packet = pq_next(packet);
    561                         // coninual data?
    562                         }else if(IS_IN_INTERVAL_OVERFLOW(old_incoming, sequence_number, socket_data->next_incoming)){
    563                                 // detach the packet
     648                                /* Coninual data? */
     649                        } else if (IS_IN_INTERVAL_OVERFLOW(old_incoming,
     650                            sequence_number, socket_data->next_incoming)) {
     651                                /* Detach the packet */
    564652                                next_packet = pq_detach(packet);
    565                                 if(packet == socket_data->incoming){
     653                                if (packet == socket_data->incoming)
    566654                                        socket_data->incoming = next_packet;
    567                                 }
    568                                 // get data length
     655                                /* Get data length */
    569656                                length = packet_get_data_length(packet);
    570657                                new_sequence_number = sequence_number + length;
    571                                 if(length <= 0){
    572                                         // remove the empty packet
    573                                         pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     658                                if (length <= 0) {
     659                                        /* Remove the empty packet */
     660                                        pq_release_remote(tcp_globals.net_phone,
     661                                            packet_get_id(packet));
    574662                                        packet = next_packet;
    575663                                        continue;
    576664                                }
    577                                 // exactly following
    578                                 if(sequence_number == socket_data->next_incoming){
    579                                         // queue received data
    580                                         ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, 1, packet_get_data_length(packet)));
    581                                         socket_data->next_incoming = new_sequence_number;
     665                                /* Exactly following */
     666                                if (sequence_number ==
     667                                    socket_data->next_incoming) {
     668                                        /* Queue received data */
     669                                        rc = tcp_queue_received_packet(socket,
     670                                            socket_data, packet, 1,
     671                                            packet_get_data_length(packet));
     672                                        if (rc != EOK)
     673                                                return rc;
     674                                        socket_data->next_incoming =
     675                                            new_sequence_number;
    582676                                        packet = next_packet;
    583677                                        continue;
    584                                 // at least partly following data?
    585                                 }else if(IS_IN_INTERVAL_OVERFLOW(sequence_number, socket_data->next_incoming, new_sequence_number)){
    586                                         if(socket_data->next_incoming < new_sequence_number){
    587                                                 length = new_sequence_number - socket_data->next_incoming;
    588                                         }else{
    589                                                 length = socket_data->next_incoming - new_sequence_number;
     678                                        /* At least partly following data? */
     679                                }
     680                                if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
     681                                    socket_data->next_incoming, new_sequence_number)) {
     682                                        if (socket_data->next_incoming <
     683                                            new_sequence_number) {
     684                                                length = new_sequence_number -
     685                                                    socket_data->next_incoming;
     686                                        } else {
     687                                                length =
     688                                                    socket_data->next_incoming -
     689                                                    new_sequence_number;
    590690                                        }
    591                                         if(! ERROR_OCCURRED(packet_trim(packet, length, 0))){
    592                                                 // queue received data
    593                                                 ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, 1, packet_get_data_length(packet)));
    594                                                 socket_data->next_incoming = new_sequence_number;
     691                                        rc = packet_trim(packet,length, 0);
     692                                        if (rc == EOK) {
     693                                                /* Queue received data */
     694                                                rc = tcp_queue_received_packet(
     695                                                    socket, socket_data, packet,
     696                                                    1, packet_get_data_length(
     697                                                    packet));
     698                                                if (rc != EOK)
     699                                                        return rc;
     700                                                socket_data->next_incoming =
     701                                                    new_sequence_number;
    595702                                                packet = next_packet;
    596703                                                continue;
    597704                                        }
    598705                                }
    599                                 // remove the duplicit or corrupted packet
    600                                 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     706                                /* Remove the duplicit or corrupted packet */
     707                                pq_release_remote(tcp_globals.net_phone,
     708                                    packet_get_id(packet));
    601709                                packet = next_packet;
    602710                                continue;
    603                         }else{
     711                        } else {
    604712                                break;
    605713                        }
    606714                }
    607         }else if(IS_IN_INTERVAL(socket_data->next_incoming, new_sequence_number, socket_data->next_incoming + socket_data->window)){
     715        } else if (IS_IN_INTERVAL(socket_data->next_incoming,
     716            new_sequence_number,
     717            socket_data->next_incoming + socket_data->window)) {
    608718                printf("in window\n");
    609                 // process acknowledgement
     719                /* Process acknowledgement */
    610720                tcp_process_acknowledgement(socket, socket_data, header);
    611721
    612                 // remove the header
     722                /* Remove the header */
    613723                total_length -= TCP_HEADER_LENGTH(header);
    614                 if(ERROR_OCCURRED(packet_trim(packet, TCP_HEADER_LENGTH(header), 0))){
    615                         return tcp_release_and_return(packet, ERROR_CODE);
    616                 }
     724                rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
     725                if (rc != EOK)
     726                        return tcp_release_and_return(packet, rc);
    617727
    618728                next_packet = pq_detach(packet);
    619729                length = packet_get_data_length(packet);
    620                 if(ERROR_OCCURRED(pq_add(&socket_data->incoming, packet, new_sequence_number, length))){
    621                         // remove the corrupted packets
    622                         pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    623                         pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
    624                 }else{
    625                         while(next_packet){
     730                rc = pq_add(&socket_data->incoming, packet, new_sequence_number,
     731                    length);
     732                if (rc != EOK) {
     733                        /* Remove the corrupted packets */
     734                        pq_release_remote(tcp_globals.net_phone,
     735                            packet_get_id(packet));
     736                        pq_release_remote(tcp_globals.net_phone,
     737                            packet_get_id(next_packet));
     738                } else {
     739                        while (next_packet) {
    626740                                new_sequence_number += length;
    627741                                tmp_packet = pq_detach(next_packet);
    628742                                length = packet_get_data_length(next_packet);
    629                                 if(ERROR_OCCURRED(pq_set_order(next_packet, new_sequence_number, length))
    630                                         || ERROR_OCCURRED(pq_insert_after(packet, next_packet))){
    631                                         pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
     743
     744                                rc = pq_set_order(next_packet,
     745                                    new_sequence_number, length);
     746                                if (rc != EOK) {
     747                                        pq_release_remote(tcp_globals.net_phone,
     748                                            packet_get_id(next_packet));
     749                                }
     750                                rc = pq_insert_after(packet, next_packet);
     751                                if (rc != EOK) {
     752                                        pq_release_remote(tcp_globals.net_phone,
     753                                            packet_get_id(next_packet));
    632754                                }
    633755                                next_packet = tmp_packet;
    634756                        }
    635757                }
    636         }else{
     758        } else {
    637759                printf("unexpected\n");
    638                 // release duplicite or restricted
     760                /* Release duplicite or restricted */
    639761                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    640762        }
    641763
    642         // change state according to the acknowledging incoming fin
    643         if(IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming, socket_data->next_incoming)){
    644                 switch(socket_data->state){
    645                         case TCP_SOCKET_FIN_WAIT_1:
    646                         case TCP_SOCKET_FIN_WAIT_2:
    647                         case TCP_SOCKET_CLOSING:
    648                                 socket_data->state = TCP_SOCKET_CLOSING;
    649                                 break;
    650                         //case TCP_ESTABLISHED:
    651                         default:
    652                                 socket_data->state = TCP_SOCKET_CLOSE_WAIT;
    653                                 break;
     764        /* Change state according to the acknowledging incoming fin */
     765        if (IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming,
     766            socket_data->next_incoming)) {
     767                switch (socket_data->state) {
     768                case TCP_SOCKET_FIN_WAIT_1:
     769                case TCP_SOCKET_FIN_WAIT_2:
     770                case TCP_SOCKET_CLOSING:
     771                        socket_data->state = TCP_SOCKET_CLOSING;
     772                        break;
     773                /*case TCP_ESTABLISHED:*/
     774                default:
     775                        socket_data->state = TCP_SOCKET_CLOSE_WAIT;
     776                        break;
    654777                }
    655778        }
    656779
    657780        packet = tcp_get_packets_to_send(socket, socket_data);
    658         if(! packet){
    659                 // create the notification packet
    660                 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 0));
    661                 ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet, 1));
    662                 packet = tcp_send_prepare_packet(socket, socket_data, packet, 1, socket_data->last_outgoing + 1);
    663         }
     781        if (!packet) {
     782                /* Create the notification packet */
     783                rc = tcp_create_notification_packet(&packet, socket,
     784                    socket_data, 0, 0);
     785                if (rc != EOK)
     786                        return rc;
     787                rc = tcp_queue_prepare_packet(socket, socket_data, packet, 1);
     788                if (rc != EOK)
     789                        return rc;
     790                packet = tcp_send_prepare_packet(socket, socket_data, packet, 1,
     791                    socket_data->last_outgoing + 1);
     792        }
     793
    664794        fibril_rwlock_write_unlock(socket_data->local_lock);
    665         // send the packet
     795
     796        /* Send the packet */
    666797        tcp_send_packets(socket_data->device_id, packet);
     798
    667799        return EOK;
    668800}
    669801
    670 int tcp_queue_received_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length){
    671         ERROR_DECLARE;
    672 
    673         packet_dimension_ref packet_dimension;
     802int tcp_queue_received_packet(socket_core_t *socket,
     803    tcp_socket_data_t *socket_data, packet_t *packet, int fragments,
     804    size_t total_length)
     805{
     806        packet_dimension_t *packet_dimension;
     807        int rc;
    674808
    675809        assert(socket);
     
    680814        assert(socket_data->window > total_length);
    681815
    682         // queue the received packet
    683         if(ERROR_OCCURRED(dyn_fifo_push(&socket->received, packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE))
    684             || ERROR_OCCURRED(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension))){
    685                 return tcp_release_and_return(packet, ERROR_CODE);
    686         }
    687 
    688         // decrease the window size
     816        /* Queue the received packet */
     817        rc = dyn_fifo_push(&socket->received, packet_get_id(packet),
     818            SOCKET_MAX_RECEIVED_SIZE);
     819        if (rc != EOK)
     820                return tcp_release_and_return(packet, rc);
     821        rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
     822            &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
     823        if (rc != EOK)
     824                return tcp_release_and_return(packet, rc);
     825
     826        /* Decrease the window size */
    689827        socket_data->window -= total_length;
    690828
    691         // notify the destination socket
    692         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);
     829        /* Notify the destination socket */
     830        async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
     831            (ipcarg_t) socket->socket_id,
     832            ((packet_dimension->content < socket_data->data_fragment_size) ?
     833            packet_dimension->content : socket_data->data_fragment_size), 0, 0,
     834            (ipcarg_t) fragments);
     835
    693836        return EOK;
    694837}
    695838
    696 int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet){
    697         ERROR_DECLARE;
    698 
    699         packet_t next_packet;
     839int tcp_process_syn_sent(socket_core_t *socket, tcp_socket_data_t *
     840    socket_data, tcp_header_t *header, packet_t *packet)
     841{
     842        packet_t *next_packet;
     843        int rc;
    700844
    701845        assert(socket);
     
    705849        assert(packet);
    706850
    707         if(header->synchronize){
    708                 // process acknowledgement
    709                 tcp_process_acknowledgement(socket, socket_data, header);
    710 
    711                 socket_data->next_incoming = ntohl(header->sequence_number) + 1;
    712                 // release additional packets
    713                 next_packet = pq_detach(packet);
    714                 if(next_packet){
    715                         pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
    716                 }
    717                 // trim if longer than the header
    718                 if((packet_get_data_length(packet) > sizeof(*header))
    719                         && ERROR_OCCURRED(packet_trim(packet, 0, packet_get_data_length(packet) - sizeof(*header)))){
    720                         return tcp_release_and_return(packet, ERROR_CODE);
    721                 }
    722                 tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
    723                 fibril_mutex_lock(&socket_data->operation.mutex);
    724                 socket_data->operation.result = tcp_queue_packet(socket, socket_data, packet, 1);
    725                 if(socket_data->operation.result == EOK){
    726                         socket_data->state = TCP_SOCKET_ESTABLISHED;
    727                         packet = tcp_get_packets_to_send(socket, socket_data);
    728                         if(packet){
    729                                 fibril_rwlock_write_unlock(socket_data->local_lock);
    730                                 // send the packet
    731                                 tcp_send_packets(socket_data->device_id, packet);
    732                                 // signal the result
    733                                 fibril_condvar_signal(&socket_data->operation.condvar);
    734                                 fibril_mutex_unlock(&socket_data->operation.mutex);
    735                                 return EOK;
    736                         }
    737                 }
    738                 fibril_mutex_unlock(&socket_data->operation.mutex);
    739         }
     851        if (!header->synchronize)
     852                return tcp_release_and_return(packet, EINVAL);
     853       
     854        /* Process acknowledgement */
     855        tcp_process_acknowledgement(socket, socket_data, header);
     856
     857        socket_data->next_incoming = ntohl(header->sequence_number) + 1;
     858
     859        /* Release additional packets */
     860        next_packet = pq_detach(packet);
     861        if (next_packet) {
     862                pq_release_remote(tcp_globals.net_phone,
     863                    packet_get_id(next_packet));
     864        }
     865
     866        /* Trim if longer than the header */
     867        if (packet_get_data_length(packet) > sizeof(*header)) {
     868                rc = packet_trim(packet, 0,
     869                    packet_get_data_length(packet) - sizeof(*header));
     870                if (rc != EOK)
     871                        return tcp_release_and_return(packet, rc);
     872        }
     873
     874        tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
     875        fibril_mutex_lock(&socket_data->operation.mutex);
     876        socket_data->operation.result = tcp_queue_packet(socket, socket_data,
     877            packet, 1);
     878
     879        if (socket_data->operation.result == EOK) {
     880                socket_data->state = TCP_SOCKET_ESTABLISHED;
     881                packet = tcp_get_packets_to_send(socket, socket_data);
     882                if (packet) {
     883                        fibril_rwlock_write_unlock( socket_data->local_lock);
     884                        /* Send the packet */
     885                        tcp_send_packets(socket_data->device_id, packet);
     886                        /* Signal the result */
     887                        fibril_condvar_signal( &socket_data->operation.condvar);
     888                        fibril_mutex_unlock( &socket_data->operation.mutex);
     889                        return EOK;
     890                }
     891        }
     892
     893        fibril_mutex_unlock(&socket_data->operation.mutex);
    740894        return tcp_release_and_return(packet, EINVAL);
    741895}
    742896
    743 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){
    744         ERROR_DECLARE;
    745 
    746         packet_t next_packet;
    747         socket_core_ref socket;
    748         tcp_socket_data_ref socket_data;
     897int tcp_process_listen(socket_core_t *listening_socket,
     898    tcp_socket_data_t *listening_socket_data, tcp_header_t *header,
     899    packet_t *packet, struct sockaddr *src, struct sockaddr *dest,
     900    size_t addrlen)
     901{
     902        packet_t *next_packet;
     903        socket_core_t *socket;
     904        tcp_socket_data_t *socket_data;
    749905        int socket_id;
    750906        int listening_socket_id = listening_socket->socket_id;
    751907        int listening_port = listening_socket->port;
     908        int rc;
    752909
    753910        assert(listening_socket);
     
    757914        assert(packet);
    758915
    759 //      printf("syn %d\n", header->synchronize);
    760         if(header->synchronize){
    761                 socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
    762                 if(! socket_data){
    763                         return tcp_release_and_return(packet, ENOMEM);
    764                 }else{
    765                         tcp_initialize_socket_data(socket_data);
    766                         socket_data->local_lock = listening_socket_data->local_lock;
    767                         socket_data->local_sockets = listening_socket_data->local_sockets;
    768                         socket_data->listening_socket_id = listening_socket->socket_id;
    769 
    770                         socket_data->next_incoming = ntohl(header->sequence_number);
    771                         socket_data->treshold = socket_data->next_incoming + ntohs(header->window);
    772 
    773                         socket_data->addrlen = addrlen;
    774                         socket_data->addr = malloc(socket_data->addrlen);
    775                         if(! socket_data->addr){
    776                                 free(socket_data);
    777                                 return tcp_release_and_return(packet, ENOMEM);
    778                         }
    779                         memcpy(socket_data->addr, src, socket_data->addrlen);
    780 
    781                         socket_data->dest_port = ntohs(header->source_port);
    782                         if(ERROR_OCCURRED(tl_set_address_port(socket_data->addr, socket_data->addrlen, socket_data->dest_port))){
    783                                 free(socket_data->addr);
    784                                 free(socket_data);
    785                                 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    786                                 return ERROR_CODE;
    787                         }
    788 
    789 //                      printf("addr %p\n", socket_data->addr, socket_data->addrlen);
    790                         // create a socket
    791                         socket_id = -1;
    792                         if(ERROR_OCCURRED(socket_create(socket_data->local_sockets, listening_socket->phone, socket_data, &socket_id))){
    793                                 free(socket_data->addr);
    794                                 free(socket_data);
    795                                 return tcp_release_and_return(packet, ERROR_CODE);
    796                         }
    797 
    798                         printf("new_sock %d\n", socket_id);
    799                         socket_data->pseudo_header = listening_socket_data->pseudo_header;
    800                         socket_data->headerlen = listening_socket_data->headerlen;
    801                         listening_socket_data->pseudo_header = NULL;
    802                         listening_socket_data->headerlen = 0;
    803 
    804                         fibril_rwlock_write_unlock(socket_data->local_lock);
    805 //                      printf("list lg\n");
    806                         fibril_rwlock_write_lock(&tcp_globals.lock);
    807 //                      printf("list locked\n");
    808                         // find the destination socket
    809                         listening_socket = socket_port_find(&tcp_globals.sockets, listening_port, SOCKET_MAP_KEY_LISTENING, 0);
    810                         if((! listening_socket) || (listening_socket->socket_id != listening_socket_id)){
    811                                 fibril_rwlock_write_unlock(&tcp_globals.lock);
    812                                 // a shadow may remain until app hangs up
    813                                 return tcp_release_and_return(packet, EOK/*ENOTSOCK*/);
    814                         }
    815 //                      printf("port %d\n", listening_socket->port);
    816                         listening_socket_data = (tcp_socket_data_ref) listening_socket->specific_data;
    817                         assert(listening_socket_data);
    818 
    819 //                      printf("list ll\n");
    820                         fibril_rwlock_write_lock(listening_socket_data->local_lock);
    821 //                      printf("list locked\n");
    822 
    823                         socket = socket_cores_find(listening_socket_data->local_sockets, socket_id);
    824                         if(! socket){
    825                                 // where is the socket?!?
    826                                 fibril_rwlock_write_unlock(&tcp_globals.lock);
    827                                 return ENOTSOCK;
    828                         }
    829                         socket_data = (tcp_socket_data_ref) socket->specific_data;
    830                         assert(socket_data);
    831 
    832 //                      uint8_t * data = socket_data->addr;
    833 //                      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]);
    834 
    835                         ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port, socket, (const char *) socket_data->addr, socket_data->addrlen);
    836                         assert(socket == socket_port_find(&tcp_globals.sockets, listening_port, (const char *) socket_data->addr, socket_data->addrlen));
    837                         //ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port);
    838                         //tcp_globals.last_used_port = socket->port;
    839 //                      printf("bound %d\n", socket->port);
    840                         fibril_rwlock_write_unlock(&tcp_globals.lock);
    841                         if(ERROR_CODE != EOK){
    842                                 socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
    843                                 return tcp_release_and_return(packet, ERROR_CODE);
    844                         }
    845 
    846                         socket_data->state = TCP_SOCKET_LISTEN;
    847                         socket_data->next_incoming = ntohl(header->sequence_number) + 1;
    848                         // release additional packets
    849                         next_packet = pq_detach(packet);
    850                         if(next_packet){
    851                                 pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
    852                         }
    853                         // trim if longer than the header
    854                         if((packet_get_data_length(packet) > sizeof(*header))
    855                                 && ERROR_OCCURRED(packet_trim(packet, 0, packet_get_data_length(packet) - sizeof(*header)))){
    856                                 socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
    857                                 return tcp_release_and_return(packet, ERROR_CODE);
    858                         }
    859                         tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
    860                         if(ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))){
    861                                 socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
    862                                 return ERROR_CODE;
    863                         }
    864                         packet = tcp_get_packets_to_send(socket, socket_data);
    865 //                      printf("send %d\n", packet_get_id(packet));
    866                         if(! packet){
    867                                 socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
    868                                 return EINVAL;
    869                         }else{
    870                                 socket_data->state = TCP_SOCKET_SYN_RECEIVED;
    871 //                              printf("unlock\n");
    872                                 fibril_rwlock_write_unlock(socket_data->local_lock);
    873                                 // send the packet
    874                                 tcp_send_packets(socket_data->device_id, packet);
    875                                 return EOK;
    876                         }
    877                 }
    878         }
    879         return tcp_release_and_return(packet, EINVAL);
    880 }
    881 
    882 int tcp_process_syn_received(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet){
    883         ERROR_DECLARE;
    884 
    885         socket_core_ref listening_socket;
    886         tcp_socket_data_ref listening_socket_data;
     916        if (!header->synchronize)
     917                return tcp_release_and_return(packet, EINVAL);
     918
     919        socket_data = (tcp_socket_data_t *) malloc(sizeof(*socket_data));
     920        if (!socket_data)
     921                return tcp_release_and_return(packet, ENOMEM);
     922
     923        tcp_initialize_socket_data(socket_data);
     924        socket_data->local_lock = listening_socket_data->local_lock;
     925        socket_data->local_sockets = listening_socket_data->local_sockets;
     926        socket_data->listening_socket_id = listening_socket->socket_id;
     927        socket_data->next_incoming = ntohl(header->sequence_number);
     928        socket_data->treshold = socket_data->next_incoming +
     929            ntohs(header->window);
     930        socket_data->addrlen = addrlen;
     931        socket_data->addr = malloc(socket_data->addrlen);
     932        if (!socket_data->addr) {
     933                free(socket_data);
     934                return tcp_release_and_return(packet, ENOMEM);
     935        }
     936
     937        memcpy(socket_data->addr, src, socket_data->addrlen);
     938        socket_data->dest_port = ntohs(header->source_port);
     939        rc = tl_set_address_port(socket_data->addr, socket_data->addrlen,
     940            socket_data->dest_port);
     941        if (rc != EOK) {
     942                free(socket_data->addr);
     943                free(socket_data);
     944                return tcp_release_and_return(packet, rc);
     945        }
     946
     947        /* Create a socket */
     948        socket_id = -1;
     949        rc = socket_create(socket_data->local_sockets, listening_socket->phone,
     950            socket_data, &socket_id);
     951        if (rc != EOK) {
     952                free(socket_data->addr);
     953                free(socket_data);
     954                return tcp_release_and_return(packet, rc);
     955        }
     956
     957        printf("new_sock %d\n", socket_id);
     958        socket_data->pseudo_header = listening_socket_data->pseudo_header;
     959        socket_data->headerlen = listening_socket_data->headerlen;
     960        listening_socket_data->pseudo_header = NULL;
     961        listening_socket_data->headerlen = 0;
     962
     963        fibril_rwlock_write_unlock(socket_data->local_lock);
     964        fibril_rwlock_write_lock(&tcp_globals.lock);
     965
     966        /* Find the destination socket */
     967        listening_socket = socket_port_find(&tcp_globals.sockets,
     968            listening_port, SOCKET_MAP_KEY_LISTENING, 0);
     969        if (!listening_socket ||
     970            (listening_socket->socket_id != listening_socket_id)) {
     971                fibril_rwlock_write_unlock(&tcp_globals.lock);
     972                /* A shadow may remain until app hangs up */
     973                return tcp_release_and_return(packet, EOK /*ENOTSOCK*/);
     974        }
     975        listening_socket_data =
     976            (tcp_socket_data_t *) listening_socket->specific_data;
     977        assert(listening_socket_data);
     978
     979        fibril_rwlock_write_lock(listening_socket_data->local_lock);
     980
     981        socket = socket_cores_find(listening_socket_data->local_sockets,
     982            socket_id);
     983        if (!socket) {
     984                /* Where is the socket?!? */
     985                fibril_rwlock_write_unlock(&tcp_globals.lock);
     986                return ENOTSOCK;
     987        }
     988        socket_data = (tcp_socket_data_t *) socket->specific_data;
     989        assert(socket_data);
     990
     991        rc = socket_port_add(&tcp_globals.sockets, listening_port, socket,
     992            (const char *) socket_data->addr, socket_data->addrlen);
     993        assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
     994            (const char *) socket_data->addr, socket_data->addrlen));
     995
     996//      rc = socket_bind_free_port(&tcp_globals.sockets, socket,
     997//          TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
     998//          tcp_globals.last_used_port);
     999//      tcp_globals.last_used_port = socket->port;
     1000        fibril_rwlock_write_unlock(&tcp_globals.lock);
     1001        if (rc != EOK) {
     1002                socket_destroy(tcp_globals.net_phone, socket->socket_id,
     1003                    socket_data->local_sockets, &tcp_globals.sockets,
     1004                    tcp_free_socket_data);
     1005                return tcp_release_and_return(packet, rc);
     1006        }
     1007
     1008        socket_data->state = TCP_SOCKET_LISTEN;
     1009        socket_data->next_incoming = ntohl(header->sequence_number) + 1;
     1010
     1011        /* Release additional packets */
     1012        next_packet = pq_detach(packet);
     1013        if (next_packet) {
     1014                pq_release_remote(tcp_globals.net_phone,
     1015                    packet_get_id(next_packet));
     1016        }
     1017
     1018        /* Trim if longer than the header */
     1019        if (packet_get_data_length(packet) > sizeof(*header)) {
     1020                rc = packet_trim(packet, 0,
     1021                    packet_get_data_length(packet) - sizeof(*header));
     1022                if (rc != EOK) {
     1023                        socket_destroy(tcp_globals.net_phone, socket->socket_id,
     1024                            socket_data->local_sockets, &tcp_globals.sockets,
     1025                            tcp_free_socket_data);
     1026                        return tcp_release_and_return(packet, rc);
     1027                }
     1028        }
     1029
     1030        tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
     1031
     1032        rc = tcp_queue_packet(socket, socket_data, packet, 1);
     1033        if (rc != EOK) {
     1034                socket_destroy(tcp_globals.net_phone, socket->socket_id,
     1035                    socket_data->local_sockets, &tcp_globals.sockets,
     1036                    tcp_free_socket_data);
     1037                return rc;
     1038        }
     1039
     1040        packet = tcp_get_packets_to_send(socket, socket_data);
     1041        if (!packet) {
     1042                socket_destroy(tcp_globals.net_phone, socket->socket_id,
     1043                    socket_data->local_sockets, &tcp_globals.sockets,
     1044                    tcp_free_socket_data);
     1045                return EINVAL;
     1046        }
     1047
     1048        socket_data->state = TCP_SOCKET_SYN_RECEIVED;
     1049        fibril_rwlock_write_unlock(socket_data->local_lock);
     1050
     1051        /* Send the packet */
     1052        tcp_send_packets(socket_data->device_id, packet);
     1053
     1054        return EOK;
     1055}
     1056
     1057int tcp_process_syn_received(socket_core_t *socket,
     1058    tcp_socket_data_t *socket_data, tcp_header_t *header, packet_t *packet)
     1059{
     1060        socket_core_t *listening_socket;
     1061        tcp_socket_data_t *listening_socket_data;
     1062        int rc;
    8871063
    8881064        assert(socket);
     
    8921068        assert(packet);
    8931069
    894         printf("syn_rec\n");
    895         if(header->acknowledge){
    896                 // process acknowledgement
    897                 tcp_process_acknowledgement(socket, socket_data, header);
    898 
    899                 socket_data->next_incoming = ntohl(header->sequence_number);// + 1;
    900                 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    901                 socket_data->state = TCP_SOCKET_ESTABLISHED;
    902                 listening_socket = socket_cores_find(socket_data->local_sockets, socket_data->listening_socket_id);
    903                 if(listening_socket){
    904                         listening_socket_data = (tcp_socket_data_ref) listening_socket->specific_data;
    905                         assert(listening_socket_data);
    906 
    907                         // queue the received packet
    908                         if(! ERROR_OCCURRED(dyn_fifo_push(&listening_socket->accepted, (-1 * socket->socket_id), listening_socket_data->backlog))){
    909                                 // notify the destination socket
    910                                 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);
    911                                 fibril_rwlock_write_unlock(socket_data->local_lock);
    912                                 return EOK;
    913                         }
    914                 }
    915                 // send FIN
    916                 socket_data->state = TCP_SOCKET_FIN_WAIT_1;
    917 
    918                 // create the notification packet
    919                 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 1));
    920 
    921                 // send the packet
    922                 ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
    923 
    924                 // flush packets
    925                 packet = tcp_get_packets_to_send(socket, socket_data);
    926                 fibril_rwlock_write_unlock(socket_data->local_lock);
    927                 if(packet){
    928                         // send the packet
    929                         tcp_send_packets(socket_data->device_id, packet);
    930                 }
    931                 return EOK;
    932         }else{
     1070        if (!header->acknowledge)
    9331071                return tcp_release_and_return(packet, EINVAL);
    934         }
    935         return EINVAL;
    936 }
    937 
    938 void tcp_process_acknowledgement(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header){
     1072
     1073        /* Process acknowledgement */
     1074        tcp_process_acknowledgement(socket, socket_data, header);
     1075
     1076        socket_data->next_incoming = ntohl(header->sequence_number);    // + 1;
     1077        pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     1078        socket_data->state = TCP_SOCKET_ESTABLISHED;
     1079        listening_socket = socket_cores_find(socket_data->local_sockets,
     1080            socket_data->listening_socket_id);
     1081        if (listening_socket) {
     1082                listening_socket_data =
     1083                    (tcp_socket_data_t *) listening_socket->specific_data;
     1084                assert(listening_socket_data);
     1085
     1086                /* Queue the received packet */
     1087                rc = dyn_fifo_push(&listening_socket->accepted,
     1088                    (-1 * socket->socket_id), listening_socket_data->backlog);
     1089                if (rc == EOK) {
     1090                        /* Notify the destination socket */
     1091                        async_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
     1092                            (ipcarg_t) listening_socket->socket_id,
     1093                            socket_data->data_fragment_size, TCP_HEADER_SIZE,
     1094                            0, (ipcarg_t) socket->socket_id);
     1095
     1096                        fibril_rwlock_write_unlock(socket_data->local_lock);
     1097                        return EOK;
     1098                }
     1099        }
     1100        /* Send FIN */
     1101        socket_data->state = TCP_SOCKET_FIN_WAIT_1;
     1102
     1103        /* Create the notification packet */
     1104        rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
     1105        if (rc != EOK)
     1106                return rc;
     1107
     1108        /* Send the packet */
     1109        rc = tcp_queue_packet(socket, socket_data, packet, 1);
     1110        if (rc != EOK)
     1111                return rc;
     1112
     1113        /* Flush packets */
     1114        packet = tcp_get_packets_to_send(socket, socket_data);
     1115        fibril_rwlock_write_unlock(socket_data->local_lock);
     1116        if (packet) {
     1117                /* Send the packet */
     1118                tcp_send_packets(socket_data->device_id, packet);
     1119        }
     1120
     1121        return EOK;
     1122}
     1123
     1124void tcp_process_acknowledgement(socket_core_t *socket,
     1125    tcp_socket_data_t *socket_data, tcp_header_t *header)
     1126{
    9391127        size_t number;
    9401128        size_t length;
    941         packet_t packet;
    942         packet_t next;
    943         packet_t acknowledged = NULL;
     1129        packet_t *packet;
     1130        packet_t *next;
     1131        packet_t *acknowledged = NULL;
    9441132        uint32_t old;
    9451133
     
    9491137        assert(header);
    9501138
    951         if(header->acknowledge){
    952                 number = ntohl(header->acknowledgement_number);
    953                 // if more data acknowledged
    954                 if(number != socket_data->expected){
    955                         old = socket_data->expected;
    956                         if(IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing, number)){
    957                                 switch(socket_data->state){
    958                                         case TCP_SOCKET_FIN_WAIT_1:
    959                                                 socket_data->state = TCP_SOCKET_FIN_WAIT_2;
    960                                                 break;
    961                                         case TCP_SOCKET_LAST_ACK:
    962                                         case TCP_SOCKET_CLOSING:
    963                                                 // fin acknowledged - release the socket in another fibril
    964                                                 tcp_prepare_timeout(tcp_release_after_timeout, socket, socket_data, 0, TCP_SOCKET_TIME_WAIT, NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true);
    965                                                 break;
    966                                         default:
    967                                                 break;
    968                                 }
     1139        if (!header->acknowledge)
     1140                return;
     1141
     1142        number = ntohl(header->acknowledgement_number);
     1143
     1144        /* If more data acknowledged */
     1145        if (number != socket_data->expected) {
     1146                old = socket_data->expected;
     1147                if (IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing,
     1148                    number)) {
     1149                        switch (socket_data->state) {
     1150                        case TCP_SOCKET_FIN_WAIT_1:
     1151                                socket_data->state = TCP_SOCKET_FIN_WAIT_2;
     1152                                break;
     1153                        case TCP_SOCKET_LAST_ACK:
     1154                        case TCP_SOCKET_CLOSING:
     1155                                /*
     1156                                 * FIN acknowledged - release the socket in
     1157                                 * another fibril.
     1158                                 */
     1159                                tcp_prepare_timeout(tcp_release_after_timeout,
     1160                                    socket, socket_data, 0,
     1161                                    TCP_SOCKET_TIME_WAIT,
     1162                                    NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true);
     1163                                break;
     1164                        default:
     1165                                break;
    9691166                        }
    970                         // update the treshold if higher than set
    971                         if(number + ntohs(header->window) > socket_data->expected + socket_data->treshold){
    972                                 socket_data->treshold = number + ntohs(header->window) - socket_data->expected;
    973                         }
    974                         // set new expected sequence number
    975                         socket_data->expected = number;
     1167                }
     1168
     1169                /* Update the treshold if higher than set */
     1170                if (number + ntohs(header->window) >
     1171                    socket_data->expected + socket_data->treshold) {
     1172                        socket_data->treshold = number + ntohs(header->window) -
     1173                            socket_data->expected;
     1174                }
     1175
     1176                /* Set new expected sequence number */
     1177                socket_data->expected = number;
     1178                socket_data->expected_count = 1;
     1179                packet = socket_data->outgoing;
     1180                while (pq_get_order(packet, &number, &length) == EOK) {
     1181                        if (IS_IN_INTERVAL_OVERFLOW((uint32_t) old,
     1182                            (uint32_t) (number + length),
     1183                            (uint32_t) socket_data->expected)) {
     1184                                next = pq_detach(packet);
     1185                                if (packet == socket_data->outgoing)
     1186                                        socket_data->outgoing = next;
     1187
     1188                                /* Add to acknowledged or release */
     1189                                if (pq_add(&acknowledged, packet, 0, 0) != EOK)
     1190                                        pq_release_remote(tcp_globals.net_phone,
     1191                                            packet_get_id(packet));
     1192                                packet = next;
     1193                        } else if (old < socket_data->expected)
     1194                                break;
     1195                }
     1196
     1197                /* Release acknowledged */
     1198                if (acknowledged) {
     1199                        pq_release_remote(tcp_globals.net_phone,
     1200                            packet_get_id(acknowledged));
     1201                }
     1202                return;
     1203                /* If the same as the previous time */
     1204        }
     1205
     1206        if (number == socket_data->expected) {
     1207                /* Increase the counter */
     1208                socket_data->expected_count++;
     1209                if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) {
    9761210                        socket_data->expected_count = 1;
    977                         packet = socket_data->outgoing;
    978                         while(pq_get_order(packet, &number, &length) == EOK){
    979                                 if(IS_IN_INTERVAL_OVERFLOW((uint32_t) old, (uint32_t)(number + length), (uint32_t) socket_data->expected)){
    980                                         next = pq_detach(packet);
    981                                         if(packet == socket_data->outgoing){
    982                                                 socket_data->outgoing = next;
    983                                         }
    984                                         // add to acknowledged or release
    985                                         if(pq_add(&acknowledged, packet, 0, 0) != EOK){
    986                                                 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    987                                         }
    988                                         packet = next;
    989                                 }else if(old < socket_data->expected){
    990                                         break;
    991                                 }
    992                         }
    993                         // release acknowledged
    994                         if(acknowledged){
    995                                 pq_release_remote(tcp_globals.net_phone, packet_get_id(acknowledged));
    996                         }
    997                         return;
    998                 // if the same as the previous time
    999                 }else if(number == socket_data->expected){
    1000                         // increase the counter
    1001                         ++ socket_data->expected_count;
    1002                         if(socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT){
    1003                                 socket_data->expected_count = 1;
    1004                                 // TODO retransmit lock
    1005                                 //tcp_retransmit_packet(socket, socket_data, number);
    1006                         }
    1007                 }
    1008         }
    1009 }
    1010 
    1011 int tcp_message_standalone(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
    1012         ERROR_DECLARE;
    1013 
    1014         packet_t packet;
     1211                        /* TODO retransmit lock */
     1212                        //tcp_retransmit_packet(socket, socket_data, number);
     1213                }
     1214        }
     1215}
     1216
     1217/** Processes the TCP message.
     1218 *
     1219 * @param[in] callid    The message identifier.
     1220 * @param[in] call      The message parameters.
     1221 * @param[out] answer   The message answer parameters.
     1222 * @param[out] answer_count The last parameter for the actual answer in the
     1223 *                      answer parameter.
     1224 * @return              EOK on success.
     1225 * @return              ENOTSUP if the message is not known.
     1226 *
     1227 * @see tcp_interface.h
     1228 * @see IS_NET_TCP_MESSAGE()
     1229 */
     1230int
     1231tcp_message_standalone(ipc_callid_t callid, ipc_call_t *call,
     1232    ipc_call_t *answer, int *answer_count)
     1233{
     1234        packet_t *packet;
     1235        int rc;
    10151236
    10161237        assert(call);
     
    10191240
    10201241        *answer_count = 0;
    1021         switch(IPC_GET_METHOD(*call)){
    1022                 case NET_TL_RECEIVED:
    1023                         //fibril_rwlock_read_lock(&tcp_globals.lock);
    1024                         if(! ERROR_OCCURRED(packet_translate_remote(tcp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
    1025                                 ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP, IPC_GET_ERROR(call));
    1026                         }
    1027                         //fibril_rwlock_read_unlock(&tcp_globals.lock);
    1028                         return ERROR_CODE;
    1029                 case IPC_M_CONNECT_TO_ME:
    1030                         return tcp_process_client_messages(callid, * call);
    1031         }
     1242        switch (IPC_GET_METHOD(*call)) {
     1243        case NET_TL_RECEIVED:
     1244//              fibril_rwlock_read_lock(&tcp_globals.lock);
     1245                rc = packet_translate_remote(tcp_globals.net_phone, &packet,
     1246                    IPC_GET_PACKET(call));
     1247                if (rc != EOK) {
     1248//                      fibril_rwlock_read_unlock(&tcp_globals.lock);
     1249                        return rc;
     1250                }
     1251                rc = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP,
     1252                    IPC_GET_ERROR(call));
     1253//              fibril_rwlock_read_unlock(&tcp_globals.lock);
     1254                return rc;
     1255        case IPC_M_CONNECT_TO_ME:
     1256                return tcp_process_client_messages(callid, *call);
     1257        }
     1258
    10321259        return ENOTSUP;
    10331260}
    10341261
    1035 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data){
     1262void tcp_refresh_socket_data(tcp_socket_data_t *socket_data)
     1263{
    10361264        assert(socket_data);
    10371265
     
    10481276}
    10491277
    1050 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data){
     1278void tcp_initialize_socket_data(tcp_socket_data_t *socket_data)
     1279{
    10511280        assert(socket_data);
    10521281
     
    10571286}
    10581287
    1059 int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call){
     1288int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
     1289{
    10601290        int res;
    10611291        bool keep_on_going = true;
    10621292        socket_cores_t local_sockets;
    10631293        int app_phone = IPC_GET_PHONE(&call);
    1064         struct sockaddr * addr;
     1294        struct sockaddr *addr;
    10651295        int socket_id;
    10661296        size_t addrlen;
     
    10691299        ipc_call_t answer;
    10701300        int answer_count;
    1071         tcp_socket_data_ref socket_data;
    1072         socket_core_ref socket;
    1073         packet_dimension_ref packet_dimension;
     1301        tcp_socket_data_t *socket_data;
     1302        socket_core_t *socket;
     1303        packet_dimension_t *packet_dimension;
    10741304
    10751305        /*
     
    10831313        fibril_rwlock_initialize(&lock);
    10841314
    1085         while(keep_on_going){
    1086 
    1087                 // answer the call
     1315        while (keep_on_going) {
     1316
     1317                /* Answer the call */
    10881318                answer_call(callid, res, &answer, answer_count);
    1089 
    1090                 // refresh data
     1319                /* Refresh data */
    10911320                refresh_answer(&answer, &answer_count);
    1092 
    1093                 // get the next call
     1321                /* Get the next call */
    10941322                callid = async_get_call(&call);
    10951323
    1096                 // process the call
    1097                 switch(IPC_GET_METHOD(call)){
    1098                         case IPC_M_PHONE_HUNGUP:
    1099                                 keep_on_going = false;
    1100                                 res = EHANGUP;
     1324                /* Process the call */
     1325                switch (IPC_GET_METHOD(call)) {
     1326                case IPC_M_PHONE_HUNGUP:
     1327                        keep_on_going = false;
     1328                        res = EHANGUP;
     1329                        break;
     1330
     1331                case NET_SOCKET:
     1332                        socket_data =
     1333                            (tcp_socket_data_t *) malloc(sizeof(*socket_data));
     1334                        if (!socket_data) {
     1335                                res = ENOMEM;
    11011336                                break;
    1102                         case NET_SOCKET:
    1103                                 socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
    1104                                 if(! socket_data){
    1105                                         res = ENOMEM;
    1106                                 }else{
    1107                                         tcp_initialize_socket_data(socket_data);
    1108                                         socket_data->local_lock = &lock;
    1109                                         socket_data->local_sockets = &local_sockets;
    1110                                         fibril_rwlock_write_lock(&lock);
    1111                                         socket_id = SOCKET_GET_SOCKET_ID(call);
    1112                                         res = socket_create(&local_sockets, app_phone, socket_data, &socket_id);
    1113                                         SOCKET_SET_SOCKET_ID(answer, socket_id);
    1114                                         fibril_rwlock_write_unlock(&lock);
    1115                                         if(res == EOK){
    1116                                                 if (tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, DEVICE_INVALID_ID, &packet_dimension) == EOK){
    1117                                                         SOCKET_SET_DATA_FRAGMENT_SIZE(answer, ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size));
    1118                                                 }
    1119 //                                              SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE);
    1120                                                 SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE);
    1121                                                 answer_count = 3;
    1122                                         }else{
    1123                                                 free(socket_data);
    1124                                         }
     1337                        }
     1338                       
     1339                        tcp_initialize_socket_data(socket_data);
     1340                        socket_data->local_lock = &lock;
     1341                        socket_data->local_sockets = &local_sockets;
     1342                        fibril_rwlock_write_lock(&lock);
     1343                        socket_id = SOCKET_GET_SOCKET_ID(call);
     1344                        res = socket_create(&local_sockets, app_phone,
     1345                            socket_data, &socket_id);
     1346                        SOCKET_SET_SOCKET_ID(answer, socket_id);
     1347                        fibril_rwlock_write_unlock(&lock);
     1348                        if (res != EOK) {
     1349                                free(socket_data);
     1350                                break;
     1351                        }
     1352                        if (tl_get_ip_packet_dimension(tcp_globals.ip_phone,
     1353                            &tcp_globals.dimensions, DEVICE_INVALID_ID,
     1354                            &packet_dimension) == EOK) {
     1355                                SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
     1356                                    ((packet_dimension->content <
     1357                                    socket_data->data_fragment_size) ?
     1358                                    packet_dimension->content :
     1359                                    socket_data->data_fragment_size));
     1360                        }
     1361//                      SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE);
     1362                        SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE);
     1363                        answer_count = 3;
     1364                        break;
     1365
     1366                case NET_SOCKET_BIND:
     1367                        res = data_receive((void **) &addr, &addrlen);
     1368                        if (res != EOK)
     1369                                break;
     1370                        fibril_rwlock_write_lock(&tcp_globals.lock);
     1371                        fibril_rwlock_write_lock(&lock);
     1372                        res = socket_bind(&local_sockets, &tcp_globals.sockets,
     1373                            SOCKET_GET_SOCKET_ID(call), addr, addrlen,
     1374                            TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
     1375                            tcp_globals.last_used_port);
     1376                        if (res == EOK) {
     1377                                socket = socket_cores_find(&local_sockets,
     1378                                    SOCKET_GET_SOCKET_ID(call));
     1379                                if (socket) {
     1380                                        socket_data = (tcp_socket_data_t *)
     1381                                            socket->specific_data;
     1382                                        assert(socket_data);
     1383                                        socket_data->state = TCP_SOCKET_LISTEN;
    11251384                                }
     1385                        }
     1386                        fibril_rwlock_write_unlock(&lock);
     1387                        fibril_rwlock_write_unlock(&tcp_globals.lock);
     1388                        free(addr);
     1389                        break;
     1390
     1391                case NET_SOCKET_LISTEN:
     1392                        fibril_rwlock_read_lock(&tcp_globals.lock);
     1393//                      fibril_rwlock_write_lock(&tcp_globals.lock);
     1394                        fibril_rwlock_write_lock(&lock);
     1395                        res = tcp_listen_message(&local_sockets,
     1396                            SOCKET_GET_SOCKET_ID(call),
     1397                            SOCKET_GET_BACKLOG(call));
     1398                        fibril_rwlock_write_unlock(&lock);
     1399//                      fibril_rwlock_write_unlock(&tcp_globals.lock);
     1400                        fibril_rwlock_read_unlock(&tcp_globals.lock);
     1401                        break;
     1402
     1403                case NET_SOCKET_CONNECT:
     1404                        res = data_receive((void **) &addr, &addrlen);
     1405                        if (res != EOK)
    11261406                                break;
    1127                         case NET_SOCKET_BIND:
    1128                                 res = data_receive((void **) &addr, &addrlen);
    1129                                 if(res == EOK){
    1130                                         fibril_rwlock_write_lock(&tcp_globals.lock);
    1131                                         fibril_rwlock_write_lock(&lock);
    1132                                         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);
    1133                                         if(res == EOK){
    1134                                                 socket = socket_cores_find(&local_sockets, SOCKET_GET_SOCKET_ID(call));
    1135                                                 if(socket){
    1136                                                         socket_data = (tcp_socket_data_ref) socket->specific_data;
    1137                                                         assert(socket_data);
    1138                                                         socket_data->state = TCP_SOCKET_LISTEN;
    1139                                                 }
    1140                                         }
    1141                                         fibril_rwlock_write_unlock(&lock);
    1142                                         fibril_rwlock_write_unlock(&tcp_globals.lock);
    1143                                         free(addr);
    1144                                 }
    1145                                 break;
    1146                         case NET_SOCKET_LISTEN:
    1147                                 fibril_rwlock_read_lock(&tcp_globals.lock);
    1148 //                              fibril_rwlock_write_lock(&tcp_globals.lock);
    1149                                 fibril_rwlock_write_lock(&lock);
    1150                                 res = tcp_listen_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_BACKLOG(call));
     1407                        /*
     1408                         * The global lock may be released in the
     1409                         * tcp_connect_message() function.
     1410                         */
     1411                        fibril_rwlock_write_lock(&tcp_globals.lock);
     1412                        fibril_rwlock_write_lock(&lock);
     1413                        res = tcp_connect_message(&local_sockets,
     1414                            SOCKET_GET_SOCKET_ID(call), addr, addrlen);
     1415                        if (res != EOK) {
    11511416                                fibril_rwlock_write_unlock(&lock);
    1152 //                              fibril_rwlock_write_unlock(&tcp_globals.lock);
    1153                                 fibril_rwlock_read_unlock(&tcp_globals.lock);
    1154                                 break;
    1155                         case NET_SOCKET_CONNECT:
    1156                                 res = data_receive((void **) &addr, &addrlen);
    1157                                 if(res == EOK){
    1158                                         // the global lock may be released in the tcp_connect_message() function
    1159                                         fibril_rwlock_write_lock(&tcp_globals.lock);
    1160                                         fibril_rwlock_write_lock(&lock);
    1161                                         res = tcp_connect_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen);
    1162                                         if(res != EOK){
    1163                                                 fibril_rwlock_write_unlock(&lock);
    1164                                                 fibril_rwlock_write_unlock(&tcp_globals.lock);
    1165                                                 free(addr);
    1166                                         }
    1167                                 }
    1168                                 break;
    1169                         case NET_SOCKET_ACCEPT:
    1170                                 fibril_rwlock_read_lock(&tcp_globals.lock);
    1171                                 fibril_rwlock_write_lock(&lock);
    1172                                 res = tcp_accept_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen);
    1173                                 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
     1417                                fibril_rwlock_write_unlock(&tcp_globals.lock);
     1418                                free(addr);
     1419                        }
     1420                        break;
     1421
     1422                case NET_SOCKET_ACCEPT:
     1423                        fibril_rwlock_read_lock(&tcp_globals.lock);
     1424                        fibril_rwlock_write_lock(&lock);
     1425                        res = tcp_accept_message(&local_sockets,
     1426                            SOCKET_GET_SOCKET_ID(call),
     1427                            SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen);
     1428                        SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
     1429                        fibril_rwlock_write_unlock(&lock);
     1430                        fibril_rwlock_read_unlock(&tcp_globals.lock);
     1431                        if (res > 0) {
     1432                                SOCKET_SET_SOCKET_ID(answer, res);
     1433                                SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
     1434                                answer_count = 3;
     1435                        }
     1436                        break;
     1437
     1438                case NET_SOCKET_SEND:
     1439                        fibril_rwlock_read_lock(&tcp_globals.lock);
     1440                        fibril_rwlock_write_lock(&lock);
     1441                        res = tcp_send_message(&local_sockets,
     1442                            SOCKET_GET_SOCKET_ID(call),
     1443                            SOCKET_GET_DATA_FRAGMENTS(call), &size,
     1444                            SOCKET_GET_FLAGS(call));
     1445                        SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
     1446                        if (res != EOK) {
    11741447                                fibril_rwlock_write_unlock(&lock);
    11751448                                fibril_rwlock_read_unlock(&tcp_globals.lock);
    1176                                 if(res > 0){
    1177                                         SOCKET_SET_SOCKET_ID(answer, res);
    1178                                         SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
    1179                                         answer_count = 3;
    1180                                 }
     1449                        } else {
     1450                                answer_count = 2;
     1451                        }
     1452                        break;
     1453
     1454                case NET_SOCKET_SENDTO:
     1455                        res = data_receive((void **) &addr, &addrlen);
     1456                        if (res != EOK)
    11811457                                break;
    1182                         case NET_SOCKET_SEND:
    1183                                 fibril_rwlock_read_lock(&tcp_globals.lock);
    1184                                 fibril_rwlock_write_lock(&lock);
    1185                                 res = tcp_send_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call));
    1186                                 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
    1187                                 if(res != EOK){
    1188                                         fibril_rwlock_write_unlock(&lock);
    1189                                         fibril_rwlock_read_unlock(&tcp_globals.lock);
    1190                                 }else{
    1191                                         answer_count = 2;
    1192                                 }
    1193                                 break;
    1194                         case NET_SOCKET_SENDTO:
    1195                                 res = data_receive((void **) &addr, &addrlen);
    1196                                 if(res == EOK){
    1197                                         fibril_rwlock_read_lock(&tcp_globals.lock);
    1198                                         fibril_rwlock_write_lock(&lock);
    1199                                         res = tcp_send_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call));
    1200                                         SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
    1201                                         if(res != EOK){
    1202                                                 fibril_rwlock_write_unlock(&lock);
    1203                                                 fibril_rwlock_read_unlock(&tcp_globals.lock);
    1204                                         }else{
    1205                                                 answer_count = 2;
    1206                                         }
    1207                                         free(addr);
    1208                                 }
    1209                                 break;
    1210                         case NET_SOCKET_RECV:
    1211                                 fibril_rwlock_read_lock(&tcp_globals.lock);
    1212                                 fibril_rwlock_write_lock(&lock);
    1213                                 res = tcp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), NULL);
     1458                        fibril_rwlock_read_lock(&tcp_globals.lock);
     1459                        fibril_rwlock_write_lock(&lock);
     1460                        res = tcp_send_message(&local_sockets,
     1461                            SOCKET_GET_SOCKET_ID(call),
     1462                            SOCKET_GET_DATA_FRAGMENTS(call), &size,
     1463                            SOCKET_GET_FLAGS(call));
     1464                        SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
     1465                        if (res != EOK) {
    12141466                                fibril_rwlock_write_unlock(&lock);
    12151467                                fibril_rwlock_read_unlock(&tcp_globals.lock);
    1216                                 if(res > 0){
    1217                                         SOCKET_SET_READ_DATA_LENGTH(answer, res);
    1218                                         answer_count = 1;
    1219                                         res = EOK;
    1220                                 }
    1221                                 break;
    1222                         case NET_SOCKET_RECVFROM:
    1223                                 fibril_rwlock_read_lock(&tcp_globals.lock);
    1224                                 fibril_rwlock_write_lock(&lock);
    1225                                 res = tcp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), &addrlen);
     1468                        } else {
     1469                                answer_count = 2;
     1470                        }
     1471                        free(addr);
     1472                        break;
     1473
     1474                case NET_SOCKET_RECV:
     1475                        fibril_rwlock_read_lock(&tcp_globals.lock);
     1476                        fibril_rwlock_write_lock(&lock);
     1477                        res = tcp_recvfrom_message(&local_sockets,
     1478                            SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
     1479                            NULL);
     1480                        fibril_rwlock_write_unlock(&lock);
     1481                        fibril_rwlock_read_unlock(&tcp_globals.lock);
     1482                        if (res > 0) {
     1483                                SOCKET_SET_READ_DATA_LENGTH(answer, res);
     1484                                answer_count = 1;
     1485                                res = EOK;
     1486                        }
     1487                        break;
     1488
     1489                case NET_SOCKET_RECVFROM:
     1490                        fibril_rwlock_read_lock(&tcp_globals.lock);
     1491                        fibril_rwlock_write_lock(&lock);
     1492                        res = tcp_recvfrom_message(&local_sockets,
     1493                            SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
     1494                            &addrlen);
     1495                        fibril_rwlock_write_unlock(&lock);
     1496                        fibril_rwlock_read_unlock(&tcp_globals.lock);
     1497                        if (res > 0) {
     1498                                SOCKET_SET_READ_DATA_LENGTH(answer, res);
     1499                                SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
     1500                                answer_count = 3;
     1501                                res = EOK;
     1502                        }
     1503                        break;
     1504
     1505                case NET_SOCKET_CLOSE:
     1506                        fibril_rwlock_write_lock(&tcp_globals.lock);
     1507                        fibril_rwlock_write_lock(&lock);
     1508                        res = tcp_close_message(&local_sockets,
     1509                            SOCKET_GET_SOCKET_ID(call));
     1510                        if (res != EOK) {
    12261511                                fibril_rwlock_write_unlock(&lock);
    1227                                 fibril_rwlock_read_unlock(&tcp_globals.lock);
    1228                                 if(res > 0){
    1229                                         SOCKET_SET_READ_DATA_LENGTH(answer, res);
    1230                                         SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
    1231                                         answer_count = 3;
    1232                                         res = EOK;
    1233                                 }
    1234                                 break;
    1235                         case NET_SOCKET_CLOSE:
    1236                                 fibril_rwlock_write_lock(&tcp_globals.lock);
    1237                                 fibril_rwlock_write_lock(&lock);
    1238                                 res = tcp_close_message(&local_sockets, SOCKET_GET_SOCKET_ID(call));
    1239                                 if(res != EOK){
    1240                                         fibril_rwlock_write_unlock(&lock);
    1241                                         fibril_rwlock_write_unlock(&tcp_globals.lock);
    1242                                 }
    1243                                 break;
    1244                         case NET_SOCKET_GETSOCKOPT:
    1245                         case NET_SOCKET_SETSOCKOPT:
    1246                         default:
    1247                                 res = ENOTSUP;
    1248                                 break;
    1249                 }
    1250         }
    1251 
    1252         // release the application phone
     1512                                fibril_rwlock_write_unlock(&tcp_globals.lock);
     1513                        }
     1514                        break;
     1515
     1516                case NET_SOCKET_GETSOCKOPT:
     1517                case NET_SOCKET_SETSOCKOPT:
     1518                default:
     1519                        res = ENOTSUP;
     1520                        break;
     1521                }
     1522        }
     1523
     1524        /* Release the application phone */
    12531525        ipc_hangup(app_phone);
    12541526
    12551527        printf("release\n");
    1256         // release all local sockets
    1257         socket_cores_release(tcp_globals.net_phone, &local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
     1528        /* Release all local sockets */
     1529        socket_cores_release(tcp_globals.net_phone, &local_sockets,
     1530            &tcp_globals.sockets, tcp_free_socket_data);
    12581531
    12591532        return EOK;
    12601533}
    12611534
    1262 int tcp_timeout(void * data){
    1263         tcp_timeout_ref timeout = data;
     1535int tcp_timeout(void *data)
     1536{
     1537        tcp_timeout_t *timeout = data;
    12641538        int keep_write_lock = false;
    1265         socket_core_ref socket;
    1266         tcp_socket_data_ref socket_data;
     1539        socket_core_t *socket;
     1540        tcp_socket_data_t *socket_data;
    12671541
    12681542        assert(timeout);
    12691543
    1270         // sleep the given timeout
     1544        /* Sleep the given timeout */
    12711545        async_usleep(timeout->timeout);
    1272         // lock the globals
    1273         if(timeout->globals_read_only){
     1546        /* Lock the globals */
     1547        if (timeout->globals_read_only)
    12741548                fibril_rwlock_read_lock(&tcp_globals.lock);
    1275         }else{
     1549        else
    12761550                fibril_rwlock_write_lock(&tcp_globals.lock);
    1277         }
    1278         // find the pending operation socket
    1279         socket = socket_port_find(&tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length);
    1280         if(socket && (socket->socket_id == timeout->socket_id)){
    1281                 socket_data = (tcp_socket_data_ref) socket->specific_data;
    1282                 assert(socket_data);
    1283                 if(socket_data->local_sockets == timeout->local_sockets){
    1284                         fibril_rwlock_write_lock(socket_data->local_lock);
    1285                         if(timeout->sequence_number){
    1286                                 // increase the timeout counter;
    1287                                 ++ socket_data->timeout_count;
    1288                                 if(socket_data->timeout_count == TCP_MAX_TIMEOUTS){
    1289                                         // TODO release as connection lost
    1290                                         //tcp_refresh_socket_data(socket_data);
    1291                                         fibril_rwlock_write_unlock(socket_data->local_lock);
    1292                                 }else{
    1293                                         // retransmit
    1294 //                                      tcp_retransmit_packet(socket, socket_data, timeout->sequence_number);
    1295                                         fibril_rwlock_write_unlock(socket_data->local_lock);
    1296                                 }
    1297                         }else{
    1298                                 fibril_mutex_lock(&socket_data->operation.mutex);
    1299                                 // set the timeout operation result if state not changed
    1300                                 if(socket_data->state == timeout->state){
    1301                                         socket_data->operation.result = ETIMEOUT;
    1302                                         // notify the main fibril
    1303                                         fibril_condvar_signal(&socket_data->operation.condvar);
    1304                                         // keep the global write lock
    1305                                         keep_write_lock = true;
    1306                                 }else{
    1307                                         // operation is ok, do nothing
    1308                                         // unlocking from now on, so the unlock order does not matter...
    1309                                         fibril_rwlock_write_unlock(socket_data->local_lock);
    1310                                 }
    1311                                 fibril_mutex_unlock(&socket_data->operation.mutex);
    1312                         }
    1313                 }
    1314         }
    1315         // unlock only if no socket
    1316         if(timeout->globals_read_only){
     1551
     1552        /* Find the pending operation socket */
     1553        socket = socket_port_find(&tcp_globals.sockets, timeout->port,
     1554            timeout->key, timeout->key_length);
     1555        if (!socket || (socket->socket_id != timeout->socket_id))
     1556                goto out;
     1557       
     1558        socket_data = (tcp_socket_data_t *) socket->specific_data;
     1559        assert(socket_data);
     1560        if (socket_data->local_sockets != timeout->local_sockets)
     1561                goto out;
     1562       
     1563        fibril_rwlock_write_lock(socket_data->local_lock);
     1564        if (timeout->sequence_number) {
     1565                /* Increase the timeout counter */
     1566                socket_data->timeout_count++;
     1567                if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
     1568                        /* TODO release as connection lost */
     1569                        //tcp_refresh_socket_data(socket_data);
     1570                        fibril_rwlock_write_unlock(socket_data->local_lock);
     1571                } else {
     1572                        /* Retransmit */
     1573//                      tcp_retransmit_packet(socket,
     1574//                          socket_data, timeout->sequence_number);
     1575                        fibril_rwlock_write_unlock(socket_data->local_lock);
     1576                }
     1577        } else {
     1578                fibril_mutex_lock(&socket_data->operation.mutex);
     1579                /* Set the timeout operation result if state not changed */
     1580                if (socket_data->state == timeout->state) {
     1581                        socket_data->operation.result = ETIMEOUT;
     1582
     1583                        /* Notify the main fibril */
     1584                        fibril_condvar_signal(&socket_data->operation.condvar);
     1585
     1586                        /* Keep the global write lock */
     1587                        keep_write_lock = true;
     1588                } else {
     1589                        /*
     1590                         * Operation is ok, do nothing.
     1591                         * Unlocking from now on, so the unlocking
     1592                         * order does not matter.
     1593                         */
     1594                        fibril_rwlock_write_unlock(socket_data->local_lock);
     1595                }
     1596                fibril_mutex_unlock(&socket_data->operation.mutex);
     1597        }
     1598
     1599out:
     1600        /* Unlock only if no socket */
     1601        if (timeout->globals_read_only)
    13171602                fibril_rwlock_read_unlock(&tcp_globals.lock);
    1318         }else if(! keep_write_lock){
    1319                 // release if not desired
     1603        else if (!keep_write_lock)
     1604                /* Release if not desired */
    13201605                fibril_rwlock_write_unlock(&tcp_globals.lock);
    1321         }
    1322         // release the timeout structure
     1606       
     1607        /* Release the timeout structure */
    13231608        free(timeout);
    13241609        return EOK;
    13251610}
    13261611
    1327 int tcp_release_after_timeout(void * data){
    1328         tcp_timeout_ref timeout = data;
    1329         socket_core_ref socket;
    1330         tcp_socket_data_ref socket_data;
    1331         fibril_rwlock_t * local_lock;
     1612int tcp_release_after_timeout(void *data)
     1613{
     1614        tcp_timeout_t *timeout = data;
     1615        socket_core_t *socket;
     1616        tcp_socket_data_t *socket_data;
     1617        fibril_rwlock_t *local_lock;
    13321618
    13331619        assert(timeout);
    13341620
    1335         // sleep the given timeout
     1621        /* Sleep the given timeout */
    13361622        async_usleep(timeout->timeout);
    1337         // lock the globals
     1623
     1624        /* Lock the globals */
    13381625        fibril_rwlock_write_lock(&tcp_globals.lock);
    1339         // find the pending operation socket
    1340         socket = socket_port_find(&tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length);
    1341         if(socket && (socket->socket_id == timeout->socket_id)){
    1342                 socket_data = (tcp_socket_data_ref) socket->specific_data;
     1626
     1627        /* Find the pending operation socket */
     1628        socket = socket_port_find(&tcp_globals.sockets, timeout->port,
     1629            timeout->key, timeout->key_length);
     1630
     1631        if (socket && (socket->socket_id == timeout->socket_id)) {
     1632                socket_data = (tcp_socket_data_t *) socket->specific_data;
    13431633                assert(socket_data);
    1344                 if(socket_data->local_sockets == timeout->local_sockets){
     1634                if (socket_data->local_sockets == timeout->local_sockets) {
    13451635                        local_lock = socket_data->local_lock;
    13461636                        fibril_rwlock_write_lock(local_lock);
    1347                         socket_destroy(tcp_globals.net_phone, timeout->socket_id, timeout->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
     1637                        socket_destroy(tcp_globals.net_phone,
     1638                            timeout->socket_id, timeout->local_sockets,
     1639                            &tcp_globals.sockets, tcp_free_socket_data);
    13481640                        fibril_rwlock_write_unlock(local_lock);
    13491641                }
    13501642        }
    1351         // unlock the globals
     1643
     1644        /* Unlock the globals */
    13521645        fibril_rwlock_write_unlock(&tcp_globals.lock);
    1353         // release the timeout structure
     1646
     1647        /* Release the timeout structure */
    13541648        free(timeout);
     1649
    13551650        return EOK;
    13561651}
    13571652
    1358 void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number){
    1359         packet_t packet;
    1360         packet_t copy;
     1653void tcp_retransmit_packet(socket_core_t *socket, tcp_socket_data_t *
     1654    socket_data, size_t sequence_number)
     1655{
     1656        packet_t *packet;
     1657        packet_t *copy;
    13611658        size_t data_length;
    13621659
     
    13651662        assert(socket->specific_data == socket_data);
    13661663
    1367         // sent packet?
     1664        /* Sent packet? */
    13681665        packet = pq_find(socket_data->outgoing, sequence_number);
    13691666        printf("retransmit %d\n", packet_get_id(packet));
    1370         if(packet){
     1667        if (packet) {
    13711668                pq_get_order(packet, NULL, &data_length);
    1372                 copy = tcp_prepare_copy(socket, socket_data, packet, data_length, sequence_number);
     1669                copy = tcp_prepare_copy(socket, socket_data, packet,
     1670                    data_length, sequence_number);
    13731671                fibril_rwlock_write_unlock(socket_data->local_lock);
    1374 //              printf("r send %d\n", packet_get_id(packet));
    1375                 if(copy){
     1672//              printf("r send %d\n", packet_get_id(packet));
     1673                if (copy)
    13761674                        tcp_send_packets(socket_data->device_id, copy);
    1377                 }
    1378         }else{
     1675        } else {
    13791676                fibril_rwlock_write_unlock(socket_data->local_lock);
    13801677        }
    13811678}
    13821679
    1383 int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog){
    1384         socket_core_ref socket;
    1385         tcp_socket_data_ref socket_data;
     1680int tcp_listen_message(socket_cores_t *local_sockets, int socket_id,
     1681    int backlog)
     1682{
     1683        socket_core_t *socket;
     1684        tcp_socket_data_t *socket_data;
    13861685
    13871686        assert(local_sockets);
    13881687
    1389         if(backlog < 0){
     1688        if (backlog < 0)
    13901689                return EINVAL;
    1391         }
    1392         // find the socket
     1690
     1691        /* Find the socket */
    13931692        socket = socket_cores_find(local_sockets, socket_id);
    1394         if(! socket){
     1693        if (!socket)
    13951694                return ENOTSOCK;
    1396         }
    1397         // get the socket specific data
    1398         socket_data = (tcp_socket_data_ref) socket->specific_data;
     1695       
     1696        /* Get the socket specific data */
     1697        socket_data = (tcp_socket_data_t *) socket->specific_data;
    13991698        assert(socket_data);
    1400         // set the backlog
     1699
     1700        /* Set the backlog */
    14011701        socket_data->backlog = backlog;
     1702
    14021703        return EOK;
    14031704}
    14041705
    1405 int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen){
    1406         ERROR_DECLARE;
    1407 
    1408         socket_core_ref socket;
     1706int tcp_connect_message(socket_cores_t *local_sockets, int socket_id,
     1707    struct sockaddr *addr, socklen_t addrlen)
     1708{
     1709        socket_core_t *socket;
     1710        int rc;
    14091711
    14101712        assert(local_sockets);
     
    14121714        assert(addrlen > 0);
    14131715
    1414         // find the socket
     1716        /* Find the socket */
    14151717        socket = socket_cores_find(local_sockets, socket_id);
    1416         if(! socket){
     1718        if (!socket)
    14171719                return ENOTSOCK;
    1418         }
    1419         if(ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr, addrlen))){
     1720       
     1721        rc = tcp_connect_core(socket, local_sockets, addr, addrlen);
     1722        if (rc != EOK) {
    14201723                tcp_free_socket_data(socket);
    1421                 // unbind if bound
    1422                 if(socket->port > 0){
    1423                         socket_ports_exclude(&tcp_globals.sockets, socket->port);
     1724                /* Unbind if bound */
     1725                if (socket->port > 0) {
     1726                        socket_ports_exclude(&tcp_globals.sockets,
     1727                            socket->port);
    14241728                        socket->port = 0;
    14251729                }
    14261730        }
    1427         return ERROR_CODE;
    1428 }
    1429 
    1430 int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen){
    1431         ERROR_DECLARE;
    1432 
    1433         tcp_socket_data_ref socket_data;
    1434         packet_t packet;
     1731        return rc;
     1732}
     1733
     1734int tcp_connect_core(socket_core_t *socket, socket_cores_t *local_sockets,
     1735    struct sockaddr *addr, socklen_t addrlen)
     1736{
     1737        tcp_socket_data_t *socket_data;
     1738        packet_t *packet;
     1739        int rc;
    14351740
    14361741        assert(socket);
     
    14381743        assert(addrlen > 0);
    14391744
    1440         // get the socket specific data
    1441         socket_data = (tcp_socket_data_ref) socket->specific_data;
     1745        /* Get the socket specific data */
     1746        socket_data = (tcp_socket_data_t *) socket->specific_data;
    14421747        assert(socket_data);
    14431748        assert(socket->specific_data == socket_data);
    1444         if((socket_data->state != TCP_SOCKET_INITIAL)
    1445                 && ((socket_data->state != TCP_SOCKET_LISTEN) || (socket->port <= 0))){
     1749        if ((socket_data->state != TCP_SOCKET_INITIAL) &&
     1750            ((socket_data->state != TCP_SOCKET_LISTEN) ||
     1751            (socket->port <= 0)))
    14461752                return EINVAL;
    1447         }
    1448         // get the destination port
    1449         ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, &socket_data->dest_port));
    1450         if(socket->port <= 0){
    1451                 // try to find a free port
    1452                 ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port));
    1453                 // set the next port as the search starting port number
     1753
     1754        /* Get the destination port */
     1755        rc = tl_get_address_port(addr, addrlen, &socket_data->dest_port);
     1756        if (rc != EOK)
     1757                return rc;
     1758       
     1759        if (socket->port <= 0) {
     1760                /* Try to find a free port */
     1761                rc = socket_bind_free_port(&tcp_globals.sockets, socket,
     1762                    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
     1763                    tcp_globals.last_used_port);
     1764                if (rc != EOK)
     1765                        return rc;
     1766                /* Set the next port as the search starting port number */
    14541767                tcp_globals.last_used_port = socket->port;
    14551768        }
    1456         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));
    1457 
    1458         // create the notification packet
    1459         ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 1, 0));
    1460 
    1461         // unlock the globals and wait for an operation
     1769
     1770        rc = ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
     1771            addr, addrlen, &socket_data->device_id,
     1772            &socket_data->pseudo_header, &socket_data->headerlen);
     1773        if (rc != EOK)
     1774                return rc;
     1775
     1776        /* Create the notification packet */
     1777        rc = tcp_create_notification_packet(&packet, socket, socket_data, 1, 0);
     1778        if (rc != EOK)
     1779                return rc;
     1780
     1781        /* Unlock the globals and wait for an operation */
    14621782        fibril_rwlock_write_unlock(&tcp_globals.lock);
    14631783
    14641784        socket_data->addr = addr;
    14651785        socket_data->addrlen = addrlen;
    1466         // send the packet
    1467         if(ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))
    1468                 || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))){
     1786
     1787        /* Send the packet */
     1788
     1789        if (((rc = tcp_queue_packet(socket, socket_data, packet, 1)) != EOK) ||
     1790            ((rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0,
     1791            TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false)) !=
     1792            EOK)) {
    14691793                socket_data->addr = NULL;
    14701794                socket_data->addrlen = 0;
    14711795                fibril_rwlock_write_lock(&tcp_globals.lock);
    1472         }else{
     1796        } else {
    14731797                packet = tcp_get_packets_to_send(socket, socket_data);
    1474                 if(packet){
     1798                if (packet) {
    14751799                        fibril_mutex_lock(&socket_data->operation.mutex);
    14761800                        fibril_rwlock_write_unlock(socket_data->local_lock);
    1477                         // send the packet
     1801
     1802                        /* Send the packet */
    14781803                        printf("connecting %d\n", packet_get_id(packet));
    14791804                        tcp_send_packets(socket_data->device_id, packet);
    1480                         // wait for a reply
    1481                         fibril_condvar_wait(&socket_data->operation.condvar, &socket_data->operation.mutex);
    1482                         ERROR_CODE = socket_data->operation.result;
    1483                         if(ERROR_CODE != EOK){
     1805
     1806                        /* Wait for a reply */
     1807                        fibril_condvar_wait(&socket_data->operation.condvar,
     1808                            &socket_data->operation.mutex);
     1809                        rc = socket_data->operation.result;
     1810                        if (rc != EOK) {
    14841811                                socket_data->addr = NULL;
    14851812                                socket_data->addrlen = 0;
    14861813                        }
    1487                 }else{
     1814                } else {
    14881815                        socket_data->addr = NULL;
    14891816                        socket_data->addrlen = 0;
    1490                         ERROR_CODE = EINTR;
     1817                        rc = EINTR;
    14911818                }
    14921819        }
    14931820
    14941821        fibril_mutex_unlock(&socket_data->operation.mutex);
    1495 
    1496         // return the result
    1497         return ERROR_CODE;
    1498 }
    1499 
    1500 int tcp_queue_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length){
    1501         ERROR_DECLARE;
    1502 
    1503         tcp_header_ref header;
     1822        return rc;
     1823}
     1824
     1825int tcp_queue_prepare_packet(socket_core_t *socket,
     1826    tcp_socket_data_t *socket_data, packet_t *packet, size_t data_length)
     1827{
     1828        tcp_header_t *header;
     1829        int rc;
    15041830
    15051831        assert(socket);
     
    15071833        assert(socket->specific_data == socket_data);
    15081834
    1509         // get tcp header
    1510         header = (tcp_header_ref) packet_get_data(packet);
    1511         if(! header){
     1835        /* Get TCP header */
     1836        header = (tcp_header_t *) packet_get_data(packet);
     1837        if (!header)
    15121838                return NO_DATA;
    1513         }
     1839       
    15141840        header->destination_port = htons(socket_data->dest_port);
    15151841        header->source_port = htons(socket->port);
    15161842        header->sequence_number = htonl(socket_data->next_outgoing);
    1517         if(ERROR_OCCURRED(packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr, socket_data->addrlen))){
     1843
     1844        rc = packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr,
     1845            socket_data->addrlen);
     1846        if (rc != EOK)
    15181847                return tcp_release_and_return(packet, EINVAL);
    1519         }
    1520         // remember the outgoing FIN
    1521         if(header->finalize){
     1848
     1849        /* Remember the outgoing FIN */
     1850        if (header->finalize)
    15221851                socket_data->fin_outgoing = socket_data->next_outgoing;
    1523         }
     1852       
    15241853        return EOK;
    15251854}
    15261855
    1527 int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length){
    1528         ERROR_DECLARE;
     1856int tcp_queue_packet(socket_core_t *socket, tcp_socket_data_t *socket_data,
     1857    packet_t *packet, size_t data_length)
     1858{
     1859        int rc;
    15291860
    15301861        assert(socket);
     
    15321863        assert(socket->specific_data == socket_data);
    15331864
    1534         ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet, data_length));
    1535 
    1536         if(ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing, data_length))){
    1537                 return tcp_release_and_return(packet, ERROR_CODE);
    1538         }
     1865        rc = tcp_queue_prepare_packet(socket, socket_data, packet, data_length);
     1866        if (rc != EOK)
     1867                return rc;
     1868
     1869        rc = pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing,
     1870            data_length);
     1871        if (rc != EOK)
     1872                return tcp_release_and_return(packet, rc);
     1873
    15391874        socket_data->next_outgoing += data_length;
    15401875        return EOK;
    15411876}
    15421877
    1543 packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data){
    1544         ERROR_DECLARE;
    1545 
    1546         packet_t packet;
    1547         packet_t copy;
    1548         packet_t sending = NULL;
    1549         packet_t previous = NULL;
     1878packet_t *tcp_get_packets_to_send(socket_core_t *socket, tcp_socket_data_t *
     1879    socket_data)
     1880{
     1881        packet_t *packet;
     1882        packet_t *copy;
     1883        packet_t *sending = NULL;
     1884        packet_t *previous = NULL;
    15501885        size_t data_length;
     1886        int rc;
    15511887
    15521888        assert(socket);
     
    15551891
    15561892        packet = pq_find(socket_data->outgoing, socket_data->last_outgoing + 1);
    1557         while(packet){
     1893        while (packet) {
    15581894                pq_get_order(packet, NULL, &data_length);
    1559                 // send only if fits into the window
    1560                 // respecting the possible overflow
    1561                 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))){
    1562                         copy = tcp_prepare_copy(socket, socket_data, packet, data_length, socket_data->last_outgoing + 1);
    1563                         if(! copy){
     1895
     1896                /*
     1897                 * Send only if fits into the window, respecting the possible
     1898                 * overflow.
     1899                 */
     1900                if (!IS_IN_INTERVAL_OVERFLOW(
     1901                    (uint32_t) socket_data->last_outgoing,
     1902                    (uint32_t) (socket_data->last_outgoing + data_length),
     1903                    (uint32_t) (socket_data->expected + socket_data->treshold)))
     1904                        break;
     1905
     1906                copy = tcp_prepare_copy(socket, socket_data, packet,
     1907                    data_length, socket_data->last_outgoing + 1);
     1908                if (!copy)
     1909                        return sending;
     1910                       
     1911                if (!sending) {
     1912                        sending = copy;
     1913                } else {
     1914                        rc = pq_insert_after(previous, copy);
     1915                        if (rc != EOK) {
     1916                                pq_release_remote(tcp_globals.net_phone,
     1917                                    packet_get_id(copy));
    15641918                                return sending;
    15651919                        }
    1566                         if(! sending){
    1567                                 sending = copy;
    1568                         }else{
    1569                                 if(ERROR_OCCURRED(pq_insert_after(previous, copy))){
    1570                                         pq_release_remote(tcp_globals.net_phone, packet_get_id(copy));
    1571                                         return sending;
    1572                                 }
    1573                         }
    1574                         previous = copy;
    1575                         packet = pq_next(packet);
    1576                         // overflow occurred ?
    1577                         if((! packet) && (socket_data->last_outgoing > socket_data->next_outgoing)){
    1578                                 printf("gpts overflow\n");
    1579                                 // continue from the beginning
    1580                                 packet = socket_data->outgoing;
    1581                         }
    1582                         socket_data->last_outgoing += data_length;
    1583                 }else{
    1584                         break;
    1585                 }
    1586         }
     1920                }
     1921
     1922                previous = copy;
     1923                packet = pq_next(packet);
     1924
     1925                /* Overflow occurred? */
     1926                if (!packet &&
     1927                    (socket_data->last_outgoing > socket_data->next_outgoing)) {
     1928                        printf("gpts overflow\n");
     1929                        /* Continue from the beginning */
     1930                        packet = socket_data->outgoing;
     1931                }
     1932                socket_data->last_outgoing += data_length;
     1933        }
     1934
    15871935        return sending;
    15881936}
    15891937
    1590 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){
    1591         ERROR_DECLARE;
    1592 
    1593         tcp_header_ref header;
     1938packet_t *tcp_send_prepare_packet(socket_core_t *socket, tcp_socket_data_t *
     1939    socket_data, packet_t *packet, size_t data_length, size_t sequence_number)
     1940{
     1941        tcp_header_t *header;
    15941942        uint32_t checksum;
     1943        int rc;
    15951944
    15961945        assert(socket);
     
    15981947        assert(socket->specific_data == socket_data);
    15991948
    1600         // adjust the pseudo header
    1601         if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(socket_data->pseudo_header, socket_data->headerlen, packet_get_data_length(packet)))){
     1949        /* Adjust the pseudo header */
     1950        rc = ip_client_set_pseudo_header_data_length(socket_data->pseudo_header,
     1951            socket_data->headerlen, packet_get_data_length(packet));
     1952        if (rc != EOK) {
    16021953                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    16031954                return NULL;
    16041955        }
    16051956
    1606         // get the header
    1607         header = (tcp_header_ref) packet_get_data(packet);
    1608         if(! header){
     1957        /* Get the header */
     1958        header = (tcp_header_t *) packet_get_data(packet);
     1959        if (!header) {
    16091960                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    16101961                return NULL;
     
    16121963        assert(ntohl(header->sequence_number) == sequence_number);
    16131964
    1614         // adjust the header
    1615         if(socket_data->next_incoming){
    1616                 header->acknowledgement_number = htonl(socket_data->next_incoming);
     1965        /* Adjust the header */
     1966        if (socket_data->next_incoming) {
     1967                header->acknowledgement_number =
     1968                    htonl(socket_data->next_incoming);
    16171969                header->acknowledge = 1;
    16181970        }
    16191971        header->window = htons(socket_data->window);
    16201972
    1621         // checksum
     1973        /* Checksum */
    16221974        header->checksum = 0;
    1623         checksum = compute_checksum(0, socket_data->pseudo_header, socket_data->headerlen);
    1624         checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet), packet_get_data_length(packet));
     1975        checksum = compute_checksum(0, socket_data->pseudo_header,
     1976            socket_data->headerlen);
     1977        checksum = compute_checksum(checksum,
     1978            (uint8_t *) packet_get_data(packet),
     1979            packet_get_data_length(packet));
    16251980        header->checksum = htons(flip_checksum(compact_checksum(checksum)));
    1626         // prepare the packet
    1627         if(ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0))
    1628         // prepare the timeout
    1629                 || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, sequence_number, socket_data->state, socket_data->timeout, true))){
     1981
     1982        /* Prepare the packet */
     1983        rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0);
     1984        if (rc != EOK) {
    16301985                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    16311986                return NULL;
    16321987        }
     1988
     1989        rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data,
     1990            sequence_number, socket_data->state, socket_data->timeout, true);
     1991        if (rc != EOK) {
     1992                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     1993                return NULL;
     1994        }
     1995
    16331996        return packet;
    16341997}
    16351998
    1636 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){
    1637         packet_t copy;
     1999packet_t *tcp_prepare_copy(socket_core_t *socket, tcp_socket_data_t *
     2000    socket_data, packet_t *packet, size_t data_length, size_t sequence_number)
     2001{
     2002        packet_t *copy;
    16382003
    16392004        assert(socket);
     
    16412006        assert(socket->specific_data == socket_data);
    16422007
    1643         // make a copy of the packet
     2008        /* Make a copy of the packet */
    16442009        copy = packet_get_copy(tcp_globals.net_phone, packet);
    1645         if(! copy){
     2010        if (!copy)
    16462011                return NULL;
    1647         }
    1648 
    1649         return tcp_send_prepare_packet(socket, socket_data, copy, data_length, sequence_number);
    1650 }
    1651 
    1652 void tcp_send_packets(device_id_t device_id, packet_t packet){
    1653         packet_t next;
    1654 
    1655         while(packet){
     2012
     2013        return tcp_send_prepare_packet(socket, socket_data, copy, data_length,
     2014            sequence_number);
     2015}
     2016
     2017void tcp_send_packets(device_id_t device_id, packet_t *packet)
     2018{
     2019        packet_t *next;
     2020
     2021        while (packet) {
    16562022                next = pq_detach(packet);
    1657                 ip_send_msg(tcp_globals.ip_phone, device_id, packet, SERVICE_TCP, 0);
     2023                ip_send_msg(tcp_globals.ip_phone, device_id, packet,
     2024                    SERVICE_TCP, 0);
    16582025                packet = next;
    16592026        }
    16602027}
    16612028
    1662 void tcp_prepare_operation_header(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize){
     2029void tcp_prepare_operation_header(socket_core_t *socket,
     2030    tcp_socket_data_t *socket_data, tcp_header_t *header, int synchronize,
     2031    int finalize)
     2032{
    16632033        assert(socket);
    16642034        assert(socket_data);
     
    16742044}
    16752045
    1676 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){
    1677         tcp_timeout_ref operation_timeout;
     2046int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
     2047    socket_core_t *socket, tcp_socket_data_t *socket_data,
     2048    size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
     2049    int globals_read_only)
     2050{
     2051        tcp_timeout_t *operation_timeout;
    16782052        fid_t fibril;
    16792053
     
    16822056        assert(socket->specific_data == socket_data);
    16832057
    1684         // prepare the timeout with key bundle structure
    1685         operation_timeout = malloc(sizeof(*operation_timeout) + socket->key_length + 1);
    1686         if(! operation_timeout){
     2058        /* Prepare the timeout with key bundle structure */
     2059        operation_timeout = malloc(sizeof(*operation_timeout) +
     2060            socket->key_length + 1);
     2061        if (!operation_timeout)
    16872062                return ENOMEM;
    1688         }
     2063
    16892064        bzero(operation_timeout, sizeof(*operation_timeout));
    16902065        operation_timeout->globals_read_only = globals_read_only;
     
    16962071        operation_timeout->state = state;
    16972072
    1698         // copy the key
    1699         operation_timeout->key = ((char *) operation_timeout) + sizeof(*operation_timeout);
     2073        /* Copy the key */
     2074        operation_timeout->key = ((char *) operation_timeout) +
     2075            sizeof(*operation_timeout);
    17002076        operation_timeout->key_length = socket->key_length;
    17012077        memcpy(operation_timeout->key, socket->key, socket->key_length);
    17022078        operation_timeout->key[operation_timeout->key_length] = '\0';
    17032079
    1704         // prepare the timeouting thread
     2080        /* Prepare the timeouting thread */
    17052081        fibril = fibril_create(timeout_function, operation_timeout);
    1706         if(! fibril){
     2082        if (!fibril) {
    17072083                free(operation_timeout);
    1708                 return EPARTY;
    1709         }
    1710 //      fibril_mutex_lock(&socket_data->operation.mutex);
    1711         // start the timeouting fibril
     2084                return EPARTY;  /* FIXME: use another EC */
     2085        }
     2086//      fibril_mutex_lock(&socket_data->operation.mutex);
     2087        /* Start the timeout fibril */
    17122088        fibril_add_ready(fibril);
    17132089        //socket_data->state = state;
     
    17152091}
    17162092
    1717 int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen){
    1718         ERROR_DECLARE;
    1719 
    1720         socket_core_ref socket;
    1721         tcp_socket_data_ref socket_data;
     2093int tcp_recvfrom_message(socket_cores_t *local_sockets, int socket_id,
     2094    int flags, size_t *addrlen)
     2095{
     2096        socket_core_t *socket;
     2097        tcp_socket_data_t *socket_data;
    17222098        int packet_id;
    1723         packet_t packet;
     2099        packet_t *packet;
    17242100        size_t length;
     2101        int rc;
    17252102
    17262103        assert(local_sockets);
    17272104
    1728         // find the socket
     2105        /* Find the socket */
    17292106        socket = socket_cores_find(local_sockets, socket_id);
    1730         if(! socket){
     2107        if (!socket)
    17312108                return ENOTSOCK;
    1732         }
    1733         // get the socket specific data
    1734         if(! socket->specific_data){
     2109
     2110        /* Get the socket specific data */
     2111        if (!socket->specific_data)
    17352112                return NO_DATA;
    1736         }
    1737         socket_data = (tcp_socket_data_ref) socket->specific_data;
    1738 
    1739         // check state
    1740         if((socket_data->state != TCP_SOCKET_ESTABLISHED) && (socket_data->state != TCP_SOCKET_CLOSE_WAIT)){
     2113
     2114        socket_data = (tcp_socket_data_t *) socket->specific_data;
     2115
     2116        /* Check state */
     2117        if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
     2118            (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
    17412119                return ENOTCONN;
    1742         }
    1743 
    1744         // send the source address if desired
    1745         if(addrlen){
    1746                 ERROR_PROPAGATE(data_reply(socket_data->addr, socket_data->addrlen));
     2120
     2121        /* Send the source address if desired */
     2122        if (addrlen) {
     2123                rc = data_reply(socket_data->addr, socket_data->addrlen);
     2124                if (rc != EOK)
     2125                        return rc;
    17472126                *addrlen = socket_data->addrlen;
    17482127        }
    17492128
    1750         // get the next received packet
     2129        /* Get the next received packet */
    17512130        packet_id = dyn_fifo_value(&socket->received);
    1752         if(packet_id < 0){
     2131        if (packet_id < 0)
    17532132                return NO_DATA;
    1754         }
    1755         ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet, packet_id));
    1756 
    1757         // reply the packets
    1758         ERROR_PROPAGATE(socket_reply_packets(packet, &length));
    1759 
    1760         // release the packet
     2133
     2134        rc = packet_translate_remote(tcp_globals.net_phone, &packet, packet_id);
     2135        if (rc != EOK)
     2136                return rc;
     2137
     2138        /* Reply the packets */
     2139        rc = socket_reply_packets(packet, &length);
     2140        if (rc != EOK)
     2141                return rc;
     2142
     2143        /* Release the packet */
    17612144        dyn_fifo_pop(&socket->received);
    17622145        pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    1763         // return the total length
     2146
     2147        /* Return the total length */
    17642148        return (int) length;
    17652149}
    17662150
    1767 int tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags){
    1768         ERROR_DECLARE;
    1769 
    1770         socket_core_ref socket;
    1771         tcp_socket_data_ref socket_data;
    1772         packet_dimension_ref packet_dimension;
    1773         packet_t packet;
     2151int tcp_send_message(socket_cores_t *local_sockets, int socket_id,
     2152    int fragments, size_t *data_fragment_size, int flags)
     2153{
     2154        socket_core_t *socket;
     2155        tcp_socket_data_t *socket_data;
     2156        packet_dimension_t *packet_dimension;
     2157        packet_t *packet;
    17742158        size_t total_length;
    1775         tcp_header_ref header;
     2159        tcp_header_t *header;
    17762160        int index;
    17772161        int result;
     2162        int rc;
    17782163
    17792164        assert(local_sockets);
    17802165        assert(data_fragment_size);
    17812166
    1782         // find the socket
     2167        /* Find the socket */
    17832168        socket = socket_cores_find(local_sockets, socket_id);
    1784         if(! socket){
     2169        if (!socket)
    17852170                return ENOTSOCK;
    1786         }
    1787         // get the socket specific data
    1788         if(! socket->specific_data){
     2171
     2172        /* Get the socket specific data */
     2173        if (!socket->specific_data)
    17892174                return NO_DATA;
    1790         }
    1791         socket_data = (tcp_socket_data_ref) socket->specific_data;
    1792 
    1793         // check state
    1794         if((socket_data->state != TCP_SOCKET_ESTABLISHED) && (socket_data->state != TCP_SOCKET_CLOSE_WAIT)){
     2175
     2176        socket_data = (tcp_socket_data_t *) socket->specific_data;
     2177
     2178        /* Check state */
     2179        if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
     2180            (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
    17952181                return ENOTCONN;
    1796         }
    1797 
    1798         ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
    1799 
    1800         *data_fragment_size = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size);
    1801 
    1802         for(index = 0; index < fragments; ++ index){
    1803                 // read the data fragment
    1804                 result = tl_socket_read_packet_data(tcp_globals.net_phone, &packet, TCP_HEADER_SIZE, packet_dimension, socket_data->addr, socket_data->addrlen);
    1805                 if(result < 0){
     2182
     2183        rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
     2184            &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
     2185        if (rc != EOK)
     2186                return rc;
     2187
     2188        *data_fragment_size =
     2189            ((packet_dimension->content < socket_data->data_fragment_size) ?
     2190            packet_dimension->content : socket_data->data_fragment_size);
     2191
     2192        for (index = 0; index < fragments; index++) {
     2193                /* Read the data fragment */
     2194                result = tl_socket_read_packet_data(tcp_globals.net_phone,
     2195                    &packet, TCP_HEADER_SIZE, packet_dimension,
     2196                    socket_data->addr, socket_data->addrlen);
     2197                if (result < 0)
    18062198                        return result;
    1807                 }
     2199
    18082200                total_length = (size_t) result;
    1809                 // prefix the tcp header
     2201
     2202                /* Prefix the TCP header */
    18102203                header = PACKET_PREFIX(packet, tcp_header_t);
    1811                 if(! header){
     2204                if (!header)
    18122205                        return tcp_release_and_return(packet, ENOMEM);
    1813                 }
     2206
    18142207                tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
    1815                 ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 0));
    1816         }
    1817 
    1818         // flush packets
     2208                rc = tcp_queue_packet(socket, socket_data, packet, 0);
     2209                if (rc != EOK)
     2210                        return rc;
     2211        }
     2212
     2213        /* Flush packets */
    18192214        packet = tcp_get_packets_to_send(socket, socket_data);
    18202215        fibril_rwlock_write_unlock(socket_data->local_lock);
    18212216        fibril_rwlock_read_unlock(&tcp_globals.lock);
    1822         if(packet){
    1823                 // send the packet
     2217
     2218        if (packet) {
     2219                /* Send the packet */
    18242220                tcp_send_packets(socket_data->device_id, packet);
    18252221        }
     
    18282224}
    18292225
    1830 int tcp_close_message(socket_cores_ref local_sockets, int socket_id){
    1831         ERROR_DECLARE;
    1832 
    1833         socket_core_ref socket;
    1834         tcp_socket_data_ref socket_data;
    1835         packet_t packet;
    1836 
    1837         // find the socket
     2226int
     2227tcp_close_message(socket_cores_t *local_sockets, int socket_id)
     2228{
     2229        socket_core_t *socket;
     2230        tcp_socket_data_t *socket_data;
     2231        packet_t *packet;
     2232        int rc;
     2233
     2234        /* Find the socket */
    18382235        socket = socket_cores_find(local_sockets, socket_id);
    1839         if(! socket){
     2236        if (!socket)
    18402237                return ENOTSOCK;
    1841         }
    1842         // get the socket specific data
    1843         socket_data = (tcp_socket_data_ref) socket->specific_data;
     2238
     2239        /* Get the socket specific data */
     2240        socket_data = (tcp_socket_data_t *) socket->specific_data;
    18442241        assert(socket_data);
    18452242
    1846         // check state
    1847         switch(socket_data->state){
    1848                 case TCP_SOCKET_ESTABLISHED:
    1849                         socket_data->state = TCP_SOCKET_FIN_WAIT_1;
    1850                         break;
    1851                 case TCP_SOCKET_CLOSE_WAIT:
    1852                         socket_data->state = TCP_SOCKET_LAST_ACK;
    1853                         break;
    1854 //              case TCP_SOCKET_LISTEN:
    1855                 default:
    1856                         // just destroy
    1857                         if(! ERROR_OCCURRED(socket_destroy(tcp_globals.net_phone, socket_id, local_sockets, &tcp_globals.sockets, tcp_free_socket_data))){
    1858                                 fibril_rwlock_write_unlock(socket_data->local_lock);
    1859                                 fibril_rwlock_write_unlock(&tcp_globals.lock);
    1860                         }
    1861                         return ERROR_CODE;
    1862         }
    1863         // send FIN
    1864         // TODO should I wait to complete?
    1865 
    1866         // create the notification packet
    1867         ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 1));
    1868 
    1869         // send the packet
    1870         ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
    1871 
    1872         // flush packets
     2243        /* Check state */
     2244        switch (socket_data->state) {
     2245        case TCP_SOCKET_ESTABLISHED:
     2246                socket_data->state = TCP_SOCKET_FIN_WAIT_1;
     2247                break;
     2248
     2249        case TCP_SOCKET_CLOSE_WAIT:
     2250                socket_data->state = TCP_SOCKET_LAST_ACK;
     2251                break;
     2252
     2253//      case TCP_SOCKET_LISTEN:
     2254
     2255        default:
     2256                /* Just destroy */
     2257                rc = socket_destroy(tcp_globals.net_phone, socket_id,
     2258                    local_sockets, &tcp_globals.sockets,
     2259                    tcp_free_socket_data);
     2260                if (rc == EOK) {
     2261                        fibril_rwlock_write_unlock(socket_data->local_lock);
     2262                        fibril_rwlock_write_unlock(&tcp_globals.lock);
     2263                }
     2264                return rc;
     2265        }
     2266
     2267        /*
     2268         * Send FIN.
     2269         * TODO should I wait to complete?
     2270         */
     2271
     2272        /* Create the notification packet */
     2273        rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
     2274        if (rc != EOK)
     2275                return rc;
     2276
     2277        /* Send the packet */
     2278        rc = tcp_queue_packet(socket, socket_data, packet, 1);
     2279        if (rc != EOK)
     2280                return rc;
     2281
     2282        /* Flush packets */
    18732283        packet = tcp_get_packets_to_send(socket, socket_data);
    18742284        fibril_rwlock_write_unlock(socket_data->local_lock);
    18752285        fibril_rwlock_write_unlock(&tcp_globals.lock);
    1876         if(packet){
    1877                 // send the packet
     2286
     2287        if (packet) {
     2288                /* Send the packet */
    18782289                tcp_send_packets(socket_data->device_id, packet);
    18792290        }
     2291
    18802292        return EOK;
    18812293}
    18822294
    1883 int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize){
    1884         ERROR_DECLARE;
    1885 
    1886         packet_dimension_ref packet_dimension;
    1887         tcp_header_ref header;
     2295int tcp_create_notification_packet(packet_t **packet, socket_core_t *socket,
     2296    tcp_socket_data_t *socket_data, int synchronize, int finalize)
     2297{
     2298        packet_dimension_t *packet_dimension;
     2299        tcp_header_t *header;
     2300        int rc;
    18882301
    18892302        assert(packet);
    18902303
    1891         // get the device packet dimension
    1892         ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
    1893         // get a new packet
    1894         *packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE, packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix);
    1895         if(! * packet){
     2304        /* Get the device packet dimension */
     2305        rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
     2306            &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
     2307        if (rc != EOK)
     2308                return rc;
     2309
     2310        /* Get a new packet */
     2311        *packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE,
     2312            packet_dimension->addr_len, packet_dimension->prefix,
     2313            packet_dimension->suffix);
     2314       
     2315        if (!*packet)
    18962316                return ENOMEM;
    1897         }
    1898         // allocate space in the packet
     2317
     2318        /* Allocate space in the packet */
    18992319        header = PACKET_SUFFIX(*packet, tcp_header_t);
    1900         if(! header){
     2320        if (!header)
    19012321                tcp_release_and_return(*packet, ENOMEM);
    1902         }
    1903 
    1904         tcp_prepare_operation_header(socket, socket_data, header, synchronize, finalize);
     2322
     2323        tcp_prepare_operation_header(socket, socket_data, header, synchronize,
     2324            finalize);
     2325
    19052326        return EOK;
    19062327}
    19072328
    1908 int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen){
    1909         ERROR_DECLARE;
    1910 
    1911         socket_core_ref accepted;
    1912         socket_core_ref socket;
    1913         tcp_socket_data_ref socket_data;
    1914         packet_dimension_ref packet_dimension;
     2329int tcp_accept_message(socket_cores_t *local_sockets, int socket_id,
     2330    int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
     2331{
     2332        socket_core_t *accepted;
     2333        socket_core_t *socket;
     2334        tcp_socket_data_t *socket_data;
     2335        packet_dimension_t *packet_dimension;
     2336        int rc;
    19152337
    19162338        assert(local_sockets);
     
    19182340        assert(addrlen);
    19192341
    1920         // find the socket
     2342        /* Find the socket */
    19212343        socket = socket_cores_find(local_sockets, socket_id);
    1922         if(! socket){
     2344        if (!socket)
    19232345                return ENOTSOCK;
    1924         }
    1925         // get the socket specific data
    1926         socket_data = (tcp_socket_data_ref) socket->specific_data;
     2346
     2347        /* Get the socket specific data */
     2348        socket_data = (tcp_socket_data_t *) socket->specific_data;
    19272349        assert(socket_data);
    19282350
    1929         // check state
    1930         if(socket_data->state != TCP_SOCKET_LISTEN){
     2351        /* Check state */
     2352        if (socket_data->state != TCP_SOCKET_LISTEN)
    19312353                return EINVAL;
    1932         }
    1933 
    1934         do{
     2354
     2355        do {
    19352356                socket_id = dyn_fifo_value(&socket->accepted);
    1936                 if(socket_id < 0){
     2357                if (socket_id < 0)
    19372358                        return ENOTSOCK;
    1938                 }
    19392359                socket_id *= -1;
    19402360
    19412361                accepted = socket_cores_find(local_sockets, socket_id);
    1942                 if(! accepted){
     2362                if (!accepted)
    19432363                        return ENOTSOCK;
    1944                 }
    1945                 // get the socket specific data
    1946                 socket_data = (tcp_socket_data_ref) accepted->specific_data;
     2364
     2365                /* Get the socket specific data */
     2366                socket_data = (tcp_socket_data_t *) accepted->specific_data;
    19472367                assert(socket_data);
    1948                 // TODO can it be in another state?
    1949                 if(socket_data->state == TCP_SOCKET_ESTABLISHED){
    1950                         ERROR_PROPAGATE(data_reply(socket_data->addr, socket_data->addrlen));
    1951                         ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
     2368                /* TODO can it be in another state? */
     2369                if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
     2370                        rc = data_reply(socket_data->addr,
     2371                            socket_data->addrlen);
     2372                        if (rc != EOK)
     2373                                return rc;
     2374                        rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
     2375                            &tcp_globals.dimensions, socket_data->device_id,
     2376                            &packet_dimension);
     2377                        if (rc != EOK)
     2378                                return rc;
    19522379                        *addrlen = socket_data->addrlen;
    1953                         *data_fragment_size = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size);
    1954                         if(new_socket_id > 0){
    1955                                 ERROR_PROPAGATE(socket_cores_update(local_sockets, accepted->socket_id, new_socket_id));
     2380
     2381                        *data_fragment_size =
     2382                            ((packet_dimension->content <
     2383                            socket_data->data_fragment_size) ?
     2384                            packet_dimension->content :
     2385                            socket_data->data_fragment_size);
     2386       
     2387                        if (new_socket_id > 0) {
     2388                                rc = socket_cores_update(local_sockets,
     2389                                    accepted->socket_id, new_socket_id);
     2390                                if (rc != EOK)
     2391                                        return rc;
    19562392                                accepted->socket_id = new_socket_id;
    19572393                        }
    19582394                }
    19592395                dyn_fifo_pop(&socket->accepted);
    1960         }while(socket_data->state != TCP_SOCKET_ESTABLISHED);
     2396        } while (socket_data->state != TCP_SOCKET_ESTABLISHED);
     2397
    19612398        printf("ret accept %d\n", accepted->socket_id);
    19622399        return accepted->socket_id;
    19632400}
    19642401
    1965 void tcp_free_socket_data(socket_core_ref socket){
    1966         tcp_socket_data_ref socket_data;
     2402void tcp_free_socket_data(socket_core_t *socket)
     2403{
     2404        tcp_socket_data_t *socket_data;
    19672405
    19682406        assert(socket);
     
    19702408        printf("destroy_socket %d\n", socket->socket_id);
    19712409
    1972         // get the socket specific data
    1973         socket_data = (tcp_socket_data_ref) socket->specific_data;
     2410        /* Get the socket specific data */
     2411        socket_data = (tcp_socket_data_t *) socket->specific_data;
    19742412        assert(socket_data);
    1975         //free the pseudo header
    1976         if(socket_data->pseudo_header){
    1977                 if(socket_data->headerlen){
     2413
     2414        /* Free the pseudo header */
     2415        if (socket_data->pseudo_header) {
     2416                if (socket_data->headerlen) {
    19782417                        printf("d pseudo\n");
    19792418                        free(socket_data->pseudo_header);
     
    19822421                socket_data->pseudo_header = NULL;
    19832422        }
     2423
    19842424        socket_data->headerlen = 0;
    1985         // free the address
    1986         if(socket_data->addr){
    1987                 if(socket_data->addrlen){
     2425
     2426        /* Free the address */
     2427        if (socket_data->addr) {
     2428                if (socket_data->addrlen) {
    19882429                        printf("d addr\n");
    19892430                        free(socket_data->addr);
     
    19952436}
    19962437
    1997 int tcp_release_and_return(packet_t packet, int result){
     2438/** Releases the packet and returns the result.
     2439 *
     2440 * @param[in] packet    The packet queue to be released.
     2441 * @param[in] result    The result to be returned.
     2442 * @return              The result parameter.
     2443 */
     2444int tcp_release_and_return(packet_t *packet, int result)
     2445{
    19982446        pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    19992447        return result;
     
    20022450/** Default thread for new connections.
    20032451 *
    2004  *  @param[in] iid The initial message identifier.
    2005  *  @param[in] icall The initial message call structure.
     2452 * @param[in] iid       The initial message identifier.
     2453 * @param[in] icall     The initial message call structure.
    20062454 *
    20072455 */
     
    20132461         */
    20142462        ipc_answer_0(iid, EOK);
    2015        
    2016         while(true) {
     2463
     2464        while (true) {
    20172465                ipc_call_t answer;
    20182466                int answer_count;
    2019                
     2467
    20202468                /* Clear the answer structure */
    20212469                refresh_answer(&answer, &answer_count);
    2022                
     2470
    20232471                /* Fetch the next message */
    20242472                ipc_call_t call;
    20252473                ipc_callid_t callid = async_get_call(&call);
    2026                
     2474
    20272475                /* Process the message */
    20282476                int res = tl_module_message_standalone(callid, &call, &answer,
    20292477                    &answer_count);
    2030                
    2031                 /* End if said to either by the message or the processing result */
    2032                 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
     2478
     2479                /*
     2480                 * End if told to either by the message or the processing
     2481                 * result.
     2482                 */
     2483                if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
     2484                    (res == EHANGUP))
    20332485                        return;
    2034                
    2035                 /* Answer the message */
     2486
     2487                /*
     2488                 * Answer the message
     2489                 */
    20362490                answer_call(callid, res, &answer, answer_count);
    20372491        }
     
    20402494/** Starts the module.
    20412495 *
    2042  *  @param argc The count of the command line arguments. Ignored parameter.
    2043  *  @param argv The command line parameters. Ignored parameter.
    2044  *
    2045  *  @returns EOK on success.
    2046  *  @returns Other error codes as defined for each specific module start function.
    2047  *
     2496 * @return              EOK on success.
     2497 * @return              Other error codes as defined for each specific module
     2498 *                      start function.
    20482499 */
    2049 int main(int argc, char *argv[])
    2050 {
    2051         ERROR_DECLARE;
    2052        
    2053         /* Start the module */
    2054         if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection)))
    2055                 return ERROR_CODE;
    2056        
    2057         return EOK;
     2500int
     2501main(int argc, char *argv[])
     2502{
     2503        int rc;
     2504
     2505        rc = tl_module_start_standalone(tl_client_connection);
     2506        return rc;
    20582507}
    20592508
Note: See TracChangeset for help on using the changeset viewer.