mirror of
https://github.com/mongodb/mongo.git
synced 2024-11-30 09:06:21 +01:00
use bind() in HostAndPort::isSelf() SERVER-1414
This commit is contained in:
parent
be9891dd18
commit
6baeb71caf
@ -28,7 +28,7 @@ namespace mongo {
|
|||||||
/** helper for manipulating host:port connection endpoints.
|
/** helper for manipulating host:port connection endpoints.
|
||||||
*/
|
*/
|
||||||
struct HostAndPort {
|
struct HostAndPort {
|
||||||
HostAndPort() : _port(-1) { }
|
HostAndPort() : _port(-1), _isSelf(NOT_CACHED) { }
|
||||||
|
|
||||||
/** From a string hostname[:portnumber]
|
/** From a string hostname[:portnumber]
|
||||||
Throws user assertion if bad config string or bad port #.
|
Throws user assertion if bad config string or bad port #.
|
||||||
@ -36,10 +36,10 @@ namespace mongo {
|
|||||||
HostAndPort(string s);
|
HostAndPort(string s);
|
||||||
|
|
||||||
/** @param p port number. -1 is ok to use default. */
|
/** @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 )
|
HostAndPort(const SockAddr& sock )
|
||||||
: _host( sock.getAddr() ) , _port( sock.getPort() ){
|
: _host( sock.getAddr() ) , _port( sock.getPort() ), _isSelf(NOT_CACHED){
|
||||||
}
|
}
|
||||||
|
|
||||||
static HostAndPort me() {
|
static HostAndPort me() {
|
||||||
@ -62,7 +62,7 @@ namespace mongo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* returns true if the host/port combo identifies this process instance. */
|
/* returns true if the host/port combo identifies this process instance. */
|
||||||
bool isSelf() const;
|
bool isSelf() const; // defined in message.cpp
|
||||||
|
|
||||||
bool isLocalHost() const;
|
bool isLocalHost() const;
|
||||||
|
|
||||||
@ -80,6 +80,12 @@ namespace mongo {
|
|||||||
// invariant (except full obj assignment):
|
// invariant (except full obj assignment):
|
||||||
string _host;
|
string _host;
|
||||||
int _port; // -1 indicates unspecified
|
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.
|
/** returns true if strings seem to be the same hostname.
|
||||||
@ -98,14 +104,6 @@ namespace mongo {
|
|||||||
return HostAndPort(h, cmdLine.port);
|
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 {
|
inline string HostAndPort::toString() const {
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss << _host;
|
ss << _host;
|
||||||
@ -127,7 +125,7 @@ namespace mongo {
|
|||||||
return _host == "localhost" || startsWith(_host.c_str(), "127.") || _host == "::1";
|
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();
|
const char *p = s.c_str();
|
||||||
uassert(13110, "HostAndPort: bad config string", *p);
|
uassert(13110, "HostAndPort: bad config string", *p);
|
||||||
const char *colon = strrchr(p, ':');
|
const char *colon = strrchr(p, ':');
|
||||||
|
@ -690,5 +690,28 @@ namespace mongo {
|
|||||||
|
|
||||||
TicketHolder connTicketHolder(20000);
|
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
|
} // namespace mongo
|
||||||
|
Loading…
Reference in New Issue
Block a user