Changeset 02a09ed in mainline for uspace/srv/net/inetsrv/pdu.c


Ignore:
Timestamp:
2013-06-28T20:20:03Z (11 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1d24ad3
Parents:
edf0d27
Message:

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/inetsrv/pdu.c

    redf0d27 r02a09ed  
    4444#include <mem.h>
    4545#include <stdlib.h>
    46 
     46#include <net/socket_codes.h>
    4747#include "inetsrv.h"
    4848#include "inet_std.h"
     
    106106    void **rdata, size_t *rsize, size_t *roffs)
    107107{
    108         uint32_t src_addr;
    109         int rc = inet_addr_pack(&packet->src, &src_addr);
    110         if (rc != EOK)
    111                 return rc;
    112        
    113         uint32_t dest_addr;
    114         rc = inet_addr_pack(&packet->dest, &dest_addr);
    115         if (rc != EOK)
    116                 return rc;
     108        addr32_t src_v4;
     109        addr128_t src_v6;
     110        uint16_t src_af = inet_addr_get(&packet->src, &src_v4, &src_v6);
     111       
     112        addr32_t dest_v4;
     113        addr128_t dest_v6;
     114        uint16_t dest_af = inet_addr_get(&packet->dest, &dest_v4, &dest_v6);
     115       
     116        if (src_af != dest_af)
     117                return EINVAL;
    117118       
    118119        /* Upper bound for fragment offset field */
     
    123124                return ELIMIT;
    124125       
    125         size_t hdr_size = sizeof(ip_header_t);
     126        size_t hdr_size;
     127       
     128        switch (src_af) {
     129        case AF_INET:
     130                hdr_size = sizeof(ip_header_t);
     131                break;
     132        case AF_INET6:
     133                // FIXME TODO
     134                assert(false);
     135        default:
     136                assert(false);
     137        }
     138       
    126139        size_t data_offs = ROUND_UP(hdr_size, 4);
    127140       
     
    164177       
    165178        /* Encode header fields */
    166         ip_header_t *hdr = (ip_header_t *) data;
    167        
    168         hdr->ver_ihl = (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t));
    169         hdr->tos = packet->tos;
    170         hdr->tot_len = host2uint16_t_be(size);
    171         hdr->id = host2uint16_t_be(ident);
    172         hdr->flags_foff = host2uint16_t_be(flags_foff);
    173         hdr->ttl = packet->ttl;
    174         hdr->proto = packet->proto;
    175         hdr->chksum = 0;
    176         hdr->src_addr = host2uint32_t_be(src_addr);
    177         hdr->dest_addr = host2uint32_t_be(dest_addr);
    178        
    179         /* Compute checksum */
    180         uint16_t chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *) hdr,
    181             hdr_size);
    182         hdr->chksum = host2uint16_t_be(chksum);
     179        ip_header_t *hdr;
     180       
     181        switch (src_af) {
     182        case AF_INET:
     183                hdr = (ip_header_t *) data;
     184               
     185                hdr->ver_ihl =
     186                    (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t));
     187                hdr->tos = packet->tos;
     188                hdr->tot_len = host2uint16_t_be(size);
     189                hdr->id = host2uint16_t_be(ident);
     190                hdr->flags_foff = host2uint16_t_be(flags_foff);
     191                hdr->ttl = packet->ttl;
     192                hdr->proto = packet->proto;
     193                hdr->chksum = 0;
     194                hdr->src_addr = host2uint32_t_be(src_v4);
     195                hdr->dest_addr = host2uint32_t_be(dest_v4);
     196               
     197                /* Compute checksum */
     198                uint16_t chksum = inet_checksum_calc(INET_CHECKSUM_INIT,
     199                    (void *) hdr, hdr_size);
     200                hdr->chksum = host2uint16_t_be(chksum);
     201               
     202                break;
     203        case AF_INET6:
     204                // FIXME TODO
     205                return ENOTSUP;
     206        default:
     207                assert(false);
     208        }
    183209       
    184210        /* Copy payload */
     
    194220int inet_pdu_decode(void *data, size_t size, inet_packet_t *packet)
    195221{
    196         ip_header_t *hdr;
    197         size_t tot_len;
    198         size_t data_offs;
    199         uint8_t version;
    200         uint16_t ident;
    201         uint16_t flags_foff;
    202         uint16_t foff;
    203 
    204222        log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_pdu_decode()");
    205223       
     
    208226                return EINVAL;
    209227        }
    210 
    211         hdr = (ip_header_t *)data;
    212 
    213         version = BIT_RANGE_EXTRACT(uint8_t, VI_VERSION_h, VI_VERSION_l,
    214             hdr->ver_ihl);
     228       
     229        ip_header_t *hdr = (ip_header_t *) data;
     230       
     231        uint8_t version = BIT_RANGE_EXTRACT(uint8_t, VI_VERSION_h,
     232            VI_VERSION_l, hdr->ver_ihl);
    215233        if (version != 4) {
    216234                log_msg(LOG_DEFAULT, LVL_DEBUG, "Version (%d) != 4", version);
    217235                return EINVAL;
    218236        }
    219 
    220         tot_len = uint16_t_be2host(hdr->tot_len);
     237       
     238        size_t tot_len = uint16_t_be2host(hdr->tot_len);
    221239        if (tot_len < sizeof(ip_header_t)) {
    222240                log_msg(LOG_DEFAULT, LVL_DEBUG, "Total Length too small (%zu)", tot_len);
     
    229247                return EINVAL;
    230248        }
    231 
    232         ident = uint16_t_be2host(hdr->id);
    233         flags_foff = uint16_t_be2host(hdr->flags_foff);
    234         foff = BIT_RANGE_EXTRACT(uint16_t, FF_FRAGOFF_h, FF_FRAGOFF_l,
     249       
     250        uint16_t ident = uint16_t_be2host(hdr->id);
     251        uint16_t flags_foff = uint16_t_be2host(hdr->flags_foff);
     252        uint16_t foff = BIT_RANGE_EXTRACT(uint16_t, FF_FRAGOFF_h, FF_FRAGOFF_l,
    235253            flags_foff);
    236254        /* XXX Checksum */
    237 
    238         inet_addr_unpack(uint32_t_be2host(hdr->src_addr), &packet->src);
    239         inet_addr_unpack(uint32_t_be2host(hdr->dest_addr), &packet->dest);
     255       
     256        inet_addr_set(uint32_t_be2host(hdr->src_addr), &packet->src);
     257        inet_addr_set(uint32_t_be2host(hdr->dest_addr), &packet->dest);
    240258        packet->tos = hdr->tos;
    241259        packet->proto = hdr->proto;
     
    248266       
    249267        /* XXX IP options */
    250         data_offs = sizeof(uint32_t) * BIT_RANGE_EXTRACT(uint8_t, VI_IHL_h,
    251             VI_IHL_l, hdr->ver_ihl);
    252 
     268        size_t data_offs = sizeof(uint32_t) *
     269            BIT_RANGE_EXTRACT(uint8_t, VI_IHL_h, VI_IHL_l, hdr->ver_ihl);
     270       
    253271        packet->size = tot_len - data_offs;
    254272        packet->data = calloc(packet->size, 1);
Note: See TracChangeset for help on using the changeset viewer.