From 811e19414893f7b39af8910352503e6335ce4268 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Tue, 15 May 2012 11:19:25 -0300 Subject: [PATCH] Modified classes to use the IPv4Address class. --- include/arp.h | 4 +- include/bootp.h | 113 +++++++++++++++++++++++++-------------- include/ipaddress.h | 13 ++--- include/utils.h | 14 ++--- src/arp.cpp | 4 +- src/bootp.cpp | 16 +++--- src/utils.cpp | 23 ++++---- tests/src/arp.cpp | 34 +++++------- tests/src/dhcp.cpp | 19 ++++--- tests/src/utils_test.cpp | 15 ++---- 10 files changed, 139 insertions(+), 116 deletions(-) diff --git a/include/arp.h b/include/arp.h index 8165540..f8c78e9 100644 --- a/include/arp.h +++ b/include/arp.h @@ -82,7 +82,7 @@ namespace Tins { * * \return Returns the sender's IP address in an uint32_t. */ - inline const IPv4Address sender_ip_addr() { return Utils::net_to_host_l(this->_arp.ar_sip); } + inline IPv4Address sender_ip_addr() { return Utils::net_to_host_l(this->_arp.ar_sip); } /** * \brief Getter for the target's hardware address. @@ -96,7 +96,7 @@ namespace Tins { * * \return Returns the target's IP address in an uint32_t. */ - inline const IPv4Address target_ip_addr() { return Utils::net_to_host_l(this->_arp.ar_tip); } + inline IPv4Address target_ip_addr() { return Utils::net_to_host_l(this->_arp.ar_tip); } /** * \brief Getter for the hardware address format. diff --git a/include/bootp.h b/include/bootp.h index 95aa6b0..46589a8 100644 --- a/include/bootp.h +++ b/include/bootp.h @@ -26,6 +26,7 @@ #include #include "pdu.h" #include "utils.h" +#include "ipaddress.h" namespace Tins { @@ -72,7 +73,8 @@ namespace Tins { */ BootP &operator= (const BootP &other); - /** \brief BootP destructor. + /** + * \brief BootP destructor. * * This frees the memory allocated to hold the vend field. */ @@ -80,32 +82,38 @@ namespace Tins { /* Getters */ - /** \brief Getter for the opcode field. + /** + * \brief Getter for the opcode field. * \return The opcode field for this BootP PDU. */ uint8_t opcode() const { return _bootp.opcode; } - /** \brief Getter for the htype field. + /** + * \brief Getter for the htype field. * \return The htype field for this BootP PDU. */ uint8_t htype() const { return _bootp.htype; } - /** \brief Getter for the hlen field. + /** + * \brief Getter for the hlen field. * \return The hlen field for this BootP PDU. */ uint8_t hlen() const { return _bootp.hlen; } - /** \brief Getter for the hops field. + /** + * \brief Getter for the hops field. * \return The hops field for this BootP PDU. */ uint8_t hops() const { return _bootp.hops; } - /** \brief Getter for the xid field. + /** + * \brief Getter for the xid field. * \return The xid field for this BootP PDU. */ uint32_t xid() const { return Utils::net_to_host_l(_bootp.xid); } - /** \brief Getter for the secs field. + /** + * \brief Getter for the secs field. * \return The secs field for this BootP PDU. */ uint16_t secs() const { return Utils::net_to_host_s(_bootp.secs); } @@ -115,129 +123,154 @@ namespace Tins { */ uint16_t padding() const { return Utils::net_to_host_s(_bootp.padding); } - /** \brief Getter for the ciaddr field. + /** + * \brief Getter for the ciaddr field. * \return The ciaddr field for this BootP PDU. */ - uint32_t ciaddr() const { return Utils::net_to_host_l(_bootp.ciaddr); } + IPv4Address ciaddr() const { return Utils::net_to_host_l(_bootp.ciaddr); } - /** \brief Getter for the yiaddr field. + /** + * \brief Getter for the yiaddr field. * \return The yiaddr field for this BootP PDU. */ - uint32_t yiaddr() const { return Utils::net_to_host_l(_bootp.yiaddr); } + IPv4Address yiaddr() const { return Utils::net_to_host_l(_bootp.yiaddr); } - /** \brief Getter for the siaddr field. + /** + * \brief Getter for the siaddr field. * \return The siaddr field for this BootP PDU. */ - uint32_t siaddr() const { return Utils::net_to_host_l(_bootp.siaddr); } + IPv4Address siaddr() const { return Utils::net_to_host_l(_bootp.siaddr); } - /** \brief Getter for the giaddr field. + /** + * \brief Getter for the giaddr field. * \return The giaddr field for this BootP PDU. */ - uint32_t giaddr() const { return Utils::net_to_host_l(_bootp.giaddr); } + IPv4Address giaddr() const { return Utils::net_to_host_l(_bootp.giaddr); } - /** \brief Getter for the chaddr field. + /** + * \brief Getter for the chaddr field. * \return The chddr field for this BootP PDU. */ const uint8_t *chaddr() const { return _bootp.chaddr; } - /** \brief Getter for the sname field. + /** + * \brief Getter for the sname field. * \return The sname field for this BootP PDU. */ const uint8_t *sname() const { return _bootp.sname; } - /** \brief Getter for the file field. + /** + * \brief Getter for the file field. * \return The file field for this BootP PDU. */ const uint8_t *file() const { return _bootp.file; } - /** \brief Getter for the vend field. + /** + * \brief Getter for the vend field. * \return The vend field for this BootP PDU. */ uint8_t *vend() { return _vend; } - /** \brief Getter for the vend field. + /** + * \brief Getter for the vend field. */ uint32_t vend_size() const { return _vend_size; } - /** \brief Getter for the header size. + /** + * \brief Getter for the header size. * \return Returns the BOOTP header size. * \sa PDU::header_size */ uint32_t header_size() const; /* Setters */ - /** \brief Setter for the opcode field. + /** + * \brief Setter for the opcode field. * \param new_opcode The opcode to be set. */ void opcode(uint8_t new_opcode); - /** \brief Setter for the htype field. + /** + * \brief Setter for the htype field. * \param new_htype The htype to be set. */ void htype(uint8_t new_htype); - /** \brief Setter for the hlen field. + /** + * \brief Setter for the hlen field. * \param new_hlen The hlen to be set. */ void hlen(uint8_t new_hlen); - /** \brief Setter for the hops field. + /** + * \brief Setter for the hops field. * \param new_hops The hops to be set. */ void hops(uint8_t new_hops); - /** \brief Setter for the xid field. + /** + * \brief Setter for the xid field. * \param new_xid The xid to be set. */ void xid(uint32_t new_xid); - /** \brief Setter for the secs field. + /** + * \brief Setter for the secs field. * \param new_secs The secs to be set. */ void secs(uint16_t new_secs); - /** \brief Setter for the padding field. + /** + * \brief Setter for the padding field. * \param new_padding The padding to be set. */ void padding(uint16_t new_padding); - /** \brief Setter for the ciaddr field. + /** + * \brief Setter for the ciaddr field. * \param new_ciaddr The ciaddr to be set. */ - void ciaddr(uint32_t new_ciaddr); + void ciaddr(IPv4Address new_ciaddr); - /** \brief Setter for the yiaddr field. + /** + * \brief Setter for the yiaddr field. * \param new_yiaddr The yiaddr to be set. */ - void yiaddr(uint32_t new_yiaddr); + void yiaddr(IPv4Address new_yiaddr); - /** \brief Setter for the siaddr field. + /** + * \brief Setter for the siaddr field. * \param new_siaddr The siaddr to be set. */ - void siaddr(uint32_t new_siaddr); + void siaddr(IPv4Address new_siaddr); - /** \brief Setter for the giaddr field. + /** + * \brief Setter for the giaddr field. * \param new_giaddr The giaddr to be set. */ - void giaddr(uint32_t new_giaddr); + void giaddr(IPv4Address new_giaddr); - /** \brief Setter for the chaddr field. + /** + * \brief Setter for the chaddr field. * 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); - /** \brief Setter for the sname field. + /** + * \brief Setter for the sname field. * \param new_sname The sname to be set. */ void sname(const uint8_t *new_sname); - /** \brief Setter for the file field. + /** + * \brief Setter for the file field. * \param new_file The file to be set. */ void file(const uint8_t *new_file); - /** \brief Setter for the vend field. + /** + * \brief Setter for the vend field. * \param new_vend The vend to be set. * \param size The size of the new vend field. */ diff --git a/include/ipaddress.h b/include/ipaddress.h index 2b2967f..81a9052 100644 --- a/include/ipaddress.h +++ b/include/ipaddress.h @@ -25,16 +25,18 @@ #include #include #include -#include "utils.h" namespace Tins { class IPv4Address { public: - IPv4Address(uint32_t ip = 0) : ip_addr(ip) {} - IPv4Address(const std::string &ip) : ip_addr(Utils::ip_to_int(ip)) {} + IPv4Address(uint32_t ip = 0); + IPv4Address(const std::string &ip); - operator uint32_t() const { return Utils::net_to_host_l(ip_addr); } - operator std::string() const { return Utils::ip_to_string(ip_addr); } + IPv4Address &operator=(uint32_t ip); + IPv4Address &operator=(const std::string &ip); + + operator uint32_t() const; + operator std::string() const; friend std::ostream &operator<<(std::ostream &output, const IPv4Address &addr) { return output << (std::string)addr; @@ -42,7 +44,6 @@ namespace Tins { private: uint32_t ip_addr; }; - }; diff --git a/include/utils.h b/include/utils.h index c6c2bac..d1425ad 100644 --- a/include/utils.h +++ b/include/utils.h @@ -31,6 +31,7 @@ #include #include #include "packetsender.h" +#include "ipaddress.h" namespace Tins { /** \brief Network utils namespace. @@ -44,7 +45,7 @@ namespace Tins { * \brief Struct that represents an interface's information. */ struct InterfaceInfo { - uint32_t ip_addr, netmask, bcast_addr; + IPv4Address ip_addr, netmask, bcast_addr; uint8_t hw_addr[6]; }; @@ -102,7 +103,7 @@ namespace Tins { * \return PDU * containing either 0 if no response was received, * or the ICMP response otherwise. */ - PDU *ping_address(uint32_t ip, PacketSender *sender, uint32_t ip_src = 0); + PDU *ping_address(uint32_t ip, PacketSender *sender, IPv4Address ip_src = 0); /** \brief Resolves the hardware address for a given ip. * @@ -113,7 +114,8 @@ namespace Tins { * \return Returns true if the hardware address was resolved successfully, * false otherwise. */ - bool resolve_hwaddr(const std::string &iface, uint32_t ip, uint8_t *buffer, PacketSender *sender); + bool resolve_hwaddr(const std::string &iface, IPv4Address ip, + uint8_t *buffer, PacketSender *sender); /** \brief List all network interfaces. * @@ -132,7 +134,7 @@ namespace Tins { * * \return bool indicating wether the operation was successfull. */ - bool interface_ip(const std::string &iface, uint32_t &ip); + bool interface_ip(const std::string &iface, IPv4Address &ip); /** * \brief Lookup the ip/hw/netmask/broadcast address of the @@ -178,7 +180,7 @@ namespace Tins { * \param ip The ip of the interface we are looking for. * \return The interface's name. */ - std::string interface_from_ip(uint32_t ip); + std::string interface_from_ip(IPv4Address ip); /** * \brief Finds the gateway's IP address for the given IP @@ -191,7 +193,7 @@ namespace Tins { * * \return bool indicating wether the lookup was successfull. */ - bool gateway_from_ip(uint32_t ip, uint32_t &gw_addr) ; + bool gateway_from_ip(IPv4Address ip, IPv4Address &gw_addr) ; /** \brief Convert 16 bit integer into network byte order. diff --git a/src/arp.cpp b/src/arp.cpp index 63620f9..806cd7d 100644 --- a/src/arp.cpp +++ b/src/arp.cpp @@ -67,7 +67,7 @@ void Tins::ARP::sender_hw_addr(const uint8_t* new_snd_hw_addr) { } void Tins::ARP::sender_ip_addr(IPv4Address new_snd_ip_addr) { - this->_arp.ar_sip = Utils::net_to_host_l(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) { @@ -75,7 +75,7 @@ void Tins::ARP::target_hw_addr(const uint8_t* new_tgt_hw_addr) { } void Tins::ARP::target_ip_addr(IPv4Address new_tgt_ip_addr) { - this->_arp.ar_tip = Utils::net_to_host_l(new_tgt_ip_addr); + this->_arp.ar_tip = new_tgt_ip_addr; } void Tins::ARP::hw_addr_format(uint16_t new_hw_addr_fmt) { diff --git a/src/bootp.cpp b/src/bootp.cpp index fc63073..68e7ecc 100644 --- a/src/bootp.cpp +++ b/src/bootp.cpp @@ -69,20 +69,20 @@ void Tins::BootP::padding(uint16_t new_padding) { _bootp.padding = Utils::net_to_host_s(new_padding); } -void Tins::BootP::ciaddr(uint32_t new_ciaddr) { - _bootp.ciaddr = Utils::net_to_host_l(new_ciaddr); +void Tins::BootP::ciaddr(IPv4Address new_ciaddr) { + _bootp.ciaddr = new_ciaddr; } -void Tins::BootP::yiaddr(uint32_t new_yiaddr) { - _bootp.yiaddr = Utils::net_to_host_l(new_yiaddr); +void Tins::BootP::yiaddr(IPv4Address new_yiaddr) { + _bootp.yiaddr = new_yiaddr; } -void Tins::BootP::siaddr(uint32_t new_siaddr) { - _bootp.siaddr = Utils::net_to_host_l(new_siaddr); +void Tins::BootP::siaddr(IPv4Address new_siaddr) { + _bootp.siaddr = new_siaddr; } -void Tins::BootP::giaddr(uint32_t new_giaddr) { - _bootp.giaddr = Utils::net_to_host_l(new_giaddr); +void Tins::BootP::giaddr(IPv4Address new_giaddr) { + _bootp.giaddr = new_giaddr; } void Tins::BootP::chaddr(const uint8_t *new_chaddr) { diff --git a/src/utils.cpp b/src/utils.cpp index 9870101..140ee6c 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -214,7 +214,7 @@ uint32_t Tins::Utils::resolve_ip(const string &to_resolve) throw (std::runtime_e return Utils::net_to_host_l(((struct in_addr**)data->h_addr_list)[0]->s_addr); } -Tins::PDU *Tins::Utils::ping_address(uint32_t ip, PacketSender *sender, uint32_t ip_src) { +Tins::PDU *Tins::Utils::ping_address(uint32_t ip, PacketSender *sender, IPv4Address ip_src) { ICMP *icmp = new ICMP(ICMP::ECHO_REQUEST); if(!ip_src) { std::string iface(Utils::interface_from_ip(ip)); @@ -225,8 +225,9 @@ Tins::PDU *Tins::Utils::ping_address(uint32_t ip, PacketSender *sender, uint32_t return sender->send_recv(&ip_packet); } -bool Tins::Utils::resolve_hwaddr(const string &iface, uint32_t ip, uint8_t *buffer, PacketSender *sender) { - uint32_t my_ip; +bool Tins::Utils::resolve_hwaddr(const string &iface, IPv4Address ip, + uint8_t *buffer, PacketSender *sender) { + IPv4Address my_ip; uint8_t my_hw[6]; if(!interface_ip(iface, my_ip) || !interface_hwaddr(iface, my_hw)) return false; @@ -244,15 +245,14 @@ bool Tins::Utils::resolve_hwaddr(const string &iface, uint32_t ip, uint8_t *buff return false; } -string Tins::Utils::interface_from_ip(uint32_t ip) { +string Tins::Utils::interface_from_ip(IPv4Address ip) { if(ip == 0x7f000001) return "lo"; ifstream input("/proc/net/route"); bool match(false); string iface; string destination, mask; - uint32_t destination_int, mask_int; - ip = Utils::net_to_host_l(ip); + uint32_t destination_int, mask_int, ip_int = ip; skip_line(input); while(!match) { input >> iface >> destination; @@ -260,14 +260,14 @@ string Tins::Utils::interface_from_ip(uint32_t ip) { input >> mask; from_hex(destination, destination_int); from_hex(mask, mask_int); - if((ip & mask_int) == destination_int) + if((ip_int & mask_int) == destination_int) return iface; skip_line(input); } return ""; } -bool Tins::Utils::gateway_from_ip(uint32_t ip, uint32_t &gw_addr) { +bool Tins::Utils::gateway_from_ip(IPv4Address ip, IPv4Address &gw_addr) { ifstream input("/proc/net/route"); bool match(false); string iface; @@ -282,8 +282,9 @@ bool Tins::Utils::gateway_from_ip(uint32_t ip, uint32_t &gw_addr) { from_hex(destination, destination_int); from_hex(mask, mask_int); if((ip & mask_int) == destination_int) { - from_hex(gw, gw_addr); - gw_addr = net_to_host_l(gw_addr); + uint32_t tmp_ip; + from_hex(gw, tmp_ip); + gw_addr = net_to_host_l(tmp_ip); return true; } skip_line(input); @@ -297,7 +298,7 @@ set Tins::Utils::network_interfaces() { return collector.ifaces; } -bool Tins::Utils::interface_ip(const string &iface, uint32_t &ip) { +bool Tins::Utils::interface_ip(const string &iface, IPv4Address &ip) { IPv4Collector collector(iface.c_str()); generic_iface_loop(collector); ip = Utils::net_to_host_l(collector.ip); diff --git a/tests/src/arp.cpp b/tests/src/arp.cpp index ba011c6..074607f 100644 --- a/tests/src/arp.cpp +++ b/tests/src/arp.cpp @@ -4,6 +4,7 @@ #include #include "arp.h" #include "utils.h" +#include "ipaddress.h" using namespace std; @@ -16,13 +17,14 @@ public: static const uint8_t hw_addr2[]; static const string ip_addr1; static const uint8_t expected_packet[]; + static const IPv4Address addr1, addr2; }; const uint8_t ARPTest::empty_addr[] = {'\x00', '\x00', '\x00', '\x00', '\x00', '\x00'}; const uint8_t ARPTest::hw_addr1[] = {'\x13', '\xda', '\xde', '\xf1', '\x01', '\x85'}; const uint8_t ARPTest::hw_addr2[] = {'\x7a', '\x1f', '\xf4', '\x39', '\xab', '\x0d'}; -const string ARPTest::ip_addr1("192.168.0.154"); 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); TEST_F(ARPTest, DefaultContructor) { ARP arp; @@ -34,7 +36,7 @@ TEST_F(ARPTest, DefaultContructor) { } TEST_F(ARPTest, CopyContructor) { - ARP arp1(0x1234, 0xa3f1, hw_addr1, hw_addr2); + 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()); @@ -48,7 +50,7 @@ TEST_F(ARPTest, CopyContructor) { } TEST_F(ARPTest, CopyAssignmentOperator) { - ARP arp1(0x1234, 0xa3f1, hw_addr1, hw_addr2); + 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()); @@ -62,35 +64,23 @@ TEST_F(ARPTest, CopyAssignmentOperator) { } TEST_F(ARPTest, CompleteContructor) { - ARP arp(0x1234, 0xa3f1, hw_addr1, hw_addr2); + ARP arp(addr1, addr2, hw_addr1, hw_addr2); EXPECT_TRUE(memcmp(arp.target_hw_addr(), hw_addr1, sizeof(hw_addr1)) == 0); EXPECT_TRUE(memcmp(arp.sender_hw_addr(), hw_addr2, sizeof(hw_addr2)) == 0); - EXPECT_EQ(arp.target_ip_addr(), 0x1234); - EXPECT_EQ(arp.sender_ip_addr(), 0xa3f1); -} - -TEST_F(ARPTest, SenderIPAddrString) { - ARP arp; - arp.sender_ip_addr(ip_addr1); - EXPECT_EQ(arp.sender_ip_addr(), Utils::ip_to_int(ip_addr1)); + EXPECT_EQ(arp.target_ip_addr(), addr1); + EXPECT_EQ(arp.sender_ip_addr(), addr2); } TEST_F(ARPTest, SenderIPAddrInt) { ARP arp; - arp.sender_ip_addr(Utils::ip_to_int(ip_addr1)); - EXPECT_EQ(arp.sender_ip_addr(), Utils::ip_to_int(ip_addr1)); -} - -TEST_F(ARPTest, TargetIPAddrString) { - ARP arp; - arp.target_ip_addr(ip_addr1); - EXPECT_EQ(arp.target_ip_addr(), Utils::ip_to_int(ip_addr1)); + arp.sender_ip_addr(addr1); + EXPECT_EQ(arp.sender_ip_addr(), addr1); } TEST_F(ARPTest, TargetIPAddrInt) { ARP arp; - arp.target_ip_addr(Utils::ip_to_int(ip_addr1)); - EXPECT_EQ(arp.target_ip_addr(), Utils::ip_to_int(ip_addr1)); + arp.target_ip_addr(addr1); + EXPECT_EQ(arp.target_ip_addr(), addr1); } TEST_F(ARPTest, TargetHWAddr) { diff --git a/tests/src/dhcp.cpp b/tests/src/dhcp.cpp index 61b0e89..76bf159 100644 --- a/tests/src/dhcp.cpp +++ b/tests/src/dhcp.cpp @@ -7,6 +7,7 @@ #include "dhcp.h" #include "utils.h" #include "ethernetII.h" +#include "ipaddress.h" using namespace std; using namespace Tins; @@ -15,6 +16,7 @@ class DHCPTest : public testing::Test { public: static const uint8_t expected_packet[]; static const uint8_t chaddr[], 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); @@ -33,6 +35,7 @@ const uint8_t DHCPTest::file[] = "\x16\xab\x54\x12\xfa\xca\x56\x7f\x1b\x65\x11\x "\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" "\x16\xab\x54\x12\xfa\xca\x56\x7f\x1b\x65\x11\xfa\xda\xfb\x19\x18"; +const IPv4Address DHCPTest::addr("192.168.8.1"); const uint8_t DHCPTest::expected_packet[] = {'\x01', '\x01', '\x06', '\x1f', '?', '\xab', '#', '\xde', '\x9f', '\x1a', '\x00', '\x00', '\xc0', '\xa8', '\x00', 'f', '\xf3', '\x16', '"', 'b', '\xa7', ' ', @@ -105,26 +108,26 @@ TEST_F(DHCPTest, Padding) { TEST_F(DHCPTest, Ciaddr) { DHCP dhcp; - dhcp.ciaddr(0x71bd167c); - EXPECT_EQ(dhcp.ciaddr(), 0x71bd167c); + dhcp.ciaddr(addr); + EXPECT_EQ(dhcp.ciaddr(), addr); } TEST_F(DHCPTest, Yiaddr) { DHCP dhcp; - dhcp.yiaddr(0x71bd167c); - EXPECT_EQ(dhcp.yiaddr(), 0x71bd167c); + dhcp.yiaddr(addr); + EXPECT_EQ(dhcp.yiaddr(), addr); } TEST_F(DHCPTest, Siaddr) { DHCP dhcp; - dhcp.siaddr(0x71bd167c); - EXPECT_EQ(dhcp.siaddr(), 0x71bd167c); + dhcp.siaddr(addr); + EXPECT_EQ(dhcp.siaddr(), addr); } TEST_F(DHCPTest, Giaddr) { DHCP dhcp; - dhcp.giaddr(0x71bd167c); - EXPECT_EQ(dhcp.giaddr(), 0x71bd167c); + dhcp.giaddr(addr); + EXPECT_EQ(dhcp.giaddr(), addr); } TEST_F(DHCPTest, Chaddr) { diff --git a/tests/src/utils_test.cpp b/tests/src/utils_test.cpp index ab331a9..a4de847 100644 --- a/tests/src/utils_test.cpp +++ b/tests/src/utils_test.cpp @@ -1,10 +1,8 @@ - #include - #include - -#include "utils.h" #include +#include "utils.h" +#include "ipaddress.h" using namespace Tins; @@ -103,7 +101,6 @@ TEST_F(UtilsTest, HwaddrToString) { } TEST_F(UtilsTest, ResolveIp) { - uint32_t localhost_ip = Utils::ip_to_int("127.0.0.1"); EXPECT_EQ(Utils::resolve_ip("localhost"), localhost_ip); @@ -112,9 +109,8 @@ TEST_F(UtilsTest, ResolveIp) { } TEST_F(UtilsTest, InterfaceIp) { - - uint32_t ip; - uint32_t localhost_ip = Utils::ip_to_int("127.0.0.1"); + IPv4Address ip; + IPv4Address localhost_ip = Utils::ip_to_int("127.0.0.1"); #ifndef WIN32 ASSERT_TRUE(Utils::interface_ip("lo", ip)); @@ -125,7 +121,6 @@ TEST_F(UtilsTest, InterfaceIp) { } TEST_F(UtilsTest, NetToHostS) { - uint16_t a = 0x01FE; uint16_t b = Utils::net_to_host_s(a); @@ -135,7 +130,6 @@ TEST_F(UtilsTest, NetToHostS) { } TEST_F(UtilsTest, NetToHostL) { - uint32_t a = 0x0102CDFE; uint32_t b = Utils::net_to_host_l(a); @@ -145,7 +139,6 @@ TEST_F(UtilsTest, NetToHostL) { } TEST_F(UtilsTest, NetToHostLL) { - uint64_t a = 0x0102030489ABCDFE; uint64_t b = Utils::net_to_host_ll(a);