Ignore:
File:
1 edited

Legend:

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

    ra2e3ee6 ra1a101d  
    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;
    117        
     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
    118122        /* Upper bound for fragment offset field */
    119         size_t fragoff_limit = 1 << (FF_FRAGOFF_h - FF_FRAGOFF_l);
    120        
     123        fragoff_limit = 1 << (FF_FRAGOFF_h - FF_FRAGOFF_l);
     124
    121125        /* Verify that total size of datagram is within reasonable bounds */
    122126        if (offs + packet->size > FRAG_OFFS_UNIT * fragoff_limit)
    123127                return ELIMIT;
    124        
    125         size_t hdr_size = sizeof(ip_header_t);
    126         size_t data_offs = ROUND_UP(hdr_size, 4);
    127        
     128
     129        hdr_size = sizeof(ip_header_t);
     130        data_offs = ROUND_UP(hdr_size, 4);
     131
    128132        assert(offs % FRAG_OFFS_UNIT == 0);
    129133        assert(offs / FRAG_OFFS_UNIT < fragoff_limit);
    130        
     134
    131135        /* Value for the fragment offset field */
    132         uint16_t foff = offs / FRAG_OFFS_UNIT;
    133        
     136        foff = offs / FRAG_OFFS_UNIT;
     137
    134138        if (hdr_size >= mtu)
    135139                return EINVAL;
    136        
     140
    137141        /* Amount of space in the PDU available for payload */
    138         size_t spc_avail = mtu - hdr_size;
     142        spc_avail = mtu - hdr_size;
    139143        spc_avail -= (spc_avail % FRAG_OFFS_UNIT);
    140        
     144
    141145        /* Amount of data (payload) to transfer */
    142         size_t xfer_size = min(packet->size - offs, spc_avail);
    143        
     146        xfer_size = min(packet->size - offs, spc_avail);
     147
    144148        /* Total PDU size */
    145         size_t size = hdr_size + xfer_size;
    146        
     149        size = hdr_size + xfer_size;
     150
    147151        /* Offset of remaining payload */
    148         size_t rem_offs = offs + xfer_size;
    149        
     152        rem_offs = offs + xfer_size;
     153
    150154        /* Flags */
    151         uint16_t flags_foff =
     155        flags_foff =
    152156            (packet->df ? BIT_V(uint16_t, FF_FLAG_DF) : 0) +
    153157            (rem_offs < packet->size ? BIT_V(uint16_t, FF_FLAG_MF) : 0) +
    154158            (foff << FF_FRAGOFF_l);
    155        
    156         void *data = calloc(size, 1);
     159
     160        data = calloc(size, 1);
    157161        if (data == NULL)
    158162                return ENOMEM;
    159        
     163
    160164        /* Allocate identifier */
    161165        fibril_mutex_lock(&ip_ident_lock);
    162         uint16_t ident = ++ip_ident;
     166        ident = ++ip_ident;
    163167        fibril_mutex_unlock(&ip_ident_lock);
    164        
     168
    165169        /* Encode header fields */
    166         ip_header_t *hdr = (ip_header_t *) data;
    167        
     170        hdr = (ip_header_t *)data;
    168171        hdr->ver_ihl = (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t));
    169172        hdr->tos = packet->tos;
     
    174177        hdr->proto = packet->proto;
    175178        hdr->chksum = 0;
    176         hdr->src_addr = host2uint32_t_be(src_addr);
    177         hdr->dest_addr = host2uint32_t_be(dest_addr);
    178        
     179        hdr->src_addr = host2uint32_t_be(packet->src.ipv4);
     180        hdr->dest_addr = host2uint32_t_be(packet->dest.ipv4);
     181
    179182        /* Compute checksum */
    180         uint16_t chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *) hdr,
    181             hdr_size);
     183        chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *)hdr, hdr_size);
    182184        hdr->chksum = host2uint16_t_be(chksum);
    183        
     185
    184186        /* Copy payload */
    185         memcpy((uint8_t *) data + data_offs, packet->data + offs, xfer_size);
    186        
     187        memcpy((uint8_t *)data + data_offs, packet->data + offs, xfer_size);
     188
    187189        *rdata = data;
    188190        *rsize = size;
    189191        *roffs = rem_offs;
    190        
     192
    191193        return EOK;
    192194}
     
    236238        /* XXX Checksum */
    237239
    238         inet_addr_unpack(uint32_t_be2host(hdr->src_addr), &packet->src);
    239         inet_addr_unpack(uint32_t_be2host(hdr->dest_addr), &packet->dest);
     240        packet->src.ipv4 = uint32_t_be2host(hdr->src_addr);
     241        packet->dest.ipv4 = uint32_t_be2host(hdr->dest_addr);
    240242        packet->tos = hdr->tos;
    241243        packet->proto = hdr->proto;
Note: See TracChangeset for help on using the changeset viewer.