From eaf66f46588cdfd8a91b93821406e1d797c6d1fb Mon Sep 17 00:00:00 2001 From: Nathan Binkert Date: Fri, 1 Oct 2004 19:48:33 -0400 Subject: [PATCH] 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 --- base/inet.cc | 15 ++++++++------- base/inet.hh | 10 +++++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/base/inet.cc b/base/inet.cc index ac0758c1f..0b9e32854 100644 --- a/base/inet.cc +++ b/base/inet.cc @@ -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; } diff --git a/base/inet.hh b/base/inet.hh index b20c90beb..8a49790a7 100644 --- a/base/inet.hh +++ b/base/inet.hh @@ -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; } -- 2.30.2