1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-27 12:14:26 +01:00

ICMP::gateway is now an IPv4Address.

This commit is contained in:
Matias Fontanini
2013-12-14 14:28:39 -03:00
parent b83c1a2a96
commit ccb8ffd1b5
3 changed files with 24 additions and 17 deletions

View File

@@ -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));
}
/**

View File

@@ -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<const uint32_t*>(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<uint32_t>(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<uint32_t>(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);

View File

@@ -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) {