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


Ignore:
Timestamp:
2010-11-14T14:08:29Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a9db9b8, d52b0044, eb522e8
Parents:
71e3289 (diff), fb04cba8 (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 networking cstyle cleanup.

File:
1 edited

Legend:

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

    r71e3289 r9e2e715  
    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
    270 tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
    271     services_t error)
     269int tcp_received_msg(device_id_t device_id, packet_t packet,
     270    services_t receiver, services_t error)
    272271{
    273272        int rc;
     
    309308                break;
    310309        case SERVICE_ICMP:
    311                 // process error
     310                /* Process error */
    312311                result = icmp_client_process_packet(packet, &type, &code, NULL,
    313312                    NULL);
     
    324323        }
    325324
    326         // TODO process received ipopts?
     325        /* TODO process received ipopts? */
    327326        result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
    328327        if (result < 0)
     
    338337                return tcp_release_and_return(packet, NO_DATA);
    339338
    340         // trim all but TCP header
     339        /* Trim all but TCP header */
    341340        rc = packet_trim(packet, offset, 0);
    342341        if (rc != EOK)
    343342                return tcp_release_and_return(packet, rc);
    344343
    345         // get tcp header
     344        /* Get tcp header */
    346345        header = (tcp_header_ref) packet_get_data(packet);
    347346        if (!header)
     
    361360                return tcp_release_and_return(packet, rc);
    362361       
    363         // find the destination socket
     362        /* Find the destination socket */
    364363        socket = socket_port_find(&tcp_globals.sockets,
    365364            ntohs(header->destination_port), (const char *) src, addrlen);
    366365        if (!socket) {
    367                 // find the listening destination socket
     366                /* Find the listening destination socket */
    368367                socket = socket_port_find(&tcp_globals.sockets,
    369368                    ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,
    370369                    0);
    371370        }
     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
    500 tcp_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)
     499int 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)
    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                 // trim the acknowledged data
     541
     542                /* Trim the acknowledged data */
    542543                while (length <= offset) {
    543                         // release the acknowledged packets
     544                        /* Release the acknowledged packets */
    544545                        next_packet = pq_next(packet);
    545546                        pq_release_remote(tcp_globals.net_phone,
     
    559560        }
    560561
    561         // release if overflowing the window
     562        /* Release if overflowing the window */
    562563/*
    563564        if (IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming +
     
    609610        }
    610611*/
    611         // the expected one arrived?
     612        /* The expected one arrived? */
    612613        if (new_sequence_number == socket_data->next_incoming) {
    613614                printf("expected\n");
    614                 // process acknowledgement
     615                /* Process acknowledgement */
    615616                tcp_process_acknowledgement(socket, socket_data, header);
    616617
    617                 // remove the header
     618                /* Remove the header */
    618619                total_length -= TCP_HEADER_LENGTH(header);
    619620                rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
     
    635636                        rc = pq_get_order(socket_data->incoming, &order, NULL);
    636637                        if (rc != EOK) {
    637                                 // remove the corrupted packet
     638                                /* Remove the corrupted packet */
    638639                                next_packet = pq_detach(packet);
    639640                                if (packet == socket_data->incoming)
     
    648649                        if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
    649650                            old_incoming, socket_data->next_incoming)) {
    650                                 // move to the next
     651                                /* Move to the next */
    651652                                packet = pq_next(packet);
    652                                 // coninual data?
     653                                /* Coninual data? */
    653654                        } else if (IS_IN_INTERVAL_OVERFLOW(old_incoming,
    654655                            sequence_number, socket_data->next_incoming)) {
    655                                 // detach the packet
     656                                /* Detach the packet */
    656657                                next_packet = pq_detach(packet);
    657658                                if (packet == socket_data->incoming)
    658659                                        socket_data->incoming = next_packet;
    659                                 // get data length
     660                                /* Get data length */
    660661                                length = packet_get_data_length(packet);
    661662                                new_sequence_number = sequence_number + length;
    662663                                if (length <= 0) {
    663                                         // remove the empty packet
     664                                        /* Remove the empty packet */
    664665                                        pq_release_remote(tcp_globals.net_phone,
    665666                                            packet_get_id(packet));
     
    667668                                        continue;
    668669                                }
    669                                 // exactly following
     670                                /* Exactly following */
    670671                                if (sequence_number ==
    671672                                    socket_data->next_incoming) {
    672                                         // queue received data
     673                                        /* Queue received data */
    673674                                        rc = tcp_queue_received_packet(socket,
    674675                                            socket_data, packet, 1,
     
    680681                                        packet = next_packet;
    681682                                        continue;
    682                                         // at least partly following data?
     683                                        /* At least partly following data? */
    683684                                }
    684685                                if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
     
    695696                                        rc = packet_trim(packet,length, 0);
    696697                                        if (rc == EOK) {
    697                                                 // queue received data
     698                                                /* Queue received data */
    698699                                                rc = tcp_queue_received_packet(
    699700                                                    socket, socket_data, packet,
     
    708709                                        }
    709710                                }
    710                                 // remove the duplicit or corrupted packet
     711                                /* Remove the duplicit or corrupted packet */
    711712                                pq_release_remote(tcp_globals.net_phone,
    712713                                    packet_get_id(packet));
     
    721722            socket_data->next_incoming + socket_data->window)) {
    722723                printf("in window\n");
    723                 // process acknowledgement
     724                /* Process acknowledgement */
    724725                tcp_process_acknowledgement(socket, socket_data, header);
    725726
    726                 // remove the header
     727                /* Remove the header */
    727728                total_length -= TCP_HEADER_LENGTH(header);
    728729                rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
     
    735736                    length);
    736737                if (rc != EOK) {
    737                         // remove the corrupted packets
     738                        /* Remove the corrupted packets */
    738739                        pq_release_remote(tcp_globals.net_phone,
    739740                            packet_get_id(packet));
     
    762763        } else {
    763764                printf("unexpected\n");
    764                 // release duplicite or restricted
     765                /* Release duplicite or restricted */
    765766                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    766767        }
    767768
    768         // change state according to the acknowledging incoming fin
     769        /* Change state according to the acknowledging incoming fin */
    769770        if (IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming,
    770771            socket_data->next_incoming)) {
     
    775776                        socket_data->state = TCP_SOCKET_CLOSING;
    776777                        break;
    777                         //case TCP_ESTABLISHED:
     778                /*case TCP_ESTABLISHED:*/
    778779                default:
    779780                        socket_data->state = TCP_SOCKET_CLOSE_WAIT;
     
    784785        packet = tcp_get_packets_to_send(socket, socket_data);
    785786        if (!packet) {
    786                 // create the notification packet
     787                /* Create the notification packet */
    787788                rc = tcp_create_notification_packet(&packet, socket,
    788789                    socket_data, 0, 0);
     
    798799        fibril_rwlock_write_unlock(socket_data->local_lock);
    799800
    800         // send the packet
     801        /* Send the packet */
    801802        tcp_send_packets(socket_data->device_id, packet);
    802803
     
    804805}
    805806
    806 int
    807 tcp_queue_received_packet(socket_core_ref socket,
     807int tcp_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
    845 tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data,
    846     tcp_header_ref header, packet_t packet)
     844int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref
     845    socket_data, tcp_header_ref header, packet_t packet)
    847846{
    848847        packet_t next_packet;
     
    858857                return tcp_release_and_return(packet, EINVAL);
    859858       
    860         // process acknowledgement
     859        /* Process acknowledgement */
    861860        tcp_process_acknowledgement(socket, socket_data, header);
    862861
    863862        socket_data->next_incoming = ntohl(header->sequence_number) + 1;
    864         // release additional packets
     863
     864        /* Release additional packets */
    865865        next_packet = pq_detach(packet);
    866866        if (next_packet) {
     
    868868                    packet_get_id(next_packet));
    869869        }
    870         // trim if longer than the header
     870
     871        /* Trim if longer than the header */
    871872        if (packet_get_data_length(packet) > sizeof(*header)) {
    872873                rc = packet_trim(packet, 0,
     
    875876                        return tcp_release_and_return(packet, rc);
    876877        }
     878
    877879        tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
    878880        fibril_mutex_lock(&socket_data->operation.mutex);
    879881        socket_data->operation.result = tcp_queue_packet(socket, socket_data,
    880882            packet, 1);
     883
    881884        if (socket_data->operation.result == EOK) {
    882885                socket_data->state = TCP_SOCKET_ESTABLISHED;
     
    884887                if (packet) {
    885888                        fibril_rwlock_write_unlock( socket_data->local_lock);
    886                         // send the packet
     889                        /* Send the packet */
    887890                        tcp_send_packets(socket_data->device_id, packet);
    888                         // signal the result
     891                        /* Signal the result */
    889892                        fibril_condvar_signal( &socket_data->operation.condvar);
    890893                        fibril_mutex_unlock( &socket_data->operation.mutex);
     
    892895                }
    893896        }
     897
    894898        fibril_mutex_unlock(&socket_data->operation.mutex);
    895899        return tcp_release_and_return(packet, EINVAL);
    896900}
    897901
    898 int
    899 tcp_process_listen(socket_core_ref listening_socket,
     902int tcp_process_listen(socket_core_ref listening_socket,
    900903    tcp_socket_data_ref listening_socket_data, tcp_header_ref header,
    901904    packet_t packet, struct sockaddr *src, struct sockaddr *dest,
     
    936939                return tcp_release_and_return(packet, ENOMEM);
    937940        }
     941
    938942        memcpy(socket_data->addr, src, socket_data->addrlen);
    939943        socket_data->dest_port = ntohs(header->source_port);
     
    946950        }
    947951
    948         // create a socket
     952        /* Create a socket */
    949953        socket_id = -1;
    950954        rc = socket_create(socket_data->local_sockets, listening_socket->phone,
     
    965969        fibril_rwlock_write_lock(&tcp_globals.lock);
    966970
    967         // find the destination socket
     971        /* Find the destination socket */
    968972        listening_socket = socket_port_find(&tcp_globals.sockets,
    969973            listening_port, SOCKET_MAP_KEY_LISTENING, 0);
     
    971975            (listening_socket->socket_id != listening_socket_id)) {
    972976                fibril_rwlock_write_unlock(&tcp_globals.lock);
    973                 // a shadow may remain until app hangs up
     977                /* A shadow may remain until app hangs up */
    974978                return tcp_release_and_return(packet, EOK /*ENOTSOCK*/);
    975979        }
     
    983987            socket_id);
    984988        if (!socket) {
    985                 // where is the socket?!?
     989                /* Where is the socket?!? */
    986990                fibril_rwlock_write_unlock(&tcp_globals.lock);
    987991                return ENOTSOCK;
     
    10101014        socket_data->next_incoming = ntohl(header->sequence_number) + 1;
    10111015
    1012         // release additional packets
     1016        /* Release additional packets */
    10131017        next_packet = pq_detach(packet);
    10141018        if (next_packet) {
     
    10171021        }
    10181022
    1019         // trim if longer than the header
     1023        /* Trim if longer than the header */
    10201024        if (packet_get_data_length(packet) > sizeof(*header)) {
    10211025                rc = packet_trim(packet, 0,
     
    10501054        fibril_rwlock_write_unlock(socket_data->local_lock);
    10511055
    1052         // send the packet
     1056        /* Send the packet */
    10531057        tcp_send_packets(socket_data->device_id, packet);
    10541058
     
    10561060}
    10571061
    1058 int
    1059 tcp_process_syn_received(socket_core_ref socket,
     1062int tcp_process_syn_received(socket_core_ref socket,
    10601063    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet)
    10611064{
     
    10731076                return tcp_release_and_return(packet, EINVAL);
    10741077
    1075         // process acknowledgement
     1078        /* Process acknowledgement */
    10761079        tcp_process_acknowledgement(socket, socket_data, header);
    10771080
     
    10861089                assert(listening_socket_data);
    10871090
    1088                 // queue the received packet
     1091                /* Queue the received packet */
    10891092                rc = dyn_fifo_push(&listening_socket->accepted,
    10901093                    (-1 * socket->socket_id), listening_socket_data->backlog);
    10911094                if (rc == EOK) {
    1092                         // notify the destination socket
     1095                        /* Notify the destination socket */
    10931096                        async_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
    10941097                            (ipcarg_t) listening_socket->socket_id,
     
    11001103                }
    11011104        }
    1102         // send FIN
     1105        /* Send FIN */
    11031106        socket_data->state = TCP_SOCKET_FIN_WAIT_1;
    11041107
    1105         // create the notification packet
     1108        /* Create the notification packet */
    11061109        rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
    11071110        if (rc != EOK)
    11081111                return rc;
    11091112
    1110         // send the packet
     1113        /* Send the packet */
    11111114        rc = tcp_queue_packet(socket, socket_data, packet, 1);
    11121115        if (rc != EOK)
    11131116                return rc;
    11141117
    1115         // flush packets
     1118        /* Flush packets */
    11161119        packet = tcp_get_packets_to_send(socket, socket_data);
    11171120        fibril_rwlock_write_unlock(socket_data->local_lock);
    11181121        if (packet) {
    1119                 // send the packet
     1122                /* Send the packet */
    11201123                tcp_send_packets(socket_data->device_id, packet);
    11211124        }
     
    11241127}
    11251128
    1126 void
    1127 tcp_process_acknowledgement(socket_core_ref socket,
     1129void tcp_process_acknowledgement(socket_core_ref socket,
    11281130    tcp_socket_data_ref socket_data, tcp_header_ref header)
    11291131{
     
    11441146
    11451147        number = ntohl(header->acknowledgement_number);
    1146         // if more data acknowledged
     1148
     1149        /* If more data acknowledged */
    11471150        if (number != socket_data->expected) {
    11481151                old = socket_data->expected;
     
    11551158                        case TCP_SOCKET_LAST_ACK:
    11561159                        case TCP_SOCKET_CLOSING:
    1157                                 // fin acknowledged - release the socket in
    1158                                 // another fibril
     1160                                /*
     1161                                 * FIN acknowledged - release the socket in
     1162                                 * another fibril.
     1163                                 */
    11591164                                tcp_prepare_timeout(tcp_release_after_timeout,
    11601165                                    socket, socket_data, 0,
     
    11661171                        }
    11671172                }
    1168                 // update the treshold if higher than set
     1173
     1174                /* Update the treshold if higher than set */
    11691175                if (number + ntohs(header->window) >
    11701176                    socket_data->expected + socket_data->treshold) {
     
    11721178                            socket_data->expected;
    11731179                }
    1174                 // set new expected sequence number
     1180
     1181                /* Set new expected sequence number */
    11751182                socket_data->expected = number;
    11761183                socket_data->expected_count = 1;
     
    11841191                                        socket_data->outgoing = next;
    11851192
    1186                                 // add to acknowledged or release
     1193                                /* Add to acknowledged or release */
    11871194                                if (pq_add(&acknowledged, packet, 0, 0) != EOK)
    11881195                                        pq_release_remote(tcp_globals.net_phone,
     
    11921199                                break;
    11931200                }
    1194                 // release acknowledged
     1201
     1202                /* Release acknowledged */
    11951203                if (acknowledged) {
    11961204                        pq_release_remote(tcp_globals.net_phone,
     
    11981206                }
    11991207                return;
    1200                 // if the same as the previous time
    1201         }
     1208                /* If the same as the previous time */
     1209        }
     1210
    12021211        if (number == socket_data->expected) {
    1203                 // increase the counter
     1212                /* Increase the counter */
    12041213                socket_data->expected_count++;
    12051214                if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) {
    12061215                        socket_data->expected_count = 1;
    1207                         // TODO retransmit lock
     1216                        /* TODO retransmit lock */
    12081217                        //tcp_retransmit_packet(socket, socket_data, number);
    12091218                }
     
    13111320        while (keep_on_going) {
    13121321
    1313                 // answer the call
     1322                /* Answer the call */
    13141323                answer_call(callid, res, &answer, answer_count);
    1315                 // refresh data
     1324                /* Refresh data */
    13161325                refresh_answer(&answer, &answer_count);
    1317                 // get the next call
     1326                /* Get the next call */
    13181327                callid = async_get_call(&call);
    13191328
    1320                 // process the call
     1329                /* Process the call */
    13211330                switch (IPC_GET_METHOD(call)) {
    13221331                case IPC_M_PHONE_HUNGUP:
     
    14011410                        if (res != EOK)
    14021411                                break;
    1403                         // the global lock may be released in the
    1404                         // tcp_connect_message() function
     1412                        /*
     1413                         * The global lock may be released in the
     1414                         * tcp_connect_message() function.
     1415                         */
    14051416                        fibril_rwlock_write_lock(&tcp_globals.lock);
    14061417                        fibril_rwlock_write_lock(&lock);
     
    15161527        }
    15171528
    1518         // release the application phone
     1529        /* Release the application phone */
    15191530        ipc_hangup(app_phone);
    15201531
    15211532        printf("release\n");
    1522         // release all local sockets
     1533        /* Release all local sockets */
    15231534        socket_cores_release(tcp_globals.net_phone, &local_sockets,
    15241535            &tcp_globals.sockets, tcp_free_socket_data);
     
    15361547        assert(timeout);
    15371548
    1538         // sleep the given timeout
     1549        /* Sleep the given timeout */
    15391550        async_usleep(timeout->timeout);
    1540         // lock the globals
     1551        /* Lock the globals */
    15411552        if (timeout->globals_read_only)
    15421553                fibril_rwlock_read_lock(&tcp_globals.lock);
     
    15441555                fibril_rwlock_write_lock(&tcp_globals.lock);
    15451556
    1546         // find the pending operation socket
     1557        /* Find the pending operation socket */
    15471558        socket = socket_port_find(&tcp_globals.sockets, timeout->port,
    15481559            timeout->key, timeout->key_length);
     
    15571568        fibril_rwlock_write_lock(socket_data->local_lock);
    15581569        if (timeout->sequence_number) {
    1559                 // increase the timeout counter;
     1570                /* Increase the timeout counter */
    15601571                socket_data->timeout_count++;
    15611572                if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
    1562                         // TODO release as connection lost
     1573                        /* TODO release as connection lost */
    15631574                        //tcp_refresh_socket_data(socket_data);
    15641575                        fibril_rwlock_write_unlock(socket_data->local_lock);
    15651576                } else {
    1566                         // retransmit
     1577                        /* Retransmit */
    15671578//                      tcp_retransmit_packet(socket,
    15681579//                          socket_data, timeout->sequence_number);
     
    15711582        } else {
    15721583                fibril_mutex_lock(&socket_data->operation.mutex);
    1573                 // set the timeout operation result if state not
    1574                 // changed
     1584                /* Set the timeout operation result if state not changed */
    15751585                if (socket_data->state == timeout->state) {
    15761586                        socket_data->operation.result = ETIMEOUT;
    1577                         // notify the main fibril
     1587
     1588                        /* Notify the main fibril */
    15781589                        fibril_condvar_signal(&socket_data->operation.condvar);
    1579                         // keep the global write lock
     1590
     1591                        /* Keep the global write lock */
    15801592                        keep_write_lock = true;
    15811593                } else {
    1582                         // operation is ok, do nothing
    1583                         // unlocking from now on, so the unlocki
    1584                         // order does not matter...
     1594                        /*
     1595                         * Operation is ok, do nothing.
     1596                         * Unlocking from now on, so the unlocking
     1597                         * order does not matter.
     1598                         */
    15851599                        fibril_rwlock_write_unlock(socket_data->local_lock);
    15861600                }
     
    15891603
    15901604out:
    1591         // unlock only if no socket
     1605        /* Unlock only if no socket */
    15921606        if (timeout->globals_read_only)
    15931607                fibril_rwlock_read_unlock(&tcp_globals.lock);
    15941608        else if (!keep_write_lock)
    1595                 // release if not desired
     1609                /* Release if not desired */
    15961610                fibril_rwlock_write_unlock(&tcp_globals.lock);
    15971611       
    1598         // release the timeout structure
     1612        /* Release the timeout structure */
    15991613        free(timeout);
    16001614        return EOK;
     
    16101624        assert(timeout);
    16111625
    1612         // sleep the given timeout
     1626        /* Sleep the given timeout */
    16131627        async_usleep(timeout->timeout);
    1614         // lock the globals
     1628
     1629        /* Lock the globals */
    16151630        fibril_rwlock_write_lock(&tcp_globals.lock);
    1616         // find the pending operation socket
     1631
     1632        /* Find the pending operation socket */
    16171633        socket = socket_port_find(&tcp_globals.sockets, timeout->port,
    16181634            timeout->key, timeout->key_length);
     1635
    16191636        if (socket && (socket->socket_id == timeout->socket_id)) {
    16201637                socket_data = (tcp_socket_data_ref) socket->specific_data;
     
    16291646                }
    16301647        }
    1631         // unlock the globals
     1648
     1649        /* Unlock the globals */
    16321650        fibril_rwlock_write_unlock(&tcp_globals.lock);
    1633         // release the timeout structure
     1651
     1652        /* Release the timeout structure */
    16341653        free(timeout);
     1654
    16351655        return EOK;
    16361656}
    16371657
    1638 void
    1639 tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
    1640     size_t sequence_number)
     1658void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref
     1659    socket_data, size_t sequence_number)
    16411660{
    16421661        packet_t packet;
     
    16481667        assert(socket->specific_data == socket_data);
    16491668
    1650         // sent packet?
     1669        /* Sent packet? */
    16511670        packet = pq_find(socket_data->outgoing, sequence_number);
    16521671        printf("retransmit %d\n", packet_get_id(packet));
     
    16641683}
    16651684
    1666 int
    1667 tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog)
     1685int tcp_listen_message(socket_cores_ref local_sockets, int socket_id,
     1686    int backlog)
    16681687{
    16691688        socket_core_ref socket;
     
    16751694                return EINVAL;
    16761695
    1677         // find the socket
     1696        /* Find the socket */
    16781697        socket = socket_cores_find(local_sockets, socket_id);
    16791698        if (!socket)
    16801699                return ENOTSOCK;
    16811700       
    1682         // get the socket specific data
     1701        /* Get the socket specific data */
    16831702        socket_data = (tcp_socket_data_ref) socket->specific_data;
    16841703        assert(socket_data);
    1685         // set the backlog
     1704
     1705        /* Set the backlog */
    16861706        socket_data->backlog = backlog;
    16871707
     
    16891709}
    16901710
    1691 int
    1692 tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
     1711int tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
    16931712    struct sockaddr *addr, socklen_t addrlen)
    16941713{
     
    17001719        assert(addrlen > 0);
    17011720
    1702         // find the socket
     1721        /* Find the socket */
    17031722        socket = socket_cores_find(local_sockets, socket_id);
    17041723        if (!socket)
     
    17081727        if (rc != EOK) {
    17091728                tcp_free_socket_data(socket);
    1710                 // unbind if bound
     1729                /* Unbind if bound */
    17111730                if (socket->port > 0) {
    17121731                        socket_ports_exclude(&tcp_globals.sockets,
     
    17181737}
    17191738
    1720 int
    1721 tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
     1739int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
    17221740    struct sockaddr *addr, socklen_t addrlen)
    17231741{
     
    17301748        assert(addrlen > 0);
    17311749
    1732         // get the socket specific data
     1750        /* Get the socket specific data */
    17331751        socket_data = (tcp_socket_data_ref) socket->specific_data;
    17341752        assert(socket_data);
     
    17391757                return EINVAL;
    17401758
    1741         // get the destination port
     1759        /* Get the destination port */
    17421760        rc = tl_get_address_port(addr, addrlen, &socket_data->dest_port);
    17431761        if (rc != EOK)
     
    17451763       
    17461764        if (socket->port <= 0) {
    1747                 // try to find a free port
     1765                /* Try to find a free port */
    17481766                rc = socket_bind_free_port(&tcp_globals.sockets, socket,
    17491767                    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
     
    17511769                if (rc != EOK)
    17521770                        return rc;
    1753                 // set the next port as the search starting port number
     1771                /* Set the next port as the search starting port number */
    17541772                tcp_globals.last_used_port = socket->port;
    17551773        }
     
    17611779                return rc;
    17621780
    1763         // create the notification packet
     1781        /* Create the notification packet */
    17641782        rc = tcp_create_notification_packet(&packet, socket, socket_data, 1, 0);
    17651783        if (rc != EOK)
    17661784                return rc;
    17671785
    1768         // unlock the globals and wait for an operation
     1786        /* Unlock the globals and wait for an operation */
    17691787        fibril_rwlock_write_unlock(&tcp_globals.lock);
    17701788
    17711789        socket_data->addr = addr;
    17721790        socket_data->addrlen = addrlen;
    1773         // send the packet
     1791
     1792        /* Send the packet */
    17741793
    17751794        if (((rc = tcp_queue_packet(socket, socket_data, packet, 1)) != EOK) ||
     
    17851804                        fibril_mutex_lock(&socket_data->operation.mutex);
    17861805                        fibril_rwlock_write_unlock(socket_data->local_lock);
    1787                         // send the packet
     1806
     1807                        /* Send the packet */
    17881808                        printf("connecting %d\n", packet_get_id(packet));
    17891809                        tcp_send_packets(socket_data->device_id, packet);
    17901810
    1791                         // wait for a reply
     1811                        /* Wait for a reply */
    17921812                        fibril_condvar_wait(&socket_data->operation.condvar,
    17931813                            &socket_data->operation.mutex);
     
    18051825
    18061826        fibril_mutex_unlock(&socket_data->operation.mutex);
    1807 
    1808         // return the result
    18091827        return rc;
    18101828}
    18111829
    1812 int
    1813 tcp_queue_prepare_packet(socket_core_ref socket,
     1830int tcp_queue_prepare_packet(socket_core_ref socket,
    18141831    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length)
    18151832{
     
    18211838        assert(socket->specific_data == socket_data);
    18221839
    1823         // get tcp header
     1840        /* Get TCP header */
    18241841        header = (tcp_header_ref) packet_get_data(packet);
    18251842        if (!header)
     
    18351852                return tcp_release_and_return(packet, EINVAL);
    18361853
    1837         // remember the outgoing FIN
     1854        /* Remember the outgoing FIN */
    18381855        if (header->finalize)
    18391856                socket_data->fin_outgoing = socket_data->next_outgoing;
     
    18421859}
    18431860
    1844 int
    1845 tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
     1861int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
    18461862    packet_t packet, size_t data_length)
    18471863{
     
    18651881}
    18661882
    1867 packet_t
    1868 tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data)
     1883packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref
     1884    socket_data)
    18691885{
    18701886        packet_t packet;
     
    18831899                pq_get_order(packet, NULL, &data_length);
    18841900
    1885                 // send only if fits into the window
    1886                 // respecting the possible overflow
     1901                /*
     1902                 * Send only if fits into the window, respecting the possible
     1903                 * overflow.
     1904                 */
    18871905                if (!IS_IN_INTERVAL_OVERFLOW(
    18881906                    (uint32_t) socket_data->last_outgoing,
     
    19091927                previous = copy;
    19101928                packet = pq_next(packet);
    1911                 // overflow occurred ?
     1929
     1930                /* Overflow occurred? */
    19121931                if (!packet &&
    19131932                    (socket_data->last_outgoing > socket_data->next_outgoing)) {
    19141933                        printf("gpts overflow\n");
    1915                         // continue from the beginning
     1934                        /* Continue from the beginning */
    19161935                        packet = socket_data->outgoing;
    19171936                }
     
    19221941}
    19231942
    1924 packet_t
    1925 tcp_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)
     1943packet_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)
    19271945{
    19281946        tcp_header_ref header;
     
    19341952        assert(socket->specific_data == socket_data);
    19351953
    1936         // adjust the pseudo header
     1954        /* Adjust the pseudo header */
    19371955        rc = ip_client_set_pseudo_header_data_length(socket_data->pseudo_header,
    19381956            socket_data->headerlen, packet_get_data_length(packet));
     
    19421960        }
    19431961
    1944         // get the header
     1962        /* Get the header */
    19451963        header = (tcp_header_ref) packet_get_data(packet);
    19461964        if (!header) {
     
    19501968        assert(ntohl(header->sequence_number) == sequence_number);
    19511969
    1952         // adjust the header
     1970        /* Adjust the header */
    19531971        if (socket_data->next_incoming) {
    19541972                header->acknowledgement_number =
     
    19581976        header->window = htons(socket_data->window);
    19591977
    1960         // checksum
     1978        /* Checksum */
    19611979        header->checksum = 0;
    19621980        checksum = compute_checksum(0, socket_data->pseudo_header,
     
    19671985        header->checksum = htons(flip_checksum(compact_checksum(checksum)));
    19681986
    1969         // prepare the packet
     1987        /* Prepare the packet */
    19701988        rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0);
    19711989        if (rc != EOK) {
     
    19731991                return NULL;
    19741992        }
     1993
    19751994        rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data,
    19761995            sequence_number, socket_data->state, socket_data->timeout, true);
     
    19832002}
    19842003
    1985 packet_t
    1986 tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref socket_data,
    1987     packet_t packet, size_t data_length, size_t sequence_number)
     2004packet_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)
    19882006{
    19892007        packet_t copy;
     
    19932011        assert(socket->specific_data == socket_data);
    19942012
    1995         // make a copy of the packet
     2013        /* Make a copy of the packet */
    19962014        copy = packet_get_copy(tcp_globals.net_phone, packet);
    19972015        if (!copy)
     
    20142032}
    20152033
    2016 void
    2017 tcp_prepare_operation_header(socket_core_ref socket,
     2034void tcp_prepare_operation_header(socket_core_ref socket,
    20182035    tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize,
    20192036    int finalize)
     
    20322049}
    20332050
    2034 int
    2035 tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
     2051int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
    20362052    socket_core_ref socket, tcp_socket_data_ref socket_data,
    20372053    size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
     
    20452061        assert(socket->specific_data == socket_data);
    20462062
    2047         // prepare the timeout with key bundle structure
     2063        /* Prepare the timeout with key bundle structure */
    20482064        operation_timeout = malloc(sizeof(*operation_timeout) +
    20492065            socket->key_length + 1);
     
    20602076        operation_timeout->state = state;
    20612077
    2062         // copy the key
     2078        /* Copy the key */
    20632079        operation_timeout->key = ((char *) operation_timeout) +
    20642080            sizeof(*operation_timeout);
     
    20672083        operation_timeout->key[operation_timeout->key_length] = '\0';
    20682084
    2069         // prepare the timeouting thread
     2085        /* Prepare the timeouting thread */
    20702086        fibril = fibril_create(timeout_function, operation_timeout);
    20712087        if (!fibril) {
     
    20742090        }
    20752091//      fibril_mutex_lock(&socket_data->operation.mutex);
    2076         // start the timeouting fibril
     2092        /* Start the timeout fibril */
    20772093        fibril_add_ready(fibril);
    20782094        //socket_data->state = state;
     
    20802096}
    20812097
    2082 int
    2083 tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
    2084     size_t *addrlen)
     2098int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id,
     2099    int flags, size_t *addrlen)
    20852100{
    20862101        socket_core_ref socket;
     
    20932108        assert(local_sockets);
    20942109
    2095         // find the socket
     2110        /* Find the socket */
    20962111        socket = socket_cores_find(local_sockets, socket_id);
    20972112        if (!socket)
    20982113                return ENOTSOCK;
    20992114
    2100         // get the socket specific data
     2115        /* Get the socket specific data */
    21012116        if (!socket->specific_data)
    21022117                return NO_DATA;
     
    21042119        socket_data = (tcp_socket_data_ref) socket->specific_data;
    21052120
    2106         // check state
     2121        /* Check state */
    21072122        if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
    21082123            (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
    21092124                return ENOTCONN;
    21102125
    2111         // send the source address if desired
     2126        /* Send the source address if desired */
    21122127        if (addrlen) {
    21132128                rc = data_reply(socket_data->addr, socket_data->addrlen);
     
    21172132        }
    21182133
    2119         // get the next received packet
     2134        /* Get the next received packet */
    21202135        packet_id = dyn_fifo_value(&socket->received);
    21212136        if (packet_id < 0)
     
    21262141                return rc;
    21272142
    2128         // reply the packets
     2143        /* Reply the packets */
    21292144        rc = socket_reply_packets(packet, &length);
    21302145        if (rc != EOK)
    21312146                return rc;
    21322147
    2133         // release the packet
     2148        /* Release the packet */
    21342149        dyn_fifo_pop(&socket->received);
    21352150        pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    2136         // return the total length
     2151
     2152        /* Return the total length */
    21372153        return (int) length;
    21382154}
    21392155
    2140 int
    2141 tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments,
    2142     size_t *data_fragment_size, int flags)
     2156int tcp_send_message(socket_cores_ref local_sockets, int socket_id,
     2157    int fragments, size_t *data_fragment_size, int flags)
    21432158{
    21442159        socket_core_ref socket;
     
    21552170        assert(data_fragment_size);
    21562171
    2157         // find the socket
     2172        /* Find the socket */
    21582173        socket = socket_cores_find(local_sockets, socket_id);
    21592174        if (!socket)
    21602175                return ENOTSOCK;
    21612176
    2162         // get the socket specific data
     2177        /* Get the socket specific data */
    21632178        if (!socket->specific_data)
    21642179                return NO_DATA;
     
    21662181        socket_data = (tcp_socket_data_ref) socket->specific_data;
    21672182
    2168         // check state
     2183        /* Check state */
    21692184        if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
    21702185            (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
     
    21812196
    21822197        for (index = 0; index < fragments; index++) {
    2183                 // read the data fragment
     2198                /* Read the data fragment */
    21842199                result = tl_socket_read_packet_data(tcp_globals.net_phone,
    21852200                    &packet, TCP_HEADER_SIZE, packet_dimension,
     
    21892204
    21902205                total_length = (size_t) result;
    2191                 // prefix the tcp header
     2206
     2207                /* Prefix the TCP header */
    21922208                header = PACKET_PREFIX(packet, tcp_header_t);
    21932209                if (!header)
     
    22002216        }
    22012217
    2202         // flush packets
     2218        /* Flush packets */
    22032219        packet = tcp_get_packets_to_send(socket, socket_data);
    22042220        fibril_rwlock_write_unlock(socket_data->local_lock);
     
    22062222
    22072223        if (packet) {
    2208                 // send the packet
     2224                /* Send the packet */
    22092225                tcp_send_packets(socket_data->device_id, packet);
    22102226        }
     
    22212237        int rc;
    22222238
    2223         // find the socket
     2239        /* Find the socket */
    22242240        socket = socket_cores_find(local_sockets, socket_id);
    22252241        if (!socket)
    22262242                return ENOTSOCK;
    22272243
    2228         // get the socket specific data
     2244        /* Get the socket specific data */
    22292245        socket_data = (tcp_socket_data_ref) socket->specific_data;
    22302246        assert(socket_data);
    22312247
    2232         // check state
     2248        /* Check state */
    22332249        switch (socket_data->state) {
    22342250        case TCP_SOCKET_ESTABLISHED:
     
    22432259
    22442260        default:
    2245                 // just destroy
     2261                /* Just destroy */
    22462262                rc = socket_destroy(tcp_globals.net_phone, socket_id,
    22472263                    local_sockets, &tcp_globals.sockets,
     
    22542270        }
    22552271
    2256         // send FIN
    2257         // TODO should I wait to complete?
    2258 
    2259         // create the notification packet
     2272        /*
     2273         * Send FIN.
     2274         * TODO should I wait to complete?
     2275         */
     2276
     2277        /* Create the notification packet */
    22602278        rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
    22612279        if (rc != EOK)
    22622280                return rc;
    22632281
    2264         // send the packet
     2282        /* Send the packet */
    22652283        rc = tcp_queue_packet(socket, socket_data, packet, 1);
    22662284        if (rc != EOK)
    22672285                return rc;
    22682286
    2269         // flush packets
     2287        /* Flush packets */
    22702288        packet = tcp_get_packets_to_send(socket, socket_data);
    22712289        fibril_rwlock_write_unlock(socket_data->local_lock);
     
    22732291
    22742292        if (packet) {
    2275                 // send the packet
     2293                /* Send the packet */
    22762294                tcp_send_packets(socket_data->device_id, packet);
    22772295        }
     
    22802298}
    22812299
    2282 int
    2283 tcp_create_notification_packet(packet_t *packet, socket_core_ref socket,
     2300int tcp_create_notification_packet(packet_t *packet, socket_core_ref socket,
    22842301    tcp_socket_data_ref socket_data, int synchronize, int finalize)
    22852302{
     
    22902307        assert(packet);
    22912308
    2292         // get the device packet dimension
     2309        /* Get the device packet dimension */
    22932310        rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
    22942311            &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
     
    22962313                return rc;
    22972314
    2298         // get a new packet
     2315        /* Get a new packet */
    22992316        *packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE,
    23002317            packet_dimension->addr_len, packet_dimension->prefix,
     
    23042321                return ENOMEM;
    23052322
    2306         // allocate space in the packet
     2323        /* Allocate space in the packet */
    23072324        header = PACKET_SUFFIX(*packet, tcp_header_t);
    23082325        if (!header)
     
    23152332}
    23162333
    2317 int
    2318 tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
     2334int tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
    23192335    int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
    23202336{
     
    23292345        assert(addrlen);
    23302346
    2331         // find the socket
     2347        /* Find the socket */
    23322348        socket = socket_cores_find(local_sockets, socket_id);
    23332349        if (!socket)
    23342350                return ENOTSOCK;
    23352351
    2336         // get the socket specific data
     2352        /* Get the socket specific data */
    23372353        socket_data = (tcp_socket_data_ref) socket->specific_data;
    23382354        assert(socket_data);
    23392355
    2340         // check state
     2356        /* Check state */
    23412357        if (socket_data->state != TCP_SOCKET_LISTEN)
    23422358                return EINVAL;
     
    23522368                        return ENOTSOCK;
    23532369
    2354                 // get the socket specific data
     2370                /* Get the socket specific data */
    23552371                socket_data = (tcp_socket_data_ref) accepted->specific_data;
    23562372                assert(socket_data);
    2357                 // TODO can it be in another state?
     2373                /* TODO can it be in another state? */
    23582374                if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
    23592375                        rc = data_reply(socket_data->addr,
     
    23892405}
    23902406
    2391 void
    2392 tcp_free_socket_data(socket_core_ref socket)
     2407void tcp_free_socket_data(socket_core_ref socket)
    23932408{
    23942409        tcp_socket_data_ref socket_data;
     
    23982413        printf("destroy_socket %d\n", socket->socket_id);
    23992414
    2400         // get the socket specific data
     2415        /* Get the socket specific data */
    24012416        socket_data = (tcp_socket_data_ref) socket->specific_data;
    24022417        assert(socket_data);
    2403         //free the pseudo header
     2418
     2419        /* Free the pseudo header */
    24042420        if (socket_data->pseudo_header) {
    24052421                if (socket_data->headerlen) {
     
    24102426                socket_data->pseudo_header = NULL;
    24112427        }
     2428
    24122429        socket_data->headerlen = 0;
    2413         // free the address
     2430
     2431        /* Free the address */
    24142432        if (socket_data->addr) {
    24152433                if (socket_data->addrlen) {
     
    24732491
    24742492                /*
    2475                    Answer the message
     2493                 * Answer the message
    24762494                 */
    24772495                answer_call(callid, res, &answer, answer_count);
Note: See TracChangeset for help on using the changeset viewer.