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


Ignore:
Timestamp:
2011-06-01T08:43:42Z (15 years ago)
Author:
Lubos Slovak <lubos.slovak@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8d6c1f1
Parents:
9e2e715 (diff), e51a514 (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:

Huuuuuge merge from development - all the work actually :)

File:
1 edited

Legend:

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

    r9e2e715 reb522e8  
    3636 */
    3737
    38 #include "tcp.h"
    39 #include "tcp_header.h"
    40 #include "tcp_module.h"
    41 
    4238#include <assert.h>
    4339#include <async.h>
     
    4844#include <errno.h>
    4945
    50 #include <ipc/ipc.h>
    5146#include <ipc/services.h>
    5247#include <ipc/net.h>
     
    6863#include <ip_interface.h>
    6964#include <icmp_client.h>
    70 #include <icmp_interface.h>
     65#include <icmp_remote.h>
    7166#include <net_interface.h>
    7267#include <socket_core.h>
    7368#include <tl_common.h>
    74 #include <tl_local.h>
    75 #include <tl_interface.h>
     69#include <tl_remote.h>
     70#include <tl_skel.h>
     71
     72#include "tcp.h"
     73#include "tcp_header.h"
    7674
    7775/** TCP module name. */
    78 #define NAME    "TCP protocol"
     76#define NAME  "tcp"
    7977
    8078/** The TCP window default value. */
     
    127125typedef struct tcp_timeout tcp_timeout_t;
    128126
    129 /** Type definition of the TCP timeout pointer.
    130  *  @see tcp_timeout
    131  */
    132 typedef tcp_timeout_t *tcp_timeout_ref;
    133 
    134127/** TCP reply timeout data.
    135128 *  Used as a timeouting fibril argument.
     
    144137
    145138        /** Local sockets. */
    146         socket_cores_ref local_sockets;
     139        socket_cores_t *local_sockets;
    147140
    148141        /** Socket identifier. */
     
    159152
    160153        /** Port map key. */
    161         char *key;
     154        uint8_t *key;
    162155
    163156        /** Port map key length. */
     
    165158};
    166159
    167 static int tcp_release_and_return(packet_t, int);
    168 static void tcp_prepare_operation_header(socket_core_ref, tcp_socket_data_ref,
    169     tcp_header_ref, int synchronize, int);
    170 static int tcp_prepare_timeout(int (*)(void *), socket_core_ref,
    171     tcp_socket_data_ref, size_t, tcp_socket_state_t, suseconds_t, int);
    172 static void tcp_free_socket_data(socket_core_ref);
     160static int tcp_release_and_return(packet_t *, int);
     161static void tcp_prepare_operation_header(socket_core_t *, tcp_socket_data_t *,
     162    tcp_header_t *, int synchronize, int);
     163static int tcp_prepare_timeout(int (*)(void *), socket_core_t *,
     164    tcp_socket_data_t *, size_t, tcp_socket_state_t, suseconds_t, int);
     165static void tcp_free_socket_data(socket_core_t *);
    173166
    174167static int tcp_timeout(void *);
     
    176169static int tcp_release_after_timeout(void *);
    177170
    178 static int tcp_process_packet(device_id_t, packet_t, services_t);
    179 static int tcp_connect_core(socket_core_ref, socket_cores_ref,
     171static int tcp_process_packet(device_id_t, packet_t *, services_t);
     172static int tcp_connect_core(socket_core_t *, socket_cores_t *,
    180173    struct sockaddr *, socklen_t);
    181 static int tcp_queue_prepare_packet(socket_core_ref, tcp_socket_data_ref,
    182     packet_t, size_t);
    183 static int tcp_queue_packet(socket_core_ref, tcp_socket_data_ref, packet_t,
     174static int tcp_queue_prepare_packet(socket_core_t *, tcp_socket_data_t *,
     175    packet_t *, size_t);
     176static int tcp_queue_packet(socket_core_t *, tcp_socket_data_t *, packet_t *,
    184177    size_t);
    185 static packet_t tcp_get_packets_to_send(socket_core_ref, tcp_socket_data_ref);
    186 static void tcp_send_packets(device_id_t, packet_t);
    187 
    188 static void tcp_process_acknowledgement(socket_core_ref, tcp_socket_data_ref,
    189     tcp_header_ref);
    190 static packet_t tcp_send_prepare_packet(socket_core_ref, tcp_socket_data_ref,
    191     packet_t, size_t, size_t);
    192 static packet_t tcp_prepare_copy(socket_core_ref, tcp_socket_data_ref, packet_t,
    193     size_t, size_t);
    194 /* static */ void tcp_retransmit_packet(socket_core_ref, tcp_socket_data_ref,
     178static packet_t *tcp_get_packets_to_send(socket_core_t *, tcp_socket_data_t *);
     179static void tcp_send_packets(device_id_t, packet_t *);
     180
     181static void tcp_process_acknowledgement(socket_core_t *, tcp_socket_data_t *,
     182    tcp_header_t *);
     183static packet_t *tcp_send_prepare_packet(socket_core_t *, tcp_socket_data_t *,
     184    packet_t *, size_t, size_t);
     185static packet_t *tcp_prepare_copy(socket_core_t *, tcp_socket_data_t *,
     186    packet_t *, size_t, size_t);
     187/* static */ void tcp_retransmit_packet(socket_core_t *, tcp_socket_data_t *,
    195188    size_t);
    196 static int tcp_create_notification_packet(packet_t *, socket_core_ref,
    197     tcp_socket_data_ref, int, int);
    198 static void tcp_refresh_socket_data(tcp_socket_data_ref);
    199 
    200 static void tcp_initialize_socket_data(tcp_socket_data_ref);
    201 
    202 static int tcp_process_listen(socket_core_ref, tcp_socket_data_ref,
    203     tcp_header_ref, packet_t, struct sockaddr *, struct sockaddr *, size_t);
    204 static int tcp_process_syn_sent(socket_core_ref, tcp_socket_data_ref,
    205     tcp_header_ref, packet_t);
    206 static int tcp_process_syn_received(socket_core_ref, tcp_socket_data_ref,
    207     tcp_header_ref, packet_t);
    208 static int tcp_process_established(socket_core_ref, tcp_socket_data_ref,
    209     tcp_header_ref, packet_t, int, size_t);
    210 static int tcp_queue_received_packet(socket_core_ref, tcp_socket_data_ref,
    211     packet_t, int, size_t);
    212 
    213 static int tcp_received_msg(device_id_t, packet_t, services_t, services_t);
     189static int tcp_create_notification_packet(packet_t **, socket_core_t *,
     190    tcp_socket_data_t *, int, int);
     191static void tcp_refresh_socket_data(tcp_socket_data_t *);
     192
     193static void tcp_initialize_socket_data(tcp_socket_data_t *);
     194
     195static int tcp_process_listen(socket_core_t *, tcp_socket_data_t *,
     196    tcp_header_t *, packet_t *, struct sockaddr *, struct sockaddr *, size_t);
     197static int tcp_process_syn_sent(socket_core_t *, tcp_socket_data_t *,
     198    tcp_header_t *, packet_t *);
     199static int tcp_process_syn_received(socket_core_t *, tcp_socket_data_t *,
     200    tcp_header_t *, packet_t *);
     201static int tcp_process_established(socket_core_t *, tcp_socket_data_t *,
     202    tcp_header_t *, packet_t *, int, size_t);
     203static int tcp_queue_received_packet(socket_core_t *, tcp_socket_data_t *,
     204    packet_t *, int, size_t);
     205static void tcp_queue_received_end_of_data(socket_core_t *socket);
     206
     207static int tcp_received_msg(device_id_t, packet_t *, services_t, services_t);
    214208static int tcp_process_client_messages(ipc_callid_t, ipc_call_t);
    215209
    216 static int tcp_listen_message(socket_cores_ref, int, int);
    217 static int tcp_connect_message(socket_cores_ref, int, struct sockaddr *,
     210static int tcp_listen_message(socket_cores_t *, int, int);
     211static int tcp_connect_message(socket_cores_t *, int, struct sockaddr *,
    218212    socklen_t);
    219 static int tcp_recvfrom_message(socket_cores_ref, int, int, size_t *);
    220 static int tcp_send_message(socket_cores_ref, int, int, size_t *, int);
    221 static int tcp_accept_message(socket_cores_ref, int, int, size_t *, size_t *);
    222 static int tcp_close_message(socket_cores_ref, int);
     213static int tcp_recvfrom_message(socket_cores_t *, int, int, size_t *);
     214static int tcp_send_message(socket_cores_t *, int, int, size_t *, int);
     215static int tcp_accept_message(socket_cores_t *, int, int, size_t *, size_t *);
     216static int tcp_close_message(socket_cores_t *, int);
    223217
    224218/** TCP global data. */
    225219tcp_globals_t tcp_globals;
    226220
    227 /** Initializes the TCP module.
    228  *
    229  * @param[in] client_connection The client connection processing function. The
    230  *                      module skeleton propagates its own one.
    231  * @returns             EOK on success.
    232  * @returns             ENOMEM if there is not enough memory left.
    233  */
    234 int tcp_initialize(async_client_conn_t client_connection)
    235 {
    236         int rc;
    237 
    238         assert(client_connection);
    239 
    240         fibril_rwlock_initialize(&tcp_globals.lock);
    241         fibril_rwlock_write_lock(&tcp_globals.lock);
    242 
    243         tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
    244             ICMP_CONNECT_TIMEOUT);
    245         tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
    246             SERVICE_TCP, client_connection);
    247         if (tcp_globals.ip_phone < 0) {
    248                 fibril_rwlock_write_unlock(&tcp_globals.lock);
    249                 return tcp_globals.ip_phone;
    250         }
    251        
    252         rc = socket_ports_initialize(&tcp_globals.sockets);
    253         if (rc != EOK)
    254                 goto out;
    255 
    256         rc = packet_dimensions_initialize(&tcp_globals.dimensions);
    257         if (rc != EOK) {
    258                 socket_ports_destroy(&tcp_globals.sockets);
    259                 goto out;
    260         }
    261 
    262         tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
    263 
    264 out:
    265         fibril_rwlock_write_unlock(&tcp_globals.lock);
    266         return rc;
    267 }
    268 
    269 int tcp_received_msg(device_id_t device_id, packet_t packet,
     221int tcp_received_msg(device_id_t device_id, packet_t *packet,
    270222    services_t receiver, services_t error)
    271223{
     
    285237}
    286238
    287 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error)
     239int tcp_process_packet(device_id_t device_id, packet_t *packet, services_t error)
    288240{
    289241        size_t length;
    290242        size_t offset;
    291243        int result;
    292         tcp_header_ref header;
    293         socket_core_ref socket;
    294         tcp_socket_data_ref socket_data;
    295         packet_t next_packet;
     244        tcp_header_t *header;
     245        socket_core_t *socket;
     246        tcp_socket_data_t *socket_data;
     247        packet_t *next_packet;
    296248        size_t total_length;
    297249        uint32_t checksum;
     
    343295
    344296        /* Get tcp header */
    345         header = (tcp_header_ref) packet_get_data(packet);
     297        header = (tcp_header_t *) packet_get_data(packet);
    346298        if (!header)
    347299                return tcp_release_and_return(packet, NO_DATA);
    348300
    349 //      printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header),
    350 //          ntohs(header->destination_port));
    351 
     301#if 0
     302        printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header),
     303            ntohs(header->destination_port));
     304#endif
    352305        result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
    353306        if (result <= 0)
     
    362315        /* Find the destination socket */
    363316        socket = socket_port_find(&tcp_globals.sockets,
    364             ntohs(header->destination_port), (const char *) src, addrlen);
     317            ntohs(header->destination_port), (uint8_t *) src, addrlen);
    365318        if (!socket) {
    366319                /* Find the listening destination socket */
    367320                socket = socket_port_find(&tcp_globals.sockets,
    368                     ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,
    369                     0);
     321                    ntohs(header->destination_port),
     322                    (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
    370323        }
    371324
     
    380333
    381334        printf("socket id %d\n", socket->socket_id);
    382         socket_data = (tcp_socket_data_ref) socket->specific_data;
     335        socket_data = (tcp_socket_data_t *) socket->specific_data;
    383336        assert(socket_data);
    384337
     
    458411
    459412has_error_service:
    460         fibril_rwlock_read_unlock(&tcp_globals.lock);
     413        fibril_rwlock_write_unlock(&tcp_globals.lock);
    461414
    462415        /* TODO error reporting/handling */
     
    497450}
    498451
    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,
     452int tcp_process_established(socket_core_t *socket, tcp_socket_data_t *
     453    socket_data, tcp_header_t *header, packet_t *packet, int fragments,
    501454    size_t total_length)
    502455{
    503         packet_t next_packet;
    504         packet_t tmp_packet;
     456        packet_t *next_packet;
     457        packet_t *tmp_packet;
    505458        uint32_t old_incoming;
    506459        size_t order;
     
    509462        size_t offset;
    510463        uint32_t new_sequence_number;
     464        bool forced_ack;
    511465        int rc;
    512466
     
    517471        assert(packet);
    518472
     473        forced_ack = false;
     474
    519475        new_sequence_number = ntohl(header->sequence_number);
    520476        old_incoming = socket_data->next_incoming;
    521477
    522         if (header->finalize)
    523                 socket_data->fin_incoming = new_sequence_number;
     478        if (header->finalize) {
     479                socket_data->fin_incoming = new_sequence_number +
     480                    total_length - TCP_HEADER_LENGTH(header);
     481        }
    524482
    525483        /* Trim begining if containing expected data */
     
    765723                /* Release duplicite or restricted */
    766724                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    767         }
    768 
    769         /* Change state according to the acknowledging incoming fin */
    770         if (IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming,
    771             socket_data->next_incoming)) {
     725                forced_ack = true;
     726        }
     727
     728        /* If next in sequence is an incoming FIN */
     729        if (socket_data->next_incoming == socket_data->fin_incoming) {
     730                /* Advance sequence number */
     731                socket_data->next_incoming += 1;
     732
     733                /* Handle FIN */
    772734                switch (socket_data->state) {
    773735                case TCP_SOCKET_FIN_WAIT_1:
     
    776738                        socket_data->state = TCP_SOCKET_CLOSING;
    777739                        break;
    778                 /*case TCP_ESTABLISHED:*/
     740                case TCP_SOCKET_ESTABLISHED:
     741                        /* Queue end-of-data marker on the socket. */
     742                        tcp_queue_received_end_of_data(socket);
     743                        socket_data->state = TCP_SOCKET_CLOSE_WAIT;
     744                        break;
    779745                default:
    780746                        socket_data->state = TCP_SOCKET_CLOSE_WAIT;
     
    784750
    785751        packet = tcp_get_packets_to_send(socket, socket_data);
    786         if (!packet) {
     752        if (!packet && (socket_data->next_incoming != old_incoming || forced_ack)) {
    787753                /* Create the notification packet */
    788754                rc = tcp_create_notification_packet(&packet, socket,
     
    805771}
    806772
    807 int tcp_queue_received_packet(socket_core_ref socket,
    808     tcp_socket_data_ref socket_data, packet_t packet, int fragments,
     773int tcp_queue_received_packet(socket_core_t *socket,
     774    tcp_socket_data_t *socket_data, packet_t *packet, int fragments,
    809775    size_t total_length)
    810776{
    811         packet_dimension_ref packet_dimension;
     777        packet_dimension_t *packet_dimension;
    812778        int rc;
    813779
     
    834800        /* Notify the destination socket */
    835801        async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
    836             (ipcarg_t) socket->socket_id,
     802            (sysarg_t) socket->socket_id,
    837803            ((packet_dimension->content < socket_data->data_fragment_size) ?
    838804            packet_dimension->content : socket_data->data_fragment_size), 0, 0,
    839             (ipcarg_t) fragments);
     805            (sysarg_t) fragments);
    840806
    841807        return EOK;
    842808}
    843809
    844 int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref
    845     socket_data, tcp_header_ref header, packet_t packet)
    846 {
    847         packet_t next_packet;
     810/** Queue end-of-data marker on the socket.
     811 *
     812 * Next element in the sequence space is FIN. Queue end-of-data marker
     813 * on the socket.
     814 *
     815 * @param socket        Socket
     816 */
     817static void tcp_queue_received_end_of_data(socket_core_t *socket)
     818{
     819        assert(socket != NULL);
     820
     821        /* Notify the destination socket */
     822        async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
     823            (sysarg_t) socket->socket_id,
     824            0, 0, 0,
     825            (sysarg_t) 0 /* 0 fragments == no more data */);
     826}
     827
     828int tcp_process_syn_sent(socket_core_t *socket, tcp_socket_data_t *
     829    socket_data, tcp_header_t *header, packet_t *packet)
     830{
     831        packet_t *next_packet;
    848832        int rc;
    849833
     
    900884}
    901885
    902 int tcp_process_listen(socket_core_ref listening_socket,
    903     tcp_socket_data_ref listening_socket_data, tcp_header_ref header,
    904     packet_t packet, struct sockaddr *src, struct sockaddr *dest,
     886int tcp_process_listen(socket_core_t *listening_socket,
     887    tcp_socket_data_t *listening_socket_data, tcp_header_t *header,
     888    packet_t *packet, struct sockaddr *src, struct sockaddr *dest,
    905889    size_t addrlen)
    906890{
    907         packet_t next_packet;
    908         socket_core_ref socket;
    909         tcp_socket_data_ref socket_data;
     891        packet_t *next_packet;
     892        socket_core_t *socket;
     893        tcp_socket_data_t *socket_data;
    910894        int socket_id;
    911895        int listening_socket_id = listening_socket->socket_id;
     
    922906                return tcp_release_and_return(packet, EINVAL);
    923907
    924         socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
     908        socket_data = (tcp_socket_data_t *) malloc(sizeof(*socket_data));
    925909        if (!socket_data)
    926910                return tcp_release_and_return(packet, ENOMEM);
     
    971955        /* Find the destination socket */
    972956        listening_socket = socket_port_find(&tcp_globals.sockets,
    973             listening_port, SOCKET_MAP_KEY_LISTENING, 0);
     957            listening_port, (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
    974958        if (!listening_socket ||
    975959            (listening_socket->socket_id != listening_socket_id)) {
     
    979963        }
    980964        listening_socket_data =
    981             (tcp_socket_data_ref) listening_socket->specific_data;
     965            (tcp_socket_data_t *) listening_socket->specific_data;
    982966        assert(listening_socket_data);
    983967
     
    991975                return ENOTSOCK;
    992976        }
    993         socket_data = (tcp_socket_data_ref) socket->specific_data;
     977        socket_data = (tcp_socket_data_t *) socket->specific_data;
    994978        assert(socket_data);
    995979
    996980        rc = socket_port_add(&tcp_globals.sockets, listening_port, socket,
    997             (const char *) socket_data->addr, socket_data->addrlen);
     981            (uint8_t *) socket_data->addr, socket_data->addrlen);
    998982        assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
    999             (const char *) socket_data->addr, socket_data->addrlen));
     983            (uint8_t *) socket_data->addr, socket_data->addrlen));
    1000984
    1001985//      rc = socket_bind_free_port(&tcp_globals.sockets, socket,
     
    10601044}
    10611045
    1062 int tcp_process_syn_received(socket_core_ref socket,
    1063     tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet)
    1064 {
    1065         socket_core_ref listening_socket;
    1066         tcp_socket_data_ref listening_socket_data;
     1046int tcp_process_syn_received(socket_core_t *socket,
     1047    tcp_socket_data_t *socket_data, tcp_header_t *header, packet_t *packet)
     1048{
     1049        socket_core_t *listening_socket;
     1050        tcp_socket_data_t *listening_socket_data;
    10671051        int rc;
    10681052
     
    10791063        tcp_process_acknowledgement(socket, socket_data, header);
    10801064
    1081         socket_data->next_incoming = ntohl(header->sequence_number);    // + 1;
     1065        socket_data->next_incoming = ntohl(header->sequence_number); /* + 1; */
    10821066        pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
    10831067        socket_data->state = TCP_SOCKET_ESTABLISHED;
     
    10861070        if (listening_socket) {
    10871071                listening_socket_data =
    1088                     (tcp_socket_data_ref) listening_socket->specific_data;
     1072                    (tcp_socket_data_t *) listening_socket->specific_data;
    10891073                assert(listening_socket_data);
    10901074
     
    10951079                        /* Notify the destination socket */
    10961080                        async_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
    1097                             (ipcarg_t) listening_socket->socket_id,
     1081                            (sysarg_t) listening_socket->socket_id,
    10981082                            socket_data->data_fragment_size, TCP_HEADER_SIZE,
    1099                             0, (ipcarg_t) socket->socket_id);
     1083                            0, (sysarg_t) socket->socket_id);
    11001084
    11011085                        fibril_rwlock_write_unlock(socket_data->local_lock);
     
    11271111}
    11281112
    1129 void tcp_process_acknowledgement(socket_core_ref socket,
    1130     tcp_socket_data_ref socket_data, tcp_header_ref header)
     1113void tcp_process_acknowledgement(socket_core_t *socket,
     1114    tcp_socket_data_t *socket_data, tcp_header_t *header)
    11311115{
    11321116        size_t number;
    11331117        size_t length;
    1134         packet_t packet;
    1135         packet_t next;
    1136         packet_t acknowledged = NULL;
     1118        packet_t *packet;
     1119        packet_t *next;
     1120        packet_t *acknowledged = NULL;
    11371121        uint32_t old;
    11381122
     
    12201204}
    12211205
     1206/** Per-connection initialization
     1207 *
     1208 */
     1209void tl_connection(void)
     1210{
     1211}
     1212
    12221213/** Processes the TCP message.
    12231214 *
     
    12271218 * @param[out] answer_count The last parameter for the actual answer in the
    12281219 *                      answer parameter.
    1229  * @returns             EOK on success.
    1230  * @returns             ENOTSUP if the message is not known.
     1220 * @return              EOK on success.
     1221 * @return              ENOTSUP if the message is not known.
    12311222 *
    12321223 * @see tcp_interface.h
    12331224 * @see IS_NET_TCP_MESSAGE()
    12341225 */
    1235 int
    1236 tcp_message_standalone(ipc_callid_t callid, ipc_call_t *call,
    1237     ipc_call_t *answer, int *answer_count)
    1238 {
    1239         packet_t packet;
    1240         int rc;
    1241 
     1226int tl_message(ipc_callid_t callid, ipc_call_t *call,
     1227    ipc_call_t *answer, size_t *answer_count)
     1228{
    12421229        assert(call);
    12431230        assert(answer);
     
    12451232
    12461233        *answer_count = 0;
    1247         switch (IPC_GET_METHOD(*call)) {
    1248         case NET_TL_RECEIVED:
    1249 //              fibril_rwlock_read_lock(&tcp_globals.lock);
    1250                 rc = packet_translate_remote(tcp_globals.net_phone, &packet,
    1251                     IPC_GET_PACKET(call));
    1252                 if (rc != EOK) {
    1253 //                      fibril_rwlock_read_unlock(&tcp_globals.lock);
    1254                         return rc;
    1255                 }
    1256                 rc = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP,
    1257                     IPC_GET_ERROR(call));
    1258 //              fibril_rwlock_read_unlock(&tcp_globals.lock);
    1259                 return rc;
     1234        switch (IPC_GET_IMETHOD(*call)) {
    12601235        case IPC_M_CONNECT_TO_ME:
    12611236                return tcp_process_client_messages(callid, *call);
     
    12651240}
    12661241
    1267 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data)
     1242void tcp_refresh_socket_data(tcp_socket_data_t *socket_data)
    12681243{
    12691244        assert(socket_data);
     
    12811256}
    12821257
    1283 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data)
     1258void tcp_initialize_socket_data(tcp_socket_data_t *socket_data)
    12841259{
    12851260        assert(socket_data);
     
    12961271        bool keep_on_going = true;
    12971272        socket_cores_t local_sockets;
    1298         int app_phone = IPC_GET_PHONE(&call);
     1273        int app_phone = IPC_GET_PHONE(call);
    12991274        struct sockaddr *addr;
    13001275        int socket_id;
     
    13031278        fibril_rwlock_t lock;
    13041279        ipc_call_t answer;
    1305         int answer_count;
    1306         tcp_socket_data_ref socket_data;
    1307         socket_core_ref socket;
    1308         packet_dimension_ref packet_dimension;
     1280        size_t answer_count;
     1281        tcp_socket_data_t *socket_data;
     1282        socket_core_t *socket;
     1283        packet_dimension_t *packet_dimension;
    13091284
    13101285        /*
     
    13281303
    13291304                /* Process the call */
    1330                 switch (IPC_GET_METHOD(call)) {
     1305                switch (IPC_GET_IMETHOD(call)) {
    13311306                case IPC_M_PHONE_HUNGUP:
    13321307                        keep_on_going = false;
     
    13361311                case NET_SOCKET:
    13371312                        socket_data =
    1338                             (tcp_socket_data_ref) malloc(sizeof(*socket_data));
     1313                            (tcp_socket_data_t *) malloc(sizeof(*socket_data));
    13391314                        if (!socket_data) {
    13401315                                res = ENOMEM;
     
    13701345
    13711346                case NET_SOCKET_BIND:
    1372                         res = data_receive((void **) &addr, &addrlen);
     1347                        res = async_data_write_accept((void **) &addr, false,
     1348                            0, 0, 0, &addrlen);
    13731349                        if (res != EOK)
    13741350                                break;
     
    13831359                                    SOCKET_GET_SOCKET_ID(call));
    13841360                                if (socket) {
    1385                                         socket_data = (tcp_socket_data_ref)
     1361                                        socket_data = (tcp_socket_data_t *)
    13861362                                            socket->specific_data;
    13871363                                        assert(socket_data);
     
    14071383
    14081384                case NET_SOCKET_CONNECT:
    1409                         res = data_receive((void **) &addr, &addrlen);
     1385                        res = async_data_write_accept((void **) &addr, false,
     1386                            0, 0, 0, &addrlen);
    14101387                        if (res != EOK)
    14111388                                break;
     
    14581435
    14591436                case NET_SOCKET_SENDTO:
    1460                         res = data_receive((void **) &addr, &addrlen);
     1437                        res = async_data_write_accept((void **) &addr, false,
     1438                            0, 0, 0, &addrlen);
    14611439                        if (res != EOK)
    14621440                                break;
     
    15281506
    15291507        /* Release the application phone */
    1530         ipc_hangup(app_phone);
     1508        async_hangup(app_phone);
    15311509
    15321510        printf("release\n");
     
    15401518int tcp_timeout(void *data)
    15411519{
    1542         tcp_timeout_ref timeout = data;
     1520        tcp_timeout_t *timeout = data;
    15431521        int keep_write_lock = false;
    1544         socket_core_ref socket;
    1545         tcp_socket_data_ref socket_data;
     1522        socket_core_t *socket;
     1523        tcp_socket_data_t *socket_data;
    15461524
    15471525        assert(timeout);
     
    15611539                goto out;
    15621540       
    1563         socket_data = (tcp_socket_data_ref) socket->specific_data;
     1541        socket_data = (tcp_socket_data_t *) socket->specific_data;
    15641542        assert(socket_data);
    15651543        if (socket_data->local_sockets != timeout->local_sockets)
     
    16171595int tcp_release_after_timeout(void *data)
    16181596{
    1619         tcp_timeout_ref timeout = data;
    1620         socket_core_ref socket;
    1621         tcp_socket_data_ref socket_data;
     1597        tcp_timeout_t *timeout = data;
     1598        socket_core_t *socket;
     1599        tcp_socket_data_t *socket_data;
    16221600        fibril_rwlock_t *local_lock;
    16231601
     
    16351613
    16361614        if (socket && (socket->socket_id == timeout->socket_id)) {
    1637                 socket_data = (tcp_socket_data_ref) socket->specific_data;
     1615                socket_data = (tcp_socket_data_t *) socket->specific_data;
    16381616                assert(socket_data);
    16391617                if (socket_data->local_sockets == timeout->local_sockets) {
     
    16561634}
    16571635
    1658 void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref
     1636void tcp_retransmit_packet(socket_core_t *socket, tcp_socket_data_t *
    16591637    socket_data, size_t sequence_number)
    16601638{
    1661         packet_t packet;
    1662         packet_t copy;
     1639        packet_t *packet;
     1640        packet_t *copy;
    16631641        size_t data_length;
    16641642
     
    16831661}
    16841662
    1685 int tcp_listen_message(socket_cores_ref local_sockets, int socket_id,
     1663int tcp_listen_message(socket_cores_t *local_sockets, int socket_id,
    16861664    int backlog)
    16871665{
    1688         socket_core_ref socket;
    1689         tcp_socket_data_ref socket_data;
     1666        socket_core_t *socket;
     1667        tcp_socket_data_t *socket_data;
    16901668
    16911669        assert(local_sockets);
     
    17001678       
    17011679        /* Get the socket specific data */
    1702         socket_data = (tcp_socket_data_ref) socket->specific_data;
     1680        socket_data = (tcp_socket_data_t *) socket->specific_data;
    17031681        assert(socket_data);
    17041682
     
    17091687}
    17101688
    1711 int tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
     1689int tcp_connect_message(socket_cores_t *local_sockets, int socket_id,
    17121690    struct sockaddr *addr, socklen_t addrlen)
    17131691{
    1714         socket_core_ref socket;
     1692        socket_core_t *socket;
    17151693        int rc;
    17161694
     
    17301708                if (socket->port > 0) {
    17311709                        socket_ports_exclude(&tcp_globals.sockets,
    1732                             socket->port);
     1710                            socket->port, free);
    17331711                        socket->port = 0;
    17341712                }
     
    17371715}
    17381716
    1739 int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
     1717int tcp_connect_core(socket_core_t *socket, socket_cores_t *local_sockets,
    17401718    struct sockaddr *addr, socklen_t addrlen)
    17411719{
    1742         tcp_socket_data_ref socket_data;
    1743         packet_t packet;
     1720        tcp_socket_data_t *socket_data;
     1721        packet_t *packet;
    17441722        int rc;
    17451723
     
    17491727
    17501728        /* Get the socket specific data */
    1751         socket_data = (tcp_socket_data_ref) socket->specific_data;
     1729        socket_data = (tcp_socket_data_t *) socket->specific_data;
    17521730        assert(socket_data);
    17531731        assert(socket->specific_data == socket_data);
     
    18051783                        fibril_rwlock_write_unlock(socket_data->local_lock);
    18061784
     1785                        socket_data->state = TCP_SOCKET_SYN_SENT;
     1786
    18071787                        /* Send the packet */
    18081788                        printf("connecting %d\n", packet_get_id(packet));
     
    18281808}
    18291809
    1830 int tcp_queue_prepare_packet(socket_core_ref socket,
    1831     tcp_socket_data_ref socket_data, packet_t packet, size_t data_length)
    1832 {
    1833         tcp_header_ref header;
     1810int tcp_queue_prepare_packet(socket_core_t *socket,
     1811    tcp_socket_data_t *socket_data, packet_t *packet, size_t data_length)
     1812{
     1813        tcp_header_t *header;
    18341814        int rc;
    18351815
     
    18391819
    18401820        /* Get TCP header */
    1841         header = (tcp_header_ref) packet_get_data(packet);
     1821        header = (tcp_header_t *) packet_get_data(packet);
    18421822        if (!header)
    18431823                return NO_DATA;
     
    18591839}
    18601840
    1861 int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
    1862     packet_t packet, size_t data_length)
     1841int tcp_queue_packet(socket_core_t *socket, tcp_socket_data_t *socket_data,
     1842    packet_t *packet, size_t data_length)
    18631843{
    18641844        int rc;
     
    18811861}
    18821862
    1883 packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref
     1863packet_t *tcp_get_packets_to_send(socket_core_t *socket, tcp_socket_data_t *
    18841864    socket_data)
    18851865{
    1886         packet_t packet;
    1887         packet_t copy;
    1888         packet_t sending = NULL;
    1889         packet_t previous = NULL;
     1866        packet_t *packet;
     1867        packet_t *copy;
     1868        packet_t *sending = NULL;
     1869        packet_t *previous = NULL;
    18901870        size_t data_length;
    18911871        int rc;
     
    19411921}
    19421922
    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)
    1945 {
    1946         tcp_header_ref header;
     1923packet_t *tcp_send_prepare_packet(socket_core_t *socket, tcp_socket_data_t *
     1924    socket_data, packet_t *packet, size_t data_length, size_t sequence_number)
     1925{
     1926        tcp_header_t *header;
    19471927        uint32_t checksum;
    19481928        int rc;
     
    19611941
    19621942        /* Get the header */
    1963         header = (tcp_header_ref) packet_get_data(packet);
     1943        header = (tcp_header_t *) packet_get_data(packet);
    19641944        if (!header) {
    19651945                pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     
    20021982}
    20031983
    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)
    2006 {
    2007         packet_t copy;
     1984packet_t *tcp_prepare_copy(socket_core_t *socket, tcp_socket_data_t *
     1985    socket_data, packet_t *packet, size_t data_length, size_t sequence_number)
     1986{
     1987        packet_t *copy;
    20081988
    20091989        assert(socket);
     
    20202000}
    20212001
    2022 void tcp_send_packets(device_id_t device_id, packet_t packet)
    2023 {
    2024         packet_t next;
     2002void tcp_send_packets(device_id_t device_id, packet_t *packet)
     2003{
     2004        packet_t *next;
    20252005
    20262006        while (packet) {
     
    20322012}
    20332013
    2034 void tcp_prepare_operation_header(socket_core_ref socket,
    2035     tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize,
     2014void tcp_prepare_operation_header(socket_core_t *socket,
     2015    tcp_socket_data_t *socket_data, tcp_header_t *header, int synchronize,
    20362016    int finalize)
    20372017{
     
    20502030
    20512031int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
    2052     socket_core_ref socket, tcp_socket_data_ref socket_data,
     2032    socket_core_t *socket, tcp_socket_data_t *socket_data,
    20532033    size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
    20542034    int globals_read_only)
    20552035{
    2056         tcp_timeout_ref operation_timeout;
     2036        tcp_timeout_t *operation_timeout;
    20572037        fid_t fibril;
    20582038
     
    20772057
    20782058        /* Copy the key */
    2079         operation_timeout->key = ((char *) operation_timeout) +
     2059        operation_timeout->key = ((uint8_t *) operation_timeout) +
    20802060            sizeof(*operation_timeout);
    20812061        operation_timeout->key_length = socket->key_length;
     
    20872067        if (!fibril) {
    20882068                free(operation_timeout);
    2089                 return EPARTY;  /* FIXME: use another EC */
    2090         }
     2069                return ENOMEM;
     2070        }
     2071
    20912072//      fibril_mutex_lock(&socket_data->operation.mutex);
    20922073        /* Start the timeout fibril */
     
    20962077}
    20972078
    2098 int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id,
     2079int tcp_recvfrom_message(socket_cores_t *local_sockets, int socket_id,
    20992080    int flags, size_t *addrlen)
    21002081{
    2101         socket_core_ref socket;
    2102         tcp_socket_data_ref socket_data;
     2082        socket_core_t *socket;
     2083        tcp_socket_data_t *socket_data;
    21032084        int packet_id;
    2104         packet_t packet;
     2085        packet_t *packet;
    21052086        size_t length;
    21062087        int rc;
     
    21172098                return NO_DATA;
    21182099
    2119         socket_data = (tcp_socket_data_ref) socket->specific_data;
     2100        socket_data = (tcp_socket_data_t *) socket->specific_data;
    21202101
    21212102        /* Check state */
     
    21542135}
    21552136
    2156 int tcp_send_message(socket_cores_ref local_sockets, int socket_id,
     2137int tcp_send_message(socket_cores_t *local_sockets, int socket_id,
    21572138    int fragments, size_t *data_fragment_size, int flags)
    21582139{
    2159         socket_core_ref socket;
    2160         tcp_socket_data_ref socket_data;
    2161         packet_dimension_ref packet_dimension;
    2162         packet_t packet;
     2140        socket_core_t *socket;
     2141        tcp_socket_data_t *socket_data;
     2142        packet_dimension_t *packet_dimension;
     2143        packet_t *packet;
    21632144        size_t total_length;
    2164         tcp_header_ref header;
     2145        tcp_header_t *header;
    21652146        int index;
    21662147        int result;
     
    21792160                return NO_DATA;
    21802161
    2181         socket_data = (tcp_socket_data_ref) socket->specific_data;
     2162        socket_data = (tcp_socket_data_t *) socket->specific_data;
    21822163
    21832164        /* Check state */
     
    22112192
    22122193                tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
    2213                 rc = tcp_queue_packet(socket, socket_data, packet, 0);
     2194                rc = tcp_queue_packet(socket, socket_data, packet, total_length);
    22142195                if (rc != EOK)
    22152196                        return rc;
     
    22302211
    22312212int
    2232 tcp_close_message(socket_cores_ref local_sockets, int socket_id)
    2233 {
    2234         socket_core_ref socket;
    2235         tcp_socket_data_ref socket_data;
    2236         packet_t packet;
     2213tcp_close_message(socket_cores_t *local_sockets, int socket_id)
     2214{
     2215        socket_core_t *socket;
     2216        tcp_socket_data_t *socket_data;
     2217        packet_t *packet;
    22372218        int rc;
    22382219
     
    22432224
    22442225        /* Get the socket specific data */
    2245         socket_data = (tcp_socket_data_ref) socket->specific_data;
     2226        socket_data = (tcp_socket_data_t *) socket->specific_data;
    22462227        assert(socket_data);
    22472228
     
    22982279}
    22992280
    2300 int tcp_create_notification_packet(packet_t *packet, socket_core_ref socket,
    2301     tcp_socket_data_ref socket_data, int synchronize, int finalize)
    2302 {
    2303         packet_dimension_ref packet_dimension;
    2304         tcp_header_ref header;
     2281int tcp_create_notification_packet(packet_t **packet, socket_core_t *socket,
     2282    tcp_socket_data_t *socket_data, int synchronize, int finalize)
     2283{
     2284        packet_dimension_t *packet_dimension;
     2285        tcp_header_t *header;
    23052286        int rc;
    23062287
     
    23322313}
    23332314
    2334 int tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
     2315int tcp_accept_message(socket_cores_t *local_sockets, int socket_id,
    23352316    int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
    23362317{
    2337         socket_core_ref accepted;
    2338         socket_core_ref socket;
    2339         tcp_socket_data_ref socket_data;
    2340         packet_dimension_ref packet_dimension;
     2318        socket_core_t *accepted;
     2319        socket_core_t *socket;
     2320        tcp_socket_data_t *socket_data;
     2321        packet_dimension_t *packet_dimension;
    23412322        int rc;
    23422323
     
    23512332
    23522333        /* Get the socket specific data */
    2353         socket_data = (tcp_socket_data_ref) socket->specific_data;
     2334        socket_data = (tcp_socket_data_t *) socket->specific_data;
    23542335        assert(socket_data);
    23552336
     
    23692350
    23702351                /* Get the socket specific data */
    2371                 socket_data = (tcp_socket_data_ref) accepted->specific_data;
     2352                socket_data = (tcp_socket_data_t *) accepted->specific_data;
    23722353                assert(socket_data);
    23732354                /* TODO can it be in another state? */
     
    24052386}
    24062387
    2407 void tcp_free_socket_data(socket_core_ref socket)
    2408 {
    2409         tcp_socket_data_ref socket_data;
     2388void tcp_free_socket_data(socket_core_t *socket)
     2389{
     2390        tcp_socket_data_t *socket_data;
    24102391
    24112392        assert(socket);
     
    24142395
    24152396        /* Get the socket specific data */
    2416         socket_data = (tcp_socket_data_ref) socket->specific_data;
     2397        socket_data = (tcp_socket_data_t *) socket->specific_data;
    24172398        assert(socket_data);
    24182399
     
    24472428 * @return              The result parameter.
    24482429 */
    2449 int tcp_release_and_return(packet_t packet, int result)
     2430int tcp_release_and_return(packet_t *packet, int result)
    24502431{
    24512432        pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
     
    24532434}
    24542435
    2455 /** Default thread for new connections.
     2436/** Process IPC messages from the IP module
    24562437 *
    2457  * @param[in] iid       The initial message identifier.
    2458  * @param[in] icall     The initial message call structure.
     2438 * @param[in]     iid   Message identifier.
     2439 * @param[in,out] icall Message parameters.
    24592440 *
    24602441 */
    2461 static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall)
    2462 {
    2463         /*
    2464          * Accept the connection
    2465          *  - Answer the first IPC_M_CONNECT_ME_TO call.
    2466          */
    2467         ipc_answer_0(iid, EOK);
    2468 
     2442static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall)
     2443{
     2444        packet_t *packet;
     2445        int rc;
     2446       
    24692447        while (true) {
    2470                 ipc_call_t answer;
    2471                 int answer_count;
    2472 
    2473                 /* Clear the answer structure */
    2474                 refresh_answer(&answer, &answer_count);
    2475 
    2476                 /* Fetch the next message */
    2477                 ipc_call_t call;
    2478                 ipc_callid_t callid = async_get_call(&call);
    2479 
    2480                 /* Process the message */
    2481                 int res = tl_module_message_standalone(callid, &call, &answer,
    2482                     &answer_count);
    2483 
    2484                 /*
    2485                  * End if told to either by the message or the processing
    2486                  * result.
    2487                  */
    2488                 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
    2489                     (res == EHANGUP))
    2490                         return;
    2491 
    2492                 /*
    2493                  * Answer the message
    2494                  */
    2495                 answer_call(callid, res, &answer, answer_count);
    2496         }
    2497 }
    2498 
    2499 /** Starts the module.
     2448                switch (IPC_GET_IMETHOD(*icall)) {
     2449                case NET_TL_RECEIVED:
     2450                        rc = packet_translate_remote(tcp_globals.net_phone, &packet,
     2451                            IPC_GET_PACKET(*icall));
     2452                        if (rc == EOK)
     2453                                rc = tcp_received_msg(IPC_GET_DEVICE(*icall), packet,
     2454                                    SERVICE_TCP, IPC_GET_ERROR(*icall));
     2455                       
     2456                        async_answer_0(iid, (sysarg_t) rc);
     2457                        break;
     2458                default:
     2459                        async_answer_0(iid, (sysarg_t) ENOTSUP);
     2460                }
     2461               
     2462                iid = async_get_call(icall);
     2463        }
     2464}
     2465
     2466/** Initialize the TCP module.
    25002467 *
    2501  * @returns             EOK on success.
    2502  * @returns             Other error codes as defined for each specific module
    2503  *                      start function.
     2468 * @param[in] net_phone Network module phone.
     2469 *
     2470 * @return EOK on success.
     2471 * @return ENOMEM if there is not enough memory left.
     2472 *
    25042473 */
    2505 int
    2506 main(int argc, char *argv[])
    2507 {
    2508         int rc;
    2509 
    2510         rc = tl_module_start_standalone(tl_client_connection);
     2474int tl_initialize(int net_phone)
     2475{
     2476        fibril_rwlock_initialize(&tcp_globals.lock);
     2477        fibril_rwlock_write_lock(&tcp_globals.lock);
     2478       
     2479        tcp_globals.net_phone = net_phone;
     2480       
     2481        tcp_globals.icmp_phone = icmp_connect_module(ICMP_CONNECT_TIMEOUT);
     2482        tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
     2483            SERVICE_TCP, tcp_receiver);
     2484        if (tcp_globals.ip_phone < 0) {
     2485                fibril_rwlock_write_unlock(&tcp_globals.lock);
     2486                return tcp_globals.ip_phone;
     2487        }
     2488       
     2489        int rc = socket_ports_initialize(&tcp_globals.sockets);
     2490        if (rc != EOK)
     2491                goto out;
     2492
     2493        rc = packet_dimensions_initialize(&tcp_globals.dimensions);
     2494        if (rc != EOK) {
     2495                socket_ports_destroy(&tcp_globals.sockets, free);
     2496                goto out;
     2497        }
     2498
     2499        tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
     2500
     2501out:
     2502        fibril_rwlock_write_unlock(&tcp_globals.lock);
    25112503        return rc;
     2504}
     2505
     2506int main(int argc, char *argv[])
     2507{
     2508        return tl_module_start(SERVICE_TCP);
    25122509}
    25132510
Note: See TracChangeset for help on using the changeset viewer.