Changeset ede63e4 in mainline for uspace/srv/net/socket/socket_core.c


Ignore:
Timestamp:
2010-01-04T23:25:48Z (14 years ago)
Author:
Lukas Mejdrech <lukasmejdrech@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
eac9722
Parents:
1a0fb3f8
Message:
  • socket identifier generation moved to libsocket, + data fragment size fix and enhancement, + [ICMP|TCP|UDP]_HEADER_SIZE definition, + int_map_update() function to alter item key
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/socket/socket_core.c

    r1a0fb3f8 rede63e4  
    3535 */
    3636
     37#include <limits.h>
     38#include <stdlib.h>
     39
    3740#include "../err.h"
    3841
     
    5154
    5255#include "socket_core.h"
     56
     57/** Maximum number of random attempts to find a new socket identifier before switching to the sequence.
     58 */
     59#define SOCKET_ID_TRIES                                 100
    5360
    5461/** Bound port sockets.
     
    94101 */
    95102int     socket_port_add_core( socket_port_ref socket_port, socket_core_ref socket, const char * key, size_t key_length );
     103
     104/** Tries to find a new free socket identifier.
     105 *  @param[in] local_sockets The local sockets to be searched.
     106 *  @param[in] positive A value indicating whether a positive identifier is requested. A negative identifier is requested if set to false.
     107 *      @returns The new socket identifier.
     108 *  @returns ELIMIT if there is no socket identifier available.
     109 */
     110static int      socket_generate_new_id( socket_cores_ref local_sockets, int positive );
    96111
    97112INT_MAP_IMPLEMENT( socket_cores, socket_core_t );
     
    230245}
    231246
     247
     248static int socket_generate_new_id( socket_cores_ref local_sockets, int positive ){
     249        int                     socket_id;
     250        int                     count;
     251
     252        count = 0;
     253//      socket_id = socket_globals.last_id;
     254        do{
     255                if( count < SOCKET_ID_TRIES ){
     256                        socket_id = rand() % INT_MAX;
     257                        ++ count;
     258                }else if( count == SOCKET_ID_TRIES ){
     259                        socket_id = 1;
     260                        ++ count;
     261                // only this branch for last_id
     262                }else{
     263                        if( socket_id < INT_MAX ){
     264                                ++ socket_id;
     265/*                      }else if( socket_globals.last_id ){
     266*                               socket_globals.last_id = 0;
     267*                               socket_id = 1;
     268*/                      }else{
     269                                return ELIMIT;
     270                        }
     271                }
     272        }while( socket_cores_find( local_sockets, (( positive ? 1 : -1 ) * socket_id )));
     273//      last_id = socket_id
     274        return socket_id;
     275}
     276
    232277int socket_create( socket_cores_ref local_sockets, int app_phone, void * specific_data, int * socket_id ){
    233278        ERROR_DECLARE;
     
    235280        socket_core_ref socket;
    236281        int                             res;
    237 
    238         if( ! socket_id ) return EBADMEM;
     282        int                             positive;
     283
     284        if( ! socket_id ) return EINVAL;
     285        // store the socket
     286        if( * socket_id <= 0 ){
     287                positive = ( * socket_id == 0 );
     288                * socket_id = socket_generate_new_id( local_sockets, positive );
     289                if( * socket_id <= 0 ){
     290                        return * socket_id;
     291                }
     292                if( ! positive ){
     293                        * socket_id *= -1;
     294                }
     295        }else if( socket_cores_find( local_sockets, * socket_id )){
     296                return EEXIST;
     297        }
    239298        socket = ( socket_core_ref ) malloc( sizeof( * socket ));
    240299        if( ! socket ) return ENOMEM;
     
    254313                return ERROR_CODE;
    255314        }
    256         // get a next free socket number
    257         socket->socket_id = socket_cores_count( local_sockets ) + 1;
    258         // store the socket
     315        socket->socket_id = * socket_id;
    259316        res = socket_cores_add( local_sockets, socket->socket_id, socket );
    260317        if( res < 0 ){
     
    264321                return res;
    265322        }
    266         // return the socket identifier
    267         * socket_id = socket->socket_id;
    268323        return EOK;
    269324}
Note: See TracChangeset for help on using the changeset viewer.