1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-29 04:54:28 +01:00

Done some minor fixes.

This commit is contained in:
Matias Fontanini
2012-08-09 23:39:32 -03:00
parent 91af0f9cc2
commit 55be59ee15
10 changed files with 398 additions and 401 deletions

View File

@@ -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;
}

View File

@@ -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) {

File diff suppressed because it is too large Load Diff

View File

@@ -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));