Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/net/tl/socket_core.c

    r61bfc370 r6b82009  
    3838#include <packet_client.h>
    3939#include <packet_remote.h>
    40 
    4140#include <net/socket_codes.h>
    4241#include <net/in.h>
     
    4443#include <net/packet.h>
    4544#include <net/modules.h>
    46 
    4745#include <stdint.h>
    4846#include <stdlib.h>
    4947#include <errno.h>
    50 
    5148#include <adt/dynamic_fifo.h>
    5249#include <adt/int_map.h>
     
    5653 * switching to the sequence.
    5754 */
    58 #define SOCKET_ID_TRIES 100
     55#define SOCKET_ID_TRIES  100
    5956
    6057/** Bound port sockets.*/
     
    7269INT_MAP_IMPLEMENT(socket_ports, socket_port_t);
    7370
    74 /** Destroys the socket.
     71/** Destroy the socket.
    7572 *
    7673 * If the socket is bound, the port is released.
    77  * Releases all buffered packets, calls the release function and removes the
     74 * Release all buffered packets, call the release function and remove the
    7875 * socket from the local sockets.
    7976 *
    80  * @param[in] packet_phone The packet server phone to release buffered packets.
    81  * @param[in] socket    The socket to be destroyed.
    82  * @param[in,out] local_sockets The local sockets to be updated.
    83  * @param[in,out] global_sockets The global sockets to be updated.
    84  * @param[in] socket_release The client release callback function.
    85  */
    86 static void
    87 socket_destroy_core(int packet_phone, socket_core_t *socket,
     77 * @param[in]     sess           Packet server session.
     78 * @param[in]     socket         Socket to be destroyed.
     79 * @param[in,out] local_sockets  Local sockets to be updated.
     80 * @param[in,out] global_sockets Global sockets to be updated.
     81 * @param[in]     socket_release Client release callback function.
     82 *
     83 */
     84static void socket_destroy_core(async_sess_t *sess, socket_core_t *socket,
    8885    socket_cores_t *local_sockets, socket_ports_t *global_sockets,
    8986    void (* socket_release)(socket_core_t *socket))
    9087{
     88        /* If bound */
     89        if (socket->port) {
     90                /* Release the port */
     91                socket_port_release(global_sockets, socket);
     92        }
     93       
     94        /* Release all received packets */
    9195        int packet_id;
    92 
    93         // if bound
    94         if (socket->port) {
    95                 // release the port
    96                 socket_port_release(global_sockets, socket);
    97         }
    98        
    99         // release all received packets
    10096        while ((packet_id = dyn_fifo_pop(&socket->received)) >= 0)
    101                 pq_release_remote(packet_phone, packet_id);
    102 
     97                pq_release_remote(sess, packet_id);
     98       
    10399        dyn_fifo_destroy(&socket->received);
    104100        dyn_fifo_destroy(&socket->accepted);
    105 
     101       
    106102        if (socket_release)
    107103                socket_release(socket);
    108 
    109         socket_cores_exclude(local_sockets, socket->socket_id);
    110 }
    111 
    112 /** Destroys local sockets.
    113  *
    114  * Releases all buffered packets and calls the release function for each of the
     104       
     105        socket_cores_exclude(local_sockets, socket->socket_id, free);
     106}
     107
     108/** Destroy local sockets.
     109 *
     110 * Release all buffered packets and call the release function for each of the
    115111 * sockets.
    116112 *
    117  * @param[in] packet_phone The packet server phone to release buffered packets.
    118  * @param[in] local_sockets The local sockets to be destroyed.
    119  * @param[in,out] global_sockets The global sockets to be updated.
    120  * @param[in] socket_release The client release callback function.
    121  */
    122 void
    123 socket_cores_release(int packet_phone, socket_cores_t *local_sockets,
     113 * @param[in]     sess           Packet server session.
     114 * @param[in]     local_sockets  Local sockets to be destroyed.
     115 * @param[in,out] global_sockets Global sockets to be updated.
     116 * @param[in]     socket_release Client release callback function.
     117 *
     118 */
     119void socket_cores_release(async_sess_t *sess, socket_cores_t *local_sockets,
    124120    socket_ports_t *global_sockets,
    125121    void (* socket_release)(socket_core_t *socket))
    126122{
    127         int index;
    128 
    129123        if (!socket_cores_is_valid(local_sockets))
    130124                return;
    131 
     125       
    132126        local_sockets->magic = 0;
    133 
     127       
     128        int index;
    134129        for (index = 0; index < local_sockets->next; ++index) {
    135130                if (socket_cores_item_is_valid(&local_sockets->items[index])) {
    136131                        local_sockets->items[index].magic = 0;
    137 
     132                       
    138133                        if (local_sockets->items[index].value) {
    139                                 socket_destroy_core(packet_phone,
     134                                socket_destroy_core(sess,
    140135                                    local_sockets->items[index].value,
    141136                                    local_sockets, global_sockets,
     
    146141                }
    147142        }
    148 
     143       
    149144        free(local_sockets->items);
    150145}
     
    166161        int rc;
    167162
    168         // create a wrapper
     163        /* Create a wrapper */
    169164        socket_ref = malloc(sizeof(*socket_ref));
    170165        if (!socket_ref)
     
    172167
    173168        *socket_ref = socket;
    174         // add the wrapper
     169        /* Add the wrapper */
    175170        rc = socket_port_map_add(&socket_port->map, key, key_length,
    176171            socket_ref);
     
    206201        int rc;
    207202
    208         // create a wrapper
     203        /* Create a wrapper */
    209204        socket_port = malloc(sizeof(*socket_port));
    210205        if (!socket_port)
     
    221216                goto fail;
    222217       
    223         // register the incomming port
     218        /* Register the incoming port */
    224219        rc = socket_ports_add(global_sockets, port, socket_port);
    225220        if (rc < 0)
     
    230225
    231226fail:
    232         socket_port_map_destroy(&socket_port->map);
     227        socket_port_map_destroy(&socket_port->map, free);
    233228        free(socket_port);
    234229        return rc;
     
    277272               
    278273                address_in = (struct sockaddr_in *) addr;
    279                 // find the socket
     274                /* Find the socket */
    280275                socket = socket_cores_find(local_sockets, socket_id);
    281276                if (!socket)
    282277                        return ENOTSOCK;
    283278               
    284                 // bind a free port?
     279                /* Bind a free port? */
    285280                if (address_in->sin_port <= 0)
    286281                        return socket_bind_free_port(global_sockets, socket,
    287282                             free_ports_start, free_ports_end, last_used_port);
    288283               
    289                 // try to find the port
     284                /* Try to find the port */
    290285                socket_port = socket_ports_find(global_sockets,
    291286                    ntohs(address_in->sin_port));
    292287                if (socket_port) {
    293                         // already used
     288                        /* Already used */
    294289                        return EADDRINUSE;
    295290                }
    296291               
    297                 // if bound
     292                /* If bound */
    298293                if (socket->port) {
    299                         // release the port
     294                        /* Release the port */
    300295                        socket_port_release(global_sockets, socket);
    301296                }
     
    333328        int index;
    334329
    335         // from the last used one
     330        /* From the last used one */
    336331        index = last_used_port;
    337332       
     
    339334                ++index;
    340335               
    341                 // til the range end
     336                /* Till the range end */
    342337                if (index >= free_ports_end) {
    343                         // start from the range beginning
     338                        /* Start from the range beginning */
    344339                        index = free_ports_start - 1;
    345340                        do {
    346341                                ++index;
    347                                 // til the last used one
     342                                /* Till the last used one */
    348343                                if (index >= last_used_port) {
    349                                         // none found
     344                                        /* None found */
    350345                                        return ENOTCONN;
    351346                                }
    352347                        } while (socket_ports_find(global_sockets, index));
    353348                       
    354                         // found, break immediately
     349                        /* Found, break immediately */
    355350                        break;
    356351                }
     
    376371
    377372        count = 0;
    378 //      socket_id = socket_globals.last_id;
     373#if 0
     374        socket_id = socket_globals.last_id;
     375#endif
    379376        do {
    380377                if (count < SOCKET_ID_TRIES) {
     
    384381                        socket_id = 1;
    385382                        ++count;
    386                 // only this branch for last_id
     383                /* Only this branch for last_id */
    387384                } else {
    388385                        if (socket_id < INT_MAX) {
    389386                                ++ socket_id;
    390 /*                      } else if(socket_globals.last_id) {
    391 *                               socket_globals.last_id = 0;
    392 *                               socket_id = 1;
    393 */                      } else {
     387#if 0
     388                        } else if(socket_globals.last_id) {
     389                                socket_globals.last_id = 0;
     390                                socket_id = 1;
     391#endif
     392                        } else {
    394393                                return ELIMIT;
    395394                        }
     
    402401}
    403402
    404 /** Creates a new socket.
    405  *
    406  * @param[in,out] local_sockets The local sockets to be updated.
    407  * @param[in] app_phone The application phone.
    408  * @param[in] specific_data The socket specific data.
    409  * @param[in,out] socket_id The new socket identifier. A new identifier is
    410  *                      chosen if set to zero or negative. A negative identifier
    411  *                      is chosen if set to negative.
    412  * @return              EOK on success.
    413  * @return              EINVAL if the socket_id parameter is NULL.
    414  * @return              ENOMEM if there is not enough memory left.
    415  */
    416 int
    417 socket_create(socket_cores_t *local_sockets, int app_phone,
     403/** Create a new socket.
     404 *
     405 * @param[in,out] local_sockets Local sockets to be updated.
     406 * @param[in]     sess          Application session.
     407 * @param[in]     specific_data Socket specific data.
     408 * @param[in,out] socket_id     New socket identifier. A new identifier
     409 *                              is chosen if set to zero or negative.
     410 *                              A negative identifier is chosen if set
     411 *                              to negative.
     412 *
     413 * @return EOK on success.
     414 * @return EINVAL if the socket_id parameter is NULL.
     415 * @return ENOMEM if there is not enough memory left.
     416 *
     417 */
     418int socket_create(socket_cores_t *local_sockets, async_sess_t* sess,
    418419    void *specific_data, int *socket_id)
    419420{
     
    425426                return EINVAL;
    426427       
    427         // store the socket
     428        /* Store the socket */
    428429        if (*socket_id <= 0) {
    429430                positive = (*socket_id == 0);
     
    441442                return ENOMEM;
    442443       
    443         // initialize
    444         socket->phone = app_phone;
     444        /* Initialize */
     445        socket->sess = sess;
    445446        socket->port = -1;
    446447        socket->key = NULL;
     
    471472}
    472473
    473 /** Destroys the socket.
     474/** Destroy the socket.
    474475 *
    475476 * If the socket is bound, the port is released.
    476  * Releases all buffered packets, calls the release function and removes the
     477 * Release all buffered packets, call the release function and remove the
    477478 * socket from the local sockets.
    478479 *
    479  * @param[in] packet_phone The packet server phone to release buffered packets.
    480  * @param[in] socket_id The socket identifier.
    481  * @param[in,out] local_sockets The local sockets to be updated.
    482  * @param[in,out] global_sockets The global sockets to be updated.
    483  * @param[in] socket_release The client release callback function.
    484  * @return              EOK on success.
    485  * @return              ENOTSOCK if the socket is not found.
     480 * @param[in]     sess           Packet server session.
     481 * @param[in]     socket_id      Socket identifier.
     482 * @param[in,out] local_sockets  Local sockets to be updated.
     483 * @param[in,out] global_sockets Global sockets to be updated.
     484 * @param[in]     socket_release Client release callback function.
     485 *
     486 * @return EOK on success.
     487 * @return ENOTSOCK if the socket is not found.
     488 *
    486489 */
    487490int
    488 socket_destroy(int packet_phone, int socket_id, socket_cores_t *local_sockets,
     491socket_destroy(async_sess_t *sess, int socket_id, socket_cores_t *local_sockets,
    489492    socket_ports_t *global_sockets,
    490493    void (*socket_release)(socket_core_t *socket))
    491494{
    492         socket_core_t *socket;
    493         int accepted_id;
    494 
    495         // find the socket
    496         socket = socket_cores_find(local_sockets, socket_id);
     495        /* Find the socket */
     496        socket_core_t *socket = socket_cores_find(local_sockets, socket_id);
    497497        if (!socket)
    498498                return ENOTSOCK;
    499499       
    500         // destroy all accepted sockets
     500        /* Destroy all accepted sockets */
     501        int accepted_id;
    501502        while ((accepted_id = dyn_fifo_pop(&socket->accepted)) >= 0)
    502                 socket_destroy(packet_phone, accepted_id, local_sockets,
     503                socket_destroy(sess, accepted_id, local_sockets,
    503504                    global_sockets, socket_release);
    504505       
    505         socket_destroy_core(packet_phone, socket, local_sockets, global_sockets,
     506        socket_destroy_core(sess, socket, local_sockets, global_sockets,
    506507            socket_release);
    507508       
     
    535536        next_packet = pq_next(packet);
    536537        if (!next_packet) {
    537                 // write all if only one fragment
     538                /* Write all if only one fragment */
    538539                rc = data_reply(packet_get_data(packet),
    539540                    packet_get_data_length(packet));
    540541                if (rc != EOK)
    541542                        return rc;
    542                 // store the total length
     543                /* Store the total length */
    543544                *length = packet_get_data_length(packet);
    544545        } else {
    545                 // count the packet fragments
     546                /* Count the packet fragments */
    546547                fragments = 1;
    547548                next_packet = pq_next(packet);
     
    549550                        ++fragments;
    550551               
    551                 // compute and store the fragment lengths
     552                /* Compute and store the fragment lengths */
    552553                lengths = (size_t *) malloc(sizeof(size_t) * fragments +
    553554                    sizeof(size_t));
     
    565566                }
    566567               
    567                 // write the fragment lengths
     568                /* Write the fragment lengths */
    568569                rc = data_reply(lengths, sizeof(int) * (fragments + 1));
    569570                if (rc != EOK) {
     
    573574                next_packet = packet;
    574575               
    575                 // write the fragments
     576                /* Write the fragments */
    576577                for (index = 0; index < fragments; ++index) {
    577578                        rc = data_reply(packet_get_data(next_packet),
     
    584585                }
    585586               
    586                 // store the total length
     587                /* Store the total length */
    587588                *length = lengths[fragments];
    588589                free(lengths);
     
    636637                return;
    637638       
    638         // find ports
     639        /* Find ports */
    639640        socket_port = socket_ports_find(global_sockets, socket->port);
    640641        if (socket_port) {
    641                 // find the socket
     642                /* Find the socket */
    642643                socket_ref = socket_port_map_find(&socket_port->map,
    643644                    socket->key, socket->key_length);
     
    646647                        --socket_port->count;
    647648                       
    648                         // release if empty
     649                        /* Release if empty */
    649650                        if (socket_port->count <= 0) {
    650                                 // destroy the map
    651                                 socket_port_map_destroy(&socket_port->map);
    652                                 // release the port
     651                                /* Destroy the map */
     652                                socket_port_map_destroy(&socket_port->map, free);
     653                                /* Release the port */
    653654                                socket_ports_exclude(global_sockets,
    654                                     socket->port);
     655                                    socket->port, free);
    655656                        } else {
    656                                 // remove
     657                                /* Remove */
    657658                                socket_port_map_exclude(&socket_port->map,
    658                                     socket->key, socket->key_length);
     659                                    socket->key, socket->key_length, free);
    659660                        }
    660661                }
     
    685686        int rc;
    686687
    687         // find ports
     688        /* Find ports */
    688689        socket_port = socket_ports_find(global_sockets, port);
    689690        if (!socket_port)
    690691                return ENOENT;
    691692       
    692         // add the socket
     693        /* Add the socket */
    693694        rc = socket_port_add_core(socket_port, socket, key, key_length);
    694695        if (rc != EOK)
Note: See TracChangeset for help on using the changeset viewer.