mirror of
https://github.com/mfontanini/libtins
synced 2026-01-29 04:54:28 +01:00
Add extract_metadata to main PDU classes
This commit is contained in:
@@ -43,6 +43,13 @@ using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::metadata ARP::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(arp_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
return metadata(sizeof(arp_header), pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
ARP::ARP(ipaddress_type target_ip,
|
||||
ipaddress_type sender_ip,
|
||||
const hwaddress_type& target_hw,
|
||||
|
||||
@@ -47,6 +47,13 @@ using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::metadata DHCP::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(bootp_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
return metadata(total_sz, pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
// Magic cookie: uint32_t.
|
||||
DHCP::DHCP()
|
||||
: size_(sizeof(uint32_t)) {
|
||||
|
||||
@@ -45,6 +45,13 @@ using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::metadata DHCPv6::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < 2)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
return metadata(total_sz, pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
DHCPv6::DHCPv6()
|
||||
: header_data_(), options_size_() {
|
||||
|
||||
|
||||
@@ -51,6 +51,13 @@ using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::metadata DNS::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(sizeof(dns_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
return metadata(total_sz, pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
DNS::DNS()
|
||||
: header_(), answers_idx_(), authority_idx_(), additional_idx_() {
|
||||
}
|
||||
|
||||
@@ -39,6 +39,13 @@ using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::metadata Dot1Q::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(dot1q_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
return metadata(sizeof(dot1q_header), pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
Dot1Q::Dot1Q(small_uint<12> tag_id, bool append_pad)
|
||||
: header_(), append_padding_(append_pad) {
|
||||
id(tag_id);
|
||||
@@ -123,11 +130,11 @@ void Dot1Q::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *)
|
||||
}
|
||||
|
||||
#if TINS_IS_LITTLE_ENDIAN
|
||||
uint16_t Dot1Q::get_id(const dot1q_hdr* hdr) {
|
||||
uint16_t Dot1Q::get_id(const dot1q_header* hdr) {
|
||||
return hdr->idL | (hdr->idH << 8);
|
||||
}
|
||||
#else
|
||||
uint16_t Dot1Q::get_id(const dot1q_hdr* hdr) {
|
||||
uint16_t Dot1Q::get_id(const dot1q_header* hdr) {
|
||||
return hdr->id;
|
||||
}
|
||||
#endif
|
||||
@@ -140,7 +147,7 @@ bool Dot1Q::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
|
||||
if (total_sz < sizeof(header_)) {
|
||||
return false;
|
||||
}
|
||||
const dot1q_hdr* dot1q_ptr = (const dot1q_hdr*)ptr;
|
||||
const dot1q_header* dot1q_ptr = (const dot1q_header*)ptr;
|
||||
if (get_id(dot1q_ptr) == get_id(&header_)) {
|
||||
ptr += sizeof(header_);
|
||||
total_sz -= sizeof(header_);
|
||||
|
||||
@@ -56,6 +56,13 @@ namespace Tins {
|
||||
|
||||
const Dot3::address_type Dot3::BROADCAST("ff:ff:ff:ff:ff:ff");
|
||||
|
||||
PDU::metadata Dot3::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(dot3_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
return metadata(sizeof(dot3_header), pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
Dot3::Dot3(const address_type& dst_hw_addr, const address_type& src_hw_addr)
|
||||
: header_() {
|
||||
this->dst_addr(dst_hw_addr);
|
||||
|
||||
@@ -46,6 +46,15 @@ using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::metadata EAPOL::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(eapol_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
const eapol_header* header = (const eapol_header*)buffer;
|
||||
uint32_t advertised_size = Endian::be_to_host<uint16_t>(header->length) + 4;
|
||||
return metadata(min(total_sz, advertised_size), pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
EAPOL::EAPOL(uint8_t packet_type, EAPOLTYPE type)
|
||||
: header_() {
|
||||
header_.version = 1;
|
||||
@@ -59,7 +68,7 @@ EAPOL::EAPOL(const uint8_t* buffer, uint32_t total_sz) {
|
||||
}
|
||||
|
||||
EAPOL* EAPOL::from_bytes(const uint8_t* buffer, uint32_t total_sz) {
|
||||
if (total_sz < sizeof(eapol_header)) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(eapol_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
const eapol_header* ptr = (const eapol_header*)buffer;
|
||||
|
||||
@@ -61,8 +61,18 @@ namespace Tins {
|
||||
|
||||
const EthernetII::address_type EthernetII::BROADCAST("ff:ff:ff:ff:ff:ff");
|
||||
|
||||
PDU::metadata EthernetII::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(ethernet_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
const ethernet_header* header = (const ethernet_header*)buffer;
|
||||
PDUType next_type = Internals::ether_type_to_pdu_flag(
|
||||
static_cast<Constants::Ethernet::e>(Endian::be_to_host(header->payload_type)));
|
||||
return metadata(sizeof(ethernet_header), pdu_flag, next_type);
|
||||
}
|
||||
|
||||
EthernetII::EthernetII(const address_type& dst_hw_addr,
|
||||
const address_type& src_hw_addr)
|
||||
const address_type& src_hw_addr)
|
||||
: header_() {
|
||||
dst_addr(dst_hw_addr);
|
||||
src_addr(src_hw_addr);
|
||||
|
||||
@@ -46,6 +46,13 @@ using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::metadata ICMP::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(icmp_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
return metadata(sizeof(icmp_header), pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
ICMP::ICMP(Flags flag)
|
||||
: orig_timestamp_or_address_mask_(), recv_timestamp_(), trans_timestamp_() {
|
||||
memset(&header_, 0, sizeof(icmp_header));
|
||||
|
||||
@@ -259,6 +259,26 @@ Constants::Ethernet::e pdu_flag_to_ether_type(PDU::PDUType flag) {
|
||||
}
|
||||
}
|
||||
|
||||
PDU::PDUType ether_type_to_pdu_flag(Constants::Ethernet::e flag) {
|
||||
switch (flag) {
|
||||
case Constants::Ethernet::IP:
|
||||
return PDU::IP;
|
||||
case Constants::Ethernet::IPV6:
|
||||
return PDU::IPv6;
|
||||
case Constants::Ethernet::ARP:
|
||||
return PDU::ARP;
|
||||
case Constants::Ethernet::VLAN:
|
||||
return PDU::DOT1Q;
|
||||
case Constants::Ethernet::PPPOED:
|
||||
return PDU::PPPOE;
|
||||
//case PDU::RSNEAPOL
|
||||
//case PDU::RC4EAPOL:
|
||||
// return Constants::Ethernet::EAPOL;
|
||||
default:
|
||||
return PDU::UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
Constants::IP::e pdu_flag_to_ip_type(PDU::PDUType flag) {
|
||||
switch(flag) {
|
||||
case PDU::IP:
|
||||
@@ -327,7 +347,30 @@ void try_parse_icmp_extensions(InputMemoryStream& stream,
|
||||
}
|
||||
}
|
||||
|
||||
bool increment(IPv4Address& addr) {
|
||||
PDU::PDUType ip_type_to_pdu_flag(Constants::IP::e flag) {
|
||||
switch(flag) {
|
||||
case Constants::IP::PROTO_IPIP:
|
||||
return PDU::IP;
|
||||
case Constants::IP::PROTO_IPV6:
|
||||
return PDU::IPv6;
|
||||
case Constants::IP::PROTO_TCP:
|
||||
return PDU::TCP;
|
||||
case Constants::IP::PROTO_UDP:
|
||||
return PDU::UDP;
|
||||
case Constants::IP::PROTO_ICMP:
|
||||
return PDU::ICMP;
|
||||
case Constants::IP::PROTO_ICMPV6:
|
||||
return PDU::ICMPv6;
|
||||
case Constants::IP::PROTO_AH:
|
||||
return PDU::IPSEC_AH;
|
||||
case Constants::IP::PROTO_ESP:
|
||||
return PDU::IPSEC_ESP;
|
||||
default:
|
||||
return PDU::UNKNOWN;
|
||||
};
|
||||
}
|
||||
|
||||
bool increment(IPv4Address &addr) {
|
||||
uint32_t addr_int = Endian::be_to_host<uint32_t>(addr);
|
||||
bool reached_end = ++addr_int == 0xffffffff;
|
||||
addr = IPv4Address(Endian::be_to_host<uint32_t>(addr_int));
|
||||
|
||||
10
src/ip.cpp
10
src/ip.cpp
@@ -59,6 +59,16 @@ namespace Tins {
|
||||
|
||||
const uint8_t IP::DEFAULT_TTL = 128;
|
||||
|
||||
PDU::metadata IP::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(ip_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
const ip_header* header = (const ip_header*)buffer;
|
||||
PDUType next_type = Internals::ip_type_to_pdu_flag(
|
||||
static_cast<Constants::IP::e>(header->protocol));
|
||||
return metadata(header->ihl * 4, pdu_flag, next_type);
|
||||
}
|
||||
|
||||
IP::IP(address_type ip_dst, address_type ip_src) {
|
||||
init_ip_fields();
|
||||
this->dst_addr(ip_dst);
|
||||
|
||||
20
src/ipv6.cpp
20
src/ipv6.cpp
@@ -51,6 +51,25 @@ using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::metadata IPv6::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(ipv6_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
InputMemoryStream stream(buffer, total_sz);
|
||||
const ipv6_header* header = (const ipv6_header*)buffer;
|
||||
uint32_t header_size = sizeof(ipv6_header);
|
||||
uint8_t current_header = header->next_header;
|
||||
stream.skip(sizeof(ipv6_header));
|
||||
while (is_extension_header(current_header)) {
|
||||
current_header = stream.read<uint8_t>();
|
||||
const uint32_t ext_size = (static_cast<uint32_t>(stream.read<uint8_t>()) + 1) * 8;
|
||||
const uint32_t payload_size = ext_size - sizeof(uint8_t) * 2;
|
||||
header_size += ext_size;
|
||||
stream.skip(payload_size);
|
||||
}
|
||||
return metadata(header_size, pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
IPv6::IPv6(address_type ip_dst, address_type ip_src, PDU* child)
|
||||
: header_(), headers_size_(0) {
|
||||
version(6);
|
||||
@@ -70,7 +89,6 @@ IPv6::IPv6(const uint8_t* buffer, uint32_t total_sz)
|
||||
// minus one, from the next_header field.
|
||||
const uint32_t ext_size = (static_cast<uint32_t>(stream.read<uint8_t>()) + 1) * 8;
|
||||
const uint32_t payload_size = ext_size - sizeof(uint8_t) * 2;
|
||||
// -1 -> next header identifier
|
||||
if (!stream.can_read(ext_size)) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
|
||||
12
src/pdu.cpp
12
src/pdu.cpp
@@ -36,6 +36,18 @@ using std::vector;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::metadata::metadata()
|
||||
: header_size(0), current_pdu_type(PDU::UNKNOWN), next_pdu_type(PDU::UNKNOWN) {
|
||||
|
||||
}
|
||||
|
||||
PDU::metadata::metadata(uint32_t header_size, PDUType current_type, PDUType next_type)
|
||||
: header_size(header_size), current_pdu_type(current_type), next_pdu_type(next_type) {
|
||||
|
||||
}
|
||||
|
||||
// PDU
|
||||
|
||||
PDU::PDU()
|
||||
: inner_pdu_() {
|
||||
|
||||
|
||||
@@ -51,6 +51,14 @@ namespace Tins {
|
||||
|
||||
const uint16_t TCP::DEFAULT_WINDOW = 32678;
|
||||
|
||||
PDU::metadata TCP::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(tcp_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
const tcp_header* header = (const tcp_header*)buffer;
|
||||
return metadata(header->doff * 4, pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
TCP::TCP(uint16_t dport, uint16_t sport)
|
||||
: header_(), options_size_(0), total_options_size_(0) {
|
||||
this->dport(dport);
|
||||
|
||||
@@ -43,6 +43,13 @@ using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::metadata UDP::extract_metadata(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if (TINS_UNLIKELY(total_sz < sizeof(udp_header))) {
|
||||
throw malformed_packet();
|
||||
}
|
||||
return metadata(sizeof(udp_header), pdu_flag, PDU::UNKNOWN);
|
||||
}
|
||||
|
||||
UDP::UDP(uint16_t dport, uint16_t sport)
|
||||
: header_() {
|
||||
this->dport(dport);
|
||||
|
||||
Reference in New Issue
Block a user