mirror of
https://github.com/mfontanini/libtins
synced 2026-01-28 20:44:26 +01:00
Refactored several classes.
This commit is contained in:
@@ -61,13 +61,6 @@ namespace Tins {
|
||||
* \param total_sz The total size of the buffer.
|
||||
*/
|
||||
ARP(const uint8_t *buffer, uint32_t total_sz);
|
||||
|
||||
/**
|
||||
* \brief Copy constructor
|
||||
*
|
||||
* \param other The object which will be copied.
|
||||
*/
|
||||
//ARP(const ARP &other);
|
||||
|
||||
/* Getters */
|
||||
/**
|
||||
@@ -282,13 +275,13 @@ namespace Tins {
|
||||
* \sa PDU::clone_packet
|
||||
*/
|
||||
PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz);
|
||||
|
||||
|
||||
/**
|
||||
* \brief Clones this PDU.
|
||||
*
|
||||
* \sa PDU::clone_pdu
|
||||
*/
|
||||
PDU *clone_pdu() const;
|
||||
PDU *clone_pdu() const {
|
||||
return do_clone_pdu<ARP>();
|
||||
}
|
||||
private:
|
||||
struct arphdr {
|
||||
uint16_t ar_hrd; /* format of hardware address */
|
||||
@@ -303,7 +296,6 @@ namespace Tins {
|
||||
uint32_t ar_tip; /* target IP address */
|
||||
} __attribute__((__packed__));
|
||||
|
||||
void copy_fields(const ARP *other);
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
|
||||
arphdr _arp;
|
||||
|
||||
@@ -80,13 +80,6 @@ namespace Tins {
|
||||
* \param total_sz The total size of the buffer.
|
||||
*/
|
||||
EthernetII(const uint8_t *buffer, uint32_t total_sz);
|
||||
|
||||
/**
|
||||
* \brief EthernetII copy constructor.
|
||||
*
|
||||
* \param other The packet which will be copied.
|
||||
*/
|
||||
EthernetII(const EthernetII &other);
|
||||
|
||||
/* Getters */
|
||||
/**
|
||||
@@ -94,27 +87,27 @@ namespace Tins {
|
||||
*
|
||||
* \return Returns the destination's mac address as a constant uint8_t pointer.
|
||||
*/
|
||||
inline const uint8_t* dst_addr() const { return _eth.dst_mac; }
|
||||
const uint8_t* dst_addr() const { return _eth.dst_mac; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the source's mac address.
|
||||
*
|
||||
* \return Returns the source's mac address as a constant uint8_t pointer.
|
||||
*/
|
||||
inline const uint8_t* src_addr() const { return _eth.src_mac; }
|
||||
const uint8_t* src_addr() const { return _eth.src_mac; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the interface.
|
||||
*
|
||||
* \return Returns the interface's index as an uint32_t.
|
||||
*/
|
||||
inline uint32_t iface() const { return this->_iface_index; }
|
||||
uint32_t iface() const { return _iface_index; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the payload_type
|
||||
* \return The payload type.
|
||||
*/
|
||||
inline uint16_t payload_type() const { return Utils::net_to_host_s(_eth.payload_type); };
|
||||
uint16_t payload_type() const { return Utils::net_to_host_s(_eth.payload_type); };
|
||||
|
||||
/* Setters */
|
||||
|
||||
@@ -199,11 +192,11 @@ namespace Tins {
|
||||
PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz);
|
||||
|
||||
/**
|
||||
* \brief Clones this PDU.
|
||||
*
|
||||
* \sa PDU::clone_pdu
|
||||
*/
|
||||
PDU *clone_pdu() const;
|
||||
PDU *clone_pdu() const {
|
||||
return do_clone_pdu<EthernetII>();
|
||||
}
|
||||
private:
|
||||
/**
|
||||
* Struct that represents the Ethernet II header
|
||||
@@ -213,11 +206,9 @@ namespace Tins {
|
||||
uint8_t src_mac[ADDR_SIZE];
|
||||
uint16_t payload_type;
|
||||
} __attribute__((__packed__));
|
||||
|
||||
void copy_fields(const EthernetII *other);
|
||||
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
|
||||
|
||||
ethhdr _eth;
|
||||
uint32_t _iface_index;
|
||||
};
|
||||
|
||||
@@ -65,13 +65,6 @@ namespace Tins {
|
||||
*/
|
||||
ICMP(const uint8_t *buffer, uint32_t total_sz);
|
||||
|
||||
/**
|
||||
* \brief Copy constructor
|
||||
*
|
||||
* \param other The object which will be copied.
|
||||
*/
|
||||
ICMP(const ICMP &other);
|
||||
|
||||
/**
|
||||
* \brief Sets the code field.
|
||||
*
|
||||
@@ -305,11 +298,11 @@ namespace Tins {
|
||||
PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz);
|
||||
|
||||
/**
|
||||
* \brief Clones this PDU.
|
||||
*
|
||||
* \sa PDU::clone_pdu
|
||||
*/
|
||||
PDU *clone_pdu() const;
|
||||
PDU *clone_pdu() const {
|
||||
return do_clone_pdu<ICMP>();
|
||||
}
|
||||
private:
|
||||
static uint16_t global_id, global_seq;
|
||||
|
||||
@@ -330,8 +323,6 @@ namespace Tins {
|
||||
uint8_t pointer;
|
||||
} un;
|
||||
} __attribute__((__packed__));
|
||||
|
||||
void copy_fields(const ICMP *other);
|
||||
|
||||
/** \brief Serialices this ICMP PDU.
|
||||
* \param buffer The buffer in which the PDU will be serialized.
|
||||
|
||||
54
include/ip.h
54
include/ip.h
@@ -101,7 +101,7 @@ namespace Tins {
|
||||
unsigned int op_class:2;
|
||||
unsigned int number:5;
|
||||
#endif
|
||||
} type;
|
||||
} __attribute__((__packed__)) type;
|
||||
|
||||
uint8_t* write(uint8_t* buffer);
|
||||
|
||||
@@ -115,8 +115,8 @@ namespace Tins {
|
||||
*/
|
||||
uint8_t data_size() const;
|
||||
private:
|
||||
uint8_t* optional_data, optional_data_size;
|
||||
} __attribute__((__packed__));
|
||||
std::vector<uint8_t> optional_data;
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Default constructor.
|
||||
@@ -136,11 +136,6 @@ namespace Tins {
|
||||
*/
|
||||
IP(IPv4Address ip_dst, IPv4Address ip_src = 0, PDU *child = 0);
|
||||
|
||||
/**
|
||||
* \brief Copy constructor.
|
||||
*/
|
||||
IP(const IP &other);
|
||||
|
||||
/**
|
||||
* \brief Constructor which creates an IP object from a buffer and adds all identifiable
|
||||
* PDUs found in the buffer as children of this one.
|
||||
@@ -149,19 +144,6 @@ namespace Tins {
|
||||
*/
|
||||
IP(const uint8_t *buffer, uint32_t total_sz);
|
||||
|
||||
/**
|
||||
* \brief Destructor for IP objects.
|
||||
*
|
||||
* Destructs IP objects releasing the allocated memory for the options
|
||||
* if options exist.
|
||||
*/
|
||||
~IP();
|
||||
|
||||
/**
|
||||
* \brief Copy assignment operator.
|
||||
*/
|
||||
IP &operator= (const IP &other);
|
||||
|
||||
/* Getters */
|
||||
|
||||
/**
|
||||
@@ -169,73 +151,73 @@ namespace Tins {
|
||||
*
|
||||
* \return The number of dwords the header occupies in an uin8_t.
|
||||
*/
|
||||
inline uint8_t head_len() const { return this->_ip.ihl; }
|
||||
uint8_t head_len() const { return this->_ip.ihl; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the type of service field.
|
||||
*
|
||||
* \return The this IP PDU's type of service.
|
||||
*/
|
||||
inline uint8_t tos() const { return _ip.tos; }
|
||||
uint8_t tos() const { return _ip.tos; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the total length field.
|
||||
*
|
||||
* \return The total length of this IP PDU.
|
||||
*/
|
||||
inline uint16_t tot_len() const { return Utils::net_to_host_s(_ip.tot_len); }
|
||||
uint16_t tot_len() const { return Utils::net_to_host_s(_ip.tot_len); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the id field.
|
||||
*
|
||||
* \return The id for this IP PDU.
|
||||
*/
|
||||
inline uint16_t id() const { return Utils::net_to_host_s(_ip.id); }
|
||||
uint16_t id() const { return Utils::net_to_host_s(_ip.id); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the fragment offset field.
|
||||
*
|
||||
* \return The fragment offset for this IP PDU.
|
||||
*/
|
||||
inline uint16_t frag_off() const { return Utils::net_to_host_s(_ip.frag_off); }
|
||||
uint16_t frag_off() const { return Utils::net_to_host_s(_ip.frag_off); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the time to live field.
|
||||
*
|
||||
* \return The time to live for this IP PDU.
|
||||
*/
|
||||
inline uint8_t ttl() const { return _ip.ttl; }
|
||||
uint8_t ttl() const { return _ip.ttl; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the protocol field.
|
||||
*
|
||||
* \return The protocol for this IP PDU.
|
||||
*/
|
||||
inline uint8_t protocol() const { return _ip.protocol; }
|
||||
uint8_t protocol() const { return _ip.protocol; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the checksum field.
|
||||
*
|
||||
* \return The checksum for this IP PDU.
|
||||
*/
|
||||
inline uint16_t check() const { return Utils::net_to_host_s(_ip.check); }
|
||||
uint16_t check() const { return Utils::net_to_host_s(_ip.check); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the source address field.
|
||||
*
|
||||
* \return The source address for this IP PDU.
|
||||
*/
|
||||
inline IPv4Address src_addr() const { return Utils::net_to_host_l(_ip.saddr); }
|
||||
IPv4Address src_addr() const { return Utils::net_to_host_l(_ip.saddr); }
|
||||
|
||||
/** \brief Getter for the destination address field.
|
||||
* \return The destination address for this IP PDU.
|
||||
*/
|
||||
inline IPv4Address dst_addr() const { return Utils::net_to_host_l(_ip.daddr); }
|
||||
IPv4Address dst_addr() const { return Utils::net_to_host_l(_ip.daddr); }
|
||||
|
||||
/** \brief Getter for the version field.
|
||||
* \return The version for this IP PDU.
|
||||
*/
|
||||
inline uint8_t version() const { return _ip.version; }
|
||||
uint8_t version() const { return _ip.version; }
|
||||
|
||||
/* Setters */
|
||||
|
||||
@@ -403,11 +385,11 @@ namespace Tins {
|
||||
PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz);
|
||||
|
||||
/**
|
||||
* \brief Clones this PDU.
|
||||
*
|
||||
* \sa PDU::clone_pdu
|
||||
*/
|
||||
PDU *clone_pdu() const;
|
||||
PDU *clone_pdu() const {
|
||||
return do_clone_pdu<IP>();
|
||||
}
|
||||
private:
|
||||
static const uint8_t DEFAULT_TTL;
|
||||
|
||||
@@ -433,10 +415,8 @@ namespace Tins {
|
||||
/*The options start here. */
|
||||
} __attribute__((__packed__));
|
||||
|
||||
void copy_fields(const IP *other);
|
||||
void init_ip_fields();
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
void cleanup();
|
||||
|
||||
iphdr _ip;
|
||||
std::list<IPOption> _ip_options;
|
||||
|
||||
@@ -270,7 +270,8 @@ namespace Tins {
|
||||
void copy_inner_pdu(const PDU &pdu);
|
||||
|
||||
|
||||
/** \brief Serializes this PDU and propagates this action to child PDUs.
|
||||
/**
|
||||
* \brief Serializes this PDU and propagates this action to child PDUs.
|
||||
*
|
||||
* \param buffer The buffer in which to store this PDU's serialization.
|
||||
* \param total_sz The total size of the buffer.
|
||||
@@ -278,7 +279,8 @@ namespace Tins {
|
||||
*/
|
||||
void serialize(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
|
||||
/** \brief Clones the inner pdu(if any).
|
||||
/**
|
||||
* \brief Clones the inner pdu(if any).
|
||||
*
|
||||
* This method clones the inner pdu using data from a buffer.
|
||||
* \param ptr The pointer from which the child PDU must be cloned.
|
||||
@@ -287,7 +289,8 @@ namespace Tins {
|
||||
*/
|
||||
PDU *clone_inner_pdu(const uint8_t *ptr, uint32_t total_sz);
|
||||
|
||||
/** \brief Serializes this TCP PDU.
|
||||
/**
|
||||
* \brief Serializes this TCP PDU.
|
||||
*
|
||||
* Each PDU must override this method and implement it's own
|
||||
* serialization.
|
||||
@@ -296,6 +299,16 @@ namespace Tins {
|
||||
* \param parent The PDU that's one level below this one on the stack. Might be 0.
|
||||
*/
|
||||
virtual void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) = 0;
|
||||
|
||||
/**
|
||||
* \brief Generic clone pdu method.
|
||||
*/
|
||||
template<class T>
|
||||
T *do_clone_pdu() const {
|
||||
T *new_pdu = new T(*static_cast<const T*>(this));
|
||||
new_pdu->copy_inner_pdu(*this);
|
||||
return new_pdu;
|
||||
}
|
||||
private:
|
||||
uint32_t _flag;
|
||||
PDU *_inner_pdu;
|
||||
|
||||
@@ -397,13 +397,13 @@ namespace Tins {
|
||||
* \return A pointer to the option, or 0 if it was not found.
|
||||
*/
|
||||
const TCPOption *search_option(Option opt) const;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Clones this PDU.
|
||||
*
|
||||
* \sa PDU::clone_pdu
|
||||
*/
|
||||
PDU *clone_pdu() const;
|
||||
PDU *clone_pdu() const {
|
||||
return do_clone_pdu<TCP>();
|
||||
}
|
||||
private:
|
||||
struct tcphdr {
|
||||
uint16_t sport;
|
||||
@@ -441,8 +441,6 @@ namespace Tins {
|
||||
} __attribute__((packed));
|
||||
|
||||
static const uint16_t DEFAULT_WINDOW;
|
||||
|
||||
void copy_fields(const TCP *other);
|
||||
|
||||
template<class T> bool generic_search(Option opt, T *value) {
|
||||
const TCPOption *option = search_option(opt);
|
||||
|
||||
Reference in New Issue
Block a user