From cc89faa334e87cd29359f59e8737583cbfced74b Mon Sep 17 00:00:00 2001
From: Sara Golemon <sgolemon@fb.com>
Date: Tue, 23 Jun 2015 15:44:37 -0700
Subject: [PATCH] Provide backfill s6_addr16 on BSD platforms

Summary: Extend the MacOS s6_addr16 hack to Net/Open/FreeBSD

Closes #106

Reviewed By: @JoelMarcey

Differential Revision: D2182912
---
 folly/detail/IPAddress.h     |  8 ++++++--
 folly/test/IPAddressTest.cpp | 13 +++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/folly/detail/IPAddress.h b/folly/detail/IPAddress.h
index 5d7df8cde..0ebd76f74 100644
--- a/folly/detail/IPAddress.h
+++ b/folly/detail/IPAddress.h
@@ -46,8 +46,12 @@ extern "C" {
 #include <folly/Conv.h>
 #include <folly/Format.h>
 
-#if defined(__APPLE__) && !defined(s6_addr16)
-# define s6_addr16 __u6_addr.__u6_addr16
+// BSDish platforms don't provide standard access to s6_addr16
+#ifndef s6_addr16
+# if defined(__APPLE__) || defined(__FreeBSD__) || \
+     defined(__NetBSD__) || defined(__OpenBSD__)
+#  define s6_addr16 __u6_addr.__u6_addr16
+# endif
 #endif
 
 namespace folly { namespace detail {
diff --git a/folly/test/IPAddressTest.cpp b/folly/test/IPAddressTest.cpp
index 2e62f5d17..cb1bc108c 100644
--- a/folly/test/IPAddressTest.cpp
+++ b/folly/test/IPAddressTest.cpp
@@ -815,6 +815,19 @@ TEST(IPAddress, InvalidBBitAccess) {
   EXPECT_THROW(asV6.getNthLSBit(-1), std::invalid_argument);
 }
 
+TEST(IPAddress, StringFormat) {
+  in6_addr a6;
+  for (int i = 0; i < 8; ++i) {
+    a6.s6_addr16[i] = htons(0x0123 + ((i%4) * 0x4444));
+  }
+  EXPECT_EQ("0123:4567:89ab:cdef:0123:4567:89ab:cdef",
+            detail::fastIpv6ToString(a6));
+
+  in_addr a4;
+  a4.s_addr = htonl(0x01020304);
+  EXPECT_EQ("1.2.3.4", detail::fastIpv4ToString(a4));
+}
+
 TEST(IPAddress, LongestCommonPrefix) {
   IPAddress ip10("10.0.0.0");
   IPAddress ip11("11.0.0.0");
-- 
2.26.2