1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Add missing member functions to ICMP extension classes

This commit is contained in:
Matias Fontanini
2015-12-14 20:22:50 -08:00
parent 187e7b1ca3
commit 6e026fcb66
3 changed files with 88 additions and 1 deletions

View File

@@ -25,6 +25,11 @@ public:
*/
typedef std::vector<uint8_t> serialization_type;
/**
* \brief Default constructor
*/
ICMPExtension();
/**
* \brief Constructs an ICMP extension from a buffer
*
@@ -33,6 +38,27 @@ public:
*/
ICMPExtension(const uint8_t* buffer, uint32_t total_sz);
/**
* \brief Setter for the extension class field
*
* \param value The new extension class field value
*/
void extension_class(uint8_t value);
/**
* \brief Setter for the extension sub-type field
*
* \param value The new extension sub-type field value
*/
void extension_type(uint8_t value);
/**
* \brief Setter for the payload field
*
* \param value The new payload field value
*/
void payload(const payload_type& value);
/**
* \brief Getter for the extension class field
*
@@ -116,7 +142,14 @@ public:
ICMPExtensionsStructure(const uint8_t* buffer, uint32_t total_sz);
/**
* \brief Setter for the checksum field
* \brief Setter for the version field
*
* \param value The new version field value
*/
void version(small_uint<4> value);
/**
* \brief Setter for the reserved field
*
* \param value The new reserved field value
*/

View File

@@ -12,6 +12,11 @@ const uint32_t ICMPExtension::BASE_HEADER_SIZE = sizeof(uint16_t) + sizeof(uint8
// ICMPExtension class
ICMPExtension::ICMPExtension()
: extension_class_(0), extension_type_(0) {
}
ICMPExtension::ICMPExtension(const uint8_t* buffer, uint32_t total_sz) {
// Check for the base header (u16 length + u8 clss + u8 type)
if (total_sz < BASE_HEADER_SIZE) {
@@ -31,6 +36,18 @@ ICMPExtension::ICMPExtension(const uint8_t* buffer, uint32_t total_sz) {
payload_.assign(buffer, buffer + length);
}
void ICMPExtension::extension_class(uint8_t value) {
extension_class_ = value;
}
void ICMPExtension::extension_type(uint8_t value) {
extension_type_ = value;
}
void ICMPExtension::payload(const payload_type& value) {
payload_ = value;
}
uint32_t ICMPExtension::size() const {
return BASE_HEADER_SIZE + payload_.size();
}
@@ -87,6 +104,13 @@ void ICMPExtensionsStructure::reserved(small_uint<12> value) {
version_and_reserved_ = Endian::host_to_be(current_value);
}
void ICMPExtensionsStructure::version(small_uint<4> value) {
uint16_t current_value = Endian::be_to_host(version_and_reserved_);
current_value &= 0xfff;
current_value |= value << 12;
version_and_reserved_ = Endian::host_to_be(current_value);
}
bool ICMPExtensionsStructure::validate_extensions(const uint8_t* buffer, uint32_t total_sz) {
if (total_sz < BASE_HEADER_SIZE) {
return false;

View File

@@ -27,6 +27,28 @@ TEST_F(ICMPExtensionTest, ConstructorFromBuffer) {
);
}
TEST_F(ICMPExtensionTest, ExtensionClass) {
ICMPExtension extension;
extension.extension_class(126);
EXPECT_EQ(126, extension.extension_class());
}
TEST_F(ICMPExtensionTest, ExtensionType) {
ICMPExtension extension;
extension.extension_type(126);
EXPECT_EQ(126, extension.extension_type());
}
TEST_F(ICMPExtensionTest, Payload) {
ICMPExtension::payload_type payload;
payload.push_back(0x92);
payload.push_back(0x1a);
payload.push_back(0xde);
ICMPExtension extension;
extension.payload(payload);
EXPECT_EQ(payload, extension.payload());
}
TEST_F(ICMPExtensionTest, ExtensionStructureValidation) {
const uint8_t input[] = { 32, 0, 197, 95, 0, 8, 1, 1, 24, 150, 1, 1 };
EXPECT_TRUE(ICMPExtensionsStructure::validate_extensions(input, sizeof(input)));
@@ -63,3 +85,11 @@ TEST_F(ICMPExtensionTest, Reserved) {
EXPECT_EQ(0xdea, structure.reserved());
EXPECT_EQ(2, structure.version());
}
TEST_F(ICMPExtensionTest, Version) {
ICMPExtensionsStructure structure;
structure.reserved(0xdea);
structure.version(0xf);
EXPECT_EQ(0xdea, structure.reserved());
EXPECT_EQ(0xf, structure.version());
}