示例#1
0
文件: isself.cpp 项目: tanakh/mongo
bool HostAndPort::isSelf() const {

    if( dyn() ) {
        MONGO_LOG(2) << "isSelf " << _dynName << ' ' << dynHostMyName() << endl;
        return dynHostMyName() == _dynName;
    }

    int _p = port();
    int p = _p == -1 ? CmdLine::DefaultDBPort : _p;

    if( p != cmdLine.port ) {
        // shortcut - ports have to match at the very least
        return false;
    }

    string host = str::stream() << this->host() << ":" << p;

    {
        // check cache for this host
        // debatably something _could_ change, but I'm not sure right now (erh 10/14/2010)
        scoped_lock lk( isSelfCommand._cacheLock );
        map<string,bool>::const_iterator i = isSelfCommand._cache.find( host );
        if ( i != isSelfCommand._cache.end() )
            return i->second;
    }

#if !defined(_WIN32) && !defined(__sunos__)
    // on linux and os x we can do a quick check for an ip match

    const vector<string> myaddrs = getMyAddrs();
    const vector<string> addrs = getAllIPs(_host);

    for (vector<string>::const_iterator i=myaddrs.begin(), iend=myaddrs.end(); i!=iend; ++i) {
        for (vector<string>::const_iterator j=addrs.begin(), jend=addrs.end(); j!=jend; ++j) {
            string a = *i;
            string b = *j;

            if ( a == b ||
                    ( str::startsWith( a , "127." ) && str::startsWith( b , "127." ) )  // 127. is all loopback
               ) {

                // add to cache
                scoped_lock lk( isSelfCommand._cacheLock );
                isSelfCommand._cache[host] = true;
                return true;
            }
        }
    }

#endif

    if ( ! Listener::getTimeTracker() ) {
        // this ensures we are actually running a server
        // this may return true later, so may want to retry
        return false;
    }

    try {
        isSelfCommand.init();
        DBClientConnection conn;
        string errmsg;
        if ( ! conn.connect( host , errmsg ) ) {
            // should this go in the cache?
            return false;
        }

        if (!noauth && cmdLine.keyFile &&
                !conn.auth("local", internalSecurity.user, internalSecurity.pwd, errmsg, false)) {
            return false;
        }

        BSONObj out;
        bool ok = conn.simpleCommand( "admin" , &out , "_isSelf" );
        bool me = ok && out["id"].type() == jstOID && isSelfCommand._id == out["id"].OID();

        // add to cache
        scoped_lock lk( isSelfCommand._cacheLock );
        isSelfCommand._cache[host] = me;

        return me;
    }
    catch ( std::exception& e ) {
        warning() << "could't check isSelf (" << host << ") " << e.what() << endl;
    }

    return false;
}
示例#2
0
    bool HostAndPort::isSelf() const {

        int _p = port();
        int p = _p == -1 ? ServerGlobalParams::DefaultDBPort : _p;

        string host = str::stream() << this->host() << ":" << p;

        {
            // check cache for this host
            // debatably something _could_ change, but I'm not sure right now (erh 10/14/2010)
            scoped_lock lk( isSelfCommand._cacheLock );
            map<string,bool>::const_iterator i = isSelfCommand._cache.find( host );
            if ( i != isSelfCommand._cache.end() )
                return i->second;
        }

#if !defined(_WIN32) && !defined(__sunos__)
        // on linux and os x we can do a quick check for an ip match

        // no need for ip match if the ports do not match
        if (p == serverGlobalParams.port) {
            const vector<string> myaddrs = getMyAddrs();
            const vector<string> addrs = getAllIPs(_host);

            for (vector<string>::const_iterator i=myaddrs.begin(), iend=myaddrs.end();
                 i!=iend; ++i) {
                for (vector<string>::const_iterator j=addrs.begin(), jend=addrs.end();
                     j!=jend; ++j) {
                    string a = *i;
                    string b = *j;

                    if ( a == b || ( str::startsWith( a , "127." ) &&
                                     str::startsWith( b , "127." ) )  // 127. is all loopback
                       ) {

                        // add to cache
                        scoped_lock lk( isSelfCommand._cacheLock );
                        isSelfCommand._cache[host] = true;
                        return true;
                    }
                }
            }
        }

#endif

        if ( ! Listener::getTimeTracker() ) {
            // this ensures we are actually running a server
            // this may return true later, so may want to retry
            return false;
        }

        try {
            isSelfCommand.init();
            DBClientConnection conn;
            string errmsg;
            if ( ! conn.connect( host , errmsg ) ) {
                // should this go in the cache?
                return false;
            }

            if (getGlobalAuthorizationManager()->isAuthEnabled() && isInternalAuthSet()) {
                if (!authenticateInternalUser(&conn)) {
                    return false;
                }
            }

            BSONObj out;
            bool ok = conn.simpleCommand( "admin" , &out , "_isSelf" );
            bool me = ok && out["id"].type() == jstOID && isSelfCommand._id == out["id"].OID();

            // add to cache
            scoped_lock lk( isSelfCommand._cacheLock );
            isSelfCommand._cache[host] = me;

            return me;
        }
        catch ( std::exception& e ) {
            warning() << "could't check isSelf (" << host << ") " << e.what() << endl;
        }

        return false;
    }