Changeset 71b00dcc in mainline for uspace/srv/net/tl/tl_common.c


Ignore:
Timestamp:
2010-03-07T22:51:38Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
60ab6c3
Parents:
b5cbff4 (diff), 31c80a5 (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:

Merge from lp:~lukasmejdrech/helenos/network.

File:
1 edited

Legend:

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

    rb5cbff4 r71b00dcc  
    5555#include "tl_common.h"
    5656
    57 DEVICE_MAP_IMPLEMENT( packet_dimensions, packet_dimension_t );
    58 
    59 int tl_get_address_port( const struct sockaddr * addr, int addrlen, uint16_t * port ){
    60         const struct sockaddr_in *      address_in;
    61         const struct sockaddr_in6 *     address_in6;
    62 
    63         if(( addrlen <= 0 ) || (( size_t ) addrlen < sizeof( struct sockaddr ))) return EINVAL;
    64         switch( addr->sa_family ){
     57DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
     58
     59int tl_get_address_port(const struct sockaddr * addr, int addrlen, uint16_t * port){
     60        const struct sockaddr_in * address_in;
     61        const struct sockaddr_in6 * address_in6;
     62
     63        if((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))){
     64                return EINVAL;
     65        }
     66        switch(addr->sa_family){
    6567                case AF_INET:
    66                         if( addrlen != sizeof( struct sockaddr_in )) return EINVAL;
    67                         address_in = ( struct sockaddr_in * ) addr;
    68                         * port = ntohs( address_in->sin_port );
     68                        if(addrlen != sizeof(struct sockaddr_in)){
     69                                return EINVAL;
     70                        }
     71                        address_in = (struct sockaddr_in *) addr;
     72                        *port = ntohs(address_in->sin_port);
    6973                        break;
    7074                case AF_INET6:
    71                         if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
    72                         address_in6 = ( struct sockaddr_in6 * ) addr;
    73                         * port = ntohs( address_in6->sin6_port );
     75                        if(addrlen != sizeof(struct sockaddr_in6)){
     76                                return EINVAL;
     77                        }
     78                        address_in6 = (struct sockaddr_in6 *) addr;
     79                        *port = ntohs(address_in6->sin6_port);
    7480                        break;
    7581                default:
     
    7985}
    8086
    81 int tl_get_ip_packet_dimension( int ip_phone, packet_dimensions_ref packet_dimensions, device_id_t device_id, packet_dimension_ref * packet_dimension ){
     87int tl_get_ip_packet_dimension(int ip_phone, packet_dimensions_ref packet_dimensions, device_id_t device_id, packet_dimension_ref * packet_dimension){
    8288        ERROR_DECLARE;
    8389
    84         if( ! packet_dimension ) return EBADMEM;
    85 
    86         * packet_dimension = packet_dimensions_find( packet_dimensions, device_id );
    87         if( ! * packet_dimension ){
     90        if(! packet_dimension){
     91                return EBADMEM;
     92        }
     93
     94        *packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
     95        if(! * packet_dimension){
    8896                // ask for and remember them if not found
    89                 * packet_dimension = malloc( sizeof( ** packet_dimension ));
    90                 if( ! * packet_dimension ) return ENOMEM;
    91                 if( ERROR_OCCURRED( ip_packet_size_req( ip_phone, device_id, * packet_dimension ))){
    92                         free( * packet_dimension );
     97                *packet_dimension = malloc(sizeof(** packet_dimension));
     98                if(! * packet_dimension){
     99                        return ENOMEM;
     100                }
     101                if(ERROR_OCCURRED(ip_packet_size_req(ip_phone, device_id, * packet_dimension))){
     102                        free(*packet_dimension);
    93103                        return ERROR_CODE;
    94104                }
    95                 ERROR_CODE = packet_dimensions_add( packet_dimensions, device_id, * packet_dimension );
    96                 if( ERROR_CODE < 0 ){
    97                         free( * packet_dimension );
     105                ERROR_CODE = packet_dimensions_add(packet_dimensions, device_id, * packet_dimension);
     106                if(ERROR_CODE < 0){
     107                        free(*packet_dimension);
    98108                        return ERROR_CODE;
    99109                }
     
    102112}
    103113
    104 int tl_update_ip_packet_dimension( packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content ){
    105         packet_dimension_ref    packet_dimension;
    106 
    107         packet_dimension = packet_dimensions_find( packet_dimensions, device_id );
    108         if( ! packet_dimension ) return ENOENT;
     114int tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content){
     115        packet_dimension_ref packet_dimension;
     116
     117        packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
     118        if(! packet_dimension){
     119                return ENOENT;
     120        }
    109121        packet_dimension->content = content;
    110         if( device_id != DEVICE_INVALID_ID ){
    111                 packet_dimension = packet_dimensions_find( packet_dimensions, DEVICE_INVALID_ID );
    112                 if( packet_dimension ){
    113                         if( packet_dimension->content >= content ){
     122        if(device_id != DEVICE_INVALID_ID){
     123                packet_dimension = packet_dimensions_find(packet_dimensions, DEVICE_INVALID_ID);
     124                if(packet_dimension){
     125                        if(packet_dimension->content >= content){
    114126                                packet_dimension->content = content;
    115127                        }else{
    116                                 packet_dimensions_exclude( packet_dimensions, DEVICE_INVALID_ID );
     128                                packet_dimensions_exclude(packet_dimensions, DEVICE_INVALID_ID);
    117129                        }
    118130                }
     
    121133}
    122134
    123 int tl_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){
    124         struct sockaddr_in *    address_in;
    125         struct sockaddr_in6 *   address_in6;
    126         size_t                                  length;
    127 
    128         if( addrlen < 0 ) return EINVAL;
    129         length = ( size_t ) addrlen;
    130         if( length < sizeof( struct sockaddr )) return EINVAL;
    131         switch( addr->sa_family ){
     135int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port){
     136        struct sockaddr_in * address_in;
     137        struct sockaddr_in6 * address_in6;
     138        size_t length;
     139
     140        if(addrlen < 0){
     141                return EINVAL;
     142        }
     143        length = (size_t) addrlen;
     144        if(length < sizeof(struct sockaddr)){
     145                return EINVAL;
     146        }
     147        switch(addr->sa_family){
    132148                case AF_INET:
    133                         if( length != sizeof( struct sockaddr_in )) return EINVAL;
    134                         address_in = ( struct sockaddr_in * ) addr;
    135                         address_in->sin_port = htons( port );
     149                        if(length != sizeof(struct sockaddr_in)){
     150                                return EINVAL;
     151                        }
     152                        address_in = (struct sockaddr_in *) addr;
     153                        address_in->sin_port = htons(port);
    136154                        return EOK;
    137155                case AF_INET6:
    138                         if( length != sizeof( struct sockaddr_in6 )) return EINVAL;
    139                         address_in6 = ( struct sockaddr_in6 * ) addr;
    140                         address_in6->sin6_port = htons( port );
     156                        if(length != sizeof(struct sockaddr_in6)){
     157                                return EINVAL;
     158                        }
     159                        address_in6 = (struct sockaddr_in6 *) addr;
     160                        address_in6->sin6_port = htons(port);
    141161                        return EOK;
    142162                default:
     
    145165}
    146166
    147 int tl_prepare_icmp_packet( int packet_phone, int icmp_phone, packet_t packet, services_t error ){
    148         packet_t        next;
    149         uint8_t *       src;
    150         int                     length;
     167int tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, services_t error){
     168        packet_t next;
     169        uint8_t * src;
     170        int length;
    151171
    152172        // detach the first packet and release the others
    153         next = pq_detach( packet );
    154         if( next ){
    155                 pq_release( packet_phone, packet_get_id( next ));
    156         }
    157         length = packet_get_addr( packet, & src, NULL );
    158         if(( length > 0 )
    159         && ( ! error )
    160         && ( icmp_phone >= 0 )
     173        next = pq_detach(packet);
     174        if(next){
     175                pq_release(packet_phone, packet_get_id(next));
     176        }
     177        length = packet_get_addr(packet, &src, NULL);
     178        if((length > 0)
     179                && (! error)
     180                && (icmp_phone >= 0)
    161181        // set both addresses to the source one (avoids the source address deletion before setting the destination one)
    162         && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){
     182                && (packet_set_addr(packet, src, src, (size_t) length) == EOK)){
    163183                return EOK;
    164184        }else{
    165                 pq_release( packet_phone, packet_get_id( packet ));
     185                pq_release(packet_phone, packet_get_id(packet));
    166186        }
    167187        return ENOENT;
    168188}
    169189
    170 int tl_socket_read_packet_data( int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen ){
     190int tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen){
    171191        ERROR_DECLARE;
    172192
    173         ipc_callid_t    callid;
    174         size_t                  length;
    175         void *                  data;
    176 
    177         if( ! dimension ) return EINVAL;
     193        ipc_callid_t callid;
     194        size_t length;
     195        void * data;
     196
     197        if(! dimension){
     198                return EINVAL;
     199        }
    178200        // get the data length
    179         if( ! async_data_write_receive( & callid, & length )) return EINVAL;
     201        if(! async_data_write_receive(&callid, &length)){
     202                return EINVAL;
     203        }
    180204        // get a new packet
    181         * packet = packet_get_4( packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix );
    182         if( ! packet ) return ENOMEM;
     205        *packet = packet_get_4(packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix);
     206        if(! packet){
     207                return ENOMEM;
     208        }
    183209        // allocate space in the packet
    184         data = packet_suffix( * packet, length );
    185         if( ! data ){
    186                 pq_release( packet_phone, packet_get_id( * packet ));
     210        data = packet_suffix(*packet, length);
     211        if(! data){
     212                pq_release(packet_phone, packet_get_id(*packet));
    187213                return ENOMEM;
    188214        }
    189215        // read the data into the packet
    190         if( ERROR_OCCURRED( async_data_write_finalize( callid, data, length ))
     216        if(ERROR_OCCURRED(async_data_write_finalize(callid, data, length))
    191217        // set the packet destination address
    192         || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen ))){
    193                 pq_release( packet_phone, packet_get_id( * packet ));
     218                || ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen))){
     219                pq_release(packet_phone, packet_get_id(*packet));
    194220                return ERROR_CODE;
    195221        }
    196         return ( int ) length;
     222        return (int) length;
    197223}
    198224
Note: See TracChangeset for help on using the changeset viewer.