Ignore:
File:
1 edited

Legend:

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

    r6b82009 r61bfc370  
    3838#include <packet_client.h>
    3939#include <packet_remote.h>
     40
    4041#include <net/socket_codes.h>
    4142#include <net/in.h>
     
    4344#include <net/packet.h>
    4445#include <net/modules.h>
     46
    4547#include <stdint.h>
    4648#include <stdlib.h>
    4749#include <errno.h>
     50
    4851#include <adt/dynamic_fifo.h>
    4952#include <adt/int_map.h>
     
    5356 * switching to the sequence.
    5457 */
    55 #define SOCKET_ID_TRIES  100
     58#define SOCKET_ID_TRIES 100
    5659
    5760/** Bound port sockets.*/
     
    6972INT_MAP_IMPLEMENT(socket_ports, socket_port_t);
    7073
    71 /** Destroy the socket.
     74/** Destroys the socket.
    7275 *
    7376 * If the socket is bound, the port is released.
    74  * Release all buffered packets, call the release function and remove the
     77 * Releases all buffered packets, calls the release function and removes the
    7578 * socket from the local sockets.
    7679 *
    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  */
    84 static void socket_destroy_core(async_sess_t *sess, socket_core_t *socket,
     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 */
     86static void
     87socket_destroy_core(int packet_phone, socket_core_t *socket,
    8588    socket_cores_t *local_sockets, socket_ports_t *global_sockets,
    8689    void (* socket_release)(socket_core_t *socket))
    8790{
    88         /* If bound */
     91        int packet_id;
     92
     93        // if bound
    8994        if (socket->port) {
    90                 /* Release the port */
     95                // release the port
    9196                socket_port_release(global_sockets, socket);
    9297        }
    9398       
    94         /* Release all received packets */
    95         int packet_id;
     99        // release all received packets
    96100        while ((packet_id = dyn_fifo_pop(&socket->received)) >= 0)
    97                 pq_release_remote(sess, packet_id);
    98        
     101                pq_release_remote(packet_phone, packet_id);
     102
    99103        dyn_fifo_destroy(&socket->received);
    100104        dyn_fifo_destroy(&socket->accepted);
    101        
     105
    102106        if (socket_release)
    103107                socket_release(socket);
    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
     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
    111115 * sockets.
    112116 *
    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  */
    119 void socket_cores_release(async_sess_t *sess, socket_cores_t *local_sockets,
     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 */
     122void
     123socket_cores_release(int packet_phone, socket_cores_t *local_sockets,
    120124    socket_ports_t *global_sockets,
    121125    void (* socket_release)(socket_core_t *socket))
    122126{
     127        int index;
     128
    123129        if (!socket_cores_is_valid(local_sockets))
    124130                return;
    125        
     131
    126132        local_sockets->magic = 0;
    127        
    128         int index;
     133
    129134        for (index = 0; index < local_sockets->next; ++index) {
    130135                if (socket_cores_item_is_valid(&local_sockets->items[index])) {
    131136                        local_sockets->items[index].magic = 0;
    132                        
     137
    133138                        if (local_sockets->items[index].value) {
    134                                 socket_destroy_core(sess,
     139                                socket_destroy_core(packet_phone,
    135140                                    local_sockets->items[index].value,
    136141                                    local_sockets, global_sockets,
     
    141146                }
    142147        }
    143        
     148
    144149        free(local_sockets->items);
    145150}
     
    161166        int rc;
    162167
    163         /* Create a wrapper */
     168        // create a wrapper
    164169        socket_ref = malloc(sizeof(*socket_ref));
    165170        if (!socket_ref)
     
    167172
    168173        *socket_ref = socket;
    169         /* Add the wrapper */
     174        // add the wrapper
    170175        rc = socket_port_map_add(&socket_port->map, key, key_length,
    171176            socket_ref);
     
    201206        int rc;
    202207
    203         /* Create a wrapper */
     208        // create a wrapper
    204209        socket_port = malloc(sizeof(*socket_port));
    205210        if (!socket_port)
     
    216221                goto fail;
    217222       
    218         /* Register the incoming port */
     223        // register the incomming port
    219224        rc = socket_ports_add(global_sockets, port, socket_port);
    220225        if (rc < 0)
     
    225230
    226231fail:
    227         socket_port_map_destroy(&socket_port->map, free);
     232        socket_port_map_destroy(&socket_port->map);
    228233        free(socket_port);
    229234        return rc;
     
    272277               
    273278                address_in = (struct sockaddr_in *) addr;
    274                 /* Find the socket */
     279                // find the socket
    275280                socket = socket_cores_find(local_sockets, socket_id);
    276281                if (!socket)
    277282                        return ENOTSOCK;
    278283               
    279                 /* Bind a free port? */
     284                // bind a free port?
    280285                if (address_in->sin_port <= 0)
    281286                        return socket_bind_free_port(global_sockets, socket,
    282287                             free_ports_start, free_ports_end, last_used_port);
    283288               
    284                 /* Try to find the port */
     289                // try to find the port
    285290                socket_port = socket_ports_find(global_sockets,
    286291                    ntohs(address_in->sin_port));
    287292                if (socket_port) {
    288                         /* Already used */
     293                        // already used
    289294                        return EADDRINUSE;
    290295                }
    291296               
    292                 /* If bound */
     297                // if bound
    293298                if (socket->port) {
    294                         /* Release the port */
     299                        // release the port
    295300                        socket_port_release(global_sockets, socket);
    296301                }
     
    328333        int index;
    329334
    330         /* From the last used one */
     335        // from the last used one
    331336        index = last_used_port;
    332337       
     
    334339                ++index;
    335340               
    336                 /* Till the range end */
     341                // til the range end
    337342                if (index >= free_ports_end) {
    338                         /* Start from the range beginning */
     343                        // start from the range beginning
    339344                        index = free_ports_start - 1;
    340345                        do {
    341346                                ++index;
    342                                 /* Till the last used one */
     347                                // til the last used one
    343348                                if (index >= last_used_port) {
    344                                         /* None found */
     349                                        // none found
    345350                                        return ENOTCONN;
    346351                                }
    347352                        } while (socket_ports_find(global_sockets, index));
    348353                       
    349                         /* Found, break immediately */
     354                        // found, break immediately
    350355                        break;
    351356                }
     
    371376
    372377        count = 0;
    373 #if 0
    374         socket_id = socket_globals.last_id;
    375 #endif
     378//      socket_id = socket_globals.last_id;
    376379        do {
    377380                if (count < SOCKET_ID_TRIES) {
     
    381384                        socket_id = 1;
    382385                        ++count;
    383                 /* Only this branch for last_id */
     386                // only this branch for last_id
    384387                } else {
    385388                        if (socket_id < INT_MAX) {
    386389                                ++ socket_id;
    387 #if 0
    388                         } else if(socket_globals.last_id) {
    389                                 socket_globals.last_id = 0;
    390                                 socket_id = 1;
    391 #endif
    392                         } else {
     390/*                      } else if(socket_globals.last_id) {
     391*                               socket_globals.last_id = 0;
     392*                               socket_id = 1;
     393*/                      } else {
    393394                                return ELIMIT;
    394395                        }
     
    401402}
    402403
    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  */
    418 int socket_create(socket_cores_t *local_sockets, async_sess_t* sess,
     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 */
     416int
     417socket_create(socket_cores_t *local_sockets, int app_phone,
    419418    void *specific_data, int *socket_id)
    420419{
     
    426425                return EINVAL;
    427426       
    428         /* Store the socket */
     427        // store the socket
    429428        if (*socket_id <= 0) {
    430429                positive = (*socket_id == 0);
     
    442441                return ENOMEM;
    443442       
    444         /* Initialize */
    445         socket->sess = sess;
     443        // initialize
     444        socket->phone = app_phone;
    446445        socket->port = -1;
    447446        socket->key = NULL;
     
    472471}
    473472
    474 /** Destroy the socket.
     473/** Destroys the socket.
    475474 *
    476475 * If the socket is bound, the port is released.
    477  * Release all buffered packets, call the release function and remove the
     476 * Releases all buffered packets, calls the release function and removes the
    478477 * socket from the local sockets.
    479478 *
    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  *
     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.
    489486 */
    490487int
    491 socket_destroy(async_sess_t *sess, int socket_id, socket_cores_t *local_sockets,
     488socket_destroy(int packet_phone, int socket_id, socket_cores_t *local_sockets,
    492489    socket_ports_t *global_sockets,
    493490    void (*socket_release)(socket_core_t *socket))
    494491{
    495         /* Find the socket */
    496         socket_core_t *socket = socket_cores_find(local_sockets, socket_id);
     492        socket_core_t *socket;
     493        int accepted_id;
     494
     495        // find the socket
     496        socket = socket_cores_find(local_sockets, socket_id);
    497497        if (!socket)
    498498                return ENOTSOCK;
    499499       
    500         /* Destroy all accepted sockets */
    501         int accepted_id;
     500        // destroy all accepted sockets
    502501        while ((accepted_id = dyn_fifo_pop(&socket->accepted)) >= 0)
    503                 socket_destroy(sess, accepted_id, local_sockets,
     502                socket_destroy(packet_phone, accepted_id, local_sockets,
    504503                    global_sockets, socket_release);
    505504       
    506         socket_destroy_core(sess, socket, local_sockets, global_sockets,
     505        socket_destroy_core(packet_phone, socket, local_sockets, global_sockets,
    507506            socket_release);
    508507       
     
    536535        next_packet = pq_next(packet);
    537536        if (!next_packet) {
    538                 /* Write all if only one fragment */
     537                // write all if only one fragment
    539538                rc = data_reply(packet_get_data(packet),
    540539                    packet_get_data_length(packet));
    541540                if (rc != EOK)
    542541                        return rc;
    543                 /* Store the total length */
     542                // store the total length
    544543                *length = packet_get_data_length(packet);
    545544        } else {
    546                 /* Count the packet fragments */
     545                // count the packet fragments
    547546                fragments = 1;
    548547                next_packet = pq_next(packet);
     
    550549                        ++fragments;
    551550               
    552                 /* Compute and store the fragment lengths */
     551                // compute and store the fragment lengths
    553552                lengths = (size_t *) malloc(sizeof(size_t) * fragments +
    554553                    sizeof(size_t));
     
    566565                }
    567566               
    568                 /* Write the fragment lengths */
     567                // write the fragment lengths
    569568                rc = data_reply(lengths, sizeof(int) * (fragments + 1));
    570569                if (rc != EOK) {
     
    574573                next_packet = packet;
    575574               
    576                 /* Write the fragments */
     575                // write the fragments
    577576                for (index = 0; index < fragments; ++index) {
    578577                        rc = data_reply(packet_get_data(next_packet),
     
    585584                }
    586585               
    587                 /* Store the total length */
     586                // store the total length
    588587                *length = lengths[fragments];
    589588                free(lengths);
     
    637636                return;
    638637       
    639         /* Find ports */
     638        // find ports
    640639        socket_port = socket_ports_find(global_sockets, socket->port);
    641640        if (socket_port) {
    642                 /* Find the socket */
     641                // find the socket
    643642                socket_ref = socket_port_map_find(&socket_port->map,
    644643                    socket->key, socket->key_length);
     
    647646                        --socket_port->count;
    648647                       
    649                         /* Release if empty */
     648                        // release if empty
    650649                        if (socket_port->count <= 0) {
    651                                 /* Destroy the map */
    652                                 socket_port_map_destroy(&socket_port->map, free);
    653                                 /* Release the port */
     650                                // destroy the map
     651                                socket_port_map_destroy(&socket_port->map);
     652                                // release the port
    654653                                socket_ports_exclude(global_sockets,
    655                                     socket->port, free);
     654                                    socket->port);
    656655                        } else {
    657                                 /* Remove */
     656                                // remove
    658657                                socket_port_map_exclude(&socket_port->map,
    659                                     socket->key, socket->key_length, free);
     658                                    socket->key, socket->key_length);
    660659                        }
    661660                }
     
    686685        int rc;
    687686
    688         /* Find ports */
     687        // find ports
    689688        socket_port = socket_ports_find(global_sockets, port);
    690689        if (!socket_port)
    691690                return ENOENT;
    692691       
    693         /* Add the socket */
     692        // add the socket
    694693        rc = socket_port_add_core(socket_port, socket, key, key_length);
    695694        if (rc != EOK)
Note: See TracChangeset for help on using the changeset viewer.