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

Add ICMP extensions structure class

This commit is contained in:
Matias Fontanini
2015-12-13 21:49:26 -08:00
parent 8aff1b4afe
commit 187e7b1ca3
5 changed files with 288 additions and 15 deletions

View File

@@ -2,7 +2,10 @@
#define TINS_ICMP_EXTENSION_H
#include <vector>
#include <list>
#include <stdint.h>
#include "small_uint.h"
#include "endianness.h"
namespace Tins {
@@ -11,7 +14,15 @@ namespace Tins {
*/
class ICMPExtension {
public:
/**
* The type used to store the payload
*/
typedef std::vector<uint8_t> payload_type;
/**
* The type that will be returned when serializing an extensions
* structure object
*/
typedef std::vector<uint8_t> serialization_type;
/**
@@ -50,7 +61,7 @@ public:
*
* \return The size of this extension
*/
uint32_t extension_size() const;
uint32_t size() const;
/**
* \brief Serializes this extension into a buffer
@@ -61,9 +72,9 @@ public:
void serialize(uint8_t* buffer, uint32_t buffer_size) const;
/**
* \brief Serializes this ICMP extension object
* \brief Serializes this extension object
*
* \return The serialized ICMP extension
* \return The serialized extension
*/
serialization_type serialize() const;
private:
@@ -73,6 +84,119 @@ private:
uint8_t extension_class_, extension_type_;
};
/**
* \brief Class that represents an ICMP extensions structure
*/
class ICMPExtensionsStructure {
public:
/**
* The type that will be returned when serializing an extensions
* structure object
*/
typedef ICMPExtension::serialization_type serialization_type;
/**
* The type used to store the list of ICMP extensions in this structure
*/
typedef std::list<ICMPExtension> extensions_type;
/**
* \brief Default constructor
*
* This sets the version to 2, as specified in RFC 4884
*/
ICMPExtensionsStructure();
/**
* \brief Constructor from a buffer.
*
* This constructor will find, parse and store the extension
* stack in the buffer.
*/
ICMPExtensionsStructure(const uint8_t* buffer, uint32_t total_sz);
/**
* \brief Setter for the checksum field
*
* \param value The new reserved field value
*/
void reserved(small_uint<12> value);
/**
* \brief Getter for the version field
*
* \return The version field value
*/
small_uint<4> version() const {
uint16_t value = Endian::be_to_host(version_and_reserved_);
return (value >> 12) & 0xf;
}
/**
* \brief Getter for the reserved field
*
* \return The reserved field value
*/
small_uint<12> reserved() const {
uint16_t value = Endian::be_to_host(version_and_reserved_);
return value & 0xfff;
}
/**
* \brief Getter for the checksum field
*
* \return The checksum field value
*/
uint16_t checksum() const { return Endian::be_to_host(checksum_); }
/**
* \brief Getter for the extensions stored by this structure
*
* \return The extensions stored in this structure
*/
const extensions_type& extensions() const { return extensions_; }
/**
* \brief Gets the size of this ICMP extensions structure
*
* \return The size of this structure
*/
uint32_t size() const;
/**
* \brief Serializes this extension structure into a buffer
*
* \param buffer The output buffer in which to store the serialization
* \param buffer_size The size of the output buffer
*/
void serialize(uint8_t* buffer, uint32_t buffer_size);
/**
* \brief Serializes this extension structure
*
* \return The serialized extension structure
*/
serialization_type serialize();
/**
* \brief Validates if the given input contains a valid extension structure
*
* The validation is performed by calculating the checksum of the input
* and comparing to the checksum value in the input buffer.
*
* \param buffer The input buffer
* \param total_sz The size of the input buffer
* \return true iff the buffer contains a valid ICMP extensions structure
*/
static bool validate_extensions(const uint8_t* buffer, uint32_t total_sz);
private:
static const uint32_t BASE_HEADER_SIZE;
uint16_t version_and_reserved_;
uint16_t checksum_;
extensions_type extensions_;
};
} // Tins
#endif // TINS_ICMP_EXTENSION_H

View File

@@ -215,17 +215,30 @@ namespace Tins {
*/
std::string to_string(PDU::PDUType pduType);
/** \brief Does the 16 bits sum of all 2 bytes elements between start and end.
/**
* \brief Does the 16 bits sum of all 2 bytes elements between start and end.
*
* This is the checksum used by IP, UDP and TCP. If there's and odd number of
* bytes, the last one is padded and added to the checksum. The checksum is performed
* using network endiannes.
* bytes, the last one is padded and added to the checksum.
* \param start The pointer to the start of the buffer.
* \param end The pointer to the end of the buffer(excluding the last element).
* \return Returns the checksum between start and end(non inclusive).
* \return Returns the checksum between start and end (non inclusive)
* in network endian
*/
uint32_t do_checksum(const uint8_t *start, const uint8_t *end);
/**
* \brief Computes the 16 bit sum of the input buffer.
*
* If there's and odd number of bytes in the buffer, the last one is padded and
* added to the checksum.
* \param start The pointer to the start of the buffer.
* \param end The pointer to the end of the buffer(excluding the last element).
* \return Returns the checksum between start and end (non inclusive)
* in network endian
*/
uint16_t sum_range(const uint8_t *start, const uint8_t *end);
/** \brief Performs the pseudo header checksum used in TCP and UDP PDUs.
*
* \param source_ip The source ip address.