mirror of
https://github.com/mfontanini/libtins
synced 2026-01-26 03:51:35 +01:00
Code cleanup and use same syntax on the entire project
Initial code cleanup More code cleanup Cleanup more code Cleanup Dot11 code Fix OSX build issue Cleanup examples Fix ref and pointer declaration syntax Fix braces
This commit is contained in:
@@ -28,7 +28,6 @@
|
||||
*/
|
||||
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
#include "macros.h"
|
||||
#ifndef _WIN32
|
||||
@@ -53,6 +52,8 @@
|
||||
#include "exceptions.h"
|
||||
#include "memory_helpers.h"
|
||||
|
||||
using std::equal;
|
||||
|
||||
using Tins::Memory::InputMemoryStream;
|
||||
using Tins::Memory::OutputMemoryStream;
|
||||
|
||||
@@ -60,18 +61,16 @@ namespace Tins {
|
||||
|
||||
const EthernetII::address_type EthernetII::BROADCAST("ff:ff:ff:ff:ff:ff");
|
||||
|
||||
EthernetII::EthernetII(const address_type &dst_hw_addr,
|
||||
const address_type &src_hw_addr)
|
||||
: _eth()
|
||||
{
|
||||
EthernetII::EthernetII(const address_type& dst_hw_addr,
|
||||
const address_type& src_hw_addr)
|
||||
: header_() {
|
||||
dst_addr(dst_hw_addr);
|
||||
src_addr(src_hw_addr);
|
||||
}
|
||||
|
||||
EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
|
||||
{
|
||||
EthernetII::EthernetII(const uint8_t* buffer, uint32_t total_sz) {
|
||||
InputMemoryStream stream(buffer, total_sz);
|
||||
stream.read(_eth);
|
||||
stream.read(header_);
|
||||
// If there's any size left
|
||||
if (stream) {
|
||||
inner_pdu(
|
||||
@@ -82,28 +81,26 @@ EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void EthernetII::dst_addr(const address_type &new_dst_addr) {
|
||||
new_dst_addr.copy(_eth.dst_mac);
|
||||
void EthernetII::dst_addr(const address_type& new_dst_addr) {
|
||||
new_dst_addr.copy(header_.dst_mac);
|
||||
}
|
||||
|
||||
void EthernetII::src_addr(const address_type &new_src_addr) {
|
||||
new_src_addr.copy(_eth.src_mac);
|
||||
void EthernetII::src_addr(const address_type& new_src_addr) {
|
||||
new_src_addr.copy(header_.src_mac);
|
||||
}
|
||||
|
||||
void EthernetII::payload_type(uint16_t new_payload_type) {
|
||||
this->_eth.payload_type = Endian::host_to_be(new_payload_type);
|
||||
header_.payload_type = Endian::host_to_be(new_payload_type);
|
||||
}
|
||||
|
||||
uint32_t EthernetII::header_size() const {
|
||||
|
||||
return sizeof(ethhdr);
|
||||
return sizeof(header_);
|
||||
}
|
||||
|
||||
uint32_t EthernetII::trailer_size() const {
|
||||
int32_t padding = 60 - sizeof(ethhdr); // EthernetII min size is 60, padding is sometimes needed
|
||||
int32_t padding = 60 - sizeof(header_); // EthernetII min size is 60, padding is sometimes needed
|
||||
if (inner_pdu()) {
|
||||
padding -= inner_pdu()->size();
|
||||
padding = std::max(0, padding);
|
||||
@@ -111,9 +108,10 @@ uint32_t EthernetII::trailer_size() const {
|
||||
return padding;
|
||||
}
|
||||
|
||||
void EthernetII::send(PacketSender &sender, const NetworkInterface &iface) {
|
||||
if(!iface)
|
||||
void EthernetII::send(PacketSender& sender, const NetworkInterface& iface) {
|
||||
if (!iface) {
|
||||
throw invalid_interface();
|
||||
}
|
||||
#if defined(HAVE_PACKET_SENDER_PCAP_SENDPACKET) || defined(BSD) || defined(__FreeBSD_kernel__)
|
||||
// Sending using pcap_sendpacket/BSD bpf packet mode is the same here
|
||||
sender.send_l2(*this, 0, 0, iface);
|
||||
@@ -130,27 +128,30 @@ void EthernetII::send(PacketSender &sender, const NetworkInterface &iface) {
|
||||
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
|
||||
addr.sll_halen = address_type::address_size;
|
||||
addr.sll_ifindex = iface.id();
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, address_type::address_size);
|
||||
memcpy(&(addr.sll_addr), header_.dst_mac, address_type::address_size);
|
||||
|
||||
sender.send_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
#endif
|
||||
}
|
||||
|
||||
bool EthernetII::matches_response(const uint8_t *ptr, uint32_t total_sz) const {
|
||||
if(total_sz < sizeof(ethhdr))
|
||||
bool EthernetII::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
|
||||
if (total_sz < sizeof(header_)) {
|
||||
return false;
|
||||
}
|
||||
const size_t addr_sz = address_type::address_size;
|
||||
const ethhdr *eth_ptr = (const ethhdr*)ptr;
|
||||
if(std::equal(_eth.src_mac, _eth.src_mac + addr_sz, eth_ptr->dst_mac)) {
|
||||
if(std::equal(_eth.src_mac, _eth.src_mac + addr_sz, eth_ptr->dst_mac) || !dst_addr().is_unicast())
|
||||
{
|
||||
return (inner_pdu()) ? inner_pdu()->matches_response(ptr + sizeof(_eth), total_sz - sizeof(_eth)) : true;
|
||||
const ethernet_header* eth_ptr = (const ethernet_header*)ptr;
|
||||
if (equal(header_.src_mac, header_.src_mac + addr_sz, eth_ptr->dst_mac)) {
|
||||
if (equal(header_.src_mac, header_.src_mac + addr_sz, eth_ptr->dst_mac) ||
|
||||
!dst_addr().is_unicast()) {
|
||||
return inner_pdu() ?
|
||||
inner_pdu()->matches_response(ptr + sizeof(header_), total_sz - sizeof(header_)) :
|
||||
true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
void EthernetII::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
if (inner_pdu()) {
|
||||
Constants::Ethernet::e flag;
|
||||
@@ -168,7 +169,7 @@ void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
|
||||
payload_type(static_cast<uint16_t>(flag));
|
||||
}
|
||||
}
|
||||
stream.write(_eth);
|
||||
stream.write(header_);
|
||||
const uint32_t trailer = trailer_size();
|
||||
if (trailer) {
|
||||
if (inner_pdu()) {
|
||||
@@ -180,7 +181,7 @@ void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
PDU *EthernetII::recv_response(PacketSender &sender, const NetworkInterface &iface) {
|
||||
PDU* EthernetII::recv_response(PacketSender& sender, const NetworkInterface& iface) {
|
||||
#if !defined(BSD) && !defined(__FreeBSD_kernel__)
|
||||
struct sockaddr_ll addr;
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
@@ -189,7 +190,7 @@ PDU *EthernetII::recv_response(PacketSender &sender, const NetworkInterface &ifa
|
||||
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
|
||||
addr.sll_halen = address_type::address_size;
|
||||
addr.sll_ifindex = iface.id();
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, address_type::address_size);
|
||||
memcpy(&(addr.sll_addr), header_.dst_mac, address_type::address_size);
|
||||
|
||||
return sender.recv_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
#else
|
||||
|
||||
Reference in New Issue
Block a user