Ignore:
File:
1 edited

Legend:

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

    r014dd57b r86f6121  
    3636 */
    3737
     38#include "tcp.h"
     39#include "tcp_header.h"
     40#include "tcp_module.h"
     41
    3842#include <assert.h>
    3943#include <async.h>
     
    6872#include <socket_core.h>
    6973#include <tl_common.h>
    70 #include <tl_remote.h>
    71 #include <tl_skel.h>
    72 
    73 #include "tcp.h"
    74 #include "tcp_header.h"
     74#include <tl_local.h>
     75#include <tl_interface.h>
    7576
    7677/** TCP module name. */
    77 #define NAME  "tcp"
     78#define NAME    "TCP protocol"
    7879
    7980/** The TCP window default value. */
     
    153154
    154155        /** Port map key. */
    155         uint8_t *key;
     156        char *key;
    156157
    157158        /** Port map key length. */
     
    219220/** TCP global data. */
    220221tcp_globals_t tcp_globals;
     222
     223/** Initializes the TCP module.
     224 *
     225 * @param[in] client_connection The client connection processing function. The
     226 *                      module skeleton propagates its own one.
     227 * @return              EOK on success.
     228 * @return              ENOMEM if there is not enough memory left.
     229 */
     230int tcp_initialize(async_client_conn_t client_connection)
     231{
     232        int rc;
     233
     234        assert(client_connection);
     235
     236        fibril_rwlock_initialize(&tcp_globals.lock);
     237        fibril_rwlock_write_lock(&tcp_globals.lock);
     238
     239        tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
     240            ICMP_CONNECT_TIMEOUT);
     241        tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
     242            SERVICE_TCP, client_connection);
     243        if (tcp_globals.ip_phone < 0) {
     244                fibril_rwlock_write_unlock(&tcp_globals.lock);
     245                return tcp_globals.ip_phone;
     246        }
     247       
     248        rc = socket_ports_initialize(&tcp_globals.sockets);
     249        if (rc != EOK)
     250                goto out;
     251
     252        rc = packet_dimensions_initialize(&tcp_globals.dimensions);
     253        if (rc != EOK) {
     254                socket_ports_destroy(&tcp_globals.sockets);
     255                goto out;
     256        }
     257
     258        tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
     259
     260out:
     261        fibril_rwlock_write_unlock(&tcp_globals.lock);
     262        return rc;
     263}
    221264
    222265int tcp_received_msg(device_id_t device_id, packet_t *packet,
     
    315358        /* Find the destination socket */
    316359        socket = socket_port_find(&tcp_globals.sockets,
    317             ntohs(header->destination_port), (uint8_t *) src, addrlen);
     360            ntohs(header->destination_port), (const char *) src, addrlen);
    318361        if (!socket) {
    319362                /* Find the listening destination socket */
    320363                socket = socket_port_find(&tcp_globals.sockets,
    321                     ntohs(header->destination_port),
    322                     (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
     364                    ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,
     365                    0);
    323366        }
    324367
     
    955998        /* Find the destination socket */
    956999        listening_socket = socket_port_find(&tcp_globals.sockets,
    957             listening_port, (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
     1000            listening_port, SOCKET_MAP_KEY_LISTENING, 0);
    9581001        if (!listening_socket ||
    9591002            (listening_socket->socket_id != listening_socket_id)) {
     
    9791022
    9801023        rc = socket_port_add(&tcp_globals.sockets, listening_port, socket,
    981             (uint8_t *) socket_data->addr, socket_data->addrlen);
     1024            (const char *) socket_data->addr, socket_data->addrlen);
    9821025        assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
    983             (uint8_t *) socket_data->addr, socket_data->addrlen));
     1026            (const char *) socket_data->addr, socket_data->addrlen));
    9841027
    9851028//      rc = socket_bind_free_port(&tcp_globals.sockets, socket,
     
    12171260 * @see IS_NET_TCP_MESSAGE()
    12181261 */
    1219 int tl_module_message(ipc_callid_t callid, ipc_call_t *call,
    1220     ipc_call_t *answer, size_t *answer_count)
    1221 {
     1262int
     1263tcp_message_standalone(ipc_callid_t callid, ipc_call_t *call,
     1264    ipc_call_t *answer, int *answer_count)
     1265{
     1266        packet_t *packet;
     1267        int rc;
     1268
    12221269        assert(call);
    12231270        assert(answer);
     
    12261273        *answer_count = 0;
    12271274        switch (IPC_GET_IMETHOD(*call)) {
     1275        case NET_TL_RECEIVED:
     1276//              fibril_rwlock_read_lock(&tcp_globals.lock);
     1277                rc = packet_translate_remote(tcp_globals.net_phone, &packet,
     1278                    IPC_GET_PACKET(call));
     1279                if (rc != EOK) {
     1280//                      fibril_rwlock_read_unlock(&tcp_globals.lock);
     1281                        return rc;
     1282                }
     1283                rc = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP,
     1284                    IPC_GET_ERROR(call));
     1285//              fibril_rwlock_read_unlock(&tcp_globals.lock);
     1286                return rc;
    12281287        case IPC_M_CONNECT_TO_ME:
    12291288                return tcp_process_client_messages(callid, *call);
     
    12641323        bool keep_on_going = true;
    12651324        socket_cores_t local_sockets;
    1266         int app_phone = IPC_GET_PHONE(call);
     1325        int app_phone = IPC_GET_PHONE(&call);
    12671326        struct sockaddr *addr;
    12681327        int socket_id;
     
    12711330        fibril_rwlock_t lock;
    12721331        ipc_call_t answer;
    1273         size_t answer_count;
     1332        int answer_count;
    12741333        tcp_socket_data_t *socket_data;
    12751334        socket_core_t *socket;
     
    20502109
    20512110        /* Copy the key */
    2052         operation_timeout->key = ((uint8_t *) operation_timeout) +
     2111        operation_timeout->key = ((char *) operation_timeout) +
    20532112            sizeof(*operation_timeout);
    20542113        operation_timeout->key_length = socket->key_length;
     
    24272486}
    24282487
    2429 /** Process IPC messages from the IP module
     2488/** Default thread for new connections.
    24302489 *
    2431  * @param[in]     iid   Message identifier.
    2432  * @param[in,out] icall Message parameters.
     2490 * @param[in] iid       The initial message identifier.
     2491 * @param[in] icall     The initial message call structure.
    24332492 *
    24342493 */
    2435 static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall)
    2436 {
    2437         packet_t *packet;
     2494static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall)
     2495{
     2496        /*
     2497         * Accept the connection
     2498         *  - Answer the first IPC_M_CONNECT_ME_TO call.
     2499         */
     2500        ipc_answer_0(iid, EOK);
     2501
     2502        while (true) {
     2503                ipc_call_t answer;
     2504                int answer_count;
     2505
     2506                /* Clear the answer structure */
     2507                refresh_answer(&answer, &answer_count);
     2508
     2509                /* Fetch the next message */
     2510                ipc_call_t call;
     2511                ipc_callid_t callid = async_get_call(&call);
     2512
     2513                /* Process the message */
     2514                int res = tl_module_message_standalone(callid, &call, &answer,
     2515                    &answer_count);
     2516
     2517                /*
     2518                 * End if told to either by the message or the processing
     2519                 * result.
     2520                 */
     2521                if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||
     2522                    (res == EHANGUP))
     2523                        return;
     2524
     2525                /*
     2526                 * Answer the message
     2527                 */
     2528                answer_call(callid, res, &answer, answer_count);
     2529        }
     2530}
     2531
     2532/** Starts the module.
     2533 *
     2534 * @return              EOK on success.
     2535 * @return              Other error codes as defined for each specific module
     2536 *                      start function.
     2537 */
     2538int
     2539main(int argc, char *argv[])
     2540{
    24382541        int rc;
    2439        
    2440         while (true) {
    2441                 switch (IPC_GET_IMETHOD(*icall)) {
    2442                 case NET_TL_RECEIVED:
    2443                         rc = packet_translate_remote(tcp_globals.net_phone, &packet,
    2444                             IPC_GET_PACKET(*icall));
    2445                         if (rc == EOK)
    2446                                 rc = tcp_received_msg(IPC_GET_DEVICE(*icall), packet,
    2447                                     SERVICE_TCP, IPC_GET_ERROR(*icall));
    2448                        
    2449                         ipc_answer_0(iid, (sysarg_t) rc);
    2450                         break;
    2451                 default:
    2452                         ipc_answer_0(iid, (sysarg_t) ENOTSUP);
    2453                 }
    2454                
    2455                 iid = async_get_call(icall);
    2456         }
    2457 }
    2458 
    2459 /** Initialize the TCP module.
    2460  *
    2461  * @param[in] net_phone Network module phone.
    2462  *
    2463  * @return EOK on success.
    2464  * @return ENOMEM if there is not enough memory left.
    2465  *
    2466  */
    2467 int tl_initialize(int net_phone)
    2468 {
    2469         fibril_rwlock_initialize(&tcp_globals.lock);
    2470         fibril_rwlock_write_lock(&tcp_globals.lock);
    2471        
    2472         tcp_globals.net_phone = net_phone;
    2473        
    2474         tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
    2475             ICMP_CONNECT_TIMEOUT);
    2476         tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
    2477             SERVICE_TCP, tcp_receiver);
    2478         if (tcp_globals.ip_phone < 0) {
    2479                 fibril_rwlock_write_unlock(&tcp_globals.lock);
    2480                 return tcp_globals.ip_phone;
    2481         }
    2482        
    2483         int rc = socket_ports_initialize(&tcp_globals.sockets);
    2484         if (rc != EOK)
    2485                 goto out;
    2486 
    2487         rc = packet_dimensions_initialize(&tcp_globals.dimensions);
    2488         if (rc != EOK) {
    2489                 socket_ports_destroy(&tcp_globals.sockets);
    2490                 goto out;
    2491         }
    2492 
    2493         tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
    2494 
    2495 out:
    2496         fibril_rwlock_write_unlock(&tcp_globals.lock);
     2542
     2543        rc = tl_module_start_standalone(tl_client_connection);
    24972544        return rc;
    2498 }
    2499 
    2500 int main(int argc, char *argv[])
    2501 {
    2502         return tl_module_start(SERVICE_TCP);
    25032545}
    25042546
Note: See TracChangeset for help on using the changeset viewer.