Ignore:
File:
1 edited

Legend:

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

    re526f08 r14f1db0  
    3636 */
    3737
    38 #include <net/socket_codes.h>
    39 #include <net/in.h>
    40 #include <net/in6.h>
    41 #include <net/inet.h>
    4238#include <async.h>
    4339#include <ipc/services.h>
    44 #include <errno.h>
    45 #include <err.h>
    46 
    47 #include <net/packet.h>
    48 #include <packet_client.h>
     40
     41#include <net_err.h>
     42#include <packet/packet.h>
     43#include <packet/packet_client.h>
    4944#include <packet_remote.h>
    50 #include <net/device.h>
     45#include <net_device.h>
    5146#include <icmp_interface.h>
     47#include <in.h>
     48#include <in6.h>
     49#include <inet.h>
     50#include <ip_local.h>
    5251#include <ip_remote.h>
     52#include <socket_codes.h>
     53#include <socket_errno.h>
    5354#include <ip_interface.h>
    5455#include <tl_interface.h>
     
    5758DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
    5859
    59 int
    60 tl_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))
    78                                 return EINVAL;
    79 
    80                 address_in6 = (struct sockaddr_in6 *) addr;
    81                 *port = ntohs(address_in6->sin6_port);
    82                 break;
    83         default:
    84                 return EAFNOSUPPORT;
    85         }
    86 
     60int 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)){
     70                                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        }
    8785        return EOK;
    8886}
     
    114112                return EBADMEM;
    115113       
    116         *packet_dimension = packet_dimensions_find(packet_dimensions,
    117             device_id);
     114        *packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
    118115        if (!*packet_dimension) {
    119116                /* Ask for and remember them if not found */
     
    139136}
    140137
    141 int
    142 tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions,
    143     device_id_t device_id, size_t content)
    144 {
     138int tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content){
    145139        packet_dimension_ref packet_dimension;
    146140
    147141        packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
    148         if (!packet_dimension)
     142        if(! packet_dimension){
    149143                return ENOENT;
     144        }
    150145        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)
     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){
    158150                                packet_dimension->content = content;
    159                         else
    160                                 packet_dimensions_exclude(packet_dimensions,
    161                                     DEVICE_INVALID_ID);
    162 
     151                        }else{
     152                                packet_dimensions_exclude(packet_dimensions, DEVICE_INVALID_ID);
     153                        }
    163154                }
    164155        }
    165 
    166156        return EOK;
    167157}
    168158
    169 int 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;
     159int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port){
     160        struct sockaddr_in * address_in;
     161        struct sockaddr_in6 * address_in6;
    173162        size_t length;
    174163
    175         if (addrlen < 0)
    176                 return EINVAL;
    177        
     164        if(addrlen < 0){
     165                return EINVAL;
     166        }
    178167        length = (size_t) addrlen;
    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))
    191                                 return EINVAL;
    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 
    200 int
    201 tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet,
    202     services_t error)
    203 {
     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)){
     174                                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
     191int tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, services_t error){
    204192        packet_t next;
    205         uint8_t *src;
     193        uint8_t * src;
    206194        int length;
    207195
     
    212200       
    213201        length = packet_get_addr(packet, &src, NULL);
    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)) {
     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)){
    218207                return EOK;
    219         } else
     208        }else{
    220209                pq_release_remote(packet_phone, packet_get_id(packet));
    221 
     210        }
    222211        return ENOENT;
    223212}
    224213
    225 int
    226 tl_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 {
     214int 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){
    230215        ERROR_DECLARE;
    231216
     
    234219        void * data;
    235220
    236         if (!dimension)
    237                 return EINVAL;
    238 
     221        if(! dimension){
     222                return EINVAL;
     223        }
    239224        // get the data length
    240         if (!async_data_write_receive(&callid, &length))
    241                 return EINVAL;
    242 
     225        if(! async_data_write_receive(&callid, &length)){
     226                return EINVAL;
     227        }
    243228        // get a new packet
    244         *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len,
    245             prefix + dimension->prefix, dimension->suffix);
    246         if (!packet)
     229        *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix);
     230        if(! packet){
    247231                return ENOMEM;
    248 
     232        }
    249233        // allocate space in the packet
    250234        data = packet_suffix(*packet, length);
    251         if (!data) {
     235        if(! data){
    252236                pq_release_remote(packet_phone, packet_get_id(*packet));
    253237                return ENOMEM;
    254238        }
    255 
    256239        // read the data into the packet
    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))) {
     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))){
    261243                pq_release_remote(packet_phone, packet_get_id(*packet));
    262244                return ERROR_CODE;
    263245        }
    264 
    265246        return (int) length;
    266247}
Note: See TracChangeset for help on using the changeset viewer.