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


Ignore:
Timestamp:
2013-06-20T16:45:58Z (11 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
08bb73b
Parents:
19a4f73
Message:

use new network address infrastructure (towards IPv6 support)

File:
1 edited

Legend:

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

    r19a4f73 ra2e3ee6  
    106106    void **rdata, size_t *rsize, size_t *roffs)
    107107{
    108         void *data;
    109         size_t size;
    110         ip_header_t *hdr;
    111         size_t hdr_size;
    112         size_t data_offs;
    113         uint16_t chksum;
    114         uint16_t ident;
    115         uint16_t flags_foff;
    116         uint16_t foff;
    117         size_t fragoff_limit;
    118         size_t xfer_size;
    119         size_t spc_avail;
    120         size_t rem_offs;
    121 
     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;
     117       
    122118        /* Upper bound for fragment offset field */
    123         fragoff_limit = 1 << (FF_FRAGOFF_h - FF_FRAGOFF_l);
    124 
     119        size_t fragoff_limit = 1 << (FF_FRAGOFF_h - FF_FRAGOFF_l);
     120       
    125121        /* Verify that total size of datagram is within reasonable bounds */
    126122        if (offs + packet->size > FRAG_OFFS_UNIT * fragoff_limit)
    127123                return ELIMIT;
    128 
    129         hdr_size = sizeof(ip_header_t);
    130         data_offs = ROUND_UP(hdr_size, 4);
    131 
     124       
     125        size_t hdr_size = sizeof(ip_header_t);
     126        size_t data_offs = ROUND_UP(hdr_size, 4);
     127       
    132128        assert(offs % FRAG_OFFS_UNIT == 0);
    133129        assert(offs / FRAG_OFFS_UNIT < fragoff_limit);
    134 
     130       
    135131        /* Value for the fragment offset field */
    136         foff = offs / FRAG_OFFS_UNIT;
    137 
     132        uint16_t foff = offs / FRAG_OFFS_UNIT;
     133       
    138134        if (hdr_size >= mtu)
    139135                return EINVAL;
    140 
     136       
    141137        /* Amount of space in the PDU available for payload */
    142         spc_avail = mtu - hdr_size;
     138        size_t spc_avail = mtu - hdr_size;
    143139        spc_avail -= (spc_avail % FRAG_OFFS_UNIT);
    144 
     140       
    145141        /* Amount of data (payload) to transfer */
    146         xfer_size = min(packet->size - offs, spc_avail);
    147 
     142        size_t xfer_size = min(packet->size - offs, spc_avail);
     143       
    148144        /* Total PDU size */
    149         size = hdr_size + xfer_size;
    150 
     145        size_t size = hdr_size + xfer_size;
     146       
    151147        /* Offset of remaining payload */
    152         rem_offs = offs + xfer_size;
    153 
     148        size_t rem_offs = offs + xfer_size;
     149       
    154150        /* Flags */
    155         flags_foff =
     151        uint16_t flags_foff =
    156152            (packet->df ? BIT_V(uint16_t, FF_FLAG_DF) : 0) +
    157153            (rem_offs < packet->size ? BIT_V(uint16_t, FF_FLAG_MF) : 0) +
    158154            (foff << FF_FRAGOFF_l);
    159 
    160         data = calloc(size, 1);
     155       
     156        void *data = calloc(size, 1);
    161157        if (data == NULL)
    162158                return ENOMEM;
    163 
     159       
    164160        /* Allocate identifier */
    165161        fibril_mutex_lock(&ip_ident_lock);
    166         ident = ++ip_ident;
     162        uint16_t ident = ++ip_ident;
    167163        fibril_mutex_unlock(&ip_ident_lock);
    168 
     164       
    169165        /* Encode header fields */
    170         hdr = (ip_header_t *)data;
     166        ip_header_t *hdr = (ip_header_t *) data;
     167       
    171168        hdr->ver_ihl = (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t));
    172169        hdr->tos = packet->tos;
     
    177174        hdr->proto = packet->proto;
    178175        hdr->chksum = 0;
    179         hdr->src_addr = host2uint32_t_be(packet->src.ipv4);
    180         hdr->dest_addr = host2uint32_t_be(packet->dest.ipv4);
    181 
     176        hdr->src_addr = host2uint32_t_be(src_addr);
     177        hdr->dest_addr = host2uint32_t_be(dest_addr);
     178       
    182179        /* Compute checksum */
    183         chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *)hdr, hdr_size);
     180        uint16_t chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *) hdr,
     181            hdr_size);
    184182        hdr->chksum = host2uint16_t_be(chksum);
    185 
     183       
    186184        /* Copy payload */
    187         memcpy((uint8_t *)data + data_offs, packet->data + offs, xfer_size);
    188 
     185        memcpy((uint8_t *) data + data_offs, packet->data + offs, xfer_size);
     186       
    189187        *rdata = data;
    190188        *rsize = size;
    191189        *roffs = rem_offs;
    192 
     190       
    193191        return EOK;
    194192}
     
    238236        /* XXX Checksum */
    239237
    240         packet->src.ipv4 = uint32_t_be2host(hdr->src_addr);
    241         packet->dest.ipv4 = uint32_t_be2host(hdr->dest_addr);
     238        inet_addr_unpack(uint32_t_be2host(hdr->src_addr), &packet->src);
     239        inet_addr_unpack(uint32_t_be2host(hdr->dest_addr), &packet->dest);
    242240        packet->tos = hdr->tos;
    243241        packet->proto = hdr->proto;
Note: See TracChangeset for help on using the changeset viewer.