Changeset f14291b in mainline for uspace/lib/net/tl/tl_common.c


Ignore:
Timestamp:
2010-10-19T20:55:53Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a93d79a
Parents:
1882525 (diff), a7a85d16 (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 mainline changes.

File:
1 edited

Legend:

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

    r1882525 rf14291b  
    3636 */
    3737
     38#include <net/socket_codes.h>
     39#include <net/in.h>
     40#include <net/in6.h>
     41#include <net/inet.h>
    3842#include <async.h>
    3943#include <ipc/services.h>
    40 
    41 #include <net_err.h>
    42 #include <packet/packet.h>
    43 #include <packet/packet_client.h>
     44#include <errno.h>
     45#include <err.h>
     46
     47#include <net/packet.h>
     48#include <packet_client.h>
    4449#include <packet_remote.h>
    45 #include <net_device.h>
     50#include <net/device.h>
    4651#include <icmp_interface.h>
    47 #include <in.h>
    48 #include <in6.h>
    49 #include <inet.h>
    50 #include <ip_local.h>
    5152#include <ip_remote.h>
    52 #include <socket_codes.h>
    53 #include <socket_errno.h>
    5453#include <ip_interface.h>
    5554#include <tl_interface.h>
     
    5857DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
    5958
    60 int tl_get_address_port(const struct sockaddr * addr, int addrlen, uint16_t * port){
    61         const struct sockaddr_in * address_in;
    62         const struct sockaddr_in6 * address_in6;
    63 
    64         if((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))){
    65                 return EINVAL;
    66         }
    67         switch(addr->sa_family){
    68                 case AF_INET:
    69                         if(addrlen != sizeof(struct sockaddr_in)){
     59int
     60tl_get_address_port(const struct sockaddr *addr, int addrlen, uint16_t *port)
     61{
     62        const struct sockaddr_in *address_in;
     63        const struct sockaddr_in6 *address_in6;
     64
     65        if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr)))
     66                return EINVAL;
     67
     68        switch (addr->sa_family) {
     69        case AF_INET:
     70                if (addrlen != sizeof(struct sockaddr_in))
     71                        return EINVAL;
     72
     73                address_in = (struct sockaddr_in *) addr;
     74                *port = ntohs(address_in->sin_port);
     75                break;
     76        case AF_INET6:
     77                if (addrlen != sizeof(struct sockaddr_in6))
    7078                                return EINVAL;
    71                         }
    72                         address_in = (struct sockaddr_in *) addr;
    73                         *port = ntohs(address_in->sin_port);
    74                         break;
    75                 case AF_INET6:
    76                         if(addrlen != sizeof(struct sockaddr_in6)){
    77                                 return EINVAL;
    78                         }
    79                         address_in6 = (struct sockaddr_in6 *) addr;
    80                         *port = ntohs(address_in6->sin6_port);
    81                         break;
    82                 default:
    83                         return EAFNOSUPPORT;
    84         }
     79
     80                address_in6 = (struct sockaddr_in6 *) addr;
     81                *port = ntohs(address_in6->sin6_port);
     82                break;
     83        default:
     84                return EAFNOSUPPORT;
     85        }
     86
    8587        return EOK;
    8688}
     
    112114                return EBADMEM;
    113115       
    114         *packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
     116        *packet_dimension = packet_dimensions_find(packet_dimensions,
     117            device_id);
    115118        if (!*packet_dimension) {
    116119                /* Ask for and remember them if not found */
     
    136139}
    137140
    138 int tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content){
     141int
     142tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions,
     143    device_id_t device_id, size_t content)
     144{
    139145        packet_dimension_ref packet_dimension;
    140146
    141147        packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
    142         if(! packet_dimension){
     148        if (!packet_dimension)
    143149                return ENOENT;
    144         }
    145150        packet_dimension->content = content;
    146         if(device_id != DEVICE_INVALID_ID){
    147                 packet_dimension = packet_dimensions_find(packet_dimensions, DEVICE_INVALID_ID);
    148                 if(packet_dimension){
    149                         if(packet_dimension->content >= content){
     151
     152        if (device_id != DEVICE_INVALID_ID) {
     153                packet_dimension = packet_dimensions_find(packet_dimensions,
     154                    DEVICE_INVALID_ID);
     155
     156                if (packet_dimension) {
     157                        if (packet_dimension->content >= content)
    150158                                packet_dimension->content = content;
    151                         }else{
    152                                 packet_dimensions_exclude(packet_dimensions, DEVICE_INVALID_ID);
    153                         }
     159                        else
     160                                packet_dimensions_exclude(packet_dimensions,
     161                                    DEVICE_INVALID_ID);
     162
    154163                }
    155164        }
     165
    156166        return EOK;
    157167}
    158168
    159 int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port){
    160         struct sockaddr_in * address_in;
    161         struct sockaddr_in6 * address_in6;
     169int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port)
     170{
     171        struct sockaddr_in *address_in;
     172        struct sockaddr_in6 *address_in6;
    162173        size_t length;
    163174
    164         if(addrlen < 0){
    165                 return EINVAL;
    166         }
     175        if (addrlen < 0)
     176                return EINVAL;
     177       
    167178        length = (size_t) addrlen;
    168         if(length < sizeof(struct sockaddr)){
    169                 return EINVAL;
    170         }
    171         switch(addr->sa_family){
    172                 case AF_INET:
    173                         if(length != sizeof(struct sockaddr_in)){
     179        if (length < sizeof(struct sockaddr))
     180                return EINVAL;
     181
     182        switch (addr->sa_family) {
     183        case AF_INET:
     184                if (length != sizeof(struct sockaddr_in))
     185                        return EINVAL;
     186                address_in = (struct sockaddr_in *) addr;
     187                address_in->sin_port = htons(port);
     188                return EOK;
     189        case AF_INET6:
     190                if (length != sizeof(struct sockaddr_in6))
    174191                                return EINVAL;
    175                         }
    176                         address_in = (struct sockaddr_in *) addr;
    177                         address_in->sin_port = htons(port);
    178                         return EOK;
    179                 case AF_INET6:
    180                         if(length != sizeof(struct sockaddr_in6)){
    181                                 return EINVAL;
    182                         }
    183                         address_in6 = (struct sockaddr_in6 *) addr;
    184                         address_in6->sin6_port = htons(port);
    185                         return EOK;
    186                 default:
    187                         return EAFNOSUPPORT;
    188         }
    189 }
    190 
    191 int tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, services_t error){
     192                address_in6 = (struct sockaddr_in6 *) addr;
     193                address_in6->sin6_port = htons(port);
     194                return EOK;
     195        default:
     196                return EAFNOSUPPORT;
     197        }
     198}
     199
     200int
     201tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet,
     202    services_t error)
     203{
    192204        packet_t next;
    193         uint8_t * src;
     205        uint8_t *src;
    194206        int length;
    195207
     
    200212       
    201213        length = packet_get_addr(packet, &src, NULL);
    202         if((length > 0)
    203                 && (! error)
    204                 && (icmp_phone >= 0)
    205         // set both addresses to the source one (avoids the source address deletion before setting the destination one)
    206                 && (packet_set_addr(packet, src, src, (size_t) length) == EOK)){
     214        if ((length > 0) && (!error) && (icmp_phone >= 0) &&
     215            // set both addresses to the source one (avoids the source address
     216            // deletion before setting the destination one)
     217            (packet_set_addr(packet, src, src, (size_t) length) == EOK)) {
    207218                return EOK;
    208         }else{
     219        } else
    209220                pq_release_remote(packet_phone, packet_get_id(packet));
    210         }
     221
    211222        return ENOENT;
    212223}
    213224
    214 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){
     225int
     226tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix,
     227    const packet_dimension_ref dimension, const struct sockaddr *addr,
     228    socklen_t addrlen)
     229{
    215230        ERROR_DECLARE;
    216231
     
    219234        void * data;
    220235
    221         if(! dimension){
    222                 return EINVAL;
    223         }
     236        if (!dimension)
     237                return EINVAL;
     238
    224239        // get the data length
    225         if(! async_data_write_receive(&callid, &length)){
    226                 return EINVAL;
    227         }
     240        if (!async_data_write_receive(&callid, &length))
     241                return EINVAL;
     242
    228243        // get a new packet
    229         *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix);
    230         if(! packet){
     244        *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len,
     245            prefix + dimension->prefix, dimension->suffix);
     246        if (!packet)
    231247                return ENOMEM;
    232         }
     248
    233249        // allocate space in the packet
    234250        data = packet_suffix(*packet, length);
    235         if(! data){
     251        if (!data) {
    236252                pq_release_remote(packet_phone, packet_get_id(*packet));
    237253                return ENOMEM;
    238254        }
     255
    239256        // read the data into the packet
    240         if(ERROR_OCCURRED(async_data_write_finalize(callid, data, length))
    241         // set the packet destination address
    242                 || ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen))){
     257        if (ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) ||
     258            // set the packet destination address
     259            ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr,
     260            addrlen))) {
    243261                pq_release_remote(packet_phone, packet_get_id(*packet));
    244262                return ERROR_CODE;
    245263        }
     264
    246265        return (int) length;
    247266}
Note: See TracChangeset for help on using the changeset viewer.