dhcp_client: use ByteString for hardware address
Since the hardware address of network device may contain
0 in the middle, using std::string could be ambiguous.
We change the type from std::string to shill::ByteString
to fix that.
Bug: 25642025
TEST=compile and unittests
Change-Id: I301cd9c351e02e7509bfbb4772c9cfbd5b00a668
diff --git a/device_info.cc b/device_info.cc
index 846949b..3ab7005 100644
--- a/device_info.cc
+++ b/device_info.cc
@@ -25,6 +25,7 @@
#include <base/logging.h>
+using shill::ByteString;
using shill::Sockets;
using shill::RTNLHandler;
using std::unique_ptr;
@@ -50,7 +51,7 @@
}
bool DeviceInfo::GetDeviceInfo(const std::string& interface_name,
- std::string* mac_address,
+ ByteString* mac_address,
unsigned int* interface_index ) {
struct ifreq ifr;
size_t if_name_len = interface_name.size();
@@ -78,7 +79,7 @@
return false;
}
*interface_index = if_index;
- *mac_address = std::string(ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
+ *mac_address = ByteString(ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
return true;
}
diff --git a/device_info.h b/device_info.h
index 6b4b487..4cb6c19 100644
--- a/device_info.h
+++ b/device_info.h
@@ -23,6 +23,7 @@
#include <base/lazy_instance.h>
#include <base/macros.h>
+#include "shill/net/byte_string.h"
#include "shill/net/rtnl_handler.h"
#include "shill/net/sockets.h"
@@ -33,7 +34,7 @@
virtual ~DeviceInfo();
static DeviceInfo* GetInstance();
bool GetDeviceInfo(const std::string& interface_name,
- std::string* mac_address,
+ shill::ByteString* mac_address,
unsigned int* interface_index);
protected:
DeviceInfo();
diff --git a/device_info_unittest.cc b/device_info_unittest.cc
index 04d22f1..cd0bd89 100644
--- a/device_info_unittest.cc
+++ b/device_info_unittest.cc
@@ -22,9 +22,11 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
+#include <shill/net/byte_string.h>
#include <shill/net/mock_sockets.h>
#include <shill/net/mock_rtnl_handler.h>
+using shill::ByteString;
using shill::MockRTNLHandler;
using shill::MockSockets;
using ::testing::_;
@@ -73,7 +75,7 @@
}
TEST_F(DeviceInfoTest, GetDeviceInfoSucceed) {
- std::string mac_address;
+ ByteString mac_address;
unsigned int interface_index;
struct ifreq ifr;
memcpy(ifr.ifr_hwaddr.sa_data, kFakeMacAddress, sizeof(kFakeMacAddress));
@@ -90,11 +92,11 @@
&interface_index));
EXPECT_EQ(interface_index, kFakeInterfaceIndex);
EXPECT_THAT(kFakeMacAddress,
- ElementsAreArray(mac_address.data(), sizeof(kFakeMacAddress)));
+ ElementsAreArray(mac_address.GetData(), sizeof(kFakeMacAddress)));
}
TEST_F(DeviceInfoTest, GetDeviceInfoNameTooLong) {
- std::string mac_address;
+ ByteString mac_address;
unsigned int interface_index;
EXPECT_FALSE(device_info_->GetDeviceInfo(kFakeLongDeviceName,
&mac_address,
@@ -102,7 +104,7 @@
}
TEST_F(DeviceInfoTest, GetDeviceInfoFailedToCreateSocket) {
- std::string mac_address;
+ ByteString mac_address;
unsigned int interface_index;
EXPECT_CALL(*sockets_, Socket(AF_INET, SOCK_DGRAM, 0)).WillOnce(Return(-1));
EXPECT_FALSE(device_info_->GetDeviceInfo(kFakeDeviceName,
@@ -111,7 +113,7 @@
}
TEST_F(DeviceInfoTest, GetDeviceInfoFailedToGetHardwareAddr) {
- std::string mac_address;
+ ByteString mac_address;
unsigned int interface_index;
EXPECT_CALL(*sockets_, Socket(AF_INET, SOCK_DGRAM, 0))
.WillOnce(Return(kFakeFd));
@@ -122,7 +124,7 @@
}
TEST_F(DeviceInfoTest, GetDeviceInfoFailedToGetInterfaceIndex) {
- std::string mac_address;
+ ByteString mac_address;
unsigned int interface_index;
EXPECT_CALL(*sockets_, Socket(AF_INET, SOCK_DGRAM, 0))
.WillOnce(Return(kFakeFd));
diff --git a/dhcp_message.cc b/dhcp_message.cc
index 7bd4f28..20100c8 100644
--- a/dhcp_message.cc
+++ b/dhcp_message.cc
@@ -27,6 +27,8 @@
#include <base/logging.h>
+using shill::ByteString;
+
namespace dhcp_client {
namespace {
@@ -121,9 +123,9 @@
message->next_server_ip_address_ = ntohl(raw_message->siaddr);
message->agent_ip_address_ = ntohl(raw_message->giaddr);
message->cookie_ = ntohl(raw_message->cookie);
- message->client_hardware_address_
- .assign(reinterpret_cast<const char*>(raw_message->chaddr),
- message->hardware_address_length_);
+ message->client_hardware_address_ = ByteString(
+ reinterpret_cast<const char*>(raw_message->chaddr),
+ message->hardware_address_length_);
message->servername_.assign(reinterpret_cast<const char*>(raw_message->sname),
kServerNameLength);
message->bootfile_.assign(reinterpret_cast<const char*>(raw_message->file),
diff --git a/dhcp_message.h b/dhcp_message.h
index da12d6c..f8d2634 100644
--- a/dhcp_message.h
+++ b/dhcp_message.h
@@ -25,6 +25,7 @@
#include <base/macros.h>
#include <dhcp_client/dhcp_options_parser.h>
+#include <shill/net/byte_string.h>
namespace dhcp_client {
@@ -72,7 +73,7 @@
const std::vector<uint32_t>& dns_server() const {return dns_server_;}
- const std::string& client_hardware_address() const {
+ const shill::ByteString& client_hardware_address() const {
return client_hardware_address_;
}
@@ -107,7 +108,7 @@
// It should be zero in client's messages.
uint32_t agent_ip_address_;
// Client's hardware address.
- std::string client_hardware_address_;
+ shill::ByteString client_hardware_address_;
// Server host name.
std::string servername_;
// Boot file name.
diff --git a/dhcpv4.cc b/dhcpv4.cc
index c69b589..184fe19 100644
--- a/dhcpv4.cc
+++ b/dhcpv4.cc
@@ -24,6 +24,7 @@
using base::Bind;
using base::Unretained;
+using shill::ByteString;
using shill::IOHandlerFactoryContainer;
namespace dhcp_client {
@@ -52,7 +53,7 @@
} // namespace
DHCPV4::DHCPV4(const std::string& interface_name,
- const std::string& hardware_address,
+ const ByteString& hardware_address,
unsigned int interface_index,
const std::string& network_id,
bool request_hostname,
diff --git a/dhcpv4.h b/dhcpv4.h
index 92f07c6..f0132c8 100644
--- a/dhcpv4.h
+++ b/dhcpv4.h
@@ -24,6 +24,7 @@
#include "dhcp_client/dhcp.h"
#include "dhcp_client/event_dispatcher_interface.h"
+#include "shill/net/byte_string.h"
#include "shill/net/io_handler_factory_container.h"
#include "shill/net/sockets.h"
@@ -32,7 +33,7 @@
class DHCPV4 : public DHCP {
public:
DHCPV4(const std::string& interface_name,
- const std::string& hardware_address,
+ const shill::ByteString& hardware_address,
unsigned int interface_index,
const std::string& network_id,
bool request_hostname,
@@ -48,7 +49,7 @@
private:
// Interface parameters.
std::string interface_name_;
- std::string hardware_address_;
+ shill::ByteString hardware_address_;
unsigned int interface_index_;
// Unique network/connection identifier,
diff --git a/service.h b/service.h
index 6adbc12..b0044aa 100644
--- a/service.h
+++ b/service.h
@@ -26,6 +26,7 @@
#include "dhcp_client/dhcp.h"
#include "dhcp_client/dhcpv4.h"
#include "dhcp_client/event_dispatcher_interface.h"
+#include "shill/net/byte_string.h"
namespace dhcp_client {
@@ -49,7 +50,7 @@
EventDispatcherInterface* event_dispatcher_;
// Interface parameters.
std::string interface_name_;
- std::string hardware_address_;
+ shill::ByteString hardware_address_;
unsigned int interface_index_;
// Unique network/connection identifier,