Commit 11bdd1c7 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS PhoneHub] Fix MessageType endianness

Before this CL, MessageSenderImpl and MessageReceiverImpl assumed that
the phone and Chrome OS device had the same endianness. This CL fixes
the issue by converting from host to network endianness when sending
messages and converting from network to host endianness when receiving
messages.

Bug: 1106937
Change-Id: Idaa3b56f9450030aaf2c5460a2cae18ec4869d82
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2453271
Auto-Submit: Kyle Horimoto <khorimoto@chromium.org>
Commit-Queue: Regan Hsu <hsuregan@chromium.org>
Reviewed-by: default avatarJimmy Gong <jimmyxgong@chromium.org>
Reviewed-by: default avatarRegan Hsu <hsuregan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814370}
parent eb71611d
...@@ -3,12 +3,13 @@ ...@@ -3,12 +3,13 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "chromeos/components/phonehub/message_receiver_impl.h" #include "chromeos/components/phonehub/message_receiver_impl.h"
#include "chromeos/components/phonehub/proto/phonehub_api.pb.h"
#include <netinet/in.h>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include "base/logging.h" #include "base/logging.h"
#include "chromeos/components/phonehub/proto/phonehub_api.pb.h"
namespace chromeos { namespace chromeos {
namespace phonehub { namespace phonehub {
...@@ -55,7 +56,8 @@ void MessageReceiverImpl::OnMessageReceived(const std::string& payload) { ...@@ -55,7 +56,8 @@ void MessageReceiverImpl::OnMessageReceived(const std::string& payload) {
// proto::MessageType. // proto::MessageType.
uint16_t* ptr = uint16_t* ptr =
reinterpret_cast<uint16_t*>(const_cast<char*>(payload.data())); reinterpret_cast<uint16_t*>(const_cast<char*>(payload.data()));
proto::MessageType message_type = static_cast<proto::MessageType>(*ptr); proto::MessageType message_type =
static_cast<proto::MessageType>(ntohs(*ptr));
PA_LOG(INFO) << "MessageReceiver received a " PA_LOG(INFO) << "MessageReceiver received a "
<< GetMessageTypeName(message_type) << " message."; << GetMessageTypeName(message_type) << " message.";
......
...@@ -4,13 +4,14 @@ ...@@ -4,13 +4,14 @@
#include "chromeos/components/phonehub/message_receiver_impl.h" #include "chromeos/components/phonehub/message_receiver_impl.h"
#include <netinet/in.h>
#include <memory>
#include "base/strings/strcat.h" #include "base/strings/strcat.h"
#include "chromeos/components/phonehub/fake_connection_manager.h" #include "chromeos/components/phonehub/fake_connection_manager.h"
#include "chromeos/components/phonehub/proto/phonehub_api.pb.h" #include "chromeos/components/phonehub/proto/phonehub_api.pb.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include <memory>
namespace chromeos { namespace chromeos {
namespace phonehub { namespace phonehub {
namespace { namespace {
...@@ -62,7 +63,7 @@ std::string SerializeMessage(proto::MessageType message_type, ...@@ -62,7 +63,7 @@ std::string SerializeMessage(proto::MessageType message_type,
// Replace the first two characters with |message_type| as a 16-bit int. // Replace the first two characters with |message_type| as a 16-bit int.
uint16_t* ptr = uint16_t* ptr =
reinterpret_cast<uint16_t*>(const_cast<char*>(message.data())); reinterpret_cast<uint16_t*>(const_cast<char*>(message.data()));
*ptr = static_cast<uint16_t>(message_type); *ptr = htons(static_cast<uint16_t>(message_type));
return message; return message;
} }
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chromeos/components/phonehub/message_sender_impl.h" #include "chromeos/components/phonehub/message_sender_impl.h"
#include <netinet/in.h>
#include "base/strings/strcat.h" #include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chromeos/components/phonehub/connection_manager.h" #include "chromeos/components/phonehub/connection_manager.h"
...@@ -21,7 +23,7 @@ std::string SerializeMessage(proto::MessageType message_type, ...@@ -21,7 +23,7 @@ std::string SerializeMessage(proto::MessageType message_type,
// Replace the first two characters with |message_type| as a 16-bit int. // Replace the first two characters with |message_type| as a 16-bit int.
uint16_t* ptr = uint16_t* ptr =
reinterpret_cast<uint16_t*>(const_cast<char*>(message.data())); reinterpret_cast<uint16_t*>(const_cast<char*>(message.data()));
*ptr = static_cast<uint16_t>(message_type); *ptr = htons(static_cast<uint16_t>(message_type));
return message; return message;
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chromeos/components/phonehub/message_sender_impl.h" #include "chromeos/components/phonehub/message_sender_impl.h"
#include <netinet/in.h>
#include <stdint.h> #include <stdint.h>
#include <memory> #include <memory>
#include <string> #include <string>
...@@ -39,7 +40,7 @@ class MessageSenderImplTest : public testing::Test { ...@@ -39,7 +40,7 @@ class MessageSenderImplTest : public testing::Test {
// bytes of |actual_message|. // bytes of |actual_message|.
uint16_t* actual_message_uint16t_ptr = uint16_t* actual_message_uint16t_ptr =
reinterpret_cast<uint16_t*>(const_cast<char*>(actual_message.data())); reinterpret_cast<uint16_t*>(const_cast<char*>(actual_message.data()));
EXPECT_EQ(static_cast<uint16_t>(expected_message_type), EXPECT_EQ(ntohs(static_cast<uint16_t>(expected_message_type)),
*actual_message_uint16t_ptr); *actual_message_uint16t_ptr);
const std::string& expected_proto_message = const std::string& expected_proto_message =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment