Ignore:
File:
1 edited

Legend:

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

    rfb04cba8 r0578271  
    4444#include <fibril_synch.h>
    4545#include <malloc.h>
    46 /* TODO remove stdio */
     46//TODO remove stdio
    4747#include <stdio.h>
    4848#include <errno.h>
     
    267267}
    268268
    269 int tcp_received_msg(device_id_t device_id, packet_t packet,
    270     services_t receiver, services_t error)
     269int
     270tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
     271    services_t error)
    271272{
    272273        int rc;
     
    308309                break;
    309310        case SERVICE_ICMP:
    310                 /* Process error */
     311                // process error
    311312                result = icmp_client_process_packet(packet, &type, &code, NULL,
    312313                    NULL);
     
    323324        }
    324325
    325         /* TODO process received ipopts? */
     326        // TODO process received ipopts?
    326327        result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
    327328        if (result < 0)
     
    337338                return tcp_release_and_return(packet, NO_DATA);
    338339
    339         /* Trim all but TCP header */
     340        // trim all but TCP header
    340341        rc = packet_trim(packet, offset, 0);
    341342        if (rc != EOK)
    342343                return tcp_release_and_return(packet, rc);
    343344
    344         /* Get tcp header */
     345        // get tcp header
    345346        header = (tcp_header_ref) packet_get_data(packet);
    346347        if (!header)
     
    360361                return tcp_release_and_return(packet, rc);
    361362       
    362         /* Find the destination socket */
     363        // find the destination socket
    363364        socket = socket_port_find(&tcp_globals.sockets,
    364365            ntohs(header->destination_port), (const char *) src, addrlen);
    365366        if (!socket) {
    366                 /* Find the listening destination socket */
     367                // find the listening destination socket
    367368                socket = socket_port_find(&tcp_globals.sockets,
    368369                    ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,
    369370                    0);
    370371        }
    371 
    372372        if (!socket) {
    373373                if (tl_prepare_icmp_packet(tcp_globals.net_phone,
     
    383383        assert(socket_data);
    384384
    385         /* Some data received, clear the timeout counter */
     385        // some data received, clear the timeout counter
    386386        socket_data->timeout_count = 0;
    387387
    388         /* Count the received packet fragments */
     388        // count the received packet fragments
    389389        next_packet = packet;
    390390        fragments = 0;
     
    399399                total_length += length;
    400400
    401                 /* Add partial checksum if set */
     401                // add partial checksum if set
    402402                if (!error) {
    403403                        checksum = compute_checksum(checksum,
     
    447447                    tcp_globals.icmp_phone, packet, error);
    448448                if (rc == EOK) {
    449                         /* Checksum error ICMP */
     449                        // checksum error ICMP
    450450                        icmp_parameter_problem_msg(tcp_globals.icmp_phone,
    451451                            ICMP_PARAM_POINTER,
     
    460460        fibril_rwlock_read_unlock(&tcp_globals.lock);
    461461
    462         /* TODO error reporting/handling */
     462        // TODO error reporting/handling
    463463        switch (socket_data->state) {
    464464        case TCP_SOCKET_LISTEN:
     
    474474                break;
    475475        case TCP_SOCKET_FIN_WAIT_1:
    476                 /* ack changing the state to FIN_WAIT_2 gets processed later */
     476                // ack changing the state to FIN_WAIT_2 gets processed later
    477477        case TCP_SOCKET_FIN_WAIT_2:
    478                 /* fin changing state to LAST_ACK gets processed later */
     478                // fin changing state to LAST_ACK gets processed later
    479479        case TCP_SOCKET_LAST_ACK:
    480                 /* ack releasing the socket get processed later */
     480                // ack releasing the socket get processed later
    481481        case TCP_SOCKET_CLOSING:
    482                 /* ack releasing the socket gets processed later */
     482                // ack releasing the socket gets processed later
    483483        case TCP_SOCKET_ESTABLISHED:
    484484                rc = tcp_process_established(socket, socket_data, header,
     
    497497}
    498498
    499 int tcp_process_established(socket_core_ref socket, tcp_socket_data_ref
    500     socket_data, tcp_header_ref header, packet_t packet, int fragments,
    501     size_t total_length)
     499int
     500tcp_process_established(socket_core_ref socket, tcp_socket_data_ref socket_data,
     501    tcp_header_ref header, packet_t packet, int fragments, size_t total_length)
    502502{
    503503        packet_t next_packet;
     
    523523                socket_data->fin_incoming = new_sequence_number;
    524524
    525         /* Trim begining if containing expected data */
     525        // trim begining if containing expected data
    526526        if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
    527527            socket_data->next_incoming, new_sequence_number + total_length)) {
    528528
    529                 /* Get the acknowledged offset */
     529                // get the acknowledged offset
    530530                if (socket_data->next_incoming < new_sequence_number) {
    531531                        offset = new_sequence_number -
     
    539539                total_length -= offset;
    540540                length = packet_get_data_length(packet);
    541 
    542                 /* Trim the acknowledged data */
     541                // trim the acknowledged data
    543542                while (length <= offset) {
    544                         /* Release the acknowledged packets */
     543                        // release the acknowledged packets
    545544                        next_packet = pq_next(packet);
    546545                        pq_release_remote(tcp_globals.net_phone,
     
    560559        }
    561560
    562         /* Release if overflowing the window */
     561        // release if overflowing the window
    563562/*
    564563        if (IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming +
     
    610609        }
    611610*/
    612         /* The expected one arrived? */
     611        // the expected one arrived?
    613612        if (new_sequence_number == socket_data->next_incoming) {
    614613                printf("expected\n");
    615                 /* Process acknowledgement */
     614                // process acknowledgement
    616615                tcp_process_acknowledgement(socket, socket_data, header);
    617616
    618                 /* Remove the header */
     617                // remove the header
    619618                total_length -= TCP_HEADER_LENGTH(header);
    620619                rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
     
    636635                        rc = pq_get_order(socket_data->incoming, &order, NULL);
    637636                        if (rc != EOK) {
    638                                 /* Remove the corrupted packet */
     637                                // remove the corrupted packet
    639638                                next_packet = pq_detach(packet);
    640639                                if (packet == socket_data->incoming)
     
    649648                        if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
    650649                            old_incoming, socket_data->next_incoming)) {
    651                                 /* Move to the next */
     650                                // move to the next
    652651                                packet = pq_next(packet);
    653                                 /* Coninual data? */
     652                                // coninual data?
    654653                        } else if (IS_IN_INTERVAL_OVERFLOW(old_incoming,
    655654                            sequence_number, socket_data->next_incoming)) {
    656                                 /* Detach the packet */
     655                                // detach the packet
    657656                                next_packet = pq_detach(packet);
    658657                                if (packet == socket_data->incoming)
    659658                                        socket_data->incoming = next_packet;
    660                                 /* Get data length */
     659                                // get data length
    661660                                length = packet_get_data_length(packet);
    662661                                new_sequence_number = sequence_number + length;
    663662                                if (length <= 0) {
    664                                         /* Remove the empty packet */
     663                                        // remove the empty packet
    665664                                        pq_release_remote(tcp_globals.net_phone,
    666665                                            packet_get_id(packet));
     
    668667                                        continue;
    669668                                }
    670                                 /* Exactly following */
     669                                // exactly following
    671670                                if (sequence_number ==
    672671                                    socket_data->next_incoming) {
    673                                         /* Queue received data */
     672                                        // queue received data
    674673                                        rc = tcp_queue_received_packet(socket,
    675674                                            socket_data, packet, 1,
     
    681680                                        packet = next_packet;
    682681                                        continue;
    683                                         /* At least partly following data? */
     682                                        // at least partly following data?
    684683                                }
    685684                                if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
     
    696695                                        rc = packet_trim(packet,length, 0);
    697696                                        if (rc == EOK) {
    698                                                 /* Queue received data */
     697                                                // queue received data
    699698                                                rc = tcp_queue_received_packet(
    700699                                                    socket, socket_data, packet,
     
    709708                                        }
    710709                                }
    711                                 /* Remove the duplicit or corrupted packet */
     710                                // remove the duplicit or corrupted packet
    712711                                pq_release_remote(tcp_globals.net_phone,
    713712                                    packet_get_id(packet));
     
    722721            socket_data->next_incoming + socket_data->window)) {
    723722                printf("in window\n");
    724                 /* Process acknowledgement */
     723                // process acknowledgement
    725724                tcp_process_acknowledgement(socket, socket_data, header);
    726725
    727                 /* Remove the header */
     726                // remove the header
    728727                total_length -= TCP_HEADER_LENGTH(header);
    729728                rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
     
    736735                    length);
    737736                if (rc != EOK) {
    738                         /* Remove the corrupted packets */
     737                        // remove the corrupted packets
    739738                        pq_release_remote(tcp_globals.net_phone,
    740739                            packet_get_id(packet));
     
    763762        } else {
    764763                printf("unexpected\n");
    765                 /* Release duplicite or restricted */
     764                // release duplicite or restricted
    766765                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    767766        }
    768767
    769         /* Change state according to the acknowledging incoming fin */
     768        // change state according to the acknowledging incoming fin
    770769        if (IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming,
    771770            socket_data->next_incoming)) {
     
    776775                        socket_data->state = TCP_SOCKET_CLOSING;
    777776                        break;
    778                 /*case TCP_ESTABLISHED:*/
     777                        //case TCP_ESTABLISHED:
    779778                default:
    780779                        socket_data->state = TCP_SOCKET_CLOSE_WAIT;
     
    785784        packet = tcp_get_packets_to_send(socket, socket_data);
    786785        if (!packet) {
    787                 /* Create the notification packet */
     786                // create the notification packet
    788787                rc = tcp_create_notification_packet(&packet, socket,
    789788                    socket_data, 0, 0);
     
    799798        fibril_rwlock_write_unlock(socket_data->local_lock);
    800799
    801         /* Send the packet */
     800        // send the packet
    802801        tcp_send_packets(socket_data->device_id, packet);
    803802
     
    805804}
    806805
    807 int tcp_queue_received_packet(socket_core_ref socket,
     806int
     807tcp_queue_received_packet(socket_core_ref socket,
    808808    tcp_socket_data_ref socket_data, packet_t packet, int fragments,
    809809    size_t total_length)
     
    819819        assert(socket_data->window > total_length);
    820820
    821         /* Queue the received packet */
     821        // queue the received packet
    822822        rc = dyn_fifo_push(&socket->received, packet_get_id(packet),
    823823            SOCKET_MAX_RECEIVED_SIZE);
     
    829829                return tcp_release_and_return(packet, rc);
    830830
    831         /* Decrease the window size */
     831        // decrease the window size
    832832        socket_data->window -= total_length;
    833833
    834         /* Notify the destination socket */
     834        // notify the destination socket
    835835        async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
    836836            (ipcarg_t) socket->socket_id,
     
    842842}
    843843
    844 int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref
    845     socket_data, tcp_header_ref header, packet_t packet)
     844int
     845tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data,
     846    tcp_header_ref header, packet_t packet)
    846847{
    847848        packet_t next_packet;
     
    857858                return tcp_release_and_return(packet, EINVAL);
    858859       
    859         /* Process acknowledgement */
     860        // process acknowledgement
    860861        tcp_process_acknowledgement(socket, socket_data, header);
    861862
    862863        socket_data->next_incoming = ntohl(header->sequence_number) + 1;
    863 
    864         /* Release additional packets */
     864        // release additional packets
    865865        next_packet = pq_detach(packet);
    866866        if (next_packet) {
     
    868868                    packet_get_id(next_packet));
    869869        }
    870 
    871         /* Trim if longer than the header */
     870        // trim if longer than the header
    872871        if (packet_get_data_length(packet) > sizeof(*header)) {
    873872                rc = packet_trim(packet, 0,
     
    876875                        return tcp_release_and_return(packet, rc);
    877876        }
    878 
    879877        tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
    880878        fibril_mutex_lock(&socket_data->operation.mutex);
    881879        socket_data->operation.result = tcp_queue_packet(socket, socket_data,
    882880            packet, 1);
    883 
    884881        if (socket_data->operation.result == EOK) {
    885882                socket_data->state = TCP_SOCKET_ESTABLISHED;
     
    887884                if (packet) {
    888885                        fibril_rwlock_write_unlock( socket_data->local_lock);
    889                         /* Send the packet */
     886                        // send the packet
    890887                        tcp_send_packets(socket_data->device_id, packet);
    891                         /* Signal the result */
     888                        // signal the result
    892889                        fibril_condvar_signal( &socket_data->operation.condvar);
    893890                        fibril_mutex_unlock( &socket_data->operation.mutex);
     
    895892                }
    896893        }
    897 
    898894        fibril_mutex_unlock(&socket_data->operation.mutex);
    899895        return tcp_release_and_return(packet, EINVAL);
    900896}
    901897
    902 int tcp_process_listen(socket_core_ref listening_socket,
     898int
     899tcp_process_listen(socket_core_ref listening_socket,
    903900    tcp_socket_data_ref listening_socket_data, tcp_header_ref header,
    904901    packet_t packet, struct sockaddr *src, struct sockaddr *dest,
     
    939936                return tcp_release_and_return(packet, ENOMEM);
    940937        }
    941 
    942938        memcpy(socket_data->addr, src, socket_data->addrlen);
    943939        socket_data->dest_port = ntohs(header->source_port);
     
    950946        }
    951947
    952         /* Create a socket */
     948        // create a socket
    953949        socket_id = -1;
    954950        rc = socket_create(socket_data->local_sockets, listening_socket->phone,
     
    969965        fibril_rwlock_write_lock(&tcp_globals.lock);
    970966
    971         /* Find the destination socket */
     967        // find the destination socket
    972968        listening_socket = socket_port_find(&tcp_globals.sockets,
    973969            listening_port, SOCKET_MAP_KEY_LISTENING, 0);
     
    975971            (listening_socket->socket_id != listening_socket_id)) {
    976972                fibril_rwlock_write_unlock(&tcp_globals.lock);
    977                 /* A shadow may remain until app hangs up */
     973                // a shadow may remain until app hangs up
    978974                return tcp_release_and_return(packet, EOK /*ENOTSOCK*/);
    979975        }
     
    987983            socket_id);
    988984        if (!socket) {
    989                 /* Where is the socket?!? */
     985                // where is the socket?!?
    990986                fibril_rwlock_write_unlock(&tcp_globals.lock);
    991987                return ENOTSOCK;
     
    10141010        socket_data->next_incoming = ntohl(header->sequence_number) + 1;
    10151011
    1016         /* Release additional packets */
     1012        // release additional packets
    10171013        next_packet = pq_detach(packet);
    10181014        if (next_packet) {
     
    10211017        }
    10221018
    1023         /* Trim if longer than the header */
     1019        // trim if longer than the header
    10241020        if (packet_get_data_length(packet) > sizeof(*header)) {
    10251021                rc = packet_trim(packet, 0,
     
    10541050        fibril_rwlock_write_unlock(socket_data->local_lock);
    10551051
    1056         /* Send the packet */
     1052        // send the packet
    10571053        tcp_send_packets(socket_data->device_id, packet);
    10581054
     
    10601056}
    10611057
    1062 int tcp_process_syn_received(socket_core_ref socket,
     1058int
     1059tcp_process_syn_received(socket_core_ref socket,
    10631060    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet)
    10641061{
     
    10761073                return tcp_release_and_return(packet, EINVAL);
    10771074
    1078         /* Process acknowledgement */
     1075        // process acknowledgement
    10791076        tcp_process_acknowledgement(socket, socket_data, header);
    10801077
     
    10891086                assert(listening_socket_data);
    10901087
    1091                 /* Queue the received packet */
     1088                // queue the received packet
    10921089                rc = dyn_fifo_push(&listening_socket->accepted,
    10931090                    (-1 * socket->socket_id), listening_socket_data->backlog);
    10941091                if (rc == EOK) {
    1095                         /* Notify the destination socket */
     1092                        // notify the destination socket
    10961093                        async_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
    10971094                            (ipcarg_t) listening_socket->socket_id,
     
    11031100                }
    11041101        }
    1105         /* Send FIN */
     1102        // send FIN
    11061103        socket_data->state = TCP_SOCKET_FIN_WAIT_1;
    11071104
    1108         /* Create the notification packet */
     1105        // create the notification packet
    11091106        rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
    11101107        if (rc != EOK)
    11111108                return rc;
    11121109
    1113         /* Send the packet */
     1110        // send the packet
    11141111        rc = tcp_queue_packet(socket, socket_data, packet, 1);
    11151112        if (rc != EOK)
    11161113                return rc;
    11171114
    1118         /* Flush packets */
     1115        // flush packets
    11191116        packet = tcp_get_packets_to_send(socket, socket_data);
    11201117        fibril_rwlock_write_unlock(socket_data->local_lock);
    11211118        if (packet) {
    1122                 /* Send the packet */
     1119                // send the packet
    11231120                tcp_send_packets(socket_data->device_id, packet);
    11241121        }
     
    11271124}
    11281125
    1129 void tcp_process_acknowledgement(socket_core_ref socket,
     1126void
     1127tcp_process_acknowledgement(socket_core_ref socket,
    11301128    tcp_socket_data_ref socket_data, tcp_header_ref header)
    11311129{
     
    11461144
    11471145        number = ntohl(header->acknowledgement_number);
    1148 
    1149         /* If more data acknowledged */
     1146        // if more data acknowledged
    11501147        if (number != socket_data->expected) {
    11511148                old = socket_data->expected;
     
    11581155                        case TCP_SOCKET_LAST_ACK:
    11591156                        case TCP_SOCKET_CLOSING:
    1160                                 /*
    1161                                  * FIN acknowledged - release the socket in
    1162                                  * another fibril.
    1163                                  */
     1157                                // fin acknowledged - release the socket in
     1158                                // another fibril
    11641159                                tcp_prepare_timeout(tcp_release_after_timeout,
    11651160                                    socket, socket_data, 0,
     
    11711166                        }
    11721167                }
    1173 
    1174                 /* Update the treshold if higher than set */
     1168                // update the treshold if higher than set
    11751169                if (number + ntohs(header->window) >
    11761170                    socket_data->expected + socket_data->treshold) {
     
    11781172                            socket_data->expected;
    11791173                }
    1180 
    1181                 /* Set new expected sequence number */
     1174                // set new expected sequence number
    11821175                socket_data->expected = number;
    11831176                socket_data->expected_count = 1;
     
    11911184                                        socket_data->outgoing = next;
    11921185
    1193                                 /* Add to acknowledged or release */
     1186                                // add to acknowledged or release
    11941187                                if (pq_add(&acknowledged, packet, 0, 0) != EOK)
    11951188                                        pq_release_remote(tcp_globals.net_phone,
     
    11991192                                break;
    12001193                }
    1201 
    1202                 /* Release acknowledged */
     1194                // release acknowledged
    12031195                if (acknowledged) {
    12041196                        pq_release_remote(tcp_globals.net_phone,
     
    12061198                }
    12071199                return;
    1208                 /* If the same as the previous time */
    1209         }
    1210 
     1200                // if the same as the previous time
     1201        }
    12111202        if (number == socket_data->expected) {
    1212                 /* Increase the counter */
     1203                // increase the counter
    12131204                socket_data->expected_count++;
    12141205                if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) {
    12151206                        socket_data->expected_count = 1;
    1216                         /* TODO retransmit lock */
     1207                        // TODO retransmit lock
    12171208                        //tcp_retransmit_packet(socket, socket_data, number);
    12181209                }
     
    13201311        while (keep_on_going) {
    13211312
    1322                 /* Answer the call */
     1313                // answer the call
    13231314                answer_call(callid, res, &answer, answer_count);
    1324                 /* Refresh data */
     1315                // refresh data
    13251316                refresh_answer(&answer, &answer_count);
    1326                 /* Get the next call */
     1317                // get the next call
    13271318                callid = async_get_call(&call);
    13281319
    1329                 /* Process the call */
     1320                // process the call
    13301321                switch (IPC_GET_METHOD(call)) {
    13311322                case IPC_M_PHONE_HUNGUP:
     
    14101401                        if (res != EOK)
    14111402                                break;
    1412                         /*
    1413                          * The global lock may be released in the
    1414                          * tcp_connect_message() function.
    1415                          */
     1403                        // the global lock may be released in the
     1404                        // tcp_connect_message() function
    14161405                        fibril_rwlock_write_lock(&tcp_globals.lock);
    14171406                        fibril_rwlock_write_lock(&lock);
     
    15271516        }
    15281517
    1529         /* Release the application phone */
     1518        // release the application phone
    15301519        ipc_hangup(app_phone);
    15311520
    15321521        printf("release\n");
    1533         /* Release all local sockets */
     1522        // release all local sockets
    15341523        socket_cores_release(tcp_globals.net_phone, &local_sockets,
    15351524            &tcp_globals.sockets, tcp_free_socket_data);
     
    15471536        assert(timeout);
    15481537
    1549         /* Sleep the given timeout */
     1538        // sleep the given timeout
    15501539        async_usleep(timeout->timeout);
    1551         /* Lock the globals */
     1540        // lock the globals
    15521541        if (timeout->globals_read_only)
    15531542                fibril_rwlock_read_lock(&tcp_globals.lock);
     
    15551544                fibril_rwlock_write_lock(&tcp_globals.lock);
    15561545
    1557         /* Find the pending operation socket */
     1546        // find the pending operation socket
    15581547        socket = socket_port_find(&tcp_globals.sockets, timeout->port,
    15591548            timeout->key, timeout->key_length);
     
    15681557        fibril_rwlock_write_lock(socket_data->local_lock);
    15691558        if (timeout->sequence_number) {
    1570                 /* Increase the timeout counter */
     1559                // increase the timeout counter;
    15711560                socket_data->timeout_count++;
    15721561                if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
    1573                         /* TODO release as connection lost */
     1562                        // TODO release as connection lost
    15741563                        //tcp_refresh_socket_data(socket_data);
    15751564                        fibril_rwlock_write_unlock(socket_data->local_lock);
    15761565                } else {
    1577                         /* Retransmit */
     1566                        // retransmit
    15781567//                      tcp_retransmit_packet(socket,
    15791568//                          socket_data, timeout->sequence_number);
     
    15821571        } else {
    15831572                fibril_mutex_lock(&socket_data->operation.mutex);
    1584                 /* Set the timeout operation result if state not changed */
     1573                // set the timeout operation result if state not
     1574                // changed
    15851575                if (socket_data->state == timeout->state) {
    15861576                        socket_data->operation.result = ETIMEOUT;
    1587 
    1588                         /* Notify the main fibril */
     1577                        // notify the main fibril
    15891578                        fibril_condvar_signal(&socket_data->operation.condvar);
    1590 
    1591                         /* Keep the global write lock */
     1579                        // keep the global write lock
    15921580                        keep_write_lock = true;
    15931581                } else {
    1594                         /*
    1595                          * Operation is ok, do nothing.
    1596                          * Unlocking from now on, so the unlocking
    1597                          * order does not matter.
    1598                          */
     1582                        // operation is ok, do nothing
     1583                        // unlocking from now on, so the unlocki
     1584                        // order does not matter...
    15991585                        fibril_rwlock_write_unlock(socket_data->local_lock);
    16001586                }
     
    16031589
    16041590out:
    1605         /* Unlock only if no socket */
     1591        // unlock only if no socket
    16061592        if (timeout->globals_read_only)
    16071593                fibril_rwlock_read_unlock(&tcp_globals.lock);
    16081594        else if (!keep_write_lock)
    1609                 /* Release if not desired */
     1595                // release if not desired
    16101596                fibril_rwlock_write_unlock(&tcp_globals.lock);
    16111597       
    1612         /* Release the timeout structure */
     1598        // release the timeout structure
    16131599        free(timeout);
    16141600        return EOK;
     
    16241610        assert(timeout);
    16251611
    1626         /* Sleep the given timeout */
     1612        // sleep the given timeout
    16271613        async_usleep(timeout->timeout);
    1628 
    1629         /* Lock the globals */
     1614        // lock the globals
    16301615        fibril_rwlock_write_lock(&tcp_globals.lock);
    1631 
    1632         /* Find the pending operation socket */
     1616        // find the pending operation socket
    16331617        socket = socket_port_find(&tcp_globals.sockets, timeout->port,
    16341618            timeout->key, timeout->key_length);
    1635 
    16361619        if (socket && (socket->socket_id == timeout->socket_id)) {
    16371620                socket_data = (tcp_socket_data_ref) socket->specific_data;
     
    16461629                }
    16471630        }
    1648 
    1649         /* Unlock the globals */
     1631        // unlock the globals
    16501632        fibril_rwlock_write_unlock(&tcp_globals.lock);
    1651 
    1652         /* Release the timeout structure */
     1633        // release the timeout structure
    16531634        free(timeout);
    1654 
    16551635        return EOK;
    16561636}
    16571637
    1658 void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref
    1659     socket_data, size_t sequence_number)
     1638void
     1639tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
     1640    size_t sequence_number)
    16601641{
    16611642        packet_t packet;
     
    16671648        assert(socket->specific_data == socket_data);
    16681649
    1669         /* Sent packet? */
     1650        // sent packet?
    16701651        packet = pq_find(socket_data->outgoing, sequence_number);
    16711652        printf("retransmit %d\n", packet_get_id(packet));
     
    16831664}
    16841665
    1685 int tcp_listen_message(socket_cores_ref local_sockets, int socket_id,
    1686     int backlog)
     1666int
     1667tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog)
    16871668{
    16881669        socket_core_ref socket;
     
    16941675                return EINVAL;
    16951676
    1696         /* Find the socket */
     1677        // find the socket
    16971678        socket = socket_cores_find(local_sockets, socket_id);
    16981679        if (!socket)
    16991680                return ENOTSOCK;
    17001681       
    1701         /* Get the socket specific data */
     1682        // get the socket specific data
    17021683        socket_data = (tcp_socket_data_ref) socket->specific_data;
    17031684        assert(socket_data);
    1704 
    1705         /* Set the backlog */
     1685        // set the backlog
    17061686        socket_data->backlog = backlog;
    17071687
     
    17091689}
    17101690
    1711 int tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
     1691int
     1692tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
    17121693    struct sockaddr *addr, socklen_t addrlen)
    17131694{
     
    17191700        assert(addrlen > 0);
    17201701
    1721         /* Find the socket */
     1702        // find the socket
    17221703        socket = socket_cores_find(local_sockets, socket_id);
    17231704        if (!socket)
     
    17271708        if (rc != EOK) {
    17281709                tcp_free_socket_data(socket);
    1729                 /* Unbind if bound */
     1710                // unbind if bound
    17301711                if (socket->port > 0) {
    17311712                        socket_ports_exclude(&tcp_globals.sockets,
     
    17371718}
    17381719
    1739 int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
     1720int
     1721tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
    17401722    struct sockaddr *addr, socklen_t addrlen)
    17411723{
     
    17481730        assert(addrlen > 0);
    17491731
    1750         /* Get the socket specific data */
     1732        // get the socket specific data
    17511733        socket_data = (tcp_socket_data_ref) socket->specific_data;
    17521734        assert(socket_data);
     
    17571739                return EINVAL;
    17581740
    1759         /* Get the destination port */
     1741        // get the destination port
    17601742        rc = tl_get_address_port(addr, addrlen, &socket_data->dest_port);
    17611743        if (rc != EOK)
     
    17631745       
    17641746        if (socket->port <= 0) {
    1765                 /* Try to find a free port */
     1747                // try to find a free port
    17661748                rc = socket_bind_free_port(&tcp_globals.sockets, socket,
    17671749                    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
     
    17691751                if (rc != EOK)
    17701752                        return rc;
    1771                 /* Set the next port as the search starting port number */
     1753                // set the next port as the search starting port number
    17721754                tcp_globals.last_used_port = socket->port;
    17731755        }
     
    17791761                return rc;
    17801762
    1781         /* Create the notification packet */
     1763        // create the notification packet
    17821764        rc = tcp_create_notification_packet(&packet, socket, socket_data, 1, 0);
    17831765        if (rc != EOK)
    17841766                return rc;
    17851767
    1786         /* Unlock the globals and wait for an operation */
     1768        // unlock the globals and wait for an operation
    17871769        fibril_rwlock_write_unlock(&tcp_globals.lock);
    17881770
    17891771        socket_data->addr = addr;
    17901772        socket_data->addrlen = addrlen;
    1791 
    1792         /* Send the packet */
     1773        // send the packet
    17931774
    17941775        if (((rc = tcp_queue_packet(socket, socket_data, packet, 1)) != EOK) ||
     
    18041785                        fibril_mutex_lock(&socket_data->operation.mutex);
    18051786                        fibril_rwlock_write_unlock(socket_data->local_lock);
    1806 
    1807                         /* Send the packet */
     1787                        // send the packet
    18081788                        printf("connecting %d\n", packet_get_id(packet));
    18091789                        tcp_send_packets(socket_data->device_id, packet);
    18101790
    1811                         /* Wait for a reply */
     1791                        // wait for a reply
    18121792                        fibril_condvar_wait(&socket_data->operation.condvar,
    18131793                            &socket_data->operation.mutex);
     
    18251805
    18261806        fibril_mutex_unlock(&socket_data->operation.mutex);
     1807
     1808        // return the result
    18271809        return rc;
    18281810}
    18291811
    1830 int tcp_queue_prepare_packet(socket_core_ref socket,
     1812int
     1813tcp_queue_prepare_packet(socket_core_ref socket,
    18311814    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length)
    18321815{
     
    18381821        assert(socket->specific_data == socket_data);
    18391822
    1840         /* Get TCP header */
     1823        // get tcp header
    18411824        header = (tcp_header_ref) packet_get_data(packet);
    18421825        if (!header)
     
    18521835                return tcp_release_and_return(packet, EINVAL);
    18531836
    1854         /* Remember the outgoing FIN */
     1837        // remember the outgoing FIN
    18551838        if (header->finalize)
    18561839                socket_data->fin_outgoing = socket_data->next_outgoing;
     
    18591842}
    18601843
    1861 int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
     1844int
     1845tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
    18621846    packet_t packet, size_t data_length)
    18631847{
     
    18811865}
    18821866
    1883 packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref
    1884     socket_data)
     1867packet_t
     1868tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data)
    18851869{
    18861870        packet_t packet;
     
    18991883                pq_get_order(packet, NULL, &data_length);
    19001884
    1901                 /*
    1902                  * Send only if fits into the window, respecting the possible
    1903                  * overflow.
    1904                  */
     1885                // send only if fits into the window
     1886                // respecting the possible overflow
    19051887                if (!IS_IN_INTERVAL_OVERFLOW(
    19061888                    (uint32_t) socket_data->last_outgoing,
     
    19271909                previous = copy;
    19281910                packet = pq_next(packet);
    1929 
    1930                 /* Overflow occurred? */
     1911                // overflow occurred ?
    19311912                if (!packet &&
    19321913                    (socket_data->last_outgoing > socket_data->next_outgoing)) {
    19331914                        printf("gpts overflow\n");
    1934                         /* Continue from the beginning */
     1915                        // continue from the beginning
    19351916                        packet = socket_data->outgoing;
    19361917                }
     
    19411922}
    19421923
    1943 packet_t tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref
    1944     socket_data, packet_t packet, size_t data_length, size_t sequence_number)
     1924packet_t
     1925tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
     1926    packet_t packet, size_t data_length, size_t sequence_number)
    19451927{
    19461928        tcp_header_ref header;
     
    19521934        assert(socket->specific_data == socket_data);
    19531935
    1954         /* Adjust the pseudo header */
     1936        // adjust the pseudo header
    19551937        rc = ip_client_set_pseudo_header_data_length(socket_data->pseudo_header,
    19561938            socket_data->headerlen, packet_get_data_length(packet));
     
    19601942        }
    19611943
    1962         /* Get the header */
     1944        // get the header
    19631945        header = (tcp_header_ref) packet_get_data(packet);
    19641946        if (!header) {
     
    19681950        assert(ntohl(header->sequence_number) == sequence_number);
    19691951
    1970         /* Adjust the header */
     1952        // adjust the header
    19711953        if (socket_data->next_incoming) {
    19721954                header->acknowledgement_number =
     
    19761958        header->window = htons(socket_data->window);
    19771959
    1978         /* Checksum */
     1960        // checksum
    19791961        header->checksum = 0;
    19801962        checksum = compute_checksum(0, socket_data->pseudo_header,
     
    19851967        header->checksum = htons(flip_checksum(compact_checksum(checksum)));
    19861968
    1987         /* Prepare the packet */
     1969        // prepare the packet
    19881970        rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0);
    19891971        if (rc != EOK) {
     
    19911973                return NULL;
    19921974        }
    1993 
    19941975        rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data,
    19951976            sequence_number, socket_data->state, socket_data->timeout, true);
     
    20021983}
    20031984
    2004 packet_t tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref
    2005     socket_data, packet_t packet, size_t data_length, size_t sequence_number)
     1985packet_t
     1986tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref socket_data,
     1987    packet_t packet, size_t data_length, size_t sequence_number)
    20061988{
    20071989        packet_t copy;
     
    20111993        assert(socket->specific_data == socket_data);
    20121994
    2013         /* Make a copy of the packet */
     1995        // make a copy of the packet
    20141996        copy = packet_get_copy(tcp_globals.net_phone, packet);
    20151997        if (!copy)
     
    20322014}
    20332015
    2034 void tcp_prepare_operation_header(socket_core_ref socket,
     2016void
     2017tcp_prepare_operation_header(socket_core_ref socket,
    20352018    tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize,
    20362019    int finalize)
     
    20492032}
    20502033
    2051 int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
     2034int
     2035tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
    20522036    socket_core_ref socket, tcp_socket_data_ref socket_data,
    20532037    size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
     
    20612045        assert(socket->specific_data == socket_data);
    20622046
    2063         /* Prepare the timeout with key bundle structure */
     2047        // prepare the timeout with key bundle structure
    20642048        operation_timeout = malloc(sizeof(*operation_timeout) +
    20652049            socket->key_length + 1);
     
    20762060        operation_timeout->state = state;
    20772061
    2078         /* Copy the key */
     2062        // copy the key
    20792063        operation_timeout->key = ((char *) operation_timeout) +
    20802064            sizeof(*operation_timeout);
     
    20832067        operation_timeout->key[operation_timeout->key_length] = '\0';
    20842068
    2085         /* Prepare the timeouting thread */
     2069        // prepare the timeouting thread
    20862070        fibril = fibril_create(timeout_function, operation_timeout);
    20872071        if (!fibril) {
     
    20902074        }
    20912075//      fibril_mutex_lock(&socket_data->operation.mutex);
    2092         /* Start the timeout fibril */
     2076        // start the timeouting fibril
    20932077        fibril_add_ready(fibril);
    20942078        //socket_data->state = state;
     
    20962080}
    20972081
    2098 int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id,
    2099     int flags, size_t *addrlen)
     2082int
     2083tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
     2084    size_t *addrlen)
    21002085{
    21012086        socket_core_ref socket;
     
    21082093        assert(local_sockets);
    21092094
    2110         /* Find the socket */
     2095        // find the socket
    21112096        socket = socket_cores_find(local_sockets, socket_id);
    21122097        if (!socket)
    21132098                return ENOTSOCK;
    21142099
    2115         /* Get the socket specific data */
     2100        // get the socket specific data
    21162101        if (!socket->specific_data)
    21172102                return NO_DATA;
     
    21192104        socket_data = (tcp_socket_data_ref) socket->specific_data;
    21202105
    2121         /* Check state */
     2106        // check state
    21222107        if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
    21232108            (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
    21242109                return ENOTCONN;
    21252110
    2126         /* Send the source address if desired */
     2111        // send the source address if desired
    21272112        if (addrlen) {
    21282113                rc = data_reply(socket_data->addr, socket_data->addrlen);
     
    21322117        }
    21332118
    2134         /* Get the next received packet */
     2119        // get the next received packet
    21352120        packet_id = dyn_fifo_value(&socket->received);
    21362121        if (packet_id < 0)
     
    21412126                return rc;
    21422127
    2143         /* Reply the packets */
     2128        // reply the packets
    21442129        rc = socket_reply_packets(packet, &length);
    21452130        if (rc != EOK)
    21462131                return rc;
    21472132
    2148         /* Release the packet */
     2133        // release the packet
    21492134        dyn_fifo_pop(&socket->received);
    21502135        pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    2151 
    2152         /* Return the total length */
     2136        // return the total length
    21532137        return (int) length;
    21542138}
    21552139
    2156 int tcp_send_message(socket_cores_ref local_sockets, int socket_id,
    2157     int fragments, size_t *data_fragment_size, int flags)
     2140int
     2141tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments,
     2142    size_t *data_fragment_size, int flags)
    21582143{
    21592144        socket_core_ref socket;
     
    21702155        assert(data_fragment_size);
    21712156
    2172         /* Find the socket */
     2157        // find the socket
    21732158        socket = socket_cores_find(local_sockets, socket_id);
    21742159        if (!socket)
    21752160                return ENOTSOCK;
    21762161
    2177         /* Get the socket specific data */
     2162        // get the socket specific data
    21782163        if (!socket->specific_data)
    21792164                return NO_DATA;
     
    21812166        socket_data = (tcp_socket_data_ref) socket->specific_data;
    21822167
    2183         /* Check state */
     2168        // check state
    21842169        if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
    21852170            (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
     
    21962181
    21972182        for (index = 0; index < fragments; index++) {
    2198                 /* Read the data fragment */
     2183                // read the data fragment
    21992184                result = tl_socket_read_packet_data(tcp_globals.net_phone,
    22002185                    &packet, TCP_HEADER_SIZE, packet_dimension,
     
    22042189
    22052190                total_length = (size_t) result;
    2206 
    2207                 /* Prefix the TCP header */
     2191                // prefix the tcp header
    22082192                header = PACKET_PREFIX(packet, tcp_header_t);
    22092193                if (!header)
     
    22162200        }
    22172201
    2218         /* Flush packets */
     2202        // flush packets
    22192203        packet = tcp_get_packets_to_send(socket, socket_data);
    22202204        fibril_rwlock_write_unlock(socket_data->local_lock);
     
    22222206
    22232207        if (packet) {
    2224                 /* Send the packet */
     2208                // send the packet
    22252209                tcp_send_packets(socket_data->device_id, packet);
    22262210        }
     
    22372221        int rc;
    22382222
    2239         /* Find the socket */
     2223        // find the socket
    22402224        socket = socket_cores_find(local_sockets, socket_id);
    22412225        if (!socket)
    22422226                return ENOTSOCK;
    22432227
    2244         /* Get the socket specific data */
     2228        // get the socket specific data
    22452229        socket_data = (tcp_socket_data_ref) socket->specific_data;
    22462230        assert(socket_data);
    22472231
    2248         /* Check state */
     2232        // check state
    22492233        switch (socket_data->state) {
    22502234        case TCP_SOCKET_ESTABLISHED:
     
    22592243
    22602244        default:
    2261                 /* Just destroy */
     2245                // just destroy
    22622246                rc = socket_destroy(tcp_globals.net_phone, socket_id,
    22632247                    local_sockets, &tcp_globals.sockets,
     
    22702254        }
    22712255
    2272         /*
    2273          * Send FIN.
    2274          * TODO should I wait to complete?
    2275          */
    2276 
    2277         /* Create the notification packet */
     2256        // send FIN
     2257        // TODO should I wait to complete?
     2258
     2259        // create the notification packet
    22782260        rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
    22792261        if (rc != EOK)
    22802262                return rc;
    22812263
    2282         /* Send the packet */
     2264        // send the packet
    22832265        rc = tcp_queue_packet(socket, socket_data, packet, 1);
    22842266        if (rc != EOK)
    22852267                return rc;
    22862268
    2287         /* Flush packets */
     2269        // flush packets
    22882270        packet = tcp_get_packets_to_send(socket, socket_data);
    22892271        fibril_rwlock_write_unlock(socket_data->local_lock);
     
    22912273
    22922274        if (packet) {
    2293                 /* Send the packet */
     2275                // send the packet
    22942276                tcp_send_packets(socket_data->device_id, packet);
    22952277        }
     
    22982280}
    22992281
    2300 int tcp_create_notification_packet(packet_t *packet, socket_core_ref socket,
     2282int
     2283tcp_create_notification_packet(packet_t *packet, socket_core_ref socket,
    23012284    tcp_socket_data_ref socket_data, int synchronize, int finalize)
    23022285{
     
    23072290        assert(packet);
    23082291
    2309         /* Get the device packet dimension */
     2292        // get the device packet dimension
    23102293        rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
    23112294            &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
     
    23132296                return rc;
    23142297
    2315         /* Get a new packet */
     2298        // get a new packet
    23162299        *packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE,
    23172300            packet_dimension->addr_len, packet_dimension->prefix,
     
    23212304                return ENOMEM;
    23222305
    2323         /* Allocate space in the packet */
     2306        // allocate space in the packet
    23242307        header = PACKET_SUFFIX(*packet, tcp_header_t);
    23252308        if (!header)
     
    23322315}
    23332316
    2334 int tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
     2317int
     2318tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
    23352319    int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
    23362320{
     
    23452329        assert(addrlen);
    23462330
    2347         /* Find the socket */
     2331        // find the socket
    23482332        socket = socket_cores_find(local_sockets, socket_id);
    23492333        if (!socket)
    23502334                return ENOTSOCK;
    23512335
    2352         /* Get the socket specific data */
     2336        // get the socket specific data
    23532337        socket_data = (tcp_socket_data_ref) socket->specific_data;
    23542338        assert(socket_data);
    23552339
    2356         /* Check state */
     2340        // check state
    23572341        if (socket_data->state != TCP_SOCKET_LISTEN)
    23582342                return EINVAL;
     
    23682352                        return ENOTSOCK;
    23692353
    2370                 /* Get the socket specific data */
     2354                // get the socket specific data
    23712355                socket_data = (tcp_socket_data_ref) accepted->specific_data;
    23722356                assert(socket_data);
    2373                 /* TODO can it be in another state? */
     2357                // TODO can it be in another state?
    23742358                if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
    23752359                        rc = data_reply(socket_data->addr,
     
    24052389}
    24062390
    2407 void tcp_free_socket_data(socket_core_ref socket)
     2391void
     2392tcp_free_socket_data(socket_core_ref socket)
    24082393{
    24092394        tcp_socket_data_ref socket_data;
     
    24132398        printf("destroy_socket %d\n", socket->socket_id);
    24142399
    2415         /* Get the socket specific data */
     2400        // get the socket specific data
    24162401        socket_data = (tcp_socket_data_ref) socket->specific_data;
    24172402        assert(socket_data);
    2418 
    2419         /* Free the pseudo header */
     2403        //free the pseudo header
    24202404        if (socket_data->pseudo_header) {
    24212405                if (socket_data->headerlen) {
     
    24262410                socket_data->pseudo_header = NULL;
    24272411        }
    2428 
    24292412        socket_data->headerlen = 0;
    2430 
    2431         /* Free the address */
     2413        // free the address
    24322414        if (socket_data->addr) {
    24332415                if (socket_data->addrlen) {
     
    24912473
    24922474                /*
    2493                  * Answer the message
     2475                   Answer the message
    24942476                 */
    24952477                answer_call(callid, res, &answer, answer_count);
Note: See TracChangeset for help on using the changeset viewer.