diff --git a/include/arp.h b/include/arp.h index 61fd434..a52e54e 100644 --- a/include/arp.h +++ b/include/arp.h @@ -67,7 +67,7 @@ namespace Tins { * * \param other The object which will be copied. */ - ARP(const ARP &other); + //ARP(const ARP &other); /* Getters */ /** @@ -75,63 +75,63 @@ namespace Tins { * * \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. * * \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. * * \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. * * \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. * * \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. * * \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. * * \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. * * \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. * * \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. * \return Returns the ARP header size. diff --git a/include/dhcp.h b/include/dhcp.h index 1bdcabc..e4daba3 100644 --- a/include/dhcp.h +++ b/include/dhcp.h @@ -24,6 +24,7 @@ #include +#include #include #include "bootp.h" @@ -131,19 +132,6 @@ namespace Tins { * \brief DHCP options struct. */ 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. * @@ -154,8 +142,24 @@ namespace Tins { * \param val The option's value. */ 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 value; }; + typedef std::list options_type; + /** * \brief Creates an instance of DHCP. * @@ -366,7 +370,7 @@ namespace Tins { /** \brief Getter for the options list. * \return The option list. */ - const std::list options() const { return _options; } + const options_type options() const { return _options; } /** * \brief Getter for the PDU's type. @@ -395,8 +399,8 @@ namespace Tins { template bool generic_search(Options opt, T *value) { const DHCPOption *option = search_option(opt); - if(option && option->length == sizeof(T)) { - *value = *(T*)option->value; + if(option && option->value.size() == sizeof(T)) { + *value = *(T*)&option->value[0]; return true; } return false; @@ -408,7 +412,7 @@ namespace Tins { uint8_t *serialize_list(const std::list &int_list, uint32_t &sz); - std::list _options; + options_type _options; uint32_t _size; }; }; diff --git a/src/arp.cpp b/src/arp.cpp index 806cd7d..d203d13 100644 --- a/src/arp.cpp +++ b/src/arp.cpp @@ -31,14 +31,18 @@ #include "constants.h" -using namespace std; +using std::string; +using std::runtime_error; +namespace Tins { -Tins::ARP::ARP(IPv4Address target_ip, IPv4Address sender_ip, - const uint8_t *target_hw, const uint8_t *sender_hw) : PDU(0x0608) { +ARP::ARP(IPv4Address target_ip, IPv4Address sender_ip, + const uint8_t *target_hw, const uint8_t *sender_hw) +: PDU(0x0608) +{ memset(&_arp, 0, sizeof(arphdr)); - hw_addr_format((uint16_t)Tins::Constants::ARP::ETHER); - prot_addr_format((uint16_t)Tins::Constants::Ethernet::IP); + hw_addr_format((uint16_t)Constants::ARP::ETHER); + prot_addr_format((uint16_t)Constants::Ethernet::IP); hw_addr_length(EthernetII::ADDR_SIZE); prot_addr_length(IP::ADDR_SIZE); sender_ip_addr(sender_ip); @@ -49,56 +53,58 @@ Tins::ARP::ARP(IPv4Address target_ip, IPv4Address sender_ip, 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)) - 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)); total_sz -= sizeof(arphdr); if(total_sz) inner_pdu(new RawPDU(buffer + sizeof(arphdr), total_sz)); } -Tins::ARP::ARP(const ARP &other) : PDU(other) { - copy_fields(&other); +void ARP::sender_hw_addr(const uint8_t* new_snd_hw_addr) { + //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) { - 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) { +void ARP::sender_ip_addr(IPv4Address 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) { - memcpy(this->_arp.ar_tha, new_tgt_hw_addr, 6); //Should this use hardware address' length? +void ARP::target_hw_addr(const uint8_t* new_tgt_hw_addr) { + //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; } -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); } -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); } -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; } -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; } -void Tins::ARP::opcode(Flags new_opcode) { +void ARP::opcode(Flags 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->sender_ip_addr(ip_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); } -void Tins::ARP::set_arp_reply(const std::string& ip_tgt, - const std::string& ip_snd, - const uint8_t* hw_tgt, - const uint8_t* hw_snd) { - +void ARP::set_arp_reply(const std::string& ip_tgt, const std::string& ip_snd, + const uint8_t* hw_tgt, const uint8_t* hw_snd) +{ this->target_ip_addr(ip_tgt); this->sender_ip_addr(ip_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); } -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)); 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)) return false; arphdr *arp_ptr = (arphdr*)ptr; 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)) return 0; PDU *child = 0, *cloned; 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; } 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; } -Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface, - IPv4Address target, - IPv4Address sender, - const uint8_t* hw_snd) { - +PDU* ARP::make_arp_request(const std::string& iface, IPv4Address target, + IPv4Address sender, const uint8_t* hw_snd) +{ /* Create ARP packet and set its attributes */ ARP* arp = new ARP(); arp->target_ip_addr(target); @@ -163,12 +166,13 @@ Tins::PDU* Tins::ARP::make_arp_request(const std::string& iface, arp->opcode(REQUEST); /* 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; } -Tins::PDU* Tins::ARP::make_arp_reply(const string& iface, IPv4Address target, - IPv4Address sender, const uint8_t* hw_tgt, const uint8_t* hw_snd) { +PDU* ARP::make_arp_reply(const string& iface, IPv4Address target, + IPv4Address sender, const uint8_t* hw_tgt, const uint8_t* hw_snd) +{ /* Create ARP packet and set its attributes */ ARP* arp = new ARP(target, sender, hw_tgt, hw_snd); arp->opcode(REPLY); @@ -178,13 +182,14 @@ Tins::PDU* Tins::ARP::make_arp_reply(const string& iface, IPv4Address target, return eth; } -Tins::PDU *Tins::ARP::clone_pdu() const { +PDU *ARP::clone_pdu() const { ARP *new_pdu = new ARP(); new_pdu->copy_fields(this); new_pdu->copy_inner_pdu(*this); 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)); } +} diff --git a/src/bootp.cpp b/src/bootp.cpp index 68e7ecc..8005334 100644 --- a/src/bootp.cpp +++ b/src/bootp.cpp @@ -117,6 +117,8 @@ void Tins::BootP::copy_bootp_fields(const BootP *other) { _vend = new uint8_t[_vend_size]; std::memcpy(_vend, other->_vend, _vend_size); } + else + _vend = 0; } Tins::PDU *Tins::BootP::clone_pdu() const { diff --git a/src/dhcp.cpp b/src/dhcp.cpp index c55e9c1..93ff3e9 100644 --- a/src/dhcp.cpp +++ b/src/dhcp.cpp @@ -26,19 +26,24 @@ #include "dhcp.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. * end of options: 1 byte. */ -Tins::DHCP::DHCP() : _size(sizeof(uint32_t) + 1) { +DHCP::DHCP() : _size(sizeof(uint32_t) + 1) { opcode(BOOTREQUEST); htype(1); //ethernet 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(); total_sz -= BootP::header_size() - vend_size(); 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); } -Tins::DHCP &Tins::DHCP::operator= (const DHCP &other) { +DHCP &DHCP::operator= (const DHCP &other) { copy_fields(&other); copy_inner_pdu(other); return *this; } -Tins::DHCP::~DHCP() { - while(_options.size()) { +DHCP::~DHCP() { + /*while(_options.size()) { delete[] _options.front().value; _options.pop_front(); - } + }*/ } -Tins::DHCP::DHCPOption::DHCPOption(uint8_t opt, uint8_t len, const uint8_t *val) : option(opt), length(len) { - if(len) { +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; + 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); if(_size + opt_size > MAX_DHCP_SIZE) return false; @@ -105,59 +111,59 @@ bool Tins::DHCP::add_option(Options opt, uint8_t len, const uint8_t *val) { return true; } -const Tins::DHCP::DHCPOption *Tins::DHCP::search_option(Options opt) const{ - for(std::list::const_iterator it = _options.begin(); it != _options.end(); ++it) { +const DHCP::DHCPOption *DHCP::search_option(Options opt) const{ + for(options_type::const_iterator it = _options.begin(); it != _options.end(); ++it) { if(it->option == opt) return &(*it); } 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); } -bool Tins::DHCP::search_type_option(uint8_t *value) { +bool DHCP::search_type_option(uint8_t *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); 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); } -bool Tins::DHCP::add_lease_time(uint32_t time) { +bool DHCP::add_lease_time(uint32_t time) { time = Utils::net_to_host_l(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); } -bool Tins::DHCP::add_renewal_time(uint32_t time) { +bool DHCP::add_renewal_time(uint32_t time) { time = Utils::net_to_host_l(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); } -bool Tins::DHCP::add_subnet_mask(uint32_t mask) { +bool DHCP::add_subnet_mask(uint32_t mask) { mask = Utils::net_to_host_l(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); } -bool Tins::DHCP::add_routers_option(const list &routers) { +bool DHCP::add_routers_option(const list &routers) { uint32_t size; uint8_t *buffer = serialize_list(routers, size); bool ret = add_option(ROUTERS, size, buffer); @@ -165,11 +171,11 @@ bool Tins::DHCP::add_routers_option(const list &routers) { return ret; } -bool Tins::DHCP::search_routers_option(std::list *routers) { +bool DHCP::search_routers_option(std::list *routers) { return generic_search(ROUTERS, routers); } -bool Tins::DHCP::add_dns_option(const list &dns) { +bool DHCP::add_dns_option(const list &dns) { uint32_t size; uint8_t *buffer = serialize_list(dns, size); bool ret = add_option(DOMAIN_NAME_SERVERS, size, buffer); @@ -177,46 +183,46 @@ bool Tins::DHCP::add_dns_option(const list &dns) { return ret; } -bool Tins::DHCP::search_dns_option(std::list *dns) { +bool DHCP::search_dns_option(std::list *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); 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); } -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); 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); } -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()); } -bool Tins::DHCP::search_domain_name(std::string *value) { +bool DHCP::search_domain_name(std::string *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); 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); } -uint8_t *Tins::DHCP::serialize_list(const list &int_list, uint32_t &sz) { +uint8_t *DHCP::serialize_list(const list &int_list, uint32_t &sz) { uint8_t *buffer = new uint8_t[int_list.size() * sizeof(uint32_t)]; uint32_t *ptr = (uint32_t*)buffer; for(list::const_iterator it = int_list.begin(); it != int_list.end(); ++it) @@ -225,11 +231,11 @@ uint8_t *Tins::DHCP::serialize_list(const list &int_list, uint32_t &sz return buffer; } -uint32_t Tins::DHCP::header_size() const { +uint32_t DHCP::header_size() const { 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()); uint8_t *result = 0; 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); // Magic cookie *((uint32_t*)result) = Utils::net_to_host_l(0x63825363); - for(std::list::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->length; - if(it->length) - std::memcpy(ptr, it->value, it->length); - ptr += it->length; + *(ptr++) = it->value.size(); + std::copy(it->value.begin(), it->value.end(), ptr); + ptr += it->value.size(); } // End of options result[_size-1] = END; @@ -252,25 +257,26 @@ void Tins::DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const P delete[] result; } -void Tins::DHCP::copy_fields(const DHCP *other) { +void DHCP::copy_fields(const DHCP *other) { BootP::copy_bootp_fields(other); _size = other->_size; - for(std::list::const_iterator it = other->_options.begin(); it != other->_options.end(); ++it) - _options.push_back(DHCPOption(it->option, it->length, it->value)); + 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)); } -Tins::PDU *Tins::DHCP::clone_pdu() const { +PDU *DHCP::clone_pdu() const { DHCP *new_pdu = new DHCP(); new_pdu->copy_fields(this); return new_pdu; } -bool Tins::DHCP::generic_search(Options opt, std::list *container) { +bool DHCP::generic_search(Options opt, std::list *container) { const DHCPOption *option = search_option(opt); if(!option) return false; - const uint32_t *ptr = (const uint32_t*)option->value; - uint32_t len = option->length; + const uint32_t *ptr = (const uint32_t*)&option->value[0]; + uint32_t len = option->value.size(); if((len % sizeof(uint32_t)) != 0) return false; while(len) { @@ -280,18 +286,20 @@ bool Tins::DHCP::generic_search(Options opt, std::list *container) { 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); if(!option) 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; } -bool Tins::DHCP::generic_search(Options opt, uint32_t *value) { +bool DHCP::generic_search(Options opt, uint32_t *value) { if(generic_search(opt, value)) { *value = Utils::net_to_host_l(*value); return true; } return false; } +} diff --git a/tests/src/arp.cpp b/tests/src/arp.cpp index 074607f..ecd9428 100644 --- a/tests/src/arp.cpp +++ b/tests/src/arp.cpp @@ -18,6 +18,8 @@ public: static const string ip_addr1; static const uint8_t expected_packet[]; 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'}; @@ -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 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) { ARP arp; EXPECT_EQ(arp.target_ip_addr(), 0); @@ -38,29 +52,22 @@ TEST_F(ARPTest, DefaultContructor) { TEST_F(ARPTest, CopyContructor) { ARP arp1(addr1, addr2, hw_addr1, hw_addr2); ARP arp2(arp1); - 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_equals(arp1, arp2); } TEST_F(ARPTest, CopyAssignmentOperator) { ARP arp1(addr1, addr2, hw_addr1, hw_addr2); ARP arp2 = arp1; - 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_equals(arp1, arp2); +} + +TEST_F(ARPTest, NestedCopy) { + ARP *nested_arp = new ARP(addr1, addr2, hw_addr1, hw_addr2); + ARP arp1(addr1, addr2, hw_addr1, hw_addr2); + arp1.inner_pdu(nested_arp); + ARP arp2(arp1); + test_equals(arp1, arp2); + test_equals(arp1, *nested_arp); } TEST_F(ARPTest, CompleteContructor) { diff --git a/tests/src/dhcp.cpp b/tests/src/dhcp.cpp index 76bf159..90b2f1b 100644 --- a/tests/src/dhcp.cpp +++ b/tests/src/dhcp.cpp @@ -64,6 +64,23 @@ TEST_F(DHCPTest, DefaultConstructor) { 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) { DHCP dhcp; 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); ASSERT_TRUE(option != 0); EXPECT_EQ(option->option, opt); - EXPECT_EQ(option->length, len); + ASSERT_EQ(option->value.size(), 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) { @@ -261,8 +278,8 @@ void DHCPTest::test_equals(const DHCP &dhcp1, const DHCP &dhcp2) { it2 = options2.begin(); while(it1 != options1.end()) { EXPECT_EQ(it1->option, it2->option); - ASSERT_EQ(it1->length, it2->length); - EXPECT_TRUE(memcmp(it1->value, it2->value, it1->length) == 0); + ASSERT_EQ(it1->value.size(), it2->value.size()); + EXPECT_TRUE(std::equal(it1->value.begin(), it1->value.end(), it2->value.begin())); it1++; it2++; } } @@ -278,12 +295,12 @@ TEST_F(DHCPTest, ConstructorFromBuffer) { EXPECT_EQ(dhcp1.xid(), 0x3fab23de); EXPECT_EQ(dhcp1.secs(), 0x9f1a); EXPECT_EQ(dhcp1.padding(), 0); - EXPECT_EQ(dhcp1.ciaddr(), Utils::ip_to_int("192.168.0.102")); - EXPECT_EQ(dhcp1.yiaddr(), Utils::ip_to_int("243.22.34.98")); - EXPECT_EQ(dhcp1.giaddr(), Utils::ip_to_int("123.43.55.254")); - EXPECT_EQ(dhcp1.siaddr(), Utils::ip_to_int("167.32.11.154")); + EXPECT_EQ(dhcp1.ciaddr(), IPv4Address("192.168.0.102")); + EXPECT_EQ(dhcp1.yiaddr(), IPv4Address("243.22.34.98")); + EXPECT_EQ(dhcp1.giaddr(), IPv4Address("123.43.55.254")); + EXPECT_EQ(dhcp1.siaddr(), IPv4Address("167.32.11.154")); 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; uint8_t *buffer = dhcp1.serialize(size); diff --git a/tests/src/ip.cpp b/tests/src/ip.cpp index 2c2175c..bf59a03 100644 --- a/tests/src/ip.cpp +++ b/tests/src/ip.cpp @@ -172,4 +172,5 @@ TEST_F(IPTest, ConstructorFromBuffer) { ASSERT_TRUE(buffer); IP ip2(buffer, size); + delete[] buffer; } diff --git a/tests/src/ipaddress.cpp b/tests/src/ipaddress.cpp new file mode 100644 index 0000000..bce6a11 --- /dev/null +++ b/tests/src/ipaddress.cpp @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#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); +} diff --git a/tests/src/tcp.cpp b/tests/src/tcp.cpp index 07cae51..3a16e02 100644 --- a/tests/src/tcp.cpp +++ b/tests/src/tcp.cpp @@ -42,6 +42,15 @@ TEST_F(TCPTest, CopyAssignmentOperator) { 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) { TCP tcp(0x6d1f, 0x78f2); EXPECT_EQ(tcp.dport(), 0x6d1f);