From 5333a9145877c6ea78ab9a47b3cafc60c8237ece Mon Sep 17 00:00:00 2001 From: Sean Cannella Date: Wed, 18 Mar 2015 15:57:53 -0700 Subject: [PATCH] SocketAddress::anyAddress should be a method Summary: Calling getaddrinfo from a static constructor is not the most reliable thing to do. Stop doing it. Test Plan: existing tests Reviewed By: davejwatson@fb.com Subscribers: dancol, mzlee, shikong, kmdent, fma, bmatheny, benyluo, ranjeeth, folly-diffs@, jsedgwick, yfeldblum FB internal diff: D1924578 Tasks: 6534662 Signature: t1:1924578:1426718586:d73bc2f001095e66fa77fde9c027af050cc26d2a --- folly/io/async/AsyncSSLSocket.h | 2 +- folly/io/async/AsyncSocket.cpp | 10 +++++++--- folly/io/async/AsyncSocket.h | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/folly/io/async/AsyncSSLSocket.h b/folly/io/async/AsyncSSLSocket.h index 9750de4e..c3d52150 100644 --- a/folly/io/async/AsyncSSLSocket.h +++ b/folly/io/async/AsyncSSLSocket.h @@ -308,7 +308,7 @@ class AsyncSSLSocket : public virtual AsyncSocket { const folly::SocketAddress& address, int timeout = 0, const OptionMap &options = emptyOptionMap, - const folly::SocketAddress& bindAddr = anyAddress) + const folly::SocketAddress& bindAddr = anyAddress()) noexcept override; using AsyncSocket::connect; diff --git a/folly/io/async/AsyncSocket.cpp b/folly/io/async/AsyncSocket.cpp index f4f08387..01ed6621 100644 --- a/folly/io/async/AsyncSocket.cpp +++ b/folly/io/async/AsyncSocket.cpp @@ -37,8 +37,6 @@ namespace folly { // static members initializers const AsyncSocket::OptionMap AsyncSocket::emptyOptionMap; -const folly::SocketAddress AsyncSocket::anyAddress = - folly::SocketAddress("0.0.0.0", 0); const AsyncSocketException socketClosedLocallyEx( AsyncSocketException::END_OF_FILE, "socket closed locally"); @@ -273,6 +271,12 @@ int AsyncSocket::detachFd() { return fd; } +const folly::SocketAddress& AsyncSocket::anyAddress() { + static const folly::SocketAddress anyAddress = + folly::SocketAddress("0.0.0.0", 0); + return anyAddress; +} + void AsyncSocket::setShutdownSocketSet(ShutdownSocketSet* newSS) { if (shutdownSocketSet_ == newSS) { return; @@ -371,7 +375,7 @@ void AsyncSocket::connect(ConnectCallback* callback, << ", fd=" << fd_ << ", host=" << address.describe().c_str(); // bind the socket - if (bindAddr != anyAddress) { + if (bindAddr != anyAddress()) { int one = 1; if (::setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) { doClose(); diff --git a/folly/io/async/AsyncSocket.h b/folly/io/async/AsyncSocket.h index 1973d3d8..e6209166 100644 --- a/folly/io/async/AsyncSocket.h +++ b/folly/io/async/AsyncSocket.h @@ -238,7 +238,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper { typedef std::map OptionMap; static const OptionMap emptyOptionMap; - static const folly::SocketAddress anyAddress; + static const folly::SocketAddress& anyAddress(); /** * Initiate a connection. @@ -254,7 +254,7 @@ class AsyncSocket : virtual public AsyncTransportWrapper { const folly::SocketAddress& address, int timeout = 0, const OptionMap &options = emptyOptionMap, - const folly::SocketAddress& bindAddr = anyAddress + const folly::SocketAddress& bindAddr = anyAddress() ) noexcept; void connect(ConnectCallback* callback, const std::string& ip, uint16_t port, int timeout = 00, -- 2.34.1