mirror of
https://github.com/mfontanini/libtins
synced 2026-01-30 21:44:26 +01:00
Removed the useless PDU::flag member. Added a PDU concatenation operator.
This commit is contained in:
@@ -36,7 +36,6 @@ namespace Tins {
|
||||
|
||||
ARP::ARP(ipaddress_type target_ip, ipaddress_type sender_ip,
|
||||
const hwaddress_type &target_hw, const hwaddress_type &sender_hw)
|
||||
: PDU(0x0608)
|
||||
{
|
||||
memset(&_arp, 0, sizeof(arphdr));
|
||||
hw_addr_format((uint16_t)Constants::ARP::ETHER);
|
||||
@@ -50,7 +49,6 @@ ARP::ARP(ipaddress_type target_ip, ipaddress_type sender_ip,
|
||||
}
|
||||
|
||||
ARP::ARP(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(Endian::host_to_be<uint16_t>(Constants::Ethernet::ARP))
|
||||
{
|
||||
if(total_sz < sizeof(arphdr))
|
||||
throw runtime_error("Not enough size for an ARP header in the buffer.");
|
||||
|
||||
@@ -26,12 +26,12 @@
|
||||
|
||||
namespace Tins{
|
||||
BootP::BootP()
|
||||
: PDU(255), _vend(64) {
|
||||
: _vend(64) {
|
||||
std::memset(&_bootp, 0, sizeof(bootphdr));
|
||||
}
|
||||
|
||||
BootP::BootP(const uint8_t *buffer, uint32_t total_sz, uint32_t vend_field_size)
|
||||
: PDU(255), _vend(vend_field_size)
|
||||
: _vend(vend_field_size)
|
||||
{
|
||||
if(total_sz < sizeof(bootphdr) + vend_field_size)
|
||||
throw std::runtime_error("Not enough size for a BootP header in the buffer.");
|
||||
|
||||
@@ -31,11 +31,11 @@ using std::list;
|
||||
|
||||
namespace Tins {
|
||||
|
||||
DNS::DNS() : PDU(255), extra_size(0) {
|
||||
DNS::DNS() : extra_size(0) {
|
||||
std::memset(&dns, 0, sizeof(dns));
|
||||
}
|
||||
|
||||
DNS::DNS(const uint8_t *buffer, uint32_t total_sz) : PDU(255), extra_size(0) {
|
||||
DNS::DNS(const uint8_t *buffer, uint32_t total_sz) : extra_size(0) {
|
||||
if(total_sz < sizeof(dnshdr))
|
||||
throw std::runtime_error("Not enough size for a DNS header in the buffer.");
|
||||
std::memcpy(&dns, buffer, sizeof(dnshdr));
|
||||
|
||||
@@ -46,20 +46,19 @@ namespace Tins {
|
||||
const Dot11::address_type Dot11::BROADCAST = "ff:ff:ff:ff:ff:ff";
|
||||
|
||||
Dot11::Dot11(const address_type &dst_hw_addr, PDU* child)
|
||||
: PDU(ETHERTYPE_IP, child), _options_size(0)
|
||||
: PDU(child), _options_size(0)
|
||||
{
|
||||
memset(&_header, 0, sizeof(ieee80211_header));
|
||||
addr1(dst_hw_addr);
|
||||
}
|
||||
|
||||
Dot11::Dot11(const ieee80211_header *header_ptr)
|
||||
: PDU(ETHERTYPE_IP)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Dot11::Dot11(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(ETHERTYPE_IP), _options_size(0)
|
||||
: _options_size(0)
|
||||
{
|
||||
if(total_sz < sizeof(_header))
|
||||
throw runtime_error("Not enough size for an Dot11 header in the buffer.");
|
||||
|
||||
@@ -28,14 +28,16 @@
|
||||
|
||||
|
||||
namespace Tins {
|
||||
EAPOL::EAPOL(uint8_t packet_type, EAPOLTYPE type) : PDU(0xff) {
|
||||
EAPOL::EAPOL(uint8_t packet_type, EAPOLTYPE type)
|
||||
{
|
||||
std::memset(&_header, 0, sizeof(_header));
|
||||
_header.version = 1;
|
||||
_header.packet_type = packet_type;
|
||||
_header.type = (uint8_t)type;
|
||||
}
|
||||
|
||||
EAPOL::EAPOL(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) {
|
||||
EAPOL::EAPOL(const uint8_t *buffer, uint32_t total_sz)
|
||||
{
|
||||
if(total_sz < sizeof(_header))
|
||||
throw std::runtime_error("Not enough size for an EAPOL header in the buffer.");
|
||||
std::memcpy(&_header, buffer, sizeof(_header));
|
||||
|
||||
@@ -40,7 +40,7 @@ const EthernetII::address_type EthernetII::BROADCAST("ff:ff:ff:ff:ff:ff");
|
||||
EthernetII::EthernetII(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr,
|
||||
PDU* child)
|
||||
: PDU(ETHERTYPE_IP, child)
|
||||
: PDU(child)
|
||||
{
|
||||
memset(&_eth, 0, sizeof(ethhdr));
|
||||
dst_addr(dst_hw_addr);
|
||||
@@ -51,7 +51,6 @@ EthernetII::EthernetII(const NetworkInterface& iface,
|
||||
}
|
||||
|
||||
EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(ETHERTYPE_IP)
|
||||
{
|
||||
if(total_sz < sizeof(ethhdr))
|
||||
throw std::runtime_error("Not enough size for an ethernetII header in the buffer.");
|
||||
|
||||
@@ -32,7 +32,8 @@
|
||||
uint16_t Tins::ICMP::global_id = 0, Tins::ICMP::global_seq = 0;
|
||||
|
||||
|
||||
Tins::ICMP::ICMP(Flags flag) : PDU(IPPROTO_ICMP) {
|
||||
Tins::ICMP::ICMP(Flags flag)
|
||||
{
|
||||
std::memset(&_icmp, 0, sizeof(icmphdr));
|
||||
switch(flag) {
|
||||
case ECHO_REPLY:
|
||||
@@ -48,7 +49,8 @@ Tins::ICMP::ICMP(Flags flag) : PDU(IPPROTO_ICMP) {
|
||||
};
|
||||
}
|
||||
|
||||
Tins::ICMP::ICMP(const uint8_t *buffer, uint32_t total_sz) : PDU(IPPROTO_ICMP) {
|
||||
Tins::ICMP::ICMP(const uint8_t *buffer, uint32_t total_sz)
|
||||
{
|
||||
if(total_sz < sizeof(icmphdr))
|
||||
throw std::runtime_error("Not enough size for an ICMP header in the buffer.");
|
||||
std::memcpy(&_icmp, buffer, sizeof(icmphdr));
|
||||
|
||||
@@ -38,7 +38,7 @@ const IEEE802_3::address_type IEEE802_3::BROADCAST("ff:ff:ff:ff:ff:ff");
|
||||
IEEE802_3::IEEE802_3(const NetworkInterface& iface,
|
||||
const address_type &dst_hw_addr, const address_type &src_hw_addr,
|
||||
PDU* child)
|
||||
: PDU(ETHERTYPE_IP, child)
|
||||
: PDU(child)
|
||||
{
|
||||
memset(&_eth, 0, sizeof(ethhdr));
|
||||
this->dst_addr(dst_hw_addr);
|
||||
@@ -49,7 +49,6 @@ IEEE802_3::IEEE802_3(const NetworkInterface& iface,
|
||||
}
|
||||
|
||||
IEEE802_3::IEEE802_3(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(ETHERTYPE_IP)
|
||||
{
|
||||
if(total_sz < sizeof(ethhdr))
|
||||
throw std::runtime_error("Not enough size for an ethernetII header in the buffer.");
|
||||
|
||||
36
src/ip.cpp
36
src/ip.cpp
@@ -43,7 +43,7 @@ namespace Tins {
|
||||
const uint8_t IP::DEFAULT_TTL = 128;
|
||||
|
||||
IP::IP(address_type ip_dst, address_type ip_src, PDU *child)
|
||||
: PDU(Constants::IP::PROTO_IP, child)
|
||||
: PDU(child)
|
||||
{
|
||||
init_ip_fields();
|
||||
this->dst_addr(ip_dst);
|
||||
@@ -51,7 +51,6 @@ IP::IP(address_type ip_dst, address_type ip_src, PDU *child)
|
||||
}
|
||||
|
||||
IP::IP(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(Constants::IP::PROTO_IP)
|
||||
{
|
||||
const char *msg = "Not enough size for an IP header in the buffer.";
|
||||
if(total_sz < sizeof(iphdr))
|
||||
@@ -329,7 +328,7 @@ bool IP::send(PacketSender& sender) {
|
||||
link_addr.sin_family = AF_INET;
|
||||
link_addr.sin_port = 0;
|
||||
link_addr.sin_addr.s_addr = _ip.daddr;
|
||||
if(inner_pdu() && inner_pdu()->flag() == IPPROTO_ICMP)
|
||||
if(inner_pdu() && inner_pdu()->pdu_type() == PDU::ICMP)
|
||||
type = PacketSender::ICMP_SOCKET;
|
||||
|
||||
return sender.send_l3(*this, (struct sockaddr*)&link_addr, sizeof(link_addr), type);
|
||||
@@ -341,7 +340,7 @@ PDU *IP::recv_response(PacketSender &sender) {
|
||||
link_addr.sin_family = AF_INET;
|
||||
link_addr.sin_port = 0;
|
||||
link_addr.sin_addr.s_addr = _ip.daddr;
|
||||
if(inner_pdu() && inner_pdu()->flag() == IPPROTO_ICMP)
|
||||
if(inner_pdu() && inner_pdu()->pdu_type() == PDU::ICMP)
|
||||
type = PacketSender::ICMP_SOCKET;
|
||||
|
||||
return sender.recv_l3(*this, (struct sockaddr*)&link_addr, sizeof(link_addr), type);
|
||||
@@ -352,15 +351,28 @@ void IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU* pare
|
||||
assert(total_sz >= my_sz);
|
||||
if(inner_pdu()) {
|
||||
uint32_t new_flag;
|
||||
new_flag = inner_pdu()->flag();
|
||||
if(new_flag == IPPROTO_IP)
|
||||
new_flag = IPPROTO_IPIP;
|
||||
|
||||
this->protocol(new_flag);
|
||||
this->flag(new_flag);
|
||||
switch(inner_pdu()->pdu_type()) {
|
||||
case PDU::IP:
|
||||
new_flag = IPPROTO_IPIP;
|
||||
break;
|
||||
case PDU::TCP:
|
||||
new_flag = IPPROTO_TCP;
|
||||
break;
|
||||
case PDU::UDP:
|
||||
new_flag = IPPROTO_UDP;
|
||||
break;
|
||||
case PDU::ICMP:
|
||||
new_flag = IPPROTO_ICMP;
|
||||
break;
|
||||
default:
|
||||
// check for other protos
|
||||
new_flag = 0xff;
|
||||
};
|
||||
protocol(new_flag);
|
||||
//flag(new_flag);
|
||||
}
|
||||
this->tot_len(total_sz);
|
||||
this->head_len(my_sz / sizeof(uint32_t));
|
||||
tot_len(total_sz);
|
||||
head_len(my_sz / sizeof(uint32_t));
|
||||
|
||||
memcpy(buffer, &_ip, sizeof(_ip));
|
||||
|
||||
|
||||
10
src/llc.cpp
10
src/llc.cpp
@@ -33,14 +33,18 @@ namespace Tins {
|
||||
const uint8_t LLC::GLOBAL_DSAP_ADDR = 0xFF;
|
||||
const uint8_t LLC::NULL_ADDR = 0x00;
|
||||
|
||||
LLC::LLC(PDU *child) : PDU(0xff, child), _type(LLC::INFORMATION) {
|
||||
LLC::LLC(PDU *child)
|
||||
: PDU(child), _type(LLC::INFORMATION)
|
||||
{
|
||||
memset(&_header, 0, sizeof(llchdr));
|
||||
control_field_length = 2;
|
||||
memset(&control_field, 0, sizeof(control_field));
|
||||
information_field_length = 0;
|
||||
}
|
||||
|
||||
LLC::LLC(uint8_t dsap, uint8_t ssap, PDU *child) : PDU(0xff, child), _type(LLC::INFORMATION) {
|
||||
LLC::LLC(uint8_t dsap, uint8_t ssap, PDU *child)
|
||||
: PDU(child), _type(LLC::INFORMATION)
|
||||
{
|
||||
_header.dsap = dsap;
|
||||
_header.ssap = ssap;
|
||||
control_field_length = 2;
|
||||
@@ -48,7 +52,7 @@ LLC::LLC(uint8_t dsap, uint8_t ssap, PDU *child) : PDU(0xff, child), _type(LLC::
|
||||
information_field_length = 0;
|
||||
}
|
||||
|
||||
LLC::LLC(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) {
|
||||
LLC::LLC(const uint8_t *buffer, uint32_t total_sz) {
|
||||
// header + 1 info byte
|
||||
if(total_sz < sizeof(_header) + 1)
|
||||
throw std::runtime_error("Not enough size for a LLC header in the buffer.");
|
||||
|
||||
@@ -26,17 +26,15 @@
|
||||
|
||||
namespace Tins {
|
||||
|
||||
PDU::PDU(uint32_t flag, PDU *next_pdu) : _flag(flag), _inner_pdu(next_pdu) {
|
||||
PDU::PDU(PDU *next_pdu) : _inner_pdu(next_pdu) {
|
||||
|
||||
}
|
||||
|
||||
PDU::PDU(const PDU &other) : _inner_pdu(0) {
|
||||
_flag = other.flag();
|
||||
copy_inner_pdu(other);
|
||||
}
|
||||
|
||||
PDU &PDU::operator=(const PDU &other) {
|
||||
_flag = other.flag();
|
||||
copy_inner_pdu(other);
|
||||
return *this;
|
||||
}
|
||||
@@ -68,10 +66,6 @@ PDU *PDU::recv_response(PacketSender &) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void PDU::flag(uint32_t new_flag) {
|
||||
_flag = new_flag;
|
||||
}
|
||||
|
||||
void PDU::inner_pdu(PDU *next_pdu) {
|
||||
delete _inner_pdu;
|
||||
_inner_pdu = next_pdu;
|
||||
|
||||
@@ -33,14 +33,13 @@
|
||||
|
||||
|
||||
Tins::RadioTap::RadioTap(const NetworkInterface &iface, PDU *child)
|
||||
: PDU(0xff, child), _iface(iface), _options_size(0)
|
||||
: PDU(child), _iface(iface), _options_size(0)
|
||||
{
|
||||
std::memset(&_radio, 0, sizeof(_radio));
|
||||
init();
|
||||
}
|
||||
|
||||
Tins::RadioTap::RadioTap(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(0xff)
|
||||
{
|
||||
static const std::string msg("Not enough size for an RadioTap header in the buffer.");
|
||||
if(total_sz < sizeof(_radio))
|
||||
|
||||
@@ -26,11 +26,16 @@
|
||||
|
||||
namespace Tins {
|
||||
RawPDU::RawPDU(const uint8_t *pload, uint32_t size)
|
||||
: PDU(255), _payload(pload, pload + size)
|
||||
: _payload(pload, pload + size)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
RawPDU::RawPDU(const std::string &data)
|
||||
: _payload(data.begin(), data.end()) {
|
||||
|
||||
}
|
||||
|
||||
uint32_t RawPDU::header_size() const {
|
||||
return _payload.size();
|
||||
}
|
||||
|
||||
@@ -32,13 +32,15 @@
|
||||
#include "eapol.h"
|
||||
|
||||
|
||||
Tins::SNAP::SNAP(PDU *child) : PDU(0xff, child) {
|
||||
Tins::SNAP::SNAP(PDU *child) : PDU(child)
|
||||
{
|
||||
std::memset(&_snap, 0, sizeof(_snap));
|
||||
_snap.dsap = _snap.ssap = 0xaa;
|
||||
_snap.control = 3;
|
||||
}
|
||||
|
||||
Tins::SNAP::SNAP(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) {
|
||||
Tins::SNAP::SNAP(const uint8_t *buffer, uint32_t total_sz)
|
||||
{
|
||||
if(total_sz < sizeof(_snap))
|
||||
throw std::runtime_error("Not enough size for a SNAP header in the buffer.");
|
||||
std::memcpy(&_snap, buffer, sizeof(_snap));
|
||||
|
||||
@@ -44,7 +44,7 @@ void BaseSniffer::init(pcap_t *phandle, const std::string &filter,
|
||||
handle = phandle;
|
||||
mask = if_mask;
|
||||
|
||||
wired = (pcap_datalink(handle) != DLT_IEEE802_11_RADIO); //better plx
|
||||
iface_type = pcap_datalink(handle);
|
||||
actual_filter.bf_insns = 0;
|
||||
if(!filter.empty() && !set_filter(filter))
|
||||
throw runtime_error("Invalid filter");
|
||||
@@ -61,10 +61,12 @@ PDU *BaseSniffer::next_packet() {
|
||||
const u_char *content = pcap_next(handle, &header);
|
||||
if(content) {
|
||||
try {
|
||||
if(wired)
|
||||
if(iface_type == DLT_EN10MB)
|
||||
ret = new EthernetII((const uint8_t*)content, header.caplen);
|
||||
else
|
||||
else if(iface_type == DLT_IEEE802_11_RADIO)
|
||||
ret = new RadioTap((const uint8_t*)content, header.caplen);
|
||||
else if(iface_type == DLT_LOOP)
|
||||
ret = new Tins::Loopback((const uint8_t*)content, header.caplen);
|
||||
}
|
||||
catch(...) {
|
||||
ret = 0;
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Tins {
|
||||
const uint16_t TCP::DEFAULT_WINDOW = 32678;
|
||||
|
||||
TCP::TCP(uint16_t dport, uint16_t sport)
|
||||
: PDU(Constants::IP::PROTO_TCP), _options_size(0), _total_options_size(0)
|
||||
: _options_size(0), _total_options_size(0)
|
||||
{
|
||||
std::memset(&_tcp, 0, sizeof(tcphdr));
|
||||
this->dport(dport);
|
||||
@@ -42,7 +42,6 @@ TCP::TCP(uint16_t dport, uint16_t sport)
|
||||
}
|
||||
|
||||
TCP::TCP(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(Constants::IP::PROTO_TCP)
|
||||
{
|
||||
if(total_sz < sizeof(tcphdr))
|
||||
throw std::runtime_error("Not enough size for an TCP header in the buffer.");
|
||||
|
||||
@@ -42,8 +42,8 @@ TCPStream::StreamInfo::StreamInfo(IPv4Address client,
|
||||
|
||||
|
||||
TCPStream::TCPStream(IP *ip, TCP *tcp, uint64_t identifier)
|
||||
: client_seq(tcp->seq()), info(ip->src_addr(), ip->dst_addr(),
|
||||
tcp->sport(), tcp->dport()), identifier(identifier),
|
||||
: client_seq(tcp->seq()), server_seq(0), info(ip->src_addr(),
|
||||
ip->dst_addr(), tcp->sport(), tcp->dport()), identifier(identifier),
|
||||
syn_ack_sent(false), fin_sent(false)
|
||||
{
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#include "rawpdu.h"
|
||||
|
||||
Tins::UDP::UDP(uint16_t dport, uint16_t sport, PDU *child)
|
||||
: PDU(Constants::IP::PROTO_UDP, child)
|
||||
: PDU(child)
|
||||
{
|
||||
this->dport(dport);
|
||||
this->sport(sport);
|
||||
@@ -38,7 +38,6 @@ Tins::UDP::UDP(uint16_t dport, uint16_t sport, PDU *child)
|
||||
}
|
||||
|
||||
Tins::UDP::UDP(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(Constants::IP::PROTO_UDP)
|
||||
{
|
||||
if(total_sz < sizeof(udphdr))
|
||||
throw std::runtime_error("Not enough size for an UDP header in the buffer.");
|
||||
|
||||
Reference in New Issue
Block a user