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


Ignore:
Timestamp:
2018-03-02T20:10:49Z (7 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f1380b7
Parents:
3061bc1
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
Message:

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

File:
1 edited

Legend:

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

    r3061bc1 ra35b458  
    107107        /* Upper bound for fragment offset field */
    108108        size_t fragoff_limit = 1 << (FF_FRAGOFF_h - FF_FRAGOFF_l + 1);
    109        
     109
    110110        /* Verify that total size of datagram is within reasonable bounds */
    111111        if (packet->size > FRAG_OFFS_UNIT * fragoff_limit)
    112112                return ELIMIT;
    113        
     113
    114114        size_t hdr_size = sizeof(ip_header_t);
    115115        if (hdr_size >= mtu)
    116116                return EINVAL;
    117        
     117
    118118        assert(hdr_size % 4 == 0);
    119119        assert(offs % FRAG_OFFS_UNIT == 0);
    120120        assert(offs / FRAG_OFFS_UNIT < fragoff_limit);
    121        
     121
    122122        /* Value for the fragment offset field */
    123123        uint16_t foff = offs / FRAG_OFFS_UNIT;
    124        
     124
    125125        /* Amount of space in the PDU available for payload */
    126126        size_t spc_avail = mtu - hdr_size;
    127127        spc_avail -= (spc_avail % FRAG_OFFS_UNIT);
    128        
     128
    129129        /* Amount of data (payload) to transfer */
    130130        size_t xfer_size = min(packet->size - offs, spc_avail);
    131        
     131
    132132        /* Total PDU size */
    133133        size_t size = hdr_size + xfer_size;
    134        
     134
    135135        /* Offset of remaining payload */
    136136        size_t rem_offs = offs + xfer_size;
    137        
     137
    138138        /* Flags */
    139139        uint16_t flags_foff =
     
    141141            (rem_offs < packet->size ? BIT_V(uint16_t, FF_FLAG_MF) : 0) +
    142142            (foff << FF_FRAGOFF_l);
    143        
     143
    144144        void *data = calloc(size, 1);
    145145        if (data == NULL)
    146146                return ENOMEM;
    147        
     147
    148148        /* Encode header fields */
    149149        ip_header_t *hdr = (ip_header_t *) data;
    150        
     150
    151151        hdr->ver_ihl =
    152152            (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t));
     
    160160        hdr->src_addr = host2uint32_t_be(src);
    161161        hdr->dest_addr = host2uint32_t_be(dest);
    162        
     162
    163163        /* Compute checksum */
    164164        uint16_t chksum = inet_checksum_calc(INET_CHECKSUM_INIT,
    165165            (void *) hdr, hdr_size);
    166166        hdr->chksum = host2uint16_t_be(chksum);
    167        
     167
    168168        /* Copy payload */
    169169        memcpy((uint8_t *) data + hdr_size, packet->data + offs, xfer_size);
    170        
     170
    171171        *rdata = data;
    172172        *rsize = size;
    173173        *roffs = rem_offs;
    174        
     174
    175175        return EOK;
    176176}
     
    200200        if (mtu < 1280)
    201201                return ELIMIT;
    202        
     202
    203203        /* Upper bound for fragment offset field */
    204204        size_t fragoff_limit = 1 << (OF_FRAGOFF_h - OF_FRAGOFF_l);
    205        
     205
    206206        /* Verify that total size of datagram is within reasonable bounds */
    207207        if (offs + packet->size > FRAG_OFFS_UNIT * fragoff_limit)
    208208                return ELIMIT;
    209        
     209
    210210        /* Determine whether we need the Fragment extension header */
    211211        bool fragment;
     
    214214        else
    215215                fragment = true;
    216        
     216
    217217        size_t hdr_size;
    218218        if (fragment)
     
    220220        else
    221221                hdr_size = sizeof(ip6_header_t);
    222        
     222
    223223        if (hdr_size >= mtu)
    224224                return EINVAL;
    225        
     225
    226226        static_assert(sizeof(ip6_header_t) % 8 == 0);
    227227        assert(hdr_size % 8 == 0);
    228228        assert(offs % FRAG_OFFS_UNIT == 0);
    229229        assert(offs / FRAG_OFFS_UNIT < fragoff_limit);
    230        
     230
    231231        /* Value for the fragment offset field */
    232232        uint16_t foff = offs / FRAG_OFFS_UNIT;
    233        
     233
    234234        /* Amount of space in the PDU available for payload */
    235235        size_t spc_avail = mtu - hdr_size;
    236236        spc_avail -= (spc_avail % FRAG_OFFS_UNIT);
    237        
     237
    238238        /* Amount of data (payload) to transfer */
    239239        size_t xfer_size = min(packet->size - offs, spc_avail);
    240        
     240
    241241        /* Total PDU size */
    242242        size_t size = hdr_size + xfer_size;
    243        
     243
    244244        /* Offset of remaining payload */
    245245        size_t rem_offs = offs + xfer_size;
    246        
     246
    247247        /* Flags */
    248248        uint16_t offsmf =
    249249            (rem_offs < packet->size ? BIT_V(uint16_t, OF_FLAG_M) : 0) +
    250250            (foff << OF_FRAGOFF_l);
    251        
     251
    252252        void *data = calloc(size, 1);
    253253        if (data == NULL)
    254254                return ENOMEM;
    255        
     255
    256256        /* Encode header fields */
    257257        ip6_header_t *hdr6 = (ip6_header_t *) data;
    258        
     258
    259259        hdr6->ver_tc = (6 << (VI_VERSION_l));
    260260        memset(hdr6->tc_fl, 0, 3);
    261261        hdr6->hop_limit = packet->ttl;
    262        
     262
    263263        host2addr128_t_be(src, hdr6->src_addr);
    264264        host2addr128_t_be(dest, hdr6->dest_addr);
    265        
     265
    266266        /* Optionally encode Fragment extension header fields */
    267267        if (fragment) {
    268268                assert(offsmf != 0);
    269                
     269
    270270                hdr6->payload_len = host2uint16_t_be(packet->size +
    271271                    sizeof(ip6_header_fragment_t));
    272272                hdr6->next = IP6_NEXT_FRAGMENT;
    273                
     273
    274274                ip6_header_fragment_t *hdr6f = (ip6_header_fragment_t *)
    275275                    (hdr6 + 1);
    276                
     276
    277277                hdr6f->next = packet->proto;
    278278                hdr6f->reserved = 0;
     
    281281        } else {
    282282                assert(offsmf == 0);
    283                
     283
    284284                hdr6->payload_len = host2uint16_t_be(packet->size);
    285285                hdr6->next = packet->proto;
    286286        }
    287        
     287
    288288        /* Copy payload */
    289289        memcpy((uint8_t *) data + hdr_size, packet->data + offs, xfer_size);
    290        
     290
    291291        *rdata = data;
    292292        *rsize = size;
    293293        *roffs = rem_offs;
    294        
     294
    295295        return EOK;
    296296}
     
    312312{
    313313        log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_pdu_decode()");
    314        
     314
    315315        if (size < sizeof(ip_header_t)) {
    316316                log_msg(LOG_DEFAULT, LVL_DEBUG, "PDU too short (%zu)", size);
    317317                return EINVAL;
    318318        }
    319        
     319
    320320        ip_header_t *hdr = (ip_header_t *) data;
    321        
     321
    322322        uint8_t version = BIT_RANGE_EXTRACT(uint8_t, VI_VERSION_h,
    323323            VI_VERSION_l, hdr->ver_ihl);
     
    326326                return EINVAL;
    327327        }
    328        
     328
    329329        size_t tot_len = uint16_t_be2host(hdr->tot_len);
    330330        if (tot_len < sizeof(ip_header_t)) {
     
    332332                return EINVAL;
    333333        }
    334        
     334
    335335        if (tot_len > size) {
    336336                log_msg(LOG_DEFAULT, LVL_DEBUG, "Total Length = %zu > PDU size = %zu",
     
    338338                return EINVAL;
    339339        }
    340        
     340
    341341        uint16_t ident = uint16_t_be2host(hdr->id);
    342342        uint16_t flags_foff = uint16_t_be2host(hdr->flags_foff);
     
    344344            flags_foff);
    345345        /* XXX Checksum */
    346        
     346
    347347        inet_addr_set(uint32_t_be2host(hdr->src_addr), &packet->src);
    348348        inet_addr_set(uint32_t_be2host(hdr->dest_addr), &packet->dest);
     
    351351        packet->ttl = hdr->ttl;
    352352        packet->ident = ident;
    353        
     353
    354354        packet->df = (flags_foff & BIT_V(uint16_t, FF_FLAG_DF)) != 0;
    355355        packet->mf = (flags_foff & BIT_V(uint16_t, FF_FLAG_MF)) != 0;
    356356        packet->offs = foff * FRAG_OFFS_UNIT;
    357        
     357
    358358        /* XXX IP options */
    359359        size_t data_offs = sizeof(uint32_t) *
    360360            BIT_RANGE_EXTRACT(uint8_t, VI_IHL_h, VI_IHL_l, hdr->ver_ihl);
    361        
     361
    362362        packet->size = tot_len - data_offs;
    363363        packet->data = calloc(packet->size, 1);
     
    366366                return ENOMEM;
    367367        }
    368        
     368
    369369        memcpy(packet->data, (uint8_t *) data + data_offs, packet->size);
    370370        packet->link_id = link_id;
    371        
     371
    372372        return EOK;
    373373}
     
    389389{
    390390        log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_pdu_decode6()");
    391        
     391
    392392        if (size < sizeof(ip6_header_t)) {
    393393                log_msg(LOG_DEFAULT, LVL_DEBUG, "PDU too short (%zu)", size);
    394394                return EINVAL;
    395395        }
    396        
     396
    397397        ip6_header_t *hdr6 = (ip6_header_t *) data;
    398        
     398
    399399        uint8_t version = BIT_RANGE_EXTRACT(uint8_t, VI_VERSION_h,
    400400            VI_VERSION_l, hdr6->ver_tc);
     
    403403                return EINVAL;
    404404        }
    405        
     405
    406406        size_t payload_len = uint16_t_be2host(hdr6->payload_len);
    407407        if (payload_len + sizeof(ip6_header_t) > size) {
     
    410410                return EINVAL;
    411411        }
    412        
     412
    413413        uint32_t ident;
    414414        uint16_t offsmf;
     
    416416        uint16_t next;
    417417        size_t data_offs = sizeof(ip6_header_t);
    418        
     418
    419419        /* Fragment extension header */
    420420        if (hdr6->next == IP6_NEXT_FRAGMENT) {
    421421                ip6_header_fragment_t *hdr6f = (ip6_header_fragment_t *)
    422422                    (hdr6 + 1);
    423                
     423
    424424                ident = uint32_t_be2host(hdr6f->id);
    425425                offsmf = uint16_t_be2host(hdr6f->offsmf);
     
    435435                next = hdr6->next;
    436436        }
    437        
     437
    438438        addr128_t src;
    439439        addr128_t dest;
    440        
     440
    441441        addr128_t_be2host(hdr6->src_addr, src);
    442442        inet_addr_set6(src, &packet->src);
    443        
     443
    444444        addr128_t_be2host(hdr6->dest_addr, dest);
    445445        inet_addr_set6(dest, &packet->dest);
    446        
     446
    447447        packet->tos = 0;
    448448        packet->proto = next;
    449449        packet->ttl = hdr6->hop_limit;
    450450        packet->ident = ident;
    451        
     451
    452452        packet->df = 1;
    453453        packet->mf = (offsmf & BIT_V(uint16_t, OF_FLAG_M)) != 0;
    454454        packet->offs = foff * FRAG_OFFS_UNIT;
    455        
     455
    456456        packet->size = payload_len;
    457457        packet->data = calloc(packet->size, 1);
     
    460460                return ENOMEM;
    461461        }
    462        
     462
    463463        memcpy(packet->data, (uint8_t *) data + data_offs, packet->size);
    464464        packet->link_id = link_id;
     
    480480        dgram->tos = 0;
    481481        dgram->size = sizeof(icmpv6_message_t) + sizeof(ndp_message_t);
    482        
     482
    483483        dgram->data = calloc(1, dgram->size);
    484484        if (dgram->data == NULL)
    485485                return ENOMEM;
    486        
     486
    487487        icmpv6_message_t *icmpv6 = (icmpv6_message_t *) dgram->data;
    488        
     488
    489489        icmpv6->type = ndp->opcode;
    490490        icmpv6->code = 0;
    491491        memset(icmpv6->un.ndp.reserved, 0, 3);
    492        
     492
    493493        ndp_message_t *message = (ndp_message_t *) (icmpv6 + 1);
    494        
     494
    495495        if (ndp->opcode == ICMPV6_NEIGHBOUR_SOLICITATION) {
    496496                host2addr128_t_be(ndp->solicited_ip, message->target_address);
     
    502502                icmpv6->un.ndp.flags = NDP_FLAG_OVERRIDE | NDP_FLAG_SOLICITED;
    503503        }
    504        
     504
    505505        message->length = 1;
    506506        addr48(ndp->sender_hw_addr, message->mac);
    507        
     507
    508508        icmpv6_phdr_t phdr;
    509        
     509
    510510        host2addr128_t_be(ndp->sender_proto_addr, phdr.src_addr);
    511511        host2addr128_t_be(ndp->target_proto_addr, phdr.dest_addr);
     
    513513        memset(phdr.zeroes, 0, 3);
    514514        phdr.next = IP_PROTO_ICMPV6;
    515        
     515
    516516        uint16_t cs_phdr =
    517517            inet_checksum_calc(INET_CHECKSUM_INIT, &phdr,
    518518            sizeof(icmpv6_phdr_t));
    519        
     519
    520520        uint16_t cs_all = inet_checksum_calc(cs_phdr, dgram->data,
    521521            dgram->size);
    522        
     522
    523523        icmpv6->checksum = host2uint16_t_be(cs_all);
    524        
     524
    525525        return EOK;
    526526}
     
    541541        if (src_ver != ip_v6)
    542542                return EINVAL;
    543        
     543
    544544        if (dgram->size < sizeof(icmpv6_message_t) + sizeof(ndp_message_t))
    545545                return EINVAL;
    546        
     546
    547547        icmpv6_message_t *icmpv6 = (icmpv6_message_t *) dgram->data;
    548        
     548
    549549        ndp->opcode = icmpv6->type;
    550        
     550
    551551        ndp_message_t *message = (ndp_message_t *) (icmpv6 + 1);
    552        
     552
    553553        addr128_t_be2host(message->target_address, ndp->target_proto_addr);
    554554        addr48(message->mac, ndp->sender_hw_addr);
    555        
     555
    556556        return EOK;
    557557}
Note: See TracChangeset for help on using the changeset viewer.