patchpanel: fix Ipv4Addr() in big endian device

Originally, Ipv4Addr() supposes the host order is little endian.
However, the implementation is wrong for the device with big endian.

This CL uses htonl() to guarantee the endian is correct.

BUG=b:279693340
TEST=FEATURES=test emerge-hatch patchpanel

Change-Id: I70d05695ba76e246b5360c37de20e39b08c5836a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/4493682
Reviewed-by: Hugo Benichi <hugobenichi@google.com>
Tested-by: Chih-Yu Huang <akahuang@chromium.org>
Commit-Queue: Chih-Yu Huang <akahuang@chromium.org>
diff --git a/patchpanel/datapath.cc b/patchpanel/datapath.cc
index 82608bd..e9fa4f0 100644
--- a/patchpanel/datapath.cc
+++ b/patchpanel/datapath.cc
@@ -150,10 +150,11 @@
 // TODO(b/279693340): Remove the function after all IPv4 address represented by
 // uint32_t are migrated to shill::IPv4Address.
 shill::IPv4Address ConvertUint32ToIPv4Address(uint32_t addr) {
-  return shill::IPv4Address(static_cast<uint8_t>(addr & 0xff),
-                            static_cast<uint8_t>((addr >> 8) & 0xff),
-                            static_cast<uint8_t>((addr >> 16) & 0xff),
-                            static_cast<uint8_t>((addr >> 24) & 0xff));
+  const uint32_t host_endian = ntohl(addr);
+  return shill::IPv4Address(static_cast<uint8_t>((host_endian >> 24) & 0xff),
+                            static_cast<uint8_t>((host_endian >> 16) & 0xff),
+                            static_cast<uint8_t>((host_endian >> 8) & 0xff),
+                            static_cast<uint8_t>(host_endian & 0xff));
 }
 
 }  // namespace
diff --git a/patchpanel/net_util.h b/patchpanel/net_util.h
index badb2eb..3418bc1 100644
--- a/patchpanel/net_util.h
+++ b/patchpanel/net_util.h
@@ -24,6 +24,7 @@
 #include <vector>
 
 #include <base/strings/stringprintf.h>
+#include <base/sys_byteorder.h>
 #include <brillo/brillo_export.h>
 
 #include "patchpanel/mac_address_generator.h"
@@ -39,8 +40,10 @@
                                           uint8_t b1,
                                           uint8_t b2,
                                           uint8_t b3) {
-  return (static_cast<uint32_t>(b3) << 24) | (static_cast<uint32_t>(b2) << 16) |
-         (static_cast<uint32_t>(b1) << 8) | static_cast<uint32_t>(b0);
+  // Use base::HostToNet32() to keep the function constexpr.
+  return base::HostToNet32(
+      (static_cast<uint32_t>(b0) << 24) | (static_cast<uint32_t>(b1) << 16) |
+      (static_cast<uint32_t>(b2) << 8) | static_cast<uint32_t>(b3));
 }
 
 // Returns the network-byte order int32 representation of the IPv4 address.