From fc9213081b213e5a0c5b5a6769347d21ea403e06 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Fri, 13 Mar 2015 11:49:26 +0100 Subject: [PATCH] moved remaining implementations in ipv6header.h to cpp; avoid conversion warnings --- src/icmp/icmpdata.cpp | 6 +++--- src/icmp/icmpheader.cpp | 11 +++++++++-- src/ip/ipv6header.cpp | 14 ++++++++++++++ src/ip/ipv6header.h | 9 +++------ 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/icmp/icmpdata.cpp b/src/icmp/icmpdata.cpp index f129508..3db5912 100644 --- a/src/icmp/icmpdata.cpp +++ b/src/icmp/icmpdata.cpp @@ -38,14 +38,14 @@ IcmpData::IcmpData(const std::size_t size_arg) bool IcmpData::match_echo_reply(const uint16_t identifier, const uint16_t sequence_number) const -{ return false; }; +{ return false; } bool IcmpData::match_destination_unreachable(const uint16_t identifier, const uint16_t sequence_number) const -{ return false; }; +{ return false; } inline std::size_t IcmpData::get_size() -{ return size; }; +{ return size; } uint32_t IcmpData::calc_checksum_part() const { return raw_data.calc_checksum_part(); } diff --git a/src/icmp/icmpheader.cpp b/src/icmp/icmpheader.cpp index 5c1ee24..bad7a6e 100644 --- a/src/icmp/icmpheader.cpp +++ b/src/icmp/icmpheader.cpp @@ -50,10 +50,17 @@ std::istream& IcmpHeader::read(std::istream &is) is.read(buf.get(), 4); type = static_cast(buf[0]); code = static_cast(buf[1]); + + // first make the 2 checksum byte unsigned with same size uint8_t checksum_msb = static_cast(buf[2]); uint8_t checksum_lsb = static_cast(buf[3]); - checksum = ( static_cast(checksum_msb) << 8 ) - + static_cast(checksum_lsb); + + // now extend type, shift and add/or them + checksum = static_cast( + (static_cast(checksum_msb) << 8) + | static_cast(checksum_lsb) ); + // need the static_cast around the result since compiler casts everything + // to int before doing | or + if types are smaller than int return is; } diff --git a/src/ip/ipv6header.cpp b/src/ip/ipv6header.cpp index 4faa518..adc91ca 100644 --- a/src/ip/ipv6header.cpp +++ b/src/ip/ipv6header.cpp @@ -48,6 +48,20 @@ uint8_t Ipv6Header::get_version() const return ( Payload[ 0 ] >> 4 ) & 0x0F; } +uint16_t Ipv6Header::get_header_length() const +{ return static_cast(40); } + +uint16_t Ipv6Header::get_total_length() const +{ + // the compiler converts anything smaller than int to int when adding + // which results in a warning when assigning the result to uint16 + return static_cast( get_payload_length() + 40 ); +} + +// in IPv6, this corresponds to the "HOP limit" +uint8_t Ipv6Header::get_time_to_live() const +{ return get_hop_limit(); } + /** * @brief Get the Differentiated Services, originally called Traffic Class. * diff --git a/src/ip/ipv6header.h b/src/ip/ipv6header.h index 57db286..31fd8ef 100644 --- a/src/ip/ipv6header.h +++ b/src/ip/ipv6header.h @@ -75,15 +75,12 @@ public: uint8_t get_next_header() const; uint8_t get_hop_limit() const; - uint16_t get_header_length() const - { return 40; } + uint16_t get_header_length() const; - uint16_t get_total_length() const - { return get_payload_length() + 40; } + uint16_t get_total_length() const; // in IPv6, this corresponds to the "HOP limit" - uint8_t get_time_to_live() const - { return get_hop_limit(); } + uint8_t get_time_to_live() const; boost::asio::ip::address get_source_address() const; boost::asio::ip::address get_destination_address() const; -- 1.7.1