1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Fixed some bugs, added some tests.

This commit is contained in:
Matias Fontanini
2012-08-02 21:21:10 -03:00
parent 383deb8641
commit c4a92d2b96
10 changed files with 223 additions and 152 deletions

View File

@@ -67,7 +67,7 @@ namespace Tins {
* *
* \param other The object which will be copied. * \param other The object which will be copied.
*/ */
ARP(const ARP &other); //ARP(const ARP &other);
/* Getters */ /* Getters */
/** /**
@@ -75,63 +75,63 @@ namespace Tins {
* *
* \return Returns the sender's hardware address in an uint8_t*. * \return Returns the sender's hardware address in an uint8_t*.
*/ */
inline const uint8_t* sender_hw_addr() { return this->_arp.ar_sha; } const uint8_t* sender_hw_addr() const { return this->_arp.ar_sha; }
/** /**
* \brief Getter for the sender's IP address. * \brief Getter for the sender's IP address.
* *
* \return Returns the sender's IP address in an uint32_t. * \return Returns the sender's IP address in an uint32_t.
*/ */
inline IPv4Address sender_ip_addr() { return Utils::net_to_host_l(this->_arp.ar_sip); } IPv4Address sender_ip_addr() const { return Utils::net_to_host_l(this->_arp.ar_sip); }
/** /**
* \brief Getter for the target's hardware address. * \brief Getter for the target's hardware address.
* *
* \return Returns the target's hardware address in an uint8_t*. * \return Returns the target's hardware address in an uint8_t*.
*/ */
inline const uint8_t* target_hw_addr() { return this->_arp.ar_tha; } const uint8_t* target_hw_addr() const { return this->_arp.ar_tha; }
/** /**
* \brief Getter for the target's IP address. * \brief Getter for the target's IP address.
* *
* \return Returns the target's IP address in an uint32_t. * \return Returns the target's IP address in an uint32_t.
*/ */
inline IPv4Address target_ip_addr() { return Utils::net_to_host_l(this->_arp.ar_tip); } IPv4Address target_ip_addr() const { return Utils::net_to_host_l(this->_arp.ar_tip); }
/** /**
* \brief Getter for the hardware address format. * \brief Getter for the hardware address format.
* *
* \return Returns the hardware address' format in an uint16_t. * \return Returns the hardware address' format in an uint16_t.
*/ */
inline uint16_t hw_addr_format() { return Utils::net_to_host_s(this->_arp.ar_hrd); } uint16_t hw_addr_format() const { return Utils::net_to_host_s(this->_arp.ar_hrd); }
/** /**
* \brief Getter for the protocol address format. * \brief Getter for the protocol address format.
* *
* \return Returns the protocol address' format in an uint16_t. * \return Returns the protocol address' format in an uint16_t.
*/ */
inline uint16_t prot_addr_format() { return Utils::net_to_host_s(this->_arp.ar_pro); } uint16_t prot_addr_format() const { return Utils::net_to_host_s(this->_arp.ar_pro); }
/** /**
* \brief Getter for the hardware address length. * \brief Getter for the hardware address length.
* *
* \return Returns the hardware address' length in an uint8_t. * \return Returns the hardware address' length in an uint8_t.
*/ */
inline uint8_t hw_addr_length() { return this->_arp.ar_hln; } uint8_t hw_addr_length() const { return this->_arp.ar_hln; }
/** /**
* \brief Getter for the protocol address length. * \brief Getter for the protocol address length.
* *
* \return Returns the protocol address' length in an uint8_t. * \return Returns the protocol address' length in an uint8_t.
*/ */
inline uint8_t prot_addr_length() { return this->_arp.ar_pln; } uint8_t prot_addr_length() const { return this->_arp.ar_pln; }
/** /**
* \brief Getter for the ARP opcode. * \brief Getter for the ARP opcode.
* *
* \return Returns the ARP opcode in an uint16_t. * \return Returns the ARP opcode in an uint16_t.
*/ */
inline uint16_t opcode() { return Utils::net_to_host_s(this->_arp.ar_op); } uint16_t opcode() const { return Utils::net_to_host_s(this->_arp.ar_op); }
/** \brief Getter for the header size. /** \brief Getter for the header size.
* \return Returns the ARP header size. * \return Returns the ARP header size.

View File

@@ -24,6 +24,7 @@
#include <list> #include <list>
#include <vector>
#include <string> #include <string>
#include "bootp.h" #include "bootp.h"
@@ -131,19 +132,6 @@ namespace Tins {
* \brief DHCP options struct. * \brief DHCP options struct.
*/ */
struct DHCPOption { struct DHCPOption {
/**
* \brief The option number.
*/
uint8_t option;
/**
* \brief The value's length in bytes.
*/
uint8_t length;
/**
* \brief The option's value.
*/
uint8_t *value;
/** /**
* \brief Creates an instance of DHCPOption. * \brief Creates an instance of DHCPOption.
* *
@@ -154,8 +142,24 @@ namespace Tins {
* \param val The option's value. * \param val The option's value.
*/ */
DHCPOption(uint8_t opt, uint8_t len, const uint8_t *val); DHCPOption(uint8_t opt, uint8_t len, const uint8_t *val);
/**
* \brief The option number.
*/
uint8_t option;
/**
* \brief The value's length in bytes.
*/
//uint8_t length;
/**
* \brief The option's value.
*/
//uint8_t *value;
std::vector<uint8_t> value;
}; };
typedef std::list<DHCPOption> options_type;
/** /**
* \brief Creates an instance of DHCP. * \brief Creates an instance of DHCP.
* *
@@ -366,7 +370,7 @@ namespace Tins {
/** \brief Getter for the options list. /** \brief Getter for the options list.
* \return The option list. * \return The option list.
*/ */
const std::list<DHCPOption> options() const { return _options; } const options_type options() const { return _options; }
/** /**
* \brief Getter for the PDU's type. * \brief Getter for the PDU's type.
@@ -395,8 +399,8 @@ namespace Tins {
template<class T> bool generic_search(Options opt, T *value) { template<class T> bool generic_search(Options opt, T *value) {
const DHCPOption *option = search_option(opt); const DHCPOption *option = search_option(opt);
if(option && option->length == sizeof(T)) { if(option && option->value.size() == sizeof(T)) {
*value = *(T*)option->value; *value = *(T*)&option->value[0];
return true; return true;
} }
return false; return false;
@@ -408,7 +412,7 @@ namespace Tins {
uint8_t *serialize_list(const std::list<uint32_t> &int_list, uint32_t &sz); uint8_t *serialize_list(const std::list<uint32_t> &int_list, uint32_t &sz);
std::list<DHCPOption> _options; options_type _options;
uint32_t _size; uint32_t _size;
}; };
}; };

View File

@@ -31,14 +31,18 @@
#include "constants.h" #include "constants.h"
using namespace std; using std::string;
using std::runtime_error;
namespace Tins {
Tins::ARP::ARP(IPv4Address target_ip, IPv4Address sender_ip, ARP::ARP(IPv4Address target_ip, IPv4Address sender_ip,
const uint8_t *target_hw, const uint8_t *sender_hw) : PDU(0x0608) { const uint8_t *target_hw, const uint8_t *sender_hw)
: PDU(0x0608)
{
memset(&_arp, 0, sizeof(arphdr)); memset(&_arp, 0, sizeof(arphdr));
hw_addr_format((uint16_t)Tins::Constants::ARP::ETHER); hw_addr_format((uint16_t)Constants::ARP::ETHER);
prot_addr_format((uint16_t)Tins::Constants::Ethernet::IP); prot_addr_format((uint16_t)Constants::Ethernet::IP);
hw_addr_length(EthernetII::ADDR_SIZE); hw_addr_length(EthernetII::ADDR_SIZE);
prot_addr_length(IP::ADDR_SIZE); prot_addr_length(IP::ADDR_SIZE);
sender_ip_addr(sender_ip); sender_ip_addr(sender_ip);
@@ -49,56 +53,58 @@ Tins::ARP::ARP(IPv4Address target_ip, IPv4Address sender_ip,
target_hw_addr(target_hw); target_hw_addr(target_hw);
} }
Tins::ARP::ARP(const uint8_t *buffer, uint32_t total_sz) : PDU(Utils::net_to_host_s(Constants::Ethernet::ARP)) { ARP::ARP(const uint8_t *buffer, uint32_t total_sz)
: PDU(Utils::net_to_host_s(Constants::Ethernet::ARP))
{
if(total_sz < sizeof(arphdr)) if(total_sz < sizeof(arphdr))
throw std::runtime_error("Not enough size for an ARP header in the buffer."); throw runtime_error("Not enough size for an ARP header in the buffer.");
memcpy(&_arp, buffer, sizeof(arphdr)); memcpy(&_arp, buffer, sizeof(arphdr));
total_sz -= sizeof(arphdr); total_sz -= sizeof(arphdr);
if(total_sz) if(total_sz)
inner_pdu(new RawPDU(buffer + sizeof(arphdr), total_sz)); inner_pdu(new RawPDU(buffer + sizeof(arphdr), total_sz));
} }
Tins::ARP::ARP(const ARP &other) : PDU(other) { void ARP::sender_hw_addr(const uint8_t* new_snd_hw_addr) {
copy_fields(&other); //Should this use hardware address' length?
memcpy(this->_arp.ar_sha, new_snd_hw_addr, 6);
} }
void Tins::ARP::sender_hw_addr(const uint8_t* new_snd_hw_addr) { void ARP::sender_ip_addr(IPv4Address new_snd_ip_addr) {
memcpy(this->_arp.ar_sha, new_snd_hw_addr, 6); //Should this use hardware address' length?
}
void Tins::ARP::sender_ip_addr(IPv4Address new_snd_ip_addr) {
this->_arp.ar_sip = new_snd_ip_addr; this->_arp.ar_sip = new_snd_ip_addr;
} }
void Tins::ARP::target_hw_addr(const uint8_t* new_tgt_hw_addr) { void ARP::target_hw_addr(const uint8_t* new_tgt_hw_addr) {
memcpy(this->_arp.ar_tha, new_tgt_hw_addr, 6); //Should this use hardware address' length? //Should this use hardware address' length?
memcpy(this->_arp.ar_tha, new_tgt_hw_addr, 6);
} }
void Tins::ARP::target_ip_addr(IPv4Address new_tgt_ip_addr) { void ARP::target_ip_addr(IPv4Address new_tgt_ip_addr) {
this->_arp.ar_tip = new_tgt_ip_addr; this->_arp.ar_tip = new_tgt_ip_addr;
} }
void Tins::ARP::hw_addr_format(uint16_t new_hw_addr_fmt) { void ARP::hw_addr_format(uint16_t new_hw_addr_fmt) {
this->_arp.ar_hrd = Utils::net_to_host_s(new_hw_addr_fmt); this->_arp.ar_hrd = Utils::net_to_host_s(new_hw_addr_fmt);
} }
void Tins::ARP::prot_addr_format(uint16_t new_prot_addr_fmt) { void ARP::prot_addr_format(uint16_t new_prot_addr_fmt) {
this->_arp.ar_pro = Utils::net_to_host_s(new_prot_addr_fmt); this->_arp.ar_pro = Utils::net_to_host_s(new_prot_addr_fmt);
} }
void Tins::ARP::hw_addr_length(uint8_t new_hw_addr_len) { void ARP::hw_addr_length(uint8_t new_hw_addr_len) {
this->_arp.ar_hln = new_hw_addr_len; this->_arp.ar_hln = new_hw_addr_len;
} }
void Tins::ARP::prot_addr_length(uint8_t new_prot_addr_len) { void ARP::prot_addr_length(uint8_t new_prot_addr_len) {
this->_arp.ar_pln = new_prot_addr_len; this->_arp.ar_pln = new_prot_addr_len;
} }
void Tins::ARP::opcode(Flags new_opcode) { void ARP::opcode(Flags new_opcode) {
this->_arp.ar_op = Utils::net_to_host_s(new_opcode); this->_arp.ar_op = Utils::net_to_host_s(new_opcode);
} }
void Tins::ARP::set_arp_request(const std::string& ip_tgt, const std::string& ip_snd, const uint8_t* hw_snd) { void ARP::set_arp_request(const string& ip_tgt, const string& ip_snd,
const uint8_t* hw_snd)
{
this->target_ip_addr(ip_tgt); this->target_ip_addr(ip_tgt);
this->sender_ip_addr(ip_snd); this->sender_ip_addr(ip_snd);
if (hw_snd) if (hw_snd)
@@ -106,11 +112,9 @@ void Tins::ARP::set_arp_request(const std::string& ip_tgt, const std::string& ip
this->opcode(REQUEST); this->opcode(REQUEST);
} }
void Tins::ARP::set_arp_reply(const std::string& ip_tgt, void ARP::set_arp_reply(const std::string& ip_tgt, const std::string& ip_snd,
const std::string& ip_snd, const uint8_t* hw_tgt, const uint8_t* hw_snd)
const uint8_t* hw_tgt, {
const uint8_t* hw_snd) {
this->target_ip_addr(ip_tgt); this->target_ip_addr(ip_tgt);
this->sender_ip_addr(ip_snd); this->sender_ip_addr(ip_snd);
this->sender_hw_addr(hw_snd); this->sender_hw_addr(hw_snd);
@@ -119,28 +123,29 @@ void Tins::ARP::set_arp_reply(const std::string& ip_tgt,
} }
uint32_t Tins::ARP::header_size() const { uint32_t ARP::header_size() const {
return sizeof(arphdr); return sizeof(arphdr);
} }
void Tins::ARP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) { void ARP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) {
assert(total_sz >= sizeof(arphdr)); assert(total_sz >= sizeof(arphdr));
memcpy(buffer, &_arp, sizeof(arphdr)); memcpy(buffer, &_arp, sizeof(arphdr));
} }
bool Tins::ARP::matches_response(uint8_t *ptr, uint32_t total_sz) { bool ARP::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(arphdr)) if(total_sz < sizeof(arphdr))
return false; return false;
arphdr *arp_ptr = (arphdr*)ptr; arphdr *arp_ptr = (arphdr*)ptr;
return arp_ptr->ar_sip == _arp.ar_tip && arp_ptr->ar_tip == _arp.ar_sip; return arp_ptr->ar_sip == _arp.ar_tip && arp_ptr->ar_tip == _arp.ar_sip;
} }
Tins::PDU *Tins::ARP::clone_packet(const uint8_t *ptr, uint32_t total_sz) { PDU *ARP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(arphdr)) if(total_sz < sizeof(arphdr))
return 0; return 0;
PDU *child = 0, *cloned; PDU *child = 0, *cloned;
if(total_sz > sizeof(arphdr)) { if(total_sz > sizeof(arphdr)) {
if((child = PDU::clone_inner_pdu(ptr + sizeof(arphdr), total_sz - sizeof(arphdr))) == 0) child = PDU::clone_inner_pdu(ptr + sizeof(arphdr), total_sz - sizeof(arphdr));
if(!child)
return 0; return 0;
} }
cloned = new ARP(ptr, std::min(total_sz, (uint32_t)sizeof(_arp))); cloned = new ARP(ptr, std::min(total_sz, (uint32_t)sizeof(_arp)));
@@ -148,11 +153,9 @@ Tins::PDU *Tins::ARP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
return cloned; return cloned;
} }
Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface, PDU* ARP::make_arp_request(const std::string& iface, IPv4Address target,
IPv4Address target, IPv4Address sender, const uint8_t* hw_snd)
IPv4Address sender, {
const uint8_t* hw_snd) {
/* Create ARP packet and set its attributes */ /* Create ARP packet and set its attributes */
ARP* arp = new ARP(); ARP* arp = new ARP();
arp->target_ip_addr(target); arp->target_ip_addr(target);
@@ -163,12 +166,13 @@ Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface,
arp->opcode(REQUEST); arp->opcode(REQUEST);
/* Create the EthernetII PDU with the ARP PDU as its inner PDU */ /* Create the EthernetII PDU with the ARP PDU as its inner PDU */
EthernetII* eth = new EthernetII(iface, Tins::EthernetII::BROADCAST, hw_snd, arp); EthernetII* eth = new EthernetII(iface, EthernetII::BROADCAST, hw_snd, arp);
return eth; return eth;
} }
Tins::PDU* Tins::ARP::make_arp_reply(const string& iface, IPv4Address target, PDU* ARP::make_arp_reply(const string& iface, IPv4Address target,
IPv4Address sender, const uint8_t* hw_tgt, const uint8_t* hw_snd) { IPv4Address sender, const uint8_t* hw_tgt, const uint8_t* hw_snd)
{
/* Create ARP packet and set its attributes */ /* Create ARP packet and set its attributes */
ARP* arp = new ARP(target, sender, hw_tgt, hw_snd); ARP* arp = new ARP(target, sender, hw_tgt, hw_snd);
arp->opcode(REPLY); arp->opcode(REPLY);
@@ -178,13 +182,14 @@ Tins::PDU* Tins::ARP::make_arp_reply(const string& iface, IPv4Address target,
return eth; return eth;
} }
Tins::PDU *Tins::ARP::clone_pdu() const { PDU *ARP::clone_pdu() const {
ARP *new_pdu = new ARP(); ARP *new_pdu = new ARP();
new_pdu->copy_fields(this); new_pdu->copy_fields(this);
new_pdu->copy_inner_pdu(*this); new_pdu->copy_inner_pdu(*this);
return new_pdu; return new_pdu;
} }
void Tins::ARP::copy_fields(const ARP *other) { void ARP::copy_fields(const ARP *other) {
std::memcpy(&_arp, &other->_arp, sizeof(_arp)); std::memcpy(&_arp, &other->_arp, sizeof(_arp));
} }
}

View File

@@ -117,6 +117,8 @@ void Tins::BootP::copy_bootp_fields(const BootP *other) {
_vend = new uint8_t[_vend_size]; _vend = new uint8_t[_vend_size];
std::memcpy(_vend, other->_vend, _vend_size); std::memcpy(_vend, other->_vend, _vend_size);
} }
else
_vend = 0;
} }
Tins::PDU *Tins::BootP::clone_pdu() const { Tins::PDU *Tins::BootP::clone_pdu() const {

View File

@@ -26,19 +26,24 @@
#include "dhcp.h" #include "dhcp.h"
#include "ethernetII.h" #include "ethernetII.h"
const uint32_t Tins::DHCP::MAX_DHCP_SIZE = 312; using std::string;
using std::list;
using std::runtime_error;
using namespace std; namespace Tins {
const uint32_t DHCP::MAX_DHCP_SIZE = 312;
/* Magic cookie: uint32_t. /* Magic cookie: uint32_t.
* end of options: 1 byte. */ * end of options: 1 byte. */
Tins::DHCP::DHCP() : _size(sizeof(uint32_t) + 1) { DHCP::DHCP() : _size(sizeof(uint32_t) + 1) {
opcode(BOOTREQUEST); opcode(BOOTREQUEST);
htype(1); //ethernet htype(1); //ethernet
hlen(EthernetII::ADDR_SIZE); hlen(EthernetII::ADDR_SIZE);
} }
Tins::DHCP::DHCP(const uint8_t *buffer, uint32_t total_sz) : BootP(buffer, total_sz, 0), _size(sizeof(uint32_t) + 1){ DHCP::DHCP(const uint8_t *buffer, uint32_t total_sz)
: BootP(buffer, total_sz, 0), _size(sizeof(uint32_t) + 1)
{
buffer += BootP::header_size() - vend_size(); buffer += BootP::header_size() - vend_size();
total_sz -= BootP::header_size() - vend_size(); total_sz -= BootP::header_size() - vend_size();
uint8_t args[2] = {0}; uint8_t args[2] = {0};
@@ -70,33 +75,34 @@ Tins::DHCP::DHCP(const uint8_t *buffer, uint32_t total_sz) : BootP(buffer, total
} }
} }
Tins::DHCP::DHCP(const DHCP &other) : BootP(other) { DHCP::DHCP(const DHCP &other) : BootP(other) {
copy_fields(&other); copy_fields(&other);
} }
Tins::DHCP &Tins::DHCP::operator= (const DHCP &other) { DHCP &DHCP::operator= (const DHCP &other) {
copy_fields(&other); copy_fields(&other);
copy_inner_pdu(other); copy_inner_pdu(other);
return *this; return *this;
} }
Tins::DHCP::~DHCP() { DHCP::~DHCP() {
while(_options.size()) { /*while(_options.size()) {
delete[] _options.front().value; delete[] _options.front().value;
_options.pop_front(); _options.pop_front();
} }*/
} }
Tins::DHCP::DHCPOption::DHCPOption(uint8_t opt, uint8_t len, const uint8_t *val) : option(opt), length(len) { DHCP::DHCPOption::DHCPOption(uint8_t opt, uint8_t len, const uint8_t *val)
if(len) { : option(opt), value(val, val ? (val + len) : val) {
/*if(len) {
value = new uint8_t[len]; value = new uint8_t[len];
std::memcpy(value, val, len); std::memcpy(value, val, len);
} }
else else
value = 0; value = 0;*/
} }
bool Tins::DHCP::add_option(Options opt, uint8_t len, const uint8_t *val) { bool DHCP::add_option(Options opt, uint8_t len, const uint8_t *val) {
uint32_t opt_size = len + (sizeof(uint8_t) << 1); uint32_t opt_size = len + (sizeof(uint8_t) << 1);
if(_size + opt_size > MAX_DHCP_SIZE) if(_size + opt_size > MAX_DHCP_SIZE)
return false; return false;
@@ -105,59 +111,59 @@ bool Tins::DHCP::add_option(Options opt, uint8_t len, const uint8_t *val) {
return true; return true;
} }
const Tins::DHCP::DHCPOption *Tins::DHCP::search_option(Options opt) const{ const DHCP::DHCPOption *DHCP::search_option(Options opt) const{
for(std::list<DHCPOption>::const_iterator it = _options.begin(); it != _options.end(); ++it) { for(options_type::const_iterator it = _options.begin(); it != _options.end(); ++it) {
if(it->option == opt) if(it->option == opt)
return &(*it); return &(*it);
} }
return 0; return 0;
} }
bool Tins::DHCP::add_type_option(Flags type) { bool DHCP::add_type_option(Flags type) {
return add_option(DHCP_MESSAGE_TYPE, sizeof(uint8_t), (const uint8_t*)&type); return add_option(DHCP_MESSAGE_TYPE, sizeof(uint8_t), (const uint8_t*)&type);
} }
bool Tins::DHCP::search_type_option(uint8_t *value) { bool DHCP::search_type_option(uint8_t *value) {
return generic_search(DHCP_MESSAGE_TYPE, value); return generic_search(DHCP_MESSAGE_TYPE, value);
} }
bool Tins::DHCP::add_server_identifier(uint32_t ip) { bool DHCP::add_server_identifier(uint32_t ip) {
ip = Utils::net_to_host_l(ip); ip = Utils::net_to_host_l(ip);
return add_option(DHCP_SERVER_IDENTIFIER, sizeof(uint32_t), (const uint8_t*)&ip); return add_option(DHCP_SERVER_IDENTIFIER, sizeof(uint32_t), (const uint8_t*)&ip);
} }
bool Tins::DHCP::search_server_identifier(uint32_t *value) { bool DHCP::search_server_identifier(uint32_t *value) {
return generic_search(DHCP_SERVER_IDENTIFIER, value); return generic_search(DHCP_SERVER_IDENTIFIER, value);
} }
bool Tins::DHCP::add_lease_time(uint32_t time) { bool DHCP::add_lease_time(uint32_t time) {
time = Utils::net_to_host_l(time); time = Utils::net_to_host_l(time);
return add_option(DHCP_LEASE_TIME, sizeof(uint32_t), (const uint8_t*)&time); return add_option(DHCP_LEASE_TIME, sizeof(uint32_t), (const uint8_t*)&time);
} }
bool Tins::DHCP::search_lease_time(uint32_t *value) { bool DHCP::search_lease_time(uint32_t *value) {
return generic_search(DHCP_LEASE_TIME, value); return generic_search(DHCP_LEASE_TIME, value);
} }
bool Tins::DHCP::add_renewal_time(uint32_t time) { bool DHCP::add_renewal_time(uint32_t time) {
time = Utils::net_to_host_l(time); time = Utils::net_to_host_l(time);
return add_option(DHCP_RENEWAL_TIME, sizeof(uint32_t), (const uint8_t*)&time); return add_option(DHCP_RENEWAL_TIME, sizeof(uint32_t), (const uint8_t*)&time);
} }
bool Tins::DHCP::search_renewal_time(uint32_t *value) { bool DHCP::search_renewal_time(uint32_t *value) {
return generic_search(DHCP_RENEWAL_TIME, value); return generic_search(DHCP_RENEWAL_TIME, value);
} }
bool Tins::DHCP::add_subnet_mask(uint32_t mask) { bool DHCP::add_subnet_mask(uint32_t mask) {
mask = Utils::net_to_host_l(mask); mask = Utils::net_to_host_l(mask);
return add_option(SUBNET_MASK, sizeof(uint32_t), (const uint8_t*)&mask); return add_option(SUBNET_MASK, sizeof(uint32_t), (const uint8_t*)&mask);
} }
bool Tins::DHCP::search_subnet_mask(uint32_t *value) { bool DHCP::search_subnet_mask(uint32_t *value) {
return generic_search(SUBNET_MASK, value); return generic_search(SUBNET_MASK, value);
} }
bool Tins::DHCP::add_routers_option(const list<uint32_t> &routers) { bool DHCP::add_routers_option(const list<uint32_t> &routers) {
uint32_t size; uint32_t size;
uint8_t *buffer = serialize_list(routers, size); uint8_t *buffer = serialize_list(routers, size);
bool ret = add_option(ROUTERS, size, buffer); bool ret = add_option(ROUTERS, size, buffer);
@@ -165,11 +171,11 @@ bool Tins::DHCP::add_routers_option(const list<uint32_t> &routers) {
return ret; return ret;
} }
bool Tins::DHCP::search_routers_option(std::list<uint32_t> *routers) { bool DHCP::search_routers_option(std::list<uint32_t> *routers) {
return generic_search(ROUTERS, routers); return generic_search(ROUTERS, routers);
} }
bool Tins::DHCP::add_dns_option(const list<uint32_t> &dns) { bool DHCP::add_dns_option(const list<uint32_t> &dns) {
uint32_t size; uint32_t size;
uint8_t *buffer = serialize_list(dns, size); uint8_t *buffer = serialize_list(dns, size);
bool ret = add_option(DOMAIN_NAME_SERVERS, size, buffer); bool ret = add_option(DOMAIN_NAME_SERVERS, size, buffer);
@@ -177,46 +183,46 @@ bool Tins::DHCP::add_dns_option(const list<uint32_t> &dns) {
return ret; return ret;
} }
bool Tins::DHCP::search_dns_option(std::list<uint32_t> *dns) { bool DHCP::search_dns_option(std::list<uint32_t> *dns) {
return generic_search(DOMAIN_NAME_SERVERS, dns); return generic_search(DOMAIN_NAME_SERVERS, dns);
} }
bool Tins::DHCP::add_broadcast_option(uint32_t addr) { bool DHCP::add_broadcast_option(uint32_t addr) {
addr = Utils::net_to_host_l(addr); addr = Utils::net_to_host_l(addr);
return add_option(BROADCAST_ADDRESS, sizeof(uint32_t), (uint8_t*)&addr); return add_option(BROADCAST_ADDRESS, sizeof(uint32_t), (uint8_t*)&addr);
} }
bool Tins::DHCP::search_broadcast_option(uint32_t *value) { bool DHCP::search_broadcast_option(uint32_t *value) {
return generic_search(BROADCAST_ADDRESS, value); return generic_search(BROADCAST_ADDRESS, value);
} }
bool Tins::DHCP::add_requested_ip_option(uint32_t addr) { bool DHCP::add_requested_ip_option(uint32_t addr) {
addr = Utils::net_to_host_l(addr); addr = Utils::net_to_host_l(addr);
return add_option(DHCP_REQUESTED_ADDRESS, sizeof(uint32_t), (uint8_t*)&addr); return add_option(DHCP_REQUESTED_ADDRESS, sizeof(uint32_t), (uint8_t*)&addr);
} }
bool Tins::DHCP::search_requested_ip_option(uint32_t *value) { bool DHCP::search_requested_ip_option(uint32_t *value) {
return generic_search(DHCP_REQUESTED_ADDRESS, value); return generic_search(DHCP_REQUESTED_ADDRESS, value);
} }
bool Tins::DHCP::add_domain_name(const string &name) { bool DHCP::add_domain_name(const string &name) {
return add_option(DOMAIN_NAME, name.size(), (const uint8_t*)name.c_str()); return add_option(DOMAIN_NAME, name.size(), (const uint8_t*)name.c_str());
} }
bool Tins::DHCP::search_domain_name(std::string *value) { bool DHCP::search_domain_name(std::string *value) {
return generic_search(DOMAIN_NAME, value); return generic_search(DOMAIN_NAME, value);
} }
bool Tins::DHCP::add_rebind_time(uint32_t time) { bool DHCP::add_rebind_time(uint32_t time) {
time = Utils::net_to_host_l(time); time = Utils::net_to_host_l(time);
return add_option(DHCP_REBINDING_TIME, sizeof(uint32_t), (uint8_t*)&time); return add_option(DHCP_REBINDING_TIME, sizeof(uint32_t), (uint8_t*)&time);
} }
bool Tins::DHCP::search_rebind_time(uint32_t *value) { bool DHCP::search_rebind_time(uint32_t *value) {
return generic_search(DHCP_REBINDING_TIME, value); return generic_search(DHCP_REBINDING_TIME, value);
} }
uint8_t *Tins::DHCP::serialize_list(const list<uint32_t> &int_list, uint32_t &sz) { uint8_t *DHCP::serialize_list(const list<uint32_t> &int_list, uint32_t &sz) {
uint8_t *buffer = new uint8_t[int_list.size() * sizeof(uint32_t)]; uint8_t *buffer = new uint8_t[int_list.size() * sizeof(uint32_t)];
uint32_t *ptr = (uint32_t*)buffer; uint32_t *ptr = (uint32_t*)buffer;
for(list<uint32_t>::const_iterator it = int_list.begin(); it != int_list.end(); ++it) for(list<uint32_t>::const_iterator it = int_list.begin(); it != int_list.end(); ++it)
@@ -225,11 +231,11 @@ uint8_t *Tins::DHCP::serialize_list(const list<uint32_t> &int_list, uint32_t &sz
return buffer; return buffer;
} }
uint32_t Tins::DHCP::header_size() const { uint32_t DHCP::header_size() const {
return BootP::header_size() - vend_size() + _size; return BootP::header_size() - vend_size() + _size;
} }
void Tins::DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) { void DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
assert(total_sz >= header_size()); assert(total_sz >= header_size());
uint8_t *result = 0; uint8_t *result = 0;
if(_size) { if(_size) {
@@ -237,12 +243,11 @@ void Tins::DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
uint8_t *ptr = result + sizeof(uint32_t); uint8_t *ptr = result + sizeof(uint32_t);
// Magic cookie // Magic cookie
*((uint32_t*)result) = Utils::net_to_host_l(0x63825363); *((uint32_t*)result) = Utils::net_to_host_l(0x63825363);
for(std::list<DHCPOption>::const_iterator it = _options.begin(); it != _options.end(); ++it) { for(options_type::const_iterator it = _options.begin(); it != _options.end(); ++it) {
*(ptr++) = it->option; *(ptr++) = it->option;
*(ptr++) = it->length; *(ptr++) = it->value.size();
if(it->length) std::copy(it->value.begin(), it->value.end(), ptr);
std::memcpy(ptr, it->value, it->length); ptr += it->value.size();
ptr += it->length;
} }
// End of options // End of options
result[_size-1] = END; result[_size-1] = END;
@@ -252,25 +257,26 @@ void Tins::DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
delete[] result; delete[] result;
} }
void Tins::DHCP::copy_fields(const DHCP *other) { void DHCP::copy_fields(const DHCP *other) {
BootP::copy_bootp_fields(other); BootP::copy_bootp_fields(other);
_size = other->_size; _size = other->_size;
for(std::list<DHCPOption>::const_iterator it = other->_options.begin(); it != other->_options.end(); ++it) for(options_type::const_iterator it = other->_options.begin(); it != other->_options.end(); ++it)
_options.push_back(DHCPOption(it->option, it->length, it->value)); _options.push_back(*it);
//_options.push_back(DHCPOption(it->option, it->length, it->value));
} }
Tins::PDU *Tins::DHCP::clone_pdu() const { PDU *DHCP::clone_pdu() const {
DHCP *new_pdu = new DHCP(); DHCP *new_pdu = new DHCP();
new_pdu->copy_fields(this); new_pdu->copy_fields(this);
return new_pdu; return new_pdu;
} }
bool Tins::DHCP::generic_search(Options opt, std::list<uint32_t> *container) { bool DHCP::generic_search(Options opt, std::list<uint32_t> *container) {
const DHCPOption *option = search_option(opt); const DHCPOption *option = search_option(opt);
if(!option) if(!option)
return false; return false;
const uint32_t *ptr = (const uint32_t*)option->value; const uint32_t *ptr = (const uint32_t*)&option->value[0];
uint32_t len = option->length; uint32_t len = option->value.size();
if((len % sizeof(uint32_t)) != 0) if((len % sizeof(uint32_t)) != 0)
return false; return false;
while(len) { while(len) {
@@ -280,18 +286,20 @@ bool Tins::DHCP::generic_search(Options opt, std::list<uint32_t> *container) {
return true; return true;
} }
bool Tins::DHCP::generic_search(Options opt, std::string *str) { bool DHCP::generic_search(Options opt, std::string *str) {
const DHCPOption *option = search_option(opt); const DHCPOption *option = search_option(opt);
if(!option) if(!option)
return false; return false;
*str = string((const char*)option->value, option->length); //*str = string((const char*)option->value, option->length);
*str = string(option->value.begin(), option->value.end());
return true; return true;
} }
bool Tins::DHCP::generic_search(Options opt, uint32_t *value) { bool DHCP::generic_search(Options opt, uint32_t *value) {
if(generic_search<uint32_t>(opt, value)) { if(generic_search<uint32_t>(opt, value)) {
*value = Utils::net_to_host_l(*value); *value = Utils::net_to_host_l(*value);
return true; return true;
} }
return false; return false;
} }
}

View File

@@ -18,6 +18,8 @@ public:
static const string ip_addr1; static const string ip_addr1;
static const uint8_t expected_packet[]; static const uint8_t expected_packet[];
static const IPv4Address addr1, addr2; static const IPv4Address addr1, addr2;
void test_equals(const ARP &arp1, const ARP &arp2);
}; };
const uint8_t ARPTest::empty_addr[] = {'\x00', '\x00', '\x00', '\x00', '\x00', '\x00'}; const uint8_t ARPTest::empty_addr[] = {'\x00', '\x00', '\x00', '\x00', '\x00', '\x00'};
@@ -26,6 +28,18 @@ const uint8_t ARPTest::hw_addr2[] = {'\x7a', '\x1f', '\xf4', '\x39', '\xab', '\x
const uint8_t ARPTest::expected_packet[] = {'\x00', '\x01', '\x08', '\x00', '\x06', '\x04', '\x00', '\x02', '\x03', '\xde', '\xf5', '\x12', '\t', '\xfa', '\xc0', '\xa8', '-', '\xe7', '\xf5', '\x12', '\xda', 'g', '\xbd', '\r', ' ', '\x9b', 'Q', '\xfe'}; const uint8_t ARPTest::expected_packet[] = {'\x00', '\x01', '\x08', '\x00', '\x06', '\x04', '\x00', '\x02', '\x03', '\xde', '\xf5', '\x12', '\t', '\xfa', '\xc0', '\xa8', '-', '\xe7', '\xf5', '\x12', '\xda', 'g', '\xbd', '\r', ' ', '\x9b', 'Q', '\xfe'};
const IPv4Address ARPTest::addr1(0x1234), ARPTest::addr2(0xa3f1); const IPv4Address ARPTest::addr1(0x1234), ARPTest::addr2(0xa3f1);
void ARPTest::test_equals(const ARP &arp1, const ARP &arp2) {
EXPECT_EQ(arp1.opcode(), arp2.opcode());
ASSERT_EQ(arp1.hw_addr_length(), arp2.hw_addr_length());
EXPECT_EQ(arp1.hw_addr_format(), arp2.hw_addr_format());
ASSERT_EQ(arp1.prot_addr_length(), arp2.prot_addr_length());
EXPECT_EQ(arp1.prot_addr_format(), arp2.prot_addr_format());
EXPECT_EQ(arp1.sender_ip_addr(), arp2.sender_ip_addr());
EXPECT_EQ(arp1.target_ip_addr(), arp2.target_ip_addr());
EXPECT_TRUE(memcmp(arp1.sender_hw_addr(), arp2.sender_hw_addr(), arp2.hw_addr_length()) == 0);
EXPECT_TRUE(memcmp(arp1.target_hw_addr(), arp2.target_hw_addr(), arp2.hw_addr_length()) == 0);
}
TEST_F(ARPTest, DefaultContructor) { TEST_F(ARPTest, DefaultContructor) {
ARP arp; ARP arp;
EXPECT_EQ(arp.target_ip_addr(), 0); EXPECT_EQ(arp.target_ip_addr(), 0);
@@ -38,29 +52,22 @@ TEST_F(ARPTest, DefaultContructor) {
TEST_F(ARPTest, CopyContructor) { TEST_F(ARPTest, CopyContructor) {
ARP arp1(addr1, addr2, hw_addr1, hw_addr2); ARP arp1(addr1, addr2, hw_addr1, hw_addr2);
ARP arp2(arp1); ARP arp2(arp1);
EXPECT_EQ(arp1.opcode(), arp2.opcode()); test_equals(arp1, arp2);
ASSERT_EQ(arp1.hw_addr_length(), arp2.hw_addr_length());
EXPECT_EQ(arp1.hw_addr_format(), arp2.hw_addr_format());
ASSERT_EQ(arp1.prot_addr_length(), arp2.prot_addr_length());
EXPECT_EQ(arp1.prot_addr_format(), arp2.prot_addr_format());
EXPECT_EQ(arp1.sender_ip_addr(), arp2.sender_ip_addr());
EXPECT_EQ(arp1.target_ip_addr(), arp2.target_ip_addr());
EXPECT_TRUE(memcmp(arp1.sender_hw_addr(), arp2.sender_hw_addr(), arp2.hw_addr_length()) == 0);
EXPECT_TRUE(memcmp(arp1.target_hw_addr(), arp2.target_hw_addr(), arp2.hw_addr_length()) == 0);
} }
TEST_F(ARPTest, CopyAssignmentOperator) { TEST_F(ARPTest, CopyAssignmentOperator) {
ARP arp1(addr1, addr2, hw_addr1, hw_addr2); ARP arp1(addr1, addr2, hw_addr1, hw_addr2);
ARP arp2 = arp1; ARP arp2 = arp1;
EXPECT_EQ(arp1.opcode(), arp2.opcode()); test_equals(arp1, arp2);
ASSERT_EQ(arp1.hw_addr_length(), arp2.hw_addr_length()); }
EXPECT_EQ(arp1.hw_addr_format(), arp2.hw_addr_format());
ASSERT_EQ(arp1.prot_addr_length(), arp2.prot_addr_length()); TEST_F(ARPTest, NestedCopy) {
EXPECT_EQ(arp1.prot_addr_format(), arp2.prot_addr_format()); ARP *nested_arp = new ARP(addr1, addr2, hw_addr1, hw_addr2);
EXPECT_EQ(arp1.sender_ip_addr(), arp2.sender_ip_addr()); ARP arp1(addr1, addr2, hw_addr1, hw_addr2);
EXPECT_EQ(arp1.target_ip_addr(), arp2.target_ip_addr()); arp1.inner_pdu(nested_arp);
EXPECT_TRUE(memcmp(arp1.sender_hw_addr(), arp2.sender_hw_addr(), arp2.hw_addr_length()) == 0); ARP arp2(arp1);
EXPECT_TRUE(memcmp(arp1.target_hw_addr(), arp2.target_hw_addr(), arp2.hw_addr_length()) == 0); test_equals(arp1, arp2);
test_equals(arp1, *nested_arp);
} }
TEST_F(ARPTest, CompleteContructor) { TEST_F(ARPTest, CompleteContructor) {

View File

@@ -64,6 +64,23 @@ TEST_F(DHCPTest, DefaultConstructor) {
EXPECT_EQ(dhcp.hlen(), EthernetII::ADDR_SIZE); EXPECT_EQ(dhcp.hlen(), EthernetII::ADDR_SIZE);
} }
TEST_F(DHCPTest, CopyConstructor) {
DHCP dhcp1(expected_packet, sizeof(expected_packet));
DHCP dhcp2(dhcp1);
test_equals(dhcp1, dhcp2);
}
TEST_F(DHCPTest, CopyAssignmentOperator) {
DHCP dhcp1(expected_packet, sizeof(expected_packet));
DHCP dhcp2 = dhcp1;
test_equals(dhcp1, dhcp2);
}
TEST_F(DHCPTest, NestedCopy) {
}
TEST_F(DHCPTest, OpCode) { TEST_F(DHCPTest, OpCode) {
DHCP dhcp; DHCP dhcp;
dhcp.opcode(0x71); dhcp.opcode(0x71);
@@ -152,9 +169,9 @@ void DHCPTest::test_option(const DHCP &dhcp, DHCP::Options opt, uint32_t len, ui
const DHCP::DHCPOption *option = dhcp.search_option(opt); const DHCP::DHCPOption *option = dhcp.search_option(opt);
ASSERT_TRUE(option != 0); ASSERT_TRUE(option != 0);
EXPECT_EQ(option->option, opt); EXPECT_EQ(option->option, opt);
EXPECT_EQ(option->length, len); ASSERT_EQ(option->value.size(), len);
if(len) if(len)
EXPECT_TRUE(memcmp(option->value, value, len) == 0); EXPECT_TRUE(std::equal(option->value.begin(), option->value.end(), value));
} }
TEST_F(DHCPTest, TypeOption) { TEST_F(DHCPTest, TypeOption) {
@@ -261,8 +278,8 @@ void DHCPTest::test_equals(const DHCP &dhcp1, const DHCP &dhcp2) {
it2 = options2.begin(); it2 = options2.begin();
while(it1 != options1.end()) { while(it1 != options1.end()) {
EXPECT_EQ(it1->option, it2->option); EXPECT_EQ(it1->option, it2->option);
ASSERT_EQ(it1->length, it2->length); ASSERT_EQ(it1->value.size(), it2->value.size());
EXPECT_TRUE(memcmp(it1->value, it2->value, it1->length) == 0); EXPECT_TRUE(std::equal(it1->value.begin(), it1->value.end(), it2->value.begin()));
it1++; it2++; it1++; it2++;
} }
} }
@@ -278,12 +295,12 @@ TEST_F(DHCPTest, ConstructorFromBuffer) {
EXPECT_EQ(dhcp1.xid(), 0x3fab23de); EXPECT_EQ(dhcp1.xid(), 0x3fab23de);
EXPECT_EQ(dhcp1.secs(), 0x9f1a); EXPECT_EQ(dhcp1.secs(), 0x9f1a);
EXPECT_EQ(dhcp1.padding(), 0); EXPECT_EQ(dhcp1.padding(), 0);
EXPECT_EQ(dhcp1.ciaddr(), Utils::ip_to_int("192.168.0.102")); EXPECT_EQ(dhcp1.ciaddr(), IPv4Address("192.168.0.102"));
EXPECT_EQ(dhcp1.yiaddr(), Utils::ip_to_int("243.22.34.98")); EXPECT_EQ(dhcp1.yiaddr(), IPv4Address("243.22.34.98"));
EXPECT_EQ(dhcp1.giaddr(), Utils::ip_to_int("123.43.55.254")); EXPECT_EQ(dhcp1.giaddr(), IPv4Address("123.43.55.254"));
EXPECT_EQ(dhcp1.siaddr(), Utils::ip_to_int("167.32.11.154")); EXPECT_EQ(dhcp1.siaddr(), IPv4Address("167.32.11.154"));
ASSERT_TRUE(dhcp1.search_server_identifier(&ip)); ASSERT_TRUE(dhcp1.search_server_identifier(&ip));
EXPECT_EQ(ip, Utils::net_to_host_l(Utils::ip_to_int("192.168.4.2"))); EXPECT_EQ(ip, Utils::net_to_host_l(IPv4Address("192.168.4.2")));
uint32_t size; uint32_t size;
uint8_t *buffer = dhcp1.serialize(size); uint8_t *buffer = dhcp1.serialize(size);

View File

@@ -172,4 +172,5 @@ TEST_F(IPTest, ConstructorFromBuffer) {
ASSERT_TRUE(buffer); ASSERT_TRUE(buffer);
IP ip2(buffer, size); IP ip2(buffer, size);
delete[] buffer;
} }

18
tests/src/ipaddress.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include <gtest/gtest.h>
#include <cstring>
#include <string>
#include <stdint.h>
#include "ipaddress.h"
#include "utils.h"
using namespace Tins;
std::string ip_string("192.168.0.225");
TEST(IPAddressTest, Constructor) {
IPv4Address addr1(ip_string);
IPv4Address addr2(Utils::ip_to_int(ip_string));
EXPECT_EQ(addr2, addr1);
EXPECT_EQ((std::string)addr1, ip_string);
EXPECT_EQ((std::string)addr2, ip_string);
}

View File

@@ -42,6 +42,15 @@ TEST_F(TCPTest, CopyAssignmentOperator) {
test_equals(tcp1, tcp2); test_equals(tcp1, tcp2);
} }
TEST_F(TCPTest, NestedCopy) {
TCP *nested_tcp = new TCP(0x6d1f, 0x78f2);
TCP tcp1(0x6d1f, 0x78f2);
tcp1.inner_pdu(nested_tcp);
TCP tcp2(tcp1);
test_equals(tcp1, tcp2);
test_equals(tcp1, *nested_tcp);
}
TEST_F(TCPTest, CompleteConstructor) { TEST_F(TCPTest, CompleteConstructor) {
TCP tcp(0x6d1f, 0x78f2); TCP tcp(0x6d1f, 0x78f2);
EXPECT_EQ(tcp.dport(), 0x6d1f); EXPECT_EQ(tcp.dport(), 0x6d1f);