mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Done some minor fixes.
This commit is contained in:
@@ -23,10 +23,11 @@
|
||||
#define TINS_BOOTP_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdint.h>
|
||||
#include <algorithm>
|
||||
#include "pdu.h"
|
||||
#include "utils.h"
|
||||
#include "ipaddress.h"
|
||||
#include "hwaddress.h"
|
||||
|
||||
|
||||
namespace Tins {
|
||||
@@ -35,8 +36,8 @@ namespace Tins {
|
||||
* \brief Class representing a BootP packet.
|
||||
*/
|
||||
class BootP : public PDU {
|
||||
|
||||
public:
|
||||
typedef HWAddress<16> chaddr_type;
|
||||
/**
|
||||
* \brief This PDU's flag.
|
||||
*/
|
||||
@@ -156,7 +157,8 @@ namespace Tins {
|
||||
* \brief Getter for the chaddr field.
|
||||
* \return The chddr field for this BootP PDU.
|
||||
*/
|
||||
const uint8_t *chaddr() const { return _bootp.chaddr; }
|
||||
//const uint8_t *chaddr() const { return _bootp.chaddr; }
|
||||
chaddr_type chaddr() const { return _bootp.chaddr; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the sname field.
|
||||
@@ -260,7 +262,17 @@ namespace Tins {
|
||||
* The new_chaddr pointer must be at least BOOTP::hlen() bytes long.
|
||||
* \param new_chaddr The chaddr to be set.
|
||||
*/
|
||||
void chaddr(const uint8_t *new_chaddr);
|
||||
template<size_t n>
|
||||
void chaddr(const HWAddress<n> &new_chaddr) {
|
||||
// Copy the new addr
|
||||
uint8_t *end = std::copy(
|
||||
new_chaddr.begin(),
|
||||
new_chaddr.begin() + std::min(n, sizeof(_bootp.chaddr)),
|
||||
_bootp.chaddr
|
||||
);
|
||||
// Fill what's left with zeros
|
||||
std::fill(end, _bootp.chaddr + chaddr_type::address_size, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Setter for the sname field.
|
||||
@@ -288,11 +300,11 @@ namespace Tins {
|
||||
PDUType pdu_type() const { return PDU::BOOTP; }
|
||||
|
||||
/**
|
||||
* \brief Clones this PDU.
|
||||
*
|
||||
* \sa PDU::clone_pdu
|
||||
*/
|
||||
PDU *clone_pdu() const;
|
||||
PDU *clone_pdu() const {
|
||||
return do_clone_pdu<BootP>();
|
||||
}
|
||||
protected:
|
||||
void copy_bootp_fields(const BootP *other);
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
|
||||
@@ -182,23 +182,6 @@ namespace Tins {
|
||||
*/
|
||||
DHCP(const uint8_t *buffer, uint32_t total_sz);
|
||||
|
||||
/**
|
||||
* \brief Copy constructor.
|
||||
*/
|
||||
DHCP(const DHCP &other);
|
||||
|
||||
/**
|
||||
* \brief Copy assignment operator.
|
||||
*/
|
||||
DHCP &operator= (const DHCP &other);
|
||||
|
||||
/**
|
||||
* \brief DHCP destructor
|
||||
*
|
||||
* Releases the memory allocated for options.
|
||||
*/
|
||||
~DHCP();
|
||||
|
||||
/**
|
||||
* \brief Adds a new option to this DHCP PDU.
|
||||
*
|
||||
@@ -391,11 +374,11 @@ namespace Tins {
|
||||
uint32_t header_size() const;
|
||||
|
||||
/**
|
||||
* \brief Clones this PDU.
|
||||
*
|
||||
* \sa PDU::clone_pdu
|
||||
*/
|
||||
PDU *clone_pdu() const;
|
||||
PDU *clone_pdu() const {
|
||||
return do_clone_pdu<DHCP>();
|
||||
}
|
||||
private:
|
||||
static const uint32_t MAX_DHCP_SIZE;
|
||||
|
||||
|
||||
@@ -49,12 +49,7 @@ namespace Tins {
|
||||
/**
|
||||
* \brief Broadcast hardware address.
|
||||
*/
|
||||
static const uint8_t *BROADCAST;
|
||||
|
||||
/**
|
||||
* \brief Dot11 address size.
|
||||
*/
|
||||
static const uint32_t ADDR_SIZE = 6;
|
||||
static const address_type BROADCAST;
|
||||
|
||||
/**
|
||||
* \brief Enum for the different types of 802.11 frames.
|
||||
@@ -970,7 +965,7 @@ namespace Tins {
|
||||
*
|
||||
* \return The fourth address as a constant uint8_t pointer.
|
||||
*/
|
||||
const uint8_t* addr4() const { return this->_addr4; }
|
||||
const address_type &addr4() const { return this->_addr4; }
|
||||
|
||||
/**
|
||||
* \brief Setter for the second address.
|
||||
@@ -1005,7 +1000,7 @@ namespace Tins {
|
||||
*
|
||||
* \param new_addr4 const uint8_t array of 6 bytes containing the new fourth address.
|
||||
*/
|
||||
void addr4(const uint8_t* new_addr4);
|
||||
void addr4(const address_type &new_addr4);
|
||||
|
||||
/**
|
||||
* \brief Returns the 802.11 frame's header length.
|
||||
@@ -1271,11 +1266,12 @@ namespace Tins {
|
||||
|
||||
void copy_ext_header(const Dot11ManagementFrame *other);
|
||||
|
||||
uint32_t management_frame_size() { return sizeof(_ext_header) + (from_ds() && to_ds()) ? sizeof(_addr4) : 0; }
|
||||
uint32_t management_frame_size() {
|
||||
return sizeof(_ext_header) + (from_ds() && to_ds()) ? address_type::address_size : 0;
|
||||
}
|
||||
private:
|
||||
ExtendedHeader _ext_header;
|
||||
uint8_t _addr4[6];
|
||||
|
||||
address_type _addr4;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -55,6 +55,15 @@ public:
|
||||
convert(address, buffer);
|
||||
}
|
||||
|
||||
template<size_t i>
|
||||
HWAddress(const HWAddress<i> &rhs) {
|
||||
std::copy(
|
||||
rhs.begin(),
|
||||
rhs.begin() + std::min(i, n),
|
||||
begin()
|
||||
);
|
||||
}
|
||||
|
||||
HWAddress& operator=(const std::string &address) {
|
||||
convert(address, buffer);
|
||||
}
|
||||
|
||||
@@ -126,13 +126,13 @@ namespace Tins {
|
||||
* \brief Getter for this PDU's type flag identifier.
|
||||
* \return The type flag identifier.
|
||||
*/
|
||||
inline uint32_t flag() const { return _flag; }
|
||||
uint32_t flag() const { return _flag; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the inner PDU.
|
||||
* \return The current inner PDU. Might be 0.
|
||||
*/
|
||||
inline PDU *inner_pdu() const { return _inner_pdu; }
|
||||
PDU *inner_pdu() const { return _inner_pdu; }
|
||||
|
||||
/** \brief Sets the flag identifier.
|
||||
*/
|
||||
|
||||
@@ -1,16 +1,39 @@
|
||||
/*
|
||||
* libtins is a net packet wrapper library for crafting and
|
||||
* interpreting sniffed packets.
|
||||
*
|
||||
* Copyright (C) 2011 Nasel
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include "bootp.h"
|
||||
|
||||
|
||||
Tins::BootP::BootP() : PDU(255), _vend_size(64) {
|
||||
namespace Tins{
|
||||
BootP::BootP() : PDU(255), _vend_size(64) {
|
||||
_vend = new uint8_t[64];
|
||||
std::memset(&_bootp, 0, sizeof(bootphdr));
|
||||
std::memset(_vend, 0, 64);
|
||||
}
|
||||
|
||||
Tins::BootP::BootP(const uint8_t *buffer, uint32_t total_sz, uint32_t vend_field_size) : PDU(255), _vend(0), _vend_size(vend_field_size) {
|
||||
BootP::BootP(const uint8_t *buffer, uint32_t total_sz, uint32_t vend_field_size)
|
||||
: PDU(255), _vend(0), _vend_size(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.");
|
||||
std::memcpy(&_bootp, buffer, sizeof(bootphdr));
|
||||
@@ -18,99 +41,95 @@ Tins::BootP::BootP(const uint8_t *buffer, uint32_t total_sz, uint32_t vend_field
|
||||
total_sz -= sizeof(bootphdr);
|
||||
if(_vend_size) {
|
||||
_vend = new uint8_t[_vend_size];
|
||||
std::memcpy(_vend, buffer, _vend_size);
|
||||
std::copy(buffer, buffer + _vend_size, _vend);
|
||||
}
|
||||
// Maybe RawPDU on what is left on the buffer?...
|
||||
}
|
||||
|
||||
Tins::BootP::BootP(const BootP &other) : PDU(other) {
|
||||
BootP::BootP(const BootP &other) : PDU(other) {
|
||||
copy_bootp_fields(&other);
|
||||
}
|
||||
|
||||
Tins::BootP &Tins::BootP::operator= (const BootP &other) {
|
||||
BootP &BootP::operator= (const BootP &other) {
|
||||
copy_bootp_fields(&other);
|
||||
copy_inner_pdu(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Tins::BootP::~BootP() {
|
||||
BootP::~BootP() {
|
||||
delete[] _vend;
|
||||
}
|
||||
|
||||
uint32_t Tins::BootP::header_size() const {
|
||||
uint32_t BootP::header_size() const {
|
||||
return sizeof(bootphdr) + _vend_size;
|
||||
}
|
||||
|
||||
void Tins::BootP::opcode(uint8_t new_opcode) {
|
||||
void BootP::opcode(uint8_t new_opcode) {
|
||||
_bootp.opcode = new_opcode;
|
||||
}
|
||||
|
||||
void Tins::BootP::htype(uint8_t new_htype) {
|
||||
void BootP::htype(uint8_t new_htype) {
|
||||
_bootp.htype = new_htype;
|
||||
}
|
||||
|
||||
void Tins::BootP::hlen(uint8_t new_hlen) {
|
||||
void BootP::hlen(uint8_t new_hlen) {
|
||||
_bootp.hlen = new_hlen;
|
||||
}
|
||||
|
||||
void Tins::BootP::hops(uint8_t new_hops) {
|
||||
void BootP::hops(uint8_t new_hops) {
|
||||
_bootp.hops = new_hops;
|
||||
}
|
||||
|
||||
void Tins::BootP::xid(uint32_t new_xid) {
|
||||
void BootP::xid(uint32_t new_xid) {
|
||||
_bootp.xid = Utils::net_to_host_l(new_xid);
|
||||
}
|
||||
|
||||
void Tins::BootP::secs(uint16_t new_secs) {
|
||||
void BootP::secs(uint16_t new_secs) {
|
||||
_bootp.secs = Utils::net_to_host_s(new_secs);
|
||||
}
|
||||
|
||||
void Tins::BootP::padding(uint16_t new_padding) {
|
||||
void BootP::padding(uint16_t new_padding) {
|
||||
_bootp.padding = Utils::net_to_host_s(new_padding);
|
||||
}
|
||||
|
||||
void Tins::BootP::ciaddr(IPv4Address new_ciaddr) {
|
||||
void BootP::ciaddr(IPv4Address new_ciaddr) {
|
||||
_bootp.ciaddr = new_ciaddr;
|
||||
}
|
||||
|
||||
void Tins::BootP::yiaddr(IPv4Address new_yiaddr) {
|
||||
void BootP::yiaddr(IPv4Address new_yiaddr) {
|
||||
_bootp.yiaddr = new_yiaddr;
|
||||
}
|
||||
|
||||
void Tins::BootP::siaddr(IPv4Address new_siaddr) {
|
||||
void BootP::siaddr(IPv4Address new_siaddr) {
|
||||
_bootp.siaddr = new_siaddr;
|
||||
}
|
||||
|
||||
void Tins::BootP::giaddr(IPv4Address new_giaddr) {
|
||||
void BootP::giaddr(IPv4Address new_giaddr) {
|
||||
_bootp.giaddr = new_giaddr;
|
||||
}
|
||||
|
||||
void Tins::BootP::chaddr(const uint8_t *new_chaddr) {
|
||||
std::memcpy(_bootp.chaddr, new_chaddr, _bootp.hlen);
|
||||
}
|
||||
|
||||
void Tins::BootP::sname(const uint8_t *new_sname) {
|
||||
void BootP::sname(const uint8_t *new_sname) {
|
||||
std::memcpy(_bootp.sname, new_sname, sizeof(_bootp.sname));
|
||||
}
|
||||
|
||||
void Tins::BootP::file(const uint8_t *new_file) {
|
||||
void BootP::file(const uint8_t *new_file) {
|
||||
std::memcpy(_bootp.file, new_file, sizeof(_bootp.file));
|
||||
}
|
||||
|
||||
void Tins::BootP::vend(uint8_t *new_vend, uint32_t size) {
|
||||
void BootP::vend(uint8_t *new_vend, uint32_t size) {
|
||||
delete[] _vend;
|
||||
_vend_size = size;
|
||||
_vend = new uint8_t[size];
|
||||
std::memcpy(_vend, new_vend, size);
|
||||
std::copy(new_vend, new_vend + size, _vend);
|
||||
}
|
||||
|
||||
void Tins::BootP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
void BootP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
assert(total_sz >= sizeof(bootphdr) + _vend_size);
|
||||
std::memcpy(buffer, &_bootp, sizeof(bootphdr));
|
||||
std::memcpy(buffer + sizeof(bootphdr), _vend, _vend_size);
|
||||
//std::memcpy(buffer + sizeof(bootphdr), _vend, _vend_size);
|
||||
std::copy(_vend, _vend + _vend_size, buffer + sizeof(bootphdr));
|
||||
}
|
||||
|
||||
void Tins::BootP::copy_bootp_fields(const BootP *other) {
|
||||
void BootP::copy_bootp_fields(const BootP *other) {
|
||||
std::memcpy(&_bootp, &other->_bootp, sizeof(_bootp));
|
||||
_vend_size = other->_vend_size;
|
||||
if(_vend_size) {
|
||||
@@ -120,9 +139,4 @@ void Tins::BootP::copy_bootp_fields(const BootP *other) {
|
||||
else
|
||||
_vend = 0;
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::BootP::clone_pdu() const {
|
||||
BootP *new_pdu = new BootP();
|
||||
new_pdu->copy_bootp_fields(this);
|
||||
return new_pdu;
|
||||
}
|
||||
|
||||
33
src/dhcp.cpp
33
src/dhcp.cpp
@@ -61,7 +61,7 @@ DHCP::DHCP(const uint8_t *buffer, uint32_t total_sz)
|
||||
throw std::runtime_error("Not enough size for a DHCP header in the buffer.");
|
||||
}
|
||||
// If the END-OF-OPTIONS was not found...
|
||||
if(args[0] != END && args[0] != PAD) {
|
||||
if(args[0] != END && args[0] != PAD) {
|
||||
// Not enough size for this option
|
||||
if(total_sz < args[1])
|
||||
throw std::runtime_error("Not enough size for a DHCP header in the buffer.");
|
||||
@@ -75,31 +75,9 @@ DHCP::DHCP(const uint8_t *buffer, uint32_t total_sz)
|
||||
}
|
||||
}
|
||||
|
||||
DHCP::DHCP(const DHCP &other) : BootP(other) {
|
||||
copy_fields(&other);
|
||||
}
|
||||
|
||||
DHCP &DHCP::operator= (const DHCP &other) {
|
||||
copy_fields(&other);
|
||||
copy_inner_pdu(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
DHCP::~DHCP() {
|
||||
/*while(_options.size()) {
|
||||
delete[] _options.front().value;
|
||||
_options.pop_front();
|
||||
}*/
|
||||
}
|
||||
|
||||
DHCP::DHCPOption::DHCPOption(uint8_t opt, uint8_t len, const uint8_t *val)
|
||||
: option(opt), value(val, val ? (val + len) : val) {
|
||||
/*if(len) {
|
||||
value = new uint8_t[len];
|
||||
std::memcpy(value, val, len);
|
||||
}
|
||||
else
|
||||
value = 0;*/
|
||||
|
||||
}
|
||||
|
||||
bool DHCP::add_option(Options opt, uint8_t len, const uint8_t *val) {
|
||||
@@ -262,13 +240,6 @@ void DHCP::copy_fields(const DHCP *other) {
|
||||
_size = other->_size;
|
||||
for(options_type::const_iterator it = other->_options.begin(); it != other->_options.end(); ++it)
|
||||
_options.push_back(*it);
|
||||
//_options.push_back(DHCPOption(it->option, it->length, it->value));
|
||||
}
|
||||
|
||||
PDU *DHCP::clone_pdu() const {
|
||||
DHCP *new_pdu = new DHCP();
|
||||
new_pdu->copy_fields(this);
|
||||
return new_pdu;
|
||||
}
|
||||
|
||||
bool DHCP::generic_search(Options opt, std::list<uint32_t> *container) {
|
||||
|
||||
583
src/dot11.cpp
583
src/dot11.cpp
File diff suppressed because it is too large
Load Diff
@@ -93,6 +93,8 @@ uint32_t Tins::EthernetII::header_size() const {
|
||||
}
|
||||
|
||||
bool Tins::EthernetII::send(PacketSender* sender) {
|
||||
if(!_iface)
|
||||
throw std::runtime_error("Interface has not been set");
|
||||
struct sockaddr_ll addr;
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "dhcp.h"
|
||||
#include "utils.h"
|
||||
#include "ethernetII.h"
|
||||
#include "hwaddress.h"
|
||||
#include "ipaddress.h"
|
||||
|
||||
using namespace std;
|
||||
@@ -15,14 +16,15 @@ using namespace Tins;
|
||||
class DHCPTest : public testing::Test {
|
||||
public:
|
||||
static const uint8_t expected_packet[];
|
||||
static const uint8_t chaddr[], sname[], file[];
|
||||
static const BootP::chaddr_type chaddr;
|
||||
static const uint8_t sname[], file[];
|
||||
static const IPv4Address addr;
|
||||
|
||||
void test_equals(const DHCP &dhcp1, const DHCP &dhcp2);
|
||||
void test_option(const DHCP &dhcp, DHCP::Options opt, uint32_t len = 0, uint8_t *value = 0);
|
||||
};
|
||||
|
||||
const uint8_t DHCPTest::chaddr[] = "\x16\xab\x54\x12\xfa\xca\x56\x7f\x1b\x65\x11\xfa\xda\xab\x19\x18";
|
||||
const BootP::chaddr_type DHCPTest::chaddr("16:ab:54:12:fa:ca:56:7f:1b:65:11:fa:da:ab:19:18");
|
||||
const uint8_t DHCPTest::sname[] = "\x16\xab\x54\x12\xfa\xca\x56\x7f\x1b\x65\x11\xfa\xda\xbb\x19\x18"
|
||||
"\x16\xab\x54\x12\xfa\xca\x56\x7f\x1b\x65\x11\xfa\xda\xcb\x19\x18"
|
||||
"\x16\xab\x54\x12\xfa\xca\x56\x7f\x1b\x65\x11\xfa\xda\xeb\x19\x18"
|
||||
@@ -150,7 +152,12 @@ TEST_F(DHCPTest, Giaddr) {
|
||||
TEST_F(DHCPTest, Chaddr) {
|
||||
DHCP dhcp;
|
||||
dhcp.chaddr(chaddr);
|
||||
EXPECT_TRUE(memcmp(dhcp.chaddr(), chaddr, dhcp.hlen()) == 0);
|
||||
EXPECT_EQ(dhcp.chaddr(), chaddr);
|
||||
|
||||
HWAddress<4> hwaddr("31:33:70:00");
|
||||
dhcp.chaddr(hwaddr);
|
||||
HWAddress<4> copied(dhcp.chaddr());
|
||||
EXPECT_EQ(copied, hwaddr);
|
||||
}
|
||||
|
||||
TEST_F(DHCPTest, Sname) {
|
||||
@@ -267,7 +274,7 @@ void DHCPTest::test_equals(const DHCP &dhcp1, const DHCP &dhcp2) {
|
||||
EXPECT_EQ(dhcp1.yiaddr(), dhcp2.yiaddr());
|
||||
EXPECT_EQ(dhcp1.siaddr(), dhcp2.siaddr());
|
||||
EXPECT_EQ(dhcp1.giaddr(), dhcp2.giaddr());
|
||||
EXPECT_TRUE(memcmp(dhcp1.chaddr(), dhcp2.chaddr(), dhcp1.hlen()) == 0);
|
||||
EXPECT_EQ(dhcp1.chaddr(), dhcp2.chaddr());
|
||||
EXPECT_TRUE(memcmp(dhcp1.sname(), dhcp2.sname(), 64) == 0);
|
||||
EXPECT_TRUE(memcmp(dhcp1.file(), dhcp2.file(), 128) == 0);
|
||||
const std::list<DHCP::DHCPOption> options1(dhcp1.options());
|
||||
|
||||
Reference in New Issue
Block a user