From dc93938852aa64604563afbc8d185062f01153f2 Mon Sep 17 00:00:00 2001 From: Subodh Iyengar Date: Mon, 4 Jul 2016 22:46:06 -0700 Subject: [PATCH] Add sa_len for sockaddr conversions Summary: Some platforms like Apple add a additional field to sockaddr called sa_len. This is not normally required by POSIX, so all posix methods work correctly when a sockaddr is passed in without a sa_len set. However other functions which are not defined by posix such as connectx require this field to operate correctly. Reviewed By: yfeldblum Differential Revision: D3514266 fbshipit-source-id: f8e2941f337222486c81c911dbd06a2ce35e4f00 --- folly/IPAddress.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/folly/IPAddress.h b/folly/IPAddress.h index 52b00a8a..c5f31c77 100644 --- a/folly/IPAddress.h +++ b/folly/IPAddress.h @@ -201,16 +201,23 @@ class IPAddress : boost::totally_ordered { } memset(dest, 0, sizeof(sockaddr_storage)); dest->ss_family = family(); + if (isV4()) { sockaddr_in *sin = reinterpret_cast(dest); sin->sin_addr = asV4().toAddr(); sin->sin_port = port; +#if defined(__APPLE__) + sin->sin_len = sizeof(*sin); +#endif return sizeof(*sin); } else if (isV6()) { sockaddr_in6 *sin = reinterpret_cast(dest); sin->sin6_addr = asV6().toAddr(); sin->sin6_port = port; sin->sin6_scope_id = asV6().getScopeId(); +#if defined(__APPLE__) + sin->sin6_len = sizeof(*sin); +#endif return sizeof(*sin); } else { throw InvalidAddressFamilyException(family()); -- 2.34.1