fix some bugs in the headers and fix checksumming.

base/inet.cc:
    we can't use a uint16_t for the intermediate checksum values
    since there may be some bits that carry.
    the length that is added in the pseudo header is the length of
    the tcp portion only.
base/inet.hh:
    silly me, the ip_hlen field is in terms of 4-byte words.
    when caclulating checksum, we're dealing with network byte order,
    so don't bother doing any byteswapping.

--HG--
extra : convert_revision : 993e3413e9febea0ba2fb6ba8bf04c053cca15ed
This commit is contained in:
Nathan Binkert 2004-10-01 19:48:33 -04:00
parent 72276af49f
commit eaf66f4658
2 changed files with 13 additions and 12 deletions

View file

@ -46,16 +46,17 @@ eaddr_string(const uint8_t a[6])
uint16_t
IpHdr::ip_cksum() const
{
uint16_t sum = ip_cksum_add(this, hlen(), 0);
return ip_cksum_carry(sum);
int sum = ip_cksum_add(this, hlen(), 0);
sum = ip_cksum_carry(sum);
return sum;
}
uint16_t
IpHdr::tu_cksum() const
{
uint16_t sum = ip_cksum_add(payload(), len() - hlen(), 0);
sum = ip_cksum_add(&ip_src, 4, sum);
sum = ip_cksum_add(&ip_dst, 4, sum);
sum += htons(ip_p + ip_len);
return ip_cksum_carry(sum);
int sum = ip_cksum_add(payload(), len() - hlen(), 0);
sum = ip_cksum_add(&ip_src, 8, sum); // source and destination
sum += htons(ip_p + len() - hlen());
sum = ip_cksum_carry(sum);
return sum;
}

View file

@ -82,7 +82,7 @@ struct EthHdr : protected eth_hdr
struct IpHdr : protected ip_hdr
{
uint8_t version() const { return ip_v; }
uint8_t hlen() const { return ip_hl; }
uint8_t hlen() const { return ip_hl * 4; }
uint8_t tos() const { return ip_tos; }
uint16_t len() const { return ntohs(ip_len); }
uint16_t id() const { return ntohs(ip_id); }
@ -90,11 +90,11 @@ struct IpHdr : protected ip_hdr
uint16_t frag_off() const { return ntohs(ip_off) & 0x1fff; }
uint8_t ttl() const { return ip_ttl; }
uint8_t proto() const { return ip_p; }
uint16_t sum() const { return ntohs(ip_sum); }
uint16_t sum() const { return ip_sum; }
uint32_t src() const { return ntohl(ip_src); }
uint32_t dst() const { return ntohl(ip_dst); }
void sum(uint16_t sum) { ip_sum = htons(sum); }
void sum(uint16_t sum) { ip_sum = sum; }
uint16_t ip_cksum() const;
uint16_t tu_cksum() const;
@ -126,10 +126,10 @@ struct TcpHdr : protected tcp_hdr
uint8_t off() const { return th_off; }
uint8_t flags() const { return th_flags & 0x3f; }
uint16_t win() const { return ntohs(th_win); }
uint16_t sum() const { return ntohs(th_sum); }
uint16_t sum() const { return th_sum; }
uint16_t urp() const { return ntohs(th_urp); }
void sum(uint16_t sum) { th_sum = htons(sum); }
void sum(uint16_t sum) { th_sum = sum; }
int size() const { return off(); }
const uint8_t *bytes() const { return (const uint8_t *)this; }