diff --git a/util/hostandport.h b/util/hostandport.h index d6b8242c241..985e2529de7 100644 --- a/util/hostandport.h +++ b/util/hostandport.h @@ -28,7 +28,7 @@ namespace mongo { /** helper for manipulating host:port connection endpoints. */ struct HostAndPort { - HostAndPort() : _port(-1) { } + HostAndPort() : _port(-1), _isSelf(NOT_CACHED) { } /** From a string hostname[:portnumber] Throws user assertion if bad config string or bad port #. @@ -36,10 +36,10 @@ namespace mongo { HostAndPort(string s); /** @param p port number. -1 is ok to use default. */ - HostAndPort(string h, int p /*= -1*/) : _host(h), _port(p) { } + HostAndPort(string h, int p /*= -1*/) : _host(h), _port(p), _isSelf(NOT_CACHED) { } HostAndPort(const SockAddr& sock ) - : _host( sock.getAddr() ) , _port( sock.getPort() ){ + : _host( sock.getAddr() ) , _port( sock.getPort() ), _isSelf(NOT_CACHED){ } static HostAndPort me() { @@ -62,7 +62,7 @@ namespace mongo { } /* returns true if the host/port combo identifies this process instance. */ - bool isSelf() const; + bool isSelf() const; // defined in message.cpp bool isLocalHost() const; @@ -80,6 +80,12 @@ namespace mongo { // invariant (except full obj assignment): string _host; int _port; // -1 indicates unspecified + + mutable enum { + NOT_CACHED = -1, + NO = 0, + YES = 1 + } _isSelf; }; /** returns true if strings seem to be the same hostname. @@ -98,14 +104,6 @@ namespace mongo { return HostAndPort(h, cmdLine.port); } - inline bool HostAndPort::isSelf() const { - int p = _port == -1 ? CmdLine::DefaultDBPort : _port; - if( p != cmdLine.port ) - return false; - - return sameHostname(getHostName(), _host) || isLocalHost(); - } - inline string HostAndPort::toString() const { stringstream ss; ss << _host; @@ -127,7 +125,7 @@ namespace mongo { return _host == "localhost" || startsWith(_host.c_str(), "127.") || _host == "::1"; } - inline HostAndPort::HostAndPort(string s) { + inline HostAndPort::HostAndPort(string s) : _isSelf(NOT_CACHED) { const char *p = s.c_str(); uassert(13110, "HostAndPort: bad config string", *p); const char *colon = strrchr(p, ':'); diff --git a/util/message.cpp b/util/message.cpp index 2c91d051f60..c7dc2aa455b 100644 --- a/util/message.cpp +++ b/util/message.cpp @@ -690,5 +690,28 @@ namespace mongo { TicketHolder connTicketHolder(20000); + bool HostAndPort::isSelf() const { + if (_isSelf == NOT_CACHED){ + int p = _port == -1 ? CmdLine::DefaultDBPort : _port; + + if( p != cmdLine.port ){ + _isSelf = NO; + } else if (sameHostname(getHostName(), _host) || isLocalHost()) { + _isSelf = YES; + } else { + SockAddr addr (_host.c_str(), 0); // port 0 is dynamically assigned + SOCKET sock = ::socket(addr.getType(), SOCK_STREAM, 0); + assert(sock != INVALID_SOCKET); + + _isSelf = ( (::bind(sock, addr.raw(), addr.addressSize) == 0) ? YES : NO ); + + closesocket(sock); + } + } + + assert(_isSelf != NOT_CACHED); + + return _isSelf; + } } // namespace mongo