diff --git a/include/icmp.h b/include/icmp.h index 6aa5d4f..56a9b3c 100644 --- a/include/icmp.h +++ b/include/icmp.h @@ -49,6 +49,11 @@ namespace Tins { */ static const PDU::PDUType pdu_flag = PDU::ICMP; + /** + * The type used to store addresses. + */ + typedef IPv4Address address_type; + /** \brief ICMP flags */ enum Flags { @@ -118,9 +123,9 @@ namespace Tins { /** * \brief Setter for the gateway field. * - * \param new_gw uint32_t with the new gateway. + * \param new_gw The new value for the gateway field. */ - void gateway(uint32_t new_gw); + void gateway(address_type new_gw); /** * \brief Setter for the mtu field. @@ -162,7 +167,7 @@ namespace Tins { * * \param new_mask the value to be set. */ - void address_mask(IPv4Address new_mask); + void address_mask(address_type new_mask); /** * \brief Sets echo request flag for this PDU. @@ -232,7 +237,7 @@ namespace Tins { * \param address Address of the gateway to which traffic should * be sent. */ - void set_redirect(uint8_t icode, uint32_t address); + void set_redirect(uint8_t icode, address_type address); /** * \brief Getter for the ICMP type flag. @@ -274,7 +279,9 @@ namespace Tins { * * \return Returns the gateway field value. */ - uint32_t gateway() const { return Endian::be_to_host(_icmp.un.gateway); } + address_type gateway() const { + return address_type(Endian::be_to_host(_icmp.un.gateway)); + } /** * \brief Getter for the pointer field. @@ -316,8 +323,8 @@ namespace Tins { * * \return Returns the address mask value. */ - IPv4Address address_mask() const { - return IPv4Address(Endian::be_to_host(_orig_timestamp_or_address_mask)); + address_type address_mask() const { + return address_type(Endian::be_to_host(_orig_timestamp_or_address_mask)); } /** diff --git a/src/icmp.cpp b/src/icmp.cpp index 2f7050f..1fa5b8c 100644 --- a/src/icmp.cpp +++ b/src/icmp.cpp @@ -70,7 +70,7 @@ ICMP::ICMP(const uint8_t *buffer, uint32_t total_sz) if(total_sz < sizeof(uint32_t)) throw malformed_packet(); const uint32_t *ptr = reinterpret_cast(buffer); - address_mask(IPv4Address(*ptr++)); + address_mask(address_type(*ptr++)); total_sz -= sizeof(uint32_t); buffer += sizeof(uint32_t); } @@ -98,8 +98,8 @@ void ICMP::sequence(uint16_t new_seq) { _icmp.un.echo.sequence = Endian::host_to_be(new_seq); } -void ICMP::gateway(uint32_t new_gw) { - _icmp.un.gateway = Endian::host_to_be(new_gw); +void ICMP::gateway(address_type new_gw) { + _icmp.un.gateway = Endian::host_to_be(static_cast(new_gw)); } void ICMP::mtu(uint16_t new_mtu) { @@ -122,7 +122,7 @@ void ICMP::transmit_timestamp(uint32_t new_timestamp) { _trans_timestamp = Endian::host_to_be(new_timestamp); } -void ICMP::address_mask(IPv4Address new_mask) { +void ICMP::address_mask(address_type new_mask) { _orig_timestamp_or_address_mask = Endian::host_to_be(static_cast(new_mask)); } @@ -184,7 +184,7 @@ void ICMP::set_source_quench() { type(SOURCE_QUENCH); } -void ICMP::set_redirect(uint8_t icode, uint32_t address) { +void ICMP::set_redirect(uint8_t icode, address_type address) { type(REDIRECT); code(icode); gateway(address); diff --git a/tests/src/icmp.cpp b/tests/src/icmp.cpp index 8db868b..27f46fc 100644 --- a/tests/src/icmp.cpp +++ b/tests/src/icmp.cpp @@ -121,8 +121,8 @@ TEST_F(ICMPTest, Type) { TEST_F(ICMPTest, Gateway) { ICMP icmp; - icmp.gateway(0x31fdb5cd); - EXPECT_EQ(icmp.gateway(), 0x31fdb5cdU); + icmp.gateway("1.2.3.4"); + EXPECT_EQ(IPv4Address("1.2.3.4"), icmp.gateway()); } TEST_F(ICMPTest, MTU) { @@ -229,10 +229,10 @@ TEST_F(ICMPTest, SetSourceQuench) { TEST_F(ICMPTest, SetRedirect) { ICMP icmp; - icmp.set_redirect(0x3d, 0xf1dc); + icmp.set_redirect(0x3d, "1.2.3.4"); EXPECT_EQ(icmp.type(), ICMP::REDIRECT); - EXPECT_EQ(icmp.code(), 0x3d); - EXPECT_EQ(icmp.gateway(), 0xf1dcU); + EXPECT_EQ(0x3d, icmp.code()); + EXPECT_EQ(IPv4Address("1.2.3.4"), icmp.gateway()); } void ICMPTest::test_equals(const ICMP &icmp1, const ICMP &icmp2) {