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


Ignore:
Timestamp:
2010-11-25T13:42:50Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8df8415
Parents:
a93d79a (diff), eb667613 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

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

    ra93d79a r8fb1bf82  
    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>
    4448#include <errno.h>
    45 #include <err.h>
    4649
    4750#include <ipc/ipc.h>
     
    7275#include <tl_interface.h>
    7376
    74 #include "tcp.h"
    75 #include "tcp_header.h"
    76 #include "tcp_module.h"
    77 
    7877/** TCP module name. */
    7978#define NAME    "TCP protocol"
     
    110109
    111110/** Returns a value indicating whether the value is in the interval respecting
    112  *  the possible overflow.
     111 * the possible overflow.
    113112 *
    114  *  The high end and/or the value may overflow, be lower than the low value.
    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.
     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.
    118118 */
    119119#define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal) \
     
    126126 */
    127127typedef struct tcp_timeout tcp_timeout_t;
    128 
    129 /** Type definition of the TCP timeout pointer.
    130  *  @see tcp_timeout
    131  */
    132 typedef tcp_timeout_t *tcp_timeout_ref;
    133128
    134129/** TCP reply timeout data.
     
    144139
    145140        /** Local sockets. */
    146         socket_cores_ref local_sockets;
     141        socket_cores_t *local_sockets;
    147142
    148143        /** Socket identifier. */
     
    165160};
    166161
    167 /** Releases the packet and returns the result.
    168  *  @param[in] packet The packet queue to be released.
    169  *  @param[in] result The result to be returned.
    170  *  @return The result parameter.
    171  */
    172 int tcp_release_and_return(packet_t packet, int result);
    173 
    174 void tcp_prepare_operation_header(socket_core_ref socket,
    175     tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize,
    176     int finalize);
    177 int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
    178     socket_core_ref socket, tcp_socket_data_ref socket_data,
    179     size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
    180     int globals_read_only);
    181 void tcp_free_socket_data(socket_core_ref socket);
    182 
    183 int tcp_timeout(void *data);
    184 
    185 int tcp_release_after_timeout(void *data);
    186 
    187 int tcp_process_packet(device_id_t device_id, packet_t packet,
    188     services_t error);
    189 int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
    190     struct sockaddr *addr, socklen_t addrlen);
    191 int tcp_queue_prepare_packet(socket_core_ref socket,
    192     tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
    193 int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
    194     packet_t packet, size_t data_length);
    195 packet_t tcp_get_packets_to_send(socket_core_ref socket,
    196     tcp_socket_data_ref socket_data);
    197 void tcp_send_packets(device_id_t device_id, packet_t packet);
    198 
    199 void tcp_process_acknowledgement(socket_core_ref socket,
    200     tcp_socket_data_ref socket_data, tcp_header_ref header);
    201 packet_t tcp_send_prepare_packet(socket_core_ref socket,
    202     tcp_socket_data_ref socket_data, packet_t packet, size_t data_length,
    203     size_t sequence_number);
    204 packet_t tcp_prepare_copy(socket_core_ref socket,
    205     tcp_socket_data_ref socket_data, packet_t packet, size_t data_length,
    206     size_t sequence_number);
    207 void tcp_retransmit_packet(socket_core_ref socket,
    208     tcp_socket_data_ref socket_data, size_t sequence_number);
    209 int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket,
    210     tcp_socket_data_ref socket_data, int synchronize, int finalize);
    211 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data);
    212 
    213 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data);
    214 
    215 int tcp_process_listen(socket_core_ref listening_socket,
    216     tcp_socket_data_ref listening_socket_data, tcp_header_ref header,
    217     packet_t packet, struct sockaddr *src, struct sockaddr *dest,
    218     size_t addrlen);
    219 int tcp_process_syn_sent(socket_core_ref socket,
    220     tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
    221 int tcp_process_syn_received(socket_core_ref socket,
    222     tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
    223 int tcp_process_established(socket_core_ref socket,
    224     tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet,
    225     int fragments, size_t total_length);
    226 int tcp_queue_received_packet(socket_core_ref socket,
    227     tcp_socket_data_ref socket_data, packet_t packet, int fragments,
    228     size_t total_length);
    229 
    230 int tcp_received_msg(device_id_t device_id, packet_t packet,
    231     services_t receiver, services_t error);
    232 int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call);
    233 
    234 int tcp_listen_message(socket_cores_ref local_sockets, int socket_id,
    235     int backlog);
    236 int tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
    237     struct sockaddr *addr, socklen_t addrlen);
    238 int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id,
    239     int flags, size_t * addrlen);
    240 int tcp_send_message(socket_cores_ref local_sockets, int socket_id,
    241     int fragments, size_t * data_fragment_size, int flags);
    242 int tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
    243     int new_socket_id, size_t * data_fragment_size, size_t * addrlen);
    244 int tcp_close_message(socket_cores_ref local_sockets, int socket_id);
     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);
    245218
    246219/** TCP global data. */
    247220tcp_globals_t tcp_globals;
    248221
     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.
     228 */
    249229int tcp_initialize(async_client_conn_t client_connection)
    250230{
    251         ERROR_DECLARE;
     231        int rc;
    252232
    253233        assert(client_connection);
     
    260240        tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
    261241            SERVICE_TCP, client_connection);
    262         if (tcp_globals.ip_phone < 0)
     242        if (tcp_globals.ip_phone < 0) {
     243                fibril_rwlock_write_unlock(&tcp_globals.lock);
    263244                return tcp_globals.ip_phone;
     245        }
    264246       
    265         ERROR_PROPAGATE(socket_ports_initialize(&tcp_globals.sockets));
    266         if (ERROR_OCCURRED(packet_dimensions_initialize(
    267             &tcp_globals.dimensions))) {
     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) {
    268253                socket_ports_destroy(&tcp_globals.sockets);
    269                 return ERROR_CODE;
     254                goto out;
    270255        }
    271256
    272257        tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
     258
     259out:
    273260        fibril_rwlock_write_unlock(&tcp_globals.lock);
    274 
    275         return EOK;
    276 }
    277 
    278 int
    279 tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
    280     services_t error)
    281 {
    282         ERROR_DECLARE;
     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;
    283268
    284269        if (receiver != SERVICE_TCP)
     
    286271
    287272        fibril_rwlock_write_lock(&tcp_globals.lock);
    288         if (ERROR_OCCURRED(tcp_process_packet(device_id, packet, error)))
     273        rc = tcp_process_packet(device_id, packet, error);
     274        if (rc != EOK)
    289275                fibril_rwlock_write_unlock(&tcp_globals.lock);
    290276
    291         printf("receive %d \n", ERROR_CODE);
    292 
    293         return ERROR_CODE;
    294 }
    295 
    296 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error)
    297 {
    298         ERROR_DECLARE;
    299 
     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{
    300284        size_t length;
    301285        size_t offset;
    302286        int result;
    303         tcp_header_ref header;
    304         socket_core_ref socket;
    305         tcp_socket_data_ref socket_data;
    306         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;
    307291        size_t total_length;
    308292        uint32_t checksum;
     
    313297        struct sockaddr *dest;
    314298        size_t addrlen;
    315 
    316         if (error) {
    317                 switch (error) {
    318                 case SERVICE_ICMP:
    319                         // process error
    320                         result = icmp_client_process_packet(packet, &type,
    321                             &code, NULL, NULL);
    322                         if (result < 0)
    323                                 return tcp_release_and_return(packet, result);
    324 
    325                         length = (size_t) result;
    326                         if (ERROR_OCCURRED(packet_trim(packet, length, 0))) {
    327                                 return tcp_release_and_return(packet,
    328                                     ERROR_CODE);
    329                         }
    330                         break;
    331 
    332                 default:
    333                         return tcp_release_and_return(packet, ENOTSUP);
    334                 }
    335         }
    336 
    337         // 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? */
    338321        result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
    339322        if (result < 0)
     
    349332                return tcp_release_and_return(packet, NO_DATA);
    350333
    351         // trim all but TCP header
    352         if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))
    353                 return tcp_release_and_return(packet, ERROR_CODE);
    354 
    355         // get tcp header
    356         header = (tcp_header_ref) packet_get_data(packet);
     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);
    357341        if (!header)
    358342                return tcp_release_and_return(packet, NO_DATA);
     
    367351        addrlen = (size_t) result;
    368352
    369         if (ERROR_OCCURRED(tl_set_address_port(src, addrlen,
    370             ntohs(header->source_port))))
    371                 return tcp_release_and_return(packet, ERROR_CODE);
     353        rc = tl_set_address_port(src, addrlen, ntohs(header->source_port));
     354        if (rc != EOK)
     355                return tcp_release_and_return(packet, rc);
    372356       
    373         // find the destination socket
     357        /* Find the destination socket */
    374358        socket = socket_port_find(&tcp_globals.sockets,
    375359            ntohs(header->destination_port), (const char *) src, addrlen);
    376360        if (!socket) {
    377                 // find the listening destination socket
     361                /* Find the listening destination socket */
    378362                socket = socket_port_find(&tcp_globals.sockets,
    379363                    ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,
    380364                    0);
    381                 if (!socket) {
    382                         if (tl_prepare_icmp_packet(tcp_globals.net_phone,
    383                             tcp_globals.icmp_phone, packet, error) == EOK) {
    384                                 icmp_destination_unreachable_msg(
    385                                     tcp_globals.icmp_phone, ICMP_PORT_UNREACH,
    386                                     0, packet);
    387                         }
    388                         return EADDRNOTAVAIL;
    389                 }
    390         }
     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
    391376        printf("socket id %d\n", socket->socket_id);
    392         socket_data = (tcp_socket_data_ref) socket->specific_data;
     377        socket_data = (tcp_socket_data_t *) socket->specific_data;
    393378        assert(socket_data);
    394379
    395         // some data received, clear the timeout counter
     380        /* Some data received, clear the timeout counter */
    396381        socket_data->timeout_count = 0;
    397382
    398         // count the received packet fragments
     383        /* Count the received packet fragments */
    399384        next_packet = packet;
    400385        fragments = 0;
     
    402387        total_length = 0;
    403388        do {
    404                 ++fragments;
     389                fragments++;
    405390                length = packet_get_data_length(next_packet);
    406391                if (length <= 0)
     
    409394                total_length += length;
    410395
    411                 // add partial checksum if set
     396                /* Add partial checksum if set */
    412397                if (!error) {
    413398                        checksum = compute_checksum(checksum,
     
    421406
    422407        if (error)
    423                 goto error;
     408                goto has_error_service;
    424409       
    425410        if (socket_data->state == TCP_SOCKET_LISTEN) {
    426 
    427411                if (socket_data->pseudo_header) {
    428412                        free(socket_data->pseudo_header);
     
    431415                }
    432416
    433                 if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src,
    434                     addrlen, dest, addrlen, total_length,
    435                     &socket_data->pseudo_header, &socket_data->headerlen))) {
     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) {
    436421                        fibril_rwlock_write_unlock(socket_data->local_lock);
    437                         return tcp_release_and_return(packet, ERROR_CODE);
    438                 }
    439 
    440         } else if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
    441             socket_data->pseudo_header, socket_data->headerlen,
    442             total_length))) {
    443                 fibril_rwlock_write_unlock(socket_data->local_lock);
    444                 return tcp_release_and_return(packet, ERROR_CODE);
     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) {
     429                        fibril_rwlock_write_unlock(socket_data->local_lock);
     430                        return tcp_release_and_return(packet, rc);
     431                }
    445432        }
    446433       
     
    452439                fibril_rwlock_write_unlock(socket_data->local_lock);
    453440
    454                 if (ERROR_NONE(tl_prepare_icmp_packet(tcp_globals.net_phone,
    455                     tcp_globals.icmp_phone, packet, error))) {
    456                         // checksum error ICMP
     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 */
    457445                        icmp_parameter_problem_msg(tcp_globals.icmp_phone,
    458446                            ICMP_PARAM_POINTER,
     
    464452        }
    465453
    466 error:
     454has_error_service:
    467455        fibril_rwlock_read_unlock(&tcp_globals.lock);
    468456
    469         // TODO error reporting/handling
     457        /* TODO error reporting/handling */
    470458        switch (socket_data->state) {
    471459        case TCP_SOCKET_LISTEN:
    472                 ERROR_CODE = tcp_process_listen(socket, socket_data, header,
    473                     packet, src, dest, addrlen);
     460                rc = tcp_process_listen(socket, socket_data, header, packet,
     461                    src, dest, addrlen);
    474462                break;
    475463        case TCP_SOCKET_SYN_RECEIVED:
    476                 ERROR_CODE = tcp_process_syn_received(socket, socket_data,
    477                     header, packet);
     464                rc = tcp_process_syn_received(socket, socket_data, header,
     465                    packet);
    478466                break;
    479467        case TCP_SOCKET_SYN_SENT:
    480                 ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header,
    481                     packet);
     468                rc = tcp_process_syn_sent(socket, socket_data, header, packet);
    482469                break;
    483470        case TCP_SOCKET_FIN_WAIT_1:
    484                 // ack changing the state to FIN_WAIT_2 gets processed later
     471                /* ack changing the state to FIN_WAIT_2 gets processed later */
    485472        case TCP_SOCKET_FIN_WAIT_2:
    486                 // fin changing state to LAST_ACK gets processed later
     473                /* fin changing state to LAST_ACK gets processed later */
    487474        case TCP_SOCKET_LAST_ACK:
    488                 // ack releasing the socket get processed later
     475                /* ack releasing the socket get processed later */
    489476        case TCP_SOCKET_CLOSING:
    490                 // ack releasing the socket gets processed later
     477                /* ack releasing the socket gets processed later */
    491478        case TCP_SOCKET_ESTABLISHED:
    492                 ERROR_CODE = tcp_process_established(socket, socket_data,
    493                     header, packet, fragments, total_length);
     479                rc = tcp_process_established(socket, socket_data, header,
     480                    packet, fragments, total_length);
    494481                break;
    495482        default:
     
    497484        }
    498485
    499         if (ERROR_CODE != EOK) {
    500                 printf("process %d\n", ERROR_CODE);
     486        if (rc != EOK) {
    501487                fibril_rwlock_write_unlock(socket_data->local_lock);
     488                printf("process %d\n", rc);
    502489        }
    503490
     
    505492}
    506493
    507 int
    508 tcp_process_established(socket_core_ref socket, tcp_socket_data_ref socket_data,
    509     tcp_header_ref header, packet_t packet, int fragments,
     494int tcp_process_established(socket_core_t *socket, tcp_socket_data_t *
     495    socket_data, tcp_header_t *header, packet_t *packet, int fragments,
    510496    size_t total_length)
    511497{
    512         ERROR_DECLARE;
    513 
    514         packet_t next_packet;
    515         packet_t tmp_packet;
     498        packet_t *next_packet;
     499        packet_t *tmp_packet;
    516500        uint32_t old_incoming;
    517501        size_t order;
     
    520504        size_t offset;
    521505        uint32_t new_sequence_number;
     506        int rc;
    522507
    523508        assert(socket);
     
    533518                socket_data->fin_incoming = new_sequence_number;
    534519
    535         // trim begining if containing expected data
     520        /* Trim begining if containing expected data */
    536521        if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
    537522            socket_data->next_incoming, new_sequence_number + total_length)) {
    538523
    539                 // get the acknowledged offset
     524                /* Get the acknowledged offset */
    540525                if (socket_data->next_incoming < new_sequence_number) {
    541526                        offset = new_sequence_number -
     
    549534                total_length -= offset;
    550535                length = packet_get_data_length(packet);
    551                 // trim the acknowledged data
     536
     537                /* Trim the acknowledged data */
    552538                while (length <= offset) {
    553                         // release the acknowledged packets
     539                        /* Release the acknowledged packets */
    554540                        next_packet = pq_next(packet);
    555541                        pq_release_remote(tcp_globals.net_phone,
     
    560546                }
    561547
    562                 if ((offset > 0) && (ERROR_OCCURRED(packet_trim(packet,
    563                     offset, 0))))
    564                         return tcp_release_and_return(packet, ERROR_CODE);
     548                if (offset > 0) {
     549                        rc = packet_trim(packet, offset, 0);
     550                        if (rc != EOK)
     551                                return tcp_release_and_return(packet, rc);
     552                }
    565553
    566554                assert(new_sequence_number == socket_data->next_incoming);
    567555        }
    568556
    569         // release if overflowing the window
     557        /* Release if overflowing the window */
    570558/*
    571559        if (IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming +
     
    594582                        if (length <= offset)
    595583                                next_packet = pq_next(next_packet);
    596                         else if (ERROR_OCCURRED(packet_trim(next_packet, 0,
    597                             length - offset)))
    598                                 return tcp_release_and_return(packet,
    599                                     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);
     590                        }
    600591                        offset -= length;
    601592                        total_length -= length - offset;
     
    614605        }
    615606*/
    616         // the expected one arrived?
     607        /* The expected one arrived? */
    617608        if (new_sequence_number == socket_data->next_incoming) {
    618609                printf("expected\n");
    619                 // process acknowledgement
     610                /* Process acknowledgement */
    620611                tcp_process_acknowledgement(socket, socket_data, header);
    621612
    622                 // remove the header
     613                /* Remove the header */
    623614                total_length -= TCP_HEADER_LENGTH(header);
    624                 if (ERROR_OCCURRED(packet_trim(packet,
    625                     TCP_HEADER_LENGTH(header), 0)))
    626                         return tcp_release_and_return(packet, ERROR_CODE);
     615                rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
     616                if (rc != EOK)
     617                        return tcp_release_and_return(packet, rc);
    627618
    628619                if (total_length) {
    629                         ERROR_PROPAGATE(tcp_queue_received_packet(socket,
    630                             socket_data, packet, fragments, total_length));
     620                        rc = tcp_queue_received_packet(socket, socket_data,
     621                            packet, fragments, total_length);
     622                        if (rc != EOK)
     623                                return rc;
    631624                } else {
    632625                        total_length = 1;
     
    636629                packet = socket_data->incoming;
    637630                while (packet) {
    638 
    639                         if (ERROR_OCCURRED(pq_get_order(socket_data->incoming,
    640                             &order, NULL))) {
    641                                 // remove the corrupted packet
     631                        rc = pq_get_order(socket_data->incoming, &order, NULL);
     632                        if (rc != EOK) {
     633                                /* Remove the corrupted packet */
    642634                                next_packet = pq_detach(packet);
    643635                                if (packet == socket_data->incoming)
     
    652644                        if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
    653645                            old_incoming, socket_data->next_incoming)) {
    654                                 // move to the next
     646                                /* Move to the next */
    655647                                packet = pq_next(packet);
    656                                 // coninual data?
     648                                /* Coninual data? */
    657649                        } else if (IS_IN_INTERVAL_OVERFLOW(old_incoming,
    658650                            sequence_number, socket_data->next_incoming)) {
    659                                 // detach the packet
     651                                /* Detach the packet */
    660652                                next_packet = pq_detach(packet);
    661653                                if (packet == socket_data->incoming)
    662654                                        socket_data->incoming = next_packet;
    663                                 // get data length
     655                                /* Get data length */
    664656                                length = packet_get_data_length(packet);
    665657                                new_sequence_number = sequence_number + length;
    666658                                if (length <= 0) {
    667                                         // remove the empty packet
     659                                        /* Remove the empty packet */
    668660                                        pq_release_remote(tcp_globals.net_phone,
    669661                                            packet_get_id(packet));
     
    671663                                        continue;
    672664                                }
    673                                 // exactly following
     665                                /* Exactly following */
    674666                                if (sequence_number ==
    675667                                    socket_data->next_incoming) {
    676                                         // queue received data
    677                                         ERROR_PROPAGATE(
    678                                             tcp_queue_received_packet(socket,
     668                                        /* Queue received data */
     669                                        rc = tcp_queue_received_packet(socket,
    679670                                            socket_data, packet, 1,
    680                                             packet_get_data_length(packet)));
     671                                            packet_get_data_length(packet));
     672                                        if (rc != EOK)
     673                                                return rc;
    681674                                        socket_data->next_incoming =
    682675                                            new_sequence_number;
    683676                                        packet = next_packet;
    684677                                        continue;
    685                                         // at least partly following data?
    686                                 } else if (IS_IN_INTERVAL_OVERFLOW(
    687                                     sequence_number, socket_data->next_incoming,
    688                                     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)) {
    689682                                        if (socket_data->next_incoming <
    690683                                            new_sequence_number) {
     
    696689                                                    new_sequence_number;
    697690                                        }
    698                                         if (ERROR_NONE(packet_trim(packet,
    699                                             length, 0))) {
    700                                                 // queue received data
    701                                                 ERROR_PROPAGATE(
    702                                                     tcp_queue_received_packet(
     691                                        rc = packet_trim(packet,length, 0);
     692                                        if (rc == EOK) {
     693                                                /* Queue received data */
     694                                                rc = tcp_queue_received_packet(
    703695                                                    socket, socket_data, packet,
    704696                                                    1, packet_get_data_length(
    705                                                     packet)));
     697                                                    packet));
     698                                                if (rc != EOK)
     699                                                        return rc;
    706700                                                socket_data->next_incoming =
    707701                                                    new_sequence_number;
     
    710704                                        }
    711705                                }
    712                                 // remove the duplicit or corrupted packet
     706                                /* Remove the duplicit or corrupted packet */
    713707                                pq_release_remote(tcp_globals.net_phone,
    714708                                    packet_get_id(packet));
     
    723717            socket_data->next_incoming + socket_data->window)) {
    724718                printf("in window\n");
    725                 // process acknowledgement
     719                /* Process acknowledgement */
    726720                tcp_process_acknowledgement(socket, socket_data, header);
    727721
    728                 // remove the header
     722                /* Remove the header */
    729723                total_length -= TCP_HEADER_LENGTH(header);
    730                 if (ERROR_OCCURRED(packet_trim(packet,
    731                     TCP_HEADER_LENGTH(header), 0)))
    732                         return tcp_release_and_return(packet, ERROR_CODE);
     724                rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
     725                if (rc != EOK)
     726                        return tcp_release_and_return(packet, rc);
    733727
    734728                next_packet = pq_detach(packet);
    735729                length = packet_get_data_length(packet);
    736                 if (ERROR_OCCURRED(pq_add(&socket_data->incoming, packet,
    737                     new_sequence_number, length))) {
    738                         // remove the corrupted packets
     730                rc = pq_add(&socket_data->incoming, packet, new_sequence_number,
     731                    length);
     732                if (rc != EOK) {
     733                        /* Remove the corrupted packets */
    739734                        pq_release_remote(tcp_globals.net_phone,
    740735                            packet_get_id(packet));
     
    746741                                tmp_packet = pq_detach(next_packet);
    747742                                length = packet_get_data_length(next_packet);
    748                                 if (ERROR_OCCURRED(pq_set_order(next_packet,
    749                                     new_sequence_number, length)) ||
    750                                     ERROR_OCCURRED(pq_insert_after(packet,
    751                                     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) {
    752752                                        pq_release_remote(tcp_globals.net_phone,
    753753                                            packet_get_id(next_packet));
     
    758758        } else {
    759759                printf("unexpected\n");
    760                 // release duplicite or restricted
     760                /* Release duplicite or restricted */
    761761                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    762762        }
    763763
    764         // change state according to the acknowledging incoming fin
     764        /* Change state according to the acknowledging incoming fin */
    765765        if (IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming,
    766766            socket_data->next_incoming)) {
     
    771771                        socket_data->state = TCP_SOCKET_CLOSING;
    772772                        break;
    773                         //case TCP_ESTABLISHED:
     773                /*case TCP_ESTABLISHED:*/
    774774                default:
    775775                        socket_data->state = TCP_SOCKET_CLOSE_WAIT;
     
    780780        packet = tcp_get_packets_to_send(socket, socket_data);
    781781        if (!packet) {
    782                 // create the notification packet
    783                 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
    784                     socket_data, 0, 0));
    785                 ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data,
    786                     packet, 1));
     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;
    787790                packet = tcp_send_prepare_packet(socket, socket_data, packet, 1,
    788791                    socket_data->last_outgoing + 1);
     
    791794        fibril_rwlock_write_unlock(socket_data->local_lock);
    792795
    793         // send the packet
     796        /* Send the packet */
    794797        tcp_send_packets(socket_data->device_id, packet);
    795798
     
    797800}
    798801
    799 int
    800 tcp_queue_received_packet(socket_core_ref socket,
    801     tcp_socket_data_ref socket_data, packet_t packet, int fragments,
     802int tcp_queue_received_packet(socket_core_t *socket,
     803    tcp_socket_data_t *socket_data, packet_t *packet, int fragments,
    802804    size_t total_length)
    803805{
    804         ERROR_DECLARE;
    805 
    806         packet_dimension_ref packet_dimension;
     806        packet_dimension_t *packet_dimension;
     807        int rc;
    807808
    808809        assert(socket);
     
    813814        assert(socket_data->window > total_length);
    814815
    815         // queue the received packet
    816         if (ERROR_OCCURRED(dyn_fifo_push(&socket->received,
    817             packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) ||
    818             ERROR_OCCURRED(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
    819             &tcp_globals.dimensions, socket_data->device_id,
    820             &packet_dimension))) {
    821                 return tcp_release_and_return(packet, ERROR_CODE);
    822         }
    823 
    824         // 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 */
    825827        socket_data->window -= total_length;
    826828
    827         // notify the destination socket
     829        /* Notify the destination socket */
    828830        async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
    829831            (ipcarg_t) socket->socket_id,
     
    835837}
    836838
    837 int
    838 tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data,
    839     tcp_header_ref header, packet_t packet)
    840 {
    841         ERROR_DECLARE;
    842 
    843         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;
    844844
    845845        assert(socket);
     
    852852                return tcp_release_and_return(packet, EINVAL);
    853853       
    854         // process acknowledgement
     854        /* Process acknowledgement */
    855855        tcp_process_acknowledgement(socket, socket_data, header);
    856856
    857857        socket_data->next_incoming = ntohl(header->sequence_number) + 1;
    858         // release additional packets
     858
     859        /* Release additional packets */
    859860        next_packet = pq_detach(packet);
    860861        if (next_packet) {
     
    862863                    packet_get_id(next_packet));
    863864        }
    864         // trim if longer than the header
    865         if ((packet_get_data_length(packet) > sizeof(*header)) &&
    866             ERROR_OCCURRED(packet_trim(packet, 0,
    867             packet_get_data_length(packet) - sizeof(*header)))) {
    868                 return tcp_release_and_return(packet, ERROR_CODE);
    869         }
     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
    870874        tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
    871875        fibril_mutex_lock(&socket_data->operation.mutex);
    872876        socket_data->operation.result = tcp_queue_packet(socket, socket_data,
    873877            packet, 1);
     878
    874879        if (socket_data->operation.result == EOK) {
    875880                socket_data->state = TCP_SOCKET_ESTABLISHED;
     
    877882                if (packet) {
    878883                        fibril_rwlock_write_unlock( socket_data->local_lock);
    879                         // send the packet
     884                        /* Send the packet */
    880885                        tcp_send_packets(socket_data->device_id, packet);
    881                         // signal the result
     886                        /* Signal the result */
    882887                        fibril_condvar_signal( &socket_data->operation.condvar);
    883888                        fibril_mutex_unlock( &socket_data->operation.mutex);
     
    885890                }
    886891        }
     892
    887893        fibril_mutex_unlock(&socket_data->operation.mutex);
    888894        return tcp_release_and_return(packet, EINVAL);
    889895}
    890896
    891 int
    892 tcp_process_listen(socket_core_ref listening_socket,
    893     tcp_socket_data_ref listening_socket_data, tcp_header_ref header,
    894     packet_t packet, struct sockaddr *src, struct sockaddr *dest,
     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,
    895900    size_t addrlen)
    896901{
    897         ERROR_DECLARE;
    898 
    899         packet_t next_packet;
    900         socket_core_ref socket;
    901         tcp_socket_data_ref socket_data;
     902        packet_t *next_packet;
     903        socket_core_t *socket;
     904        tcp_socket_data_t *socket_data;
    902905        int socket_id;
    903906        int listening_socket_id = listening_socket->socket_id;
    904907        int listening_port = listening_socket->port;
     908        int rc;
    905909
    906910        assert(listening_socket);
     
    913917                return tcp_release_and_return(packet, EINVAL);
    914918
    915         socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
     919        socket_data = (tcp_socket_data_t *) malloc(sizeof(*socket_data));
    916920        if (!socket_data)
    917921                return tcp_release_and_return(packet, ENOMEM);
     
    930934                return tcp_release_and_return(packet, ENOMEM);
    931935        }
     936
    932937        memcpy(socket_data->addr, src, socket_data->addrlen);
    933938        socket_data->dest_port = ntohs(header->source_port);
    934         if (ERROR_OCCURRED(tl_set_address_port(socket_data->addr,
    935             socket_data->addrlen, socket_data->dest_port))) {
     939        rc = tl_set_address_port(socket_data->addr, socket_data->addrlen,
     940            socket_data->dest_port);
     941        if (rc != EOK) {
    936942                free(socket_data->addr);
    937943                free(socket_data);
    938                 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    939                 return ERROR_CODE;
    940         }
    941 
    942         // create a socket
     944                return tcp_release_and_return(packet, rc);
     945        }
     946
     947        /* Create a socket */
    943948        socket_id = -1;
    944         if (ERROR_OCCURRED(socket_create(socket_data->local_sockets,
    945             listening_socket->phone, socket_data, &socket_id))) {
     949        rc = socket_create(socket_data->local_sockets, listening_socket->phone,
     950            socket_data, &socket_id);
     951        if (rc != EOK) {
    946952                free(socket_data->addr);
    947953                free(socket_data);
    948                 return tcp_release_and_return(packet, ERROR_CODE);
     954                return tcp_release_and_return(packet, rc);
    949955        }
    950956
     
    958964        fibril_rwlock_write_lock(&tcp_globals.lock);
    959965
    960         // find the destination socket
     966        /* Find the destination socket */
    961967        listening_socket = socket_port_find(&tcp_globals.sockets,
    962968            listening_port, SOCKET_MAP_KEY_LISTENING, 0);
    963         if ((!listening_socket) ||
     969        if (!listening_socket ||
    964970            (listening_socket->socket_id != listening_socket_id)) {
    965971                fibril_rwlock_write_unlock(&tcp_globals.lock);
    966                 // a shadow may remain until app hangs up
     972                /* A shadow may remain until app hangs up */
    967973                return tcp_release_and_return(packet, EOK /*ENOTSOCK*/);
    968974        }
    969975        listening_socket_data =
    970             (tcp_socket_data_ref) listening_socket->specific_data;
     976            (tcp_socket_data_t *) listening_socket->specific_data;
    971977        assert(listening_socket_data);
    972978
     
    976982            socket_id);
    977983        if (!socket) {
    978                 // where is the socket?!?
     984                /* Where is the socket?!? */
    979985                fibril_rwlock_write_unlock(&tcp_globals.lock);
    980986                return ENOTSOCK;
    981987        }
    982         socket_data = (tcp_socket_data_ref) socket->specific_data;
     988        socket_data = (tcp_socket_data_t *) socket->specific_data;
    983989        assert(socket_data);
    984990
    985         ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port,
    986             socket, (const char *) socket_data->addr, socket_data->addrlen);
     991        rc = socket_port_add(&tcp_globals.sockets, listening_port, socket,
     992            (const char *) socket_data->addr, socket_data->addrlen);
    987993        assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
    988994            (const char *) socket_data->addr, socket_data->addrlen));
    989995
    990 //      ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket,
     996//      rc = socket_bind_free_port(&tcp_globals.sockets, socket,
    991997//          TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
    992998//          tcp_globals.last_used_port);
    993999//      tcp_globals.last_used_port = socket->port;
    9941000        fibril_rwlock_write_unlock(&tcp_globals.lock);
    995         if (ERROR_CODE != EOK) {
     1001        if (rc != EOK) {
    9961002                socket_destroy(tcp_globals.net_phone, socket->socket_id,
    9971003                    socket_data->local_sockets, &tcp_globals.sockets,
    9981004                    tcp_free_socket_data);
    999                 return tcp_release_and_return(packet, ERROR_CODE);
     1005                return tcp_release_and_return(packet, rc);
    10001006        }
    10011007
     
    10031009        socket_data->next_incoming = ntohl(header->sequence_number) + 1;
    10041010
    1005         // release additional packets
     1011        /* Release additional packets */
    10061012        next_packet = pq_detach(packet);
    10071013        if (next_packet) {
     
    10101016        }
    10111017
    1012         // trim if longer than the header
    1013         if ((packet_get_data_length(packet) > sizeof(*header)) &&
    1014             ERROR_OCCURRED(packet_trim(packet, 0,
    1015             packet_get_data_length(packet) - sizeof(*header)))) {
     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) {
    10161034                socket_destroy(tcp_globals.net_phone, socket->socket_id,
    10171035                    socket_data->local_sockets, &tcp_globals.sockets,
    10181036                    tcp_free_socket_data);
    1019                 return tcp_release_and_return(packet, ERROR_CODE);
    1020         }
    1021 
    1022         tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
    1023 
    1024         if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))) {
    1025                 socket_destroy(tcp_globals.net_phone, socket->socket_id,
    1026                     socket_data->local_sockets, &tcp_globals.sockets,
    1027                     tcp_free_socket_data);
    1028                 return ERROR_CODE;
     1037                return rc;
    10291038        }
    10301039
     
    10401049        fibril_rwlock_write_unlock(socket_data->local_lock);
    10411050
    1042         // send the packet
     1051        /* Send the packet */
    10431052        tcp_send_packets(socket_data->device_id, packet);
    10441053
     
    10461055}
    10471056
    1048 int
    1049 tcp_process_syn_received(socket_core_ref socket,
    1050     tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet)
    1051 {
    1052         ERROR_DECLARE;
    1053 
    1054         socket_core_ref listening_socket;
    1055         tcp_socket_data_ref listening_socket_data;
     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;
    10561063
    10571064        assert(socket);
     
    10641071                return tcp_release_and_return(packet, EINVAL);
    10651072
    1066         // process acknowledgement
     1073        /* Process acknowledgement */
    10671074        tcp_process_acknowledgement(socket, socket_data, header);
    10681075
     
    10741081        if (listening_socket) {
    10751082                listening_socket_data =
    1076                     (tcp_socket_data_ref) listening_socket->specific_data;
     1083                    (tcp_socket_data_t *) listening_socket->specific_data;
    10771084                assert(listening_socket_data);
    10781085
    1079                 // queue the received packet
    1080                 if (ERROR_NONE(dyn_fifo_push(&listening_socket->accepted,
    1081                     (-1 * socket->socket_id),
    1082                     listening_socket_data->backlog))) {
    1083 
    1084                         // notify the destination socket
     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 */
    10851091                        async_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
    10861092                            (ipcarg_t) listening_socket->socket_id,
     
    10921098                }
    10931099        }
    1094         // send FIN
     1100        /* Send FIN */
    10951101        socket_data->state = TCP_SOCKET_FIN_WAIT_1;
    10961102
    1097         // create the notification packet
    1098         ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
    1099             socket_data, 0, 1));
    1100 
    1101         // send the packet
    1102         ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
    1103 
    1104         // flush packets
     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 */
    11051114        packet = tcp_get_packets_to_send(socket, socket_data);
    11061115        fibril_rwlock_write_unlock(socket_data->local_lock);
    11071116        if (packet) {
    1108                 // send the packet
     1117                /* Send the packet */
    11091118                tcp_send_packets(socket_data->device_id, packet);
    11101119        }
     
    11131122}
    11141123
    1115 void
    1116 tcp_process_acknowledgement(socket_core_ref socket,
    1117     tcp_socket_data_ref socket_data, tcp_header_ref header)
     1124void tcp_process_acknowledgement(socket_core_t *socket,
     1125    tcp_socket_data_t *socket_data, tcp_header_t *header)
    11181126{
    11191127        size_t number;
    11201128        size_t length;
    1121         packet_t packet;
    1122         packet_t next;
    1123         packet_t acknowledged = NULL;
     1129        packet_t *packet;
     1130        packet_t *next;
     1131        packet_t *acknowledged = NULL;
    11241132        uint32_t old;
    11251133
     
    11331141
    11341142        number = ntohl(header->acknowledgement_number);
    1135         // if more data acknowledged
     1143
     1144        /* If more data acknowledged */
    11361145        if (number != socket_data->expected) {
    11371146                old = socket_data->expected;
     
    11441153                        case TCP_SOCKET_LAST_ACK:
    11451154                        case TCP_SOCKET_CLOSING:
    1146                                 // fin acknowledged - release the socket in
    1147                                 // another fibril
     1155                                /*
     1156                                 * FIN acknowledged - release the socket in
     1157                                 * another fibril.
     1158                                 */
    11481159                                tcp_prepare_timeout(tcp_release_after_timeout,
    11491160                                    socket, socket_data, 0,
     
    11551166                        }
    11561167                }
    1157                 // update the treshold if higher than set
     1168
     1169                /* Update the treshold if higher than set */
    11581170                if (number + ntohs(header->window) >
    11591171                    socket_data->expected + socket_data->treshold) {
     
    11611173                            socket_data->expected;
    11621174                }
    1163                 // set new expected sequence number
     1175
     1176                /* Set new expected sequence number */
    11641177                socket_data->expected = number;
    11651178                socket_data->expected_count = 1;
     
    11731186                                        socket_data->outgoing = next;
    11741187
    1175                                 // add to acknowledged or release
     1188                                /* Add to acknowledged or release */
    11761189                                if (pq_add(&acknowledged, packet, 0, 0) != EOK)
    11771190                                        pq_release_remote(tcp_globals.net_phone,
     
    11811194                                break;
    11821195                }
    1183                 // release acknowledged
     1196
     1197                /* Release acknowledged */
    11841198                if (acknowledged) {
    11851199                        pq_release_remote(tcp_globals.net_phone,
     
    11871201                }
    11881202                return;
    1189                 // if the same as the previous time
    1190         }
     1203                /* If the same as the previous time */
     1204        }
     1205
    11911206        if (number == socket_data->expected) {
    1192                 // increase the counter
    1193                 ++socket_data->expected_count;
     1207                /* Increase the counter */
     1208                socket_data->expected_count++;
    11941209                if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) {
    11951210                        socket_data->expected_count = 1;
    1196                         // TODO retransmit lock
     1211                        /* TODO retransmit lock */
    11971212                        //tcp_retransmit_packet(socket, socket_data, number);
    11981213                }
     
    12001215}
    12011216
    1202 int tcp_message_standalone(ipc_callid_t callid, ipc_call_t * call,
    1203     ipc_call_t * answer, int *answer_count)
    1204 {
    1205         ERROR_DECLARE;
    1206 
    1207         packet_t packet;
     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;
    12081236
    12091237        assert(call);
     
    12141242        switch (IPC_GET_METHOD(*call)) {
    12151243        case NET_TL_RECEIVED:
    1216                 //fibril_rwlock_read_lock(&tcp_globals.lock);
    1217                 if (ERROR_NONE(packet_translate_remote(tcp_globals.net_phone,
    1218                     &packet, IPC_GET_PACKET(call)))) {
    1219                         ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call),
    1220                             packet, SERVICE_TCP, IPC_GET_ERROR(call));
    1221                 }
    1222                 //fibril_rwlock_read_unlock(&tcp_globals.lock);
    1223                 return ERROR_CODE;
    1224 
     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;
    12251255        case IPC_M_CONNECT_TO_ME:
    12261256                return tcp_process_client_messages(callid, *call);
     
    12301260}
    12311261
    1232 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data)
     1262void tcp_refresh_socket_data(tcp_socket_data_t *socket_data)
    12331263{
    12341264        assert(socket_data);
     
    12461276}
    12471277
    1248 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data)
     1278void tcp_initialize_socket_data(tcp_socket_data_t *socket_data)
    12491279{
    12501280        assert(socket_data);
     
    12691299        ipc_call_t answer;
    12701300        int answer_count;
    1271         tcp_socket_data_ref socket_data;
    1272         socket_core_ref socket;
    1273         packet_dimension_ref packet_dimension;
     1301        tcp_socket_data_t *socket_data;
     1302        socket_core_t *socket;
     1303        packet_dimension_t *packet_dimension;
    12741304
    12751305        /*
     
    12851315        while (keep_on_going) {
    12861316
    1287                 // answer the call
     1317                /* Answer the call */
    12881318                answer_call(callid, res, &answer, answer_count);
    1289                 // refresh data
     1319                /* Refresh data */
    12901320                refresh_answer(&answer, &answer_count);
    1291                 // get the next call
     1321                /* Get the next call */
    12921322                callid = async_get_call(&call);
    12931323
    1294                 // process the call
     1324                /* Process the call */
    12951325                switch (IPC_GET_METHOD(call)) {
    12961326                case IPC_M_PHONE_HUNGUP:
     
    13011331                case NET_SOCKET:
    13021332                        socket_data =
    1303                             (tcp_socket_data_ref) malloc(sizeof(*socket_data));
     1333                            (tcp_socket_data_t *) malloc(sizeof(*socket_data));
    13041334                        if (!socket_data) {
    13051335                                res = ENOMEM;
     
    13481378                                    SOCKET_GET_SOCKET_ID(call));
    13491379                                if (socket) {
    1350                                         socket_data = (tcp_socket_data_ref)
     1380                                        socket_data = (tcp_socket_data_t *)
    13511381                                            socket->specific_data;
    13521382                                        assert(socket_data);
     
    13751405                        if (res != EOK)
    13761406                                break;
    1377                         // the global lock may be released in the
    1378                         // tcp_connect_message() function
     1407                        /*
     1408                         * The global lock may be released in the
     1409                         * tcp_connect_message() function.
     1410                         */
    13791411                        fibril_rwlock_write_lock(&tcp_globals.lock);
    13801412                        fibril_rwlock_write_lock(&lock);
     
    14901522        }
    14911523
    1492         // release the application phone
     1524        /* Release the application phone */
    14931525        ipc_hangup(app_phone);
    14941526
    14951527        printf("release\n");
    1496         // release all local sockets
     1528        /* Release all local sockets */
    14971529        socket_cores_release(tcp_globals.net_phone, &local_sockets,
    14981530            &tcp_globals.sockets, tcp_free_socket_data);
     
    15031535int tcp_timeout(void *data)
    15041536{
    1505         tcp_timeout_ref timeout = data;
     1537        tcp_timeout_t *timeout = data;
    15061538        int keep_write_lock = false;
    1507         socket_core_ref socket;
    1508         tcp_socket_data_ref socket_data;
     1539        socket_core_t *socket;
     1540        tcp_socket_data_t *socket_data;
    15091541
    15101542        assert(timeout);
    15111543
    1512         // sleep the given timeout
     1544        /* Sleep the given timeout */
    15131545        async_usleep(timeout->timeout);
    1514         // lock the globals
     1546        /* Lock the globals */
    15151547        if (timeout->globals_read_only)
    15161548                fibril_rwlock_read_lock(&tcp_globals.lock);
     
    15181550                fibril_rwlock_write_lock(&tcp_globals.lock);
    15191551
    1520         // find the pending operation socket
     1552        /* Find the pending operation socket */
    15211553        socket = socket_port_find(&tcp_globals.sockets, timeout->port,
    15221554            timeout->key, timeout->key_length);
    1523         if (!(socket && (socket->socket_id == timeout->socket_id)))
     1555        if (!socket || (socket->socket_id != timeout->socket_id))
    15241556                goto out;
    15251557       
    1526         socket_data = (tcp_socket_data_ref) socket->specific_data;
     1558        socket_data = (tcp_socket_data_t *) socket->specific_data;
    15271559        assert(socket_data);
    15281560        if (socket_data->local_sockets != timeout->local_sockets)
     
    15311563        fibril_rwlock_write_lock(socket_data->local_lock);
    15321564        if (timeout->sequence_number) {
    1533                 // increase the timeout counter;
    1534                 ++socket_data->timeout_count;
     1565                /* Increase the timeout counter */
     1566                socket_data->timeout_count++;
    15351567                if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
    1536                         // TODO release as connection lost
     1568                        /* TODO release as connection lost */
    15371569                        //tcp_refresh_socket_data(socket_data);
    15381570                        fibril_rwlock_write_unlock(socket_data->local_lock);
    15391571                } else {
    1540                         // retransmit
     1572                        /* Retransmit */
    15411573//                      tcp_retransmit_packet(socket,
    15421574//                          socket_data, timeout->sequence_number);
     
    15451577        } else {
    15461578                fibril_mutex_lock(&socket_data->operation.mutex);
    1547                 // set the timeout operation result if state not
    1548                 // changed
     1579                /* Set the timeout operation result if state not changed */
    15491580                if (socket_data->state == timeout->state) {
    15501581                        socket_data->operation.result = ETIMEOUT;
    1551                         // notify the main fibril
     1582
     1583                        /* Notify the main fibril */
    15521584                        fibril_condvar_signal(&socket_data->operation.condvar);
    1553                         // keep the global write lock
     1585
     1586                        /* Keep the global write lock */
    15541587                        keep_write_lock = true;
    15551588                } else {
    1556                         // operation is ok, do nothing
    1557                         // unlocking from now on, so the unlocki
    1558                         // order does not matter...
     1589                        /*
     1590                         * Operation is ok, do nothing.
     1591                         * Unlocking from now on, so the unlocking
     1592                         * order does not matter.
     1593                         */
    15591594                        fibril_rwlock_write_unlock(socket_data->local_lock);
    15601595                }
     
    15631598
    15641599out:
    1565         // unlock only if no socket
     1600        /* Unlock only if no socket */
    15661601        if (timeout->globals_read_only)
    15671602                fibril_rwlock_read_unlock(&tcp_globals.lock);
    15681603        else if (!keep_write_lock)
    1569                 // release if not desired
     1604                /* Release if not desired */
    15701605                fibril_rwlock_write_unlock(&tcp_globals.lock);
    15711606       
    1572         // release the timeout structure
     1607        /* Release the timeout structure */
    15731608        free(timeout);
    15741609        return EOK;
     
    15771612int tcp_release_after_timeout(void *data)
    15781613{
    1579         tcp_timeout_ref timeout = data;
    1580         socket_core_ref socket;
    1581         tcp_socket_data_ref socket_data;
     1614        tcp_timeout_t *timeout = data;
     1615        socket_core_t *socket;
     1616        tcp_socket_data_t *socket_data;
    15821617        fibril_rwlock_t *local_lock;
    15831618
    15841619        assert(timeout);
    15851620
    1586         // sleep the given timeout
     1621        /* Sleep the given timeout */
    15871622        async_usleep(timeout->timeout);
    1588         // lock the globals
     1623
     1624        /* Lock the globals */
    15891625        fibril_rwlock_write_lock(&tcp_globals.lock);
    1590         // find the pending operation socket
     1626
     1627        /* Find the pending operation socket */
    15911628        socket = socket_port_find(&tcp_globals.sockets, timeout->port,
    15921629            timeout->key, timeout->key_length);
     1630
    15931631        if (socket && (socket->socket_id == timeout->socket_id)) {
    1594                 socket_data = (tcp_socket_data_ref) socket->specific_data;
     1632                socket_data = (tcp_socket_data_t *) socket->specific_data;
    15951633                assert(socket_data);
    15961634                if (socket_data->local_sockets == timeout->local_sockets) {
     
    16031641                }
    16041642        }
    1605         // unlock the globals
     1643
     1644        /* Unlock the globals */
    16061645        fibril_rwlock_write_unlock(&tcp_globals.lock);
    1607         // release the timeout structure
     1646
     1647        /* Release the timeout structure */
    16081648        free(timeout);
     1649
    16091650        return EOK;
    16101651}
    16111652
    1612 void
    1613 tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
    1614     size_t sequence_number)
    1615 {
    1616         packet_t packet;
    1617         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;
    16181658        size_t data_length;
    16191659
     
    16221662        assert(socket->specific_data == socket_data);
    16231663
    1624         // sent packet?
     1664        /* Sent packet? */
    16251665        packet = pq_find(socket_data->outgoing, sequence_number);
    16261666        printf("retransmit %d\n", packet_get_id(packet));
     
    16381678}
    16391679
    1640 int
    1641 tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog)
    1642 {
    1643         socket_core_ref socket;
    1644         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;
    16451685
    16461686        assert(local_sockets);
     
    16491689                return EINVAL;
    16501690
    1651         // find the socket
     1691        /* Find the socket */
    16521692        socket = socket_cores_find(local_sockets, socket_id);
    16531693        if (!socket)
    16541694                return ENOTSOCK;
    16551695       
    1656         // get the socket specific data
    1657         socket_data = (tcp_socket_data_ref) socket->specific_data;
     1696        /* Get the socket specific data */
     1697        socket_data = (tcp_socket_data_t *) socket->specific_data;
    16581698        assert(socket_data);
    1659         // set the backlog
     1699
     1700        /* Set the backlog */
    16601701        socket_data->backlog = backlog;
    16611702
     
    16631704}
    16641705
    1665 int
    1666 tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
     1706int tcp_connect_message(socket_cores_t *local_sockets, int socket_id,
    16671707    struct sockaddr *addr, socklen_t addrlen)
    16681708{
    1669         ERROR_DECLARE;
    1670 
    1671         socket_core_ref socket;
     1709        socket_core_t *socket;
     1710        int rc;
    16721711
    16731712        assert(local_sockets);
     
    16751714        assert(addrlen > 0);
    16761715
    1677         // find the socket
     1716        /* Find the socket */
    16781717        socket = socket_cores_find(local_sockets, socket_id);
    16791718        if (!socket)
    16801719                return ENOTSOCK;
    16811720       
    1682         if (ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr,
    1683             addrlen))) {
     1721        rc = tcp_connect_core(socket, local_sockets, addr, addrlen);
     1722        if (rc != EOK) {
    16841723                tcp_free_socket_data(socket);
    1685                 // unbind if bound
     1724                /* Unbind if bound */
    16861725                if (socket->port > 0) {
    16871726                        socket_ports_exclude(&tcp_globals.sockets,
     
    16901729                }
    16911730        }
    1692         return ERROR_CODE;
    1693 }
    1694 
    1695 int
    1696 tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
     1731        return rc;
     1732}
     1733
     1734int tcp_connect_core(socket_core_t *socket, socket_cores_t *local_sockets,
    16971735    struct sockaddr *addr, socklen_t addrlen)
    16981736{
    1699         ERROR_DECLARE;
    1700 
    1701         tcp_socket_data_ref socket_data;
    1702         packet_t packet;
     1737        tcp_socket_data_t *socket_data;
     1738        packet_t *packet;
     1739        int rc;
    17031740
    17041741        assert(socket);
     
    17061743        assert(addrlen > 0);
    17071744
    1708         // get the socket specific data
    1709         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;
    17101747        assert(socket_data);
    17111748        assert(socket->specific_data == socket_data);
     
    17151752                return EINVAL;
    17161753
    1717         // get the destination port
    1718         ERROR_PROPAGATE(tl_get_address_port(addr, addrlen,
    1719             &socket_data->dest_port));
     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       
    17201759        if (socket->port <= 0) {
    1721                 // try to find a free port
    1722                 ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets,
    1723                     socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
    1724                     tcp_globals.last_used_port));
    1725                 // set the next port as the search starting port number
     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 */
    17261767                tcp_globals.last_used_port = socket->port;
    17271768        }
    17281769
    1729         ERROR_PROPAGATE(ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
     1770        rc = ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
    17301771            addr, addrlen, &socket_data->device_id,
    1731             &socket_data->pseudo_header, &socket_data->headerlen));
    1732 
    1733         // create the notification packet
    1734         ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
    1735             socket_data, 1, 0));
    1736 
    1737         // unlock the globals and wait for an operation
     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 */
    17381782        fibril_rwlock_write_unlock(&tcp_globals.lock);
    17391783
    17401784        socket_data->addr = addr;
    17411785        socket_data->addrlen = addrlen;
    1742         // send the packet
    1743         if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1)) ||
    1744             ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data,
    1745             0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))) {
    1746 
     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)) {
    17471793                socket_data->addr = NULL;
    17481794                socket_data->addrlen = 0;
    17491795                fibril_rwlock_write_lock(&tcp_globals.lock);
    1750 
    17511796        } else {
    1752 
    17531797                packet = tcp_get_packets_to_send(socket, socket_data);
    17541798                if (packet) {
    17551799                        fibril_mutex_lock(&socket_data->operation.mutex);
    17561800                        fibril_rwlock_write_unlock(socket_data->local_lock);
    1757                         // send the packet
     1801
     1802                        /* Send the packet */
    17581803                        printf("connecting %d\n", packet_get_id(packet));
    17591804                        tcp_send_packets(socket_data->device_id, packet);
    17601805
    1761                         // wait for a reply
     1806                        /* Wait for a reply */
    17621807                        fibril_condvar_wait(&socket_data->operation.condvar,
    17631808                            &socket_data->operation.mutex);
    1764                         ERROR_CODE = socket_data->operation.result;
    1765                         if (ERROR_CODE != EOK) {
     1809                        rc = socket_data->operation.result;
     1810                        if (rc != EOK) {
    17661811                                socket_data->addr = NULL;
    17671812                                socket_data->addrlen = 0;
     
    17701815                        socket_data->addr = NULL;
    17711816                        socket_data->addrlen = 0;
    1772                         ERROR_CODE = EINTR;
     1817                        rc = EINTR;
    17731818                }
    17741819        }
    17751820
    17761821        fibril_mutex_unlock(&socket_data->operation.mutex);
    1777 
    1778         // return the result
    1779         return ERROR_CODE;
    1780 }
    1781 
    1782 int
    1783 tcp_queue_prepare_packet(socket_core_ref socket,
    1784     tcp_socket_data_ref socket_data, packet_t packet, size_t data_length)
    1785 {
    1786         ERROR_DECLARE;
    1787 
    1788         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;
    17891830
    17901831        assert(socket);
     
    17921833        assert(socket->specific_data == socket_data);
    17931834
    1794         // get tcp header
    1795         header = (tcp_header_ref) packet_get_data(packet);
     1835        /* Get TCP header */
     1836        header = (tcp_header_t *) packet_get_data(packet);
    17961837        if (!header)
    17971838                return NO_DATA;
     
    18011842        header->sequence_number = htonl(socket_data->next_outgoing);
    18021843
    1803         if (ERROR_OCCURRED(packet_set_addr(packet, NULL,
    1804             (uint8_t *) socket_data->addr, socket_data->addrlen)))
     1844        rc = packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr,
     1845            socket_data->addrlen);
     1846        if (rc != EOK)
    18051847                return tcp_release_and_return(packet, EINVAL);
    18061848
    1807         // remember the outgoing FIN
     1849        /* Remember the outgoing FIN */
    18081850        if (header->finalize)
    18091851                socket_data->fin_outgoing = socket_data->next_outgoing;
     
    18121854}
    18131855
    1814 int
    1815 tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
    1816     packet_t packet, size_t data_length)
    1817 {
    1818         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;
    18191860
    18201861        assert(socket);
     
    18221863        assert(socket->specific_data == socket_data);
    18231864
    1824         ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet,
    1825             data_length));
    1826 
    1827         if (ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet,
    1828             socket_data->next_outgoing, data_length)))
    1829                 return tcp_release_and_return(packet, ERROR_CODE);
     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);
    18301873
    18311874        socket_data->next_outgoing += data_length;
     
    18331876}
    18341877
    1835 packet_t
    1836 tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data)
    1837 {
    1838         ERROR_DECLARE;
    1839 
    1840         packet_t packet;
    1841         packet_t copy;
    1842         packet_t sending = NULL;
    1843         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;
    18441885        size_t data_length;
     1886        int rc;
    18451887
    18461888        assert(socket);
     
    18521894                pq_get_order(packet, NULL, &data_length);
    18531895
    1854                 // send only if fits into the window
    1855                 // respecting the possible overflow
     1896                /*
     1897                 * Send only if fits into the window, respecting the possible
     1898                 * overflow.
     1899                 */
    18561900                if (!IS_IN_INTERVAL_OVERFLOW(
    18571901                    (uint32_t) socket_data->last_outgoing,
     
    18671911                if (!sending) {
    18681912                        sending = copy;
    1869                 } else if (ERROR_OCCURRED(pq_insert_after(previous, copy))) {
    1870                         pq_release_remote(tcp_globals.net_phone,
    1871                             packet_get_id(copy));
    1872                         return sending;
     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));
     1918                                return sending;
     1919                        }
    18731920                }
    18741921
    18751922                previous = copy;
    18761923                packet = pq_next(packet);
    1877                 // overflow occurred ?
    1878                 if ((!packet) &&
     1924
     1925                /* Overflow occurred? */
     1926                if (!packet &&
    18791927                    (socket_data->last_outgoing > socket_data->next_outgoing)) {
    18801928                        printf("gpts overflow\n");
    1881                         // continue from the beginning
     1929                        /* Continue from the beginning */
    18821930                        packet = socket_data->outgoing;
    18831931                }
     
    18881936}
    18891937
    1890 packet_t
    1891 tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
    1892     packet_t packet, size_t data_length, size_t sequence_number)
    1893 {
    1894         ERROR_DECLARE;
    1895 
    1896         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;
    18971942        uint32_t checksum;
     1943        int rc;
    18981944
    18991945        assert(socket);
     
    19011947        assert(socket->specific_data == socket_data);
    19021948
    1903         // adjust the pseudo header
    1904         if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
    1905             socket_data->pseudo_header, socket_data->headerlen,
    1906             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) {
    19071953                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    19081954                return NULL;
    19091955        }
    19101956
    1911         // get the header
    1912         header = (tcp_header_ref) packet_get_data(packet);
     1957        /* Get the header */
     1958        header = (tcp_header_t *) packet_get_data(packet);
    19131959        if (!header) {
    19141960                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     
    19171963        assert(ntohl(header->sequence_number) == sequence_number);
    19181964
    1919         // adjust the header
     1965        /* Adjust the header */
    19201966        if (socket_data->next_incoming) {
    19211967                header->acknowledgement_number =
     
    19251971        header->window = htons(socket_data->window);
    19261972
    1927         // checksum
     1973        /* Checksum */
    19281974        header->checksum = 0;
    19291975        checksum = compute_checksum(0, socket_data->pseudo_header,
    19301976            socket_data->headerlen);
    1931         checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet),
     1977        checksum = compute_checksum(checksum,
     1978            (uint8_t *) packet_get_data(packet),
    19321979            packet_get_data_length(packet));
    19331980        header->checksum = htons(flip_checksum(compact_checksum(checksum)));
    19341981
    1935         // prepare the packet
    1936         if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0,
    1937             0, 0)) || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket,
    1938             socket_data, sequence_number, socket_data->state,
    1939             socket_data->timeout, true))) {
     1982        /* Prepare the packet */
     1983        rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0);
     1984        if (rc != EOK) {
    19401985                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    19411986                return NULL;
    19421987        }
    19431988
     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
    19441996        return packet;
    19451997}
    19461998
    1947 packet_t
    1948 tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref socket_data,
    1949     packet_t packet, size_t data_length, size_t sequence_number)
    1950 {
    1951         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;
    19522003
    19532004        assert(socket);
     
    19552006        assert(socket->specific_data == socket_data);
    19562007
    1957         // make a copy of the packet
     2008        /* Make a copy of the packet */
    19582009        copy = packet_get_copy(tcp_globals.net_phone, packet);
    19592010        if (!copy)
     
    19642015}
    19652016
    1966 void tcp_send_packets(device_id_t device_id, packet_t packet)
    1967 {
    1968         packet_t next;
     2017void tcp_send_packets(device_id_t device_id, packet_t *packet)
     2018{
     2019        packet_t *next;
    19692020
    19702021        while (packet) {
     
    19762027}
    19772028
    1978 void
    1979 tcp_prepare_operation_header(socket_core_ref socket,
    1980     tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize,
     2029void tcp_prepare_operation_header(socket_core_t *socket,
     2030    tcp_socket_data_t *socket_data, tcp_header_t *header, int synchronize,
    19812031    int finalize)
    19822032{
     
    19942044}
    19952045
    1996 int
    1997 tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
    1998     socket_core_ref socket, tcp_socket_data_ref socket_data,
     2046int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
     2047    socket_core_t *socket, tcp_socket_data_t *socket_data,
    19992048    size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
    20002049    int globals_read_only)
    20012050{
    2002         tcp_timeout_ref operation_timeout;
     2051        tcp_timeout_t *operation_timeout;
    20032052        fid_t fibril;
    20042053
     
    20072056        assert(socket->specific_data == socket_data);
    20082057
    2009         // prepare the timeout with key bundle structure
     2058        /* Prepare the timeout with key bundle structure */
    20102059        operation_timeout = malloc(sizeof(*operation_timeout) +
    20112060            socket->key_length + 1);
     
    20222071        operation_timeout->state = state;
    20232072
    2024         // copy the key
     2073        /* Copy the key */
    20252074        operation_timeout->key = ((char *) operation_timeout) +
    20262075            sizeof(*operation_timeout);
     
    20292078        operation_timeout->key[operation_timeout->key_length] = '\0';
    20302079
    2031         // prepare the timeouting thread
     2080        /* Prepare the timeouting thread */
    20322081        fibril = fibril_create(timeout_function, operation_timeout);
    20332082        if (!fibril) {
    20342083                free(operation_timeout);
    2035                 return EPARTY;
     2084                return EPARTY;  /* FIXME: use another EC */
    20362085        }
    20372086//      fibril_mutex_lock(&socket_data->operation.mutex);
    2038         // start the timeouting fibril
     2087        /* Start the timeout fibril */
    20392088        fibril_add_ready(fibril);
    20402089        //socket_data->state = state;
     
    20422091}
    20432092
    2044 int
    2045 tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
    2046     size_t * addrlen)
    2047 {
    2048         ERROR_DECLARE;
    2049 
    2050         socket_core_ref socket;
    2051         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;
    20522098        int packet_id;
    2053         packet_t packet;
     2099        packet_t *packet;
    20542100        size_t length;
     2101        int rc;
    20552102
    20562103        assert(local_sockets);
    20572104
    2058         // find the socket
     2105        /* Find the socket */
    20592106        socket = socket_cores_find(local_sockets, socket_id);
    20602107        if (!socket)
    20612108                return ENOTSOCK;
    20622109
    2063         // get the socket specific data
     2110        /* Get the socket specific data */
    20642111        if (!socket->specific_data)
    20652112                return NO_DATA;
    20662113
    2067         socket_data = (tcp_socket_data_ref) socket->specific_data;
    2068 
    2069         // check state
     2114        socket_data = (tcp_socket_data_t *) socket->specific_data;
     2115
     2116        /* Check state */
    20702117        if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
    20712118            (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
    20722119                return ENOTCONN;
    20732120
    2074         // send the source address if desired
     2121        /* Send the source address if desired */
    20752122        if (addrlen) {
    2076                 ERROR_PROPAGATE(data_reply(socket_data->addr,
    2077                     socket_data->addrlen));
     2123                rc = data_reply(socket_data->addr, socket_data->addrlen);
     2124                if (rc != EOK)
     2125                        return rc;
    20782126                *addrlen = socket_data->addrlen;
    20792127        }
    20802128
    2081         // get the next received packet
     2129        /* Get the next received packet */
    20822130        packet_id = dyn_fifo_value(&socket->received);
    20832131        if (packet_id < 0)
    20842132                return NO_DATA;
    20852133
    2086         ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet,
    2087             packet_id));
    2088 
    2089         // reply the packets
    2090         ERROR_PROPAGATE(socket_reply_packets(packet, &length));
    2091 
    2092         // release the packet
     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 */
    20932144        dyn_fifo_pop(&socket->received);
    20942145        pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    2095         // return the total length
     2146
     2147        /* Return the total length */
    20962148        return (int) length;
    20972149}
    20982150
    2099 int
    2100 tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments,
    2101     size_t * data_fragment_size, int flags)
    2102 {
    2103         ERROR_DECLARE;
    2104 
    2105         socket_core_ref socket;
    2106         tcp_socket_data_ref socket_data;
    2107         packet_dimension_ref packet_dimension;
    2108         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;
    21092158        size_t total_length;
    2110         tcp_header_ref header;
     2159        tcp_header_t *header;
    21112160        int index;
    21122161        int result;
     2162        int rc;
    21132163
    21142164        assert(local_sockets);
    21152165        assert(data_fragment_size);
    21162166
    2117         // find the socket
     2167        /* Find the socket */
    21182168        socket = socket_cores_find(local_sockets, socket_id);
    21192169        if (!socket)
    21202170                return ENOTSOCK;
    21212171
    2122         // get the socket specific data
     2172        /* Get the socket specific data */
    21232173        if (!socket->specific_data)
    21242174                return NO_DATA;
    21252175
    2126         socket_data = (tcp_socket_data_ref) socket->specific_data;
    2127 
    2128         // check state
     2176        socket_data = (tcp_socket_data_t *) socket->specific_data;
     2177
     2178        /* Check state */
    21292179        if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
    21302180            (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
    21312181                return ENOTCONN;
    21322182
    2133         ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
    2134            &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
     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;
    21352187
    21362188        *data_fragment_size =
     
    21382190            packet_dimension->content : socket_data->data_fragment_size);
    21392191
    2140         for (index = 0; index < fragments; ++index) {
    2141                 // read the data fragment
     2192        for (index = 0; index < fragments; index++) {
     2193                /* Read the data fragment */
    21422194                result = tl_socket_read_packet_data(tcp_globals.net_phone,
    21432195                    &packet, TCP_HEADER_SIZE, packet_dimension,
     
    21472199
    21482200                total_length = (size_t) result;
    2149                 // prefix the tcp header
     2201
     2202                /* Prefix the TCP header */
    21502203                header = PACKET_PREFIX(packet, tcp_header_t);
    21512204                if (!header)
     
    21532206
    21542207                tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
    2155                 ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet,
    2156                     0));
    2157         }
    2158 
    2159         // flush packets
     2208                rc = tcp_queue_packet(socket, socket_data, packet, 0);
     2209                if (rc != EOK)
     2210                        return rc;
     2211        }
     2212
     2213        /* Flush packets */
    21602214        packet = tcp_get_packets_to_send(socket, socket_data);
    21612215        fibril_rwlock_write_unlock(socket_data->local_lock);
     
    21632217
    21642218        if (packet) {
    2165                 // send the packet
     2219                /* Send the packet */
    21662220                tcp_send_packets(socket_data->device_id, packet);
    21672221        }
     
    21712225
    21722226int
    2173 tcp_close_message(socket_cores_ref local_sockets, int socket_id)
    2174 {
    2175         ERROR_DECLARE;
    2176 
    2177         socket_core_ref socket;
    2178         tcp_socket_data_ref socket_data;
    2179         packet_t packet;
    2180 
    2181         // find the socket
     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 */
    21822235        socket = socket_cores_find(local_sockets, socket_id);
    21832236        if (!socket)
    21842237                return ENOTSOCK;
    21852238
    2186         // get the socket specific data
    2187         socket_data = (tcp_socket_data_ref) socket->specific_data;
     2239        /* Get the socket specific data */
     2240        socket_data = (tcp_socket_data_t *) socket->specific_data;
    21882241        assert(socket_data);
    21892242
    2190         // check state
     2243        /* Check state */
    21912244        switch (socket_data->state) {
    21922245        case TCP_SOCKET_ESTABLISHED:
     
    22012254
    22022255        default:
    2203                 // just destroy
    2204                 if (ERROR_NONE(socket_destroy(tcp_globals.net_phone, socket_id,
     2256                /* Just destroy */
     2257                rc = socket_destroy(tcp_globals.net_phone, socket_id,
    22052258                    local_sockets, &tcp_globals.sockets,
    2206                     tcp_free_socket_data))) {
     2259                    tcp_free_socket_data);
     2260                if (rc == EOK) {
    22072261                        fibril_rwlock_write_unlock(socket_data->local_lock);
    22082262                        fibril_rwlock_write_unlock(&tcp_globals.lock);
    22092263                }
    2210                 return ERROR_CODE;
    2211         }
    2212 
    2213         // send FIN
    2214         // TODO should I wait to complete?
    2215 
    2216         // create the notification packet
    2217         ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
    2218             socket_data, 0, 1));
    2219 
    2220         // send the packet
    2221         ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
    2222 
    2223         // flush packets
     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 */
    22242283        packet = tcp_get_packets_to_send(socket, socket_data);
    22252284        fibril_rwlock_write_unlock(socket_data->local_lock);
     
    22272286
    22282287        if (packet) {
    2229                 // send the packet
     2288                /* Send the packet */
    22302289                tcp_send_packets(socket_data->device_id, packet);
    22312290        }
     
    22342293}
    22352294
    2236 int
    2237 tcp_create_notification_packet(packet_t * packet, socket_core_ref socket,
    2238     tcp_socket_data_ref socket_data, int synchronize, int finalize)
    2239 {
    2240         ERROR_DECLARE;
    2241 
    2242         packet_dimension_ref packet_dimension;
    2243         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;
    22442301
    22452302        assert(packet);
    22462303
    2247         // get the device packet dimension
    2248         ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
    2249             &tcp_globals.dimensions, socket_data->device_id,
    2250             &packet_dimension));
    2251 
    2252         // get a new 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 */
    22532311        *packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE,
    22542312            packet_dimension->addr_len, packet_dimension->prefix,
     
    22582316                return ENOMEM;
    22592317
    2260         // allocate space in the packet
     2318        /* Allocate space in the packet */
    22612319        header = PACKET_SUFFIX(*packet, tcp_header_t);
    22622320        if (!header)
     
    22692327}
    22702328
    2271 int
    2272 tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
    2273     int new_socket_id, size_t * data_fragment_size, size_t * addrlen)
    2274 {
    2275         ERROR_DECLARE;
    2276 
    2277         socket_core_ref accepted;
    2278         socket_core_ref socket;
    2279         tcp_socket_data_ref socket_data;
    2280         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;
    22812337
    22822338        assert(local_sockets);
     
    22842340        assert(addrlen);
    22852341
    2286         // find the socket
     2342        /* Find the socket */
    22872343        socket = socket_cores_find(local_sockets, socket_id);
    22882344        if (!socket)
    22892345                return ENOTSOCK;
    22902346
    2291         // get the socket specific data
    2292         socket_data = (tcp_socket_data_ref) socket->specific_data;
     2347        /* Get the socket specific data */
     2348        socket_data = (tcp_socket_data_t *) socket->specific_data;
    22932349        assert(socket_data);
    22942350
    2295         // check state
     2351        /* Check state */
    22962352        if (socket_data->state != TCP_SOCKET_LISTEN)
    22972353                return EINVAL;
     
    23072363                        return ENOTSOCK;
    23082364
    2309                 // get the socket specific data
    2310                 socket_data = (tcp_socket_data_ref) accepted->specific_data;
     2365                /* Get the socket specific data */
     2366                socket_data = (tcp_socket_data_t *) accepted->specific_data;
    23112367                assert(socket_data);
    2312                 // TODO can it be in another state?
     2368                /* TODO can it be in another state? */
    23132369                if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
    2314                         ERROR_PROPAGATE(data_reply(socket_data->addr,
    2315                             socket_data->addrlen));
    2316                         ERROR_PROPAGATE(tl_get_ip_packet_dimension(
    2317                             tcp_globals.ip_phone, &tcp_globals.dimensions,
    2318                             socket_data->device_id, &packet_dimension));
     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;
    23192379                        *addrlen = socket_data->addrlen;
    23202380
     
    23262386       
    23272387                        if (new_socket_id > 0) {
    2328                                 ERROR_PROPAGATE(socket_cores_update(
    2329                                     local_sockets, accepted->socket_id,
    2330                                     new_socket_id));
     2388                                rc = socket_cores_update(local_sockets,
     2389                                    accepted->socket_id, new_socket_id);
     2390                                if (rc != EOK)
     2391                                        return rc;
    23312392                                accepted->socket_id = new_socket_id;
    23322393                        }
     
    23392400}
    23402401
    2341 void
    2342 tcp_free_socket_data(socket_core_ref socket)
    2343 {
    2344         tcp_socket_data_ref socket_data;
     2402void tcp_free_socket_data(socket_core_t *socket)
     2403{
     2404        tcp_socket_data_t *socket_data;
    23452405
    23462406        assert(socket);
     
    23482408        printf("destroy_socket %d\n", socket->socket_id);
    23492409
    2350         // get the socket specific data
    2351         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;
    23522412        assert(socket_data);
    2353         //free the pseudo header
     2413
     2414        /* Free the pseudo header */
    23542415        if (socket_data->pseudo_header) {
    23552416                if (socket_data->headerlen) {
     
    23602421                socket_data->pseudo_header = NULL;
    23612422        }
     2423
    23622424        socket_data->headerlen = 0;
    2363         // free the address
     2425
     2426        /* Free the address */
    23642427        if (socket_data->addr) {
    23652428                if (socket_data->addrlen) {
     
    23732436}
    23742437
    2375 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)
    23762445{
    23772446        pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     
    23812450/** Default thread for new connections.
    23822451 *
    2383  *  @param[in] iid The initial message identifier.
    2384  *  @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.
    23852454 *
    23862455 */
     
    23972466                int answer_count;
    23982467
    2399                 /*
    2400                    Clear the answer structure
    2401                  */
     2468                /* Clear the answer structure */
    24022469                refresh_answer(&answer, &answer_count);
    24032470
    2404                 /*
    2405                    Fetch the next message
    2406                  */
     2471                /* Fetch the next message */
    24072472                ipc_call_t call;
    24082473                ipc_callid_t callid = async_get_call(&call);
    24092474
    2410                 /*
    2411                    Process the message
    2412                  */
     2475                /* Process the message */
    24132476                int res = tl_module_message_standalone(callid, &call, &answer,
    24142477                    &answer_count);
    24152478
    24162479                /*
    2417                    End if said to either by the message or the processing result
     2480                 * End if told to either by the message or the processing
     2481                 * result.
    24182482                 */
    24192483                if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
     
    24222486
    24232487                /*
    2424                    Answer the message
     2488                 * Answer the message
    24252489                 */
    24262490                answer_call(callid, res, &answer, answer_count);
     
    24302494/** Starts the module.
    24312495 *
    2432  *  @param argc The count of the command line arguments. Ignored parameter.
    2433  *  @param argv The command line parameters. Ignored parameter.
    2434  *
    2435  *  @returns EOK on success.
    2436  *  @returns Other error codes as defined for each specific module start function.
    2437  *
     2496 * @return              EOK on success.
     2497 * @return              Other error codes as defined for each specific module
     2498 *                      start function.
    24382499 */
    24392500int
    24402501main(int argc, char *argv[])
    24412502{
    2442         ERROR_DECLARE;
    2443 
    2444         /*
    2445            Start the module
    2446          */
    2447         if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection)))
    2448                 return ERROR_CODE;
    2449 
    2450         return EOK;
     2503        int rc;
     2504
     2505        rc = tl_module_start_standalone(tl_client_connection);
     2506        return rc;
    24512507}
    24522508
Note: See TracChangeset for help on using the changeset viewer.