void _pmdExternClient::logout() { if ( _isAuthed ) { CHAR szTmp[ 16 ] = { 0 } ; ossSnprintf( szTmp, sizeof(szTmp)-1, "%llu", _pEDUCB->getID() ) ; PD_AUDIT( AUDIT_ACCESS, getUsername(), getPeerIPAddr(), getPeerPort(), "LOGOUT", AUDIT_OBJ_SESSION, szTmp, SDB_OK, "User[UserName:%s, RemoteIP:%s, RemotePort:%u, " "LocalIP:%s, LocalPort:%u] logout succeed", getUsername(), getPeerIPAddr(), getPeerPort(), getLocalIPAddr(), getLocalPort() ) ; } _isAuthed = FALSE ; }
void TCPServer::handleClose(PSocketContext pSocketContext) { auto pClient = findClients(pSocketContext); LOG("客户端[%s-%d]正常离线", pClient->getPeerIP(), pClient->getPeerPort()); removeClients(pClient); }
void QAsioTcpSocket::hostConnected() { qDebug() << this << " QAsioTcpSocket::hostConnected() peerIp:" << getPeerIp() << " peerPort:" << getPeerPort(); QCoreApplication::postEvent(this,new QAsioEvent(QAsioEvent::Connected)); }
// PD_TRACE_DECLARE_FUNCTION ( SDB_OSSSK_CONNECT, "ossSocket::connect" ) INT32 _ossSocket::connect ( INT32 timeout ) { INT32 rc = SDB_OK ; PD_TRACE_ENTRY ( SDB_OSSSK_CONNECT ); SDB_ASSERT ( !_peerAddress.sin_addr.s_addr, "Cannot connect without close/init" ) ; #if defined (_LINUX) INT32 flags = fcntl( native(), F_GETFL, 0) ; if ( fcntl( native(), F_SETFL, flags | O_NONBLOCK ) <0 ) { PD_LOG( PDERROR, "failed to fcntl sock:%d",native() ) ; rc = SDB_SYS ; goto error ; } rc = ::connect ( _fd, (struct sockaddr *) &_sockAddress, _addressLen ) ; if ( rc != SDB_OK ) { if ( SOCKET_GETLASTERROR == EINPROGRESS ) { rc = _complete( timeout ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to complete connect, rc = %d", rc ) ; goto error ; } } else { PD_LOG ( PDERROR, "Failed to connect, rc = %d", SOCKET_GETLASTERROR ) ; rc = SDB_NETWORK ; goto error ; } } else { } if ( fcntl( native(), F_SETFL, flags & ~O_NONBLOCK ) <0 ) { PD_LOG( PDERROR, "failed to fcntl sock:%d",native() ) ; close() ; rc = SDB_SYS ; goto error ; } #elif defined (_WINDOWS) rc = ::connect ( _fd, (struct sockaddr *) &_sockAddress, _addressLen ) ; if ( rc ) { PD_LOG ( PDERROR, "Failed to connect, rc = %d", SOCKET_GETLASTERROR ) ; rc = SDB_NETWORK ; goto error ; } #endif rc = getsockname ( _fd, (sockaddr*)&_sockAddress, &_addressLen ) ; if ( rc ) { PD_LOG ( PDERROR, "Failed to get local address, rc=%d", rc ) ; rc = SDB_NETWORK ; goto error ; } rc = getpeername ( _fd, (sockaddr*)&_peerAddress, &_peerAddressLen ) ; if ( rc ) { PD_LOG ( PDERROR, "Failed to get peer address, rc=%d", rc ) ; rc = SDB_NETWORK ; goto error ; } if ( _sockAddress.sin_port == _peerAddress.sin_port && _sockAddress.sin_addr.s_addr == _peerAddress.sin_addr.s_addr ) { PD_LOG( PDERROR, "Local addr is the same with remote addr, " "local port: %u, remote port: %u", getLocalPort(), getPeerPort() ) ; rc = SDB_NETWORK ; goto error ; } done : PD_TRACE_EXITRC ( SDB_OSSSK_CONNECT, rc ); return rc ; error : close() ; goto done ; }
UINT16 _pmdExternClient::getFromPort() const { return getPeerPort() ; }
INT32 _pmdExternClient::authenticate( MsgHeader *pMsg ) { #if defined ( SDB_ENGINE ) INT32 rc = SDB_OK ; BSONObj authObj ; BSONElement user, pass ; rc = extractAuthMsg( pMsg, authObj ) ; if ( rc ) { PD_LOG( PDERROR, "Client[%s] extract auth msg failed, rc: %d", clientName(), rc ) ; goto error ; } user = authObj.getField( SDB_AUTH_USER ) ; pass = authObj.getField( SDB_AUTH_PASSWD ) ; _isAuthed = FALSE ; if ( SDB_ROLE_STANDALONE == pmdGetDBRole() ) // not auth { _isAuthed = TRUE ; goto done ; } else if ( SDB_ROLE_OM == pmdGetDBRole() ) { rc = sdbGetOMManager()->authenticate( authObj, _pEDUCB ) ; if ( rc ) { PD_LOG( PDERROR, "Client[%s] authenticate failed[user: %s, " "passwd: %s], rc: %d", clientName(), user.valuestrsafe(), pass.valuestrsafe(), rc ) ; goto error ; } _isAuthed = TRUE ; } else if ( SDB_ROLE_COORD == pmdGetDBRole() ) { INT64 contextID = -1 ; rtnContextBuf buf ; CoordCB *pCoordcb = pmdGetKRCB()->getCoordCB(); rtnCoordProcesserFactory *pProcesserFactory = pCoordcb->getProcesserFactory(); rtnCoordOperator *pOperator = NULL ; pOperator = pProcesserFactory->getOperator( pMsg->opCode ); rc = pOperator->execute( pMsg, _pEDUCB, contextID, &buf ) ; // special handling for password verification when there is no // addrlist specified. Usually this happen when there is only // one coord node before creating the first catalog if ( MSG_AUTH_VERIFY_REQ == pMsg->opCode && SDB_CAT_NO_ADDR_LIST == rc ) { rc = SDB_OK ; _isAuthed = TRUE ; } else if ( rc ) { PD_LOG( PDERROR, "Client[%s] authenticate failed[user: %s, " "passwd: %s], rc: %d", clientName(), user.valuestrsafe(), pass.valuestrsafe(), rc ) ; goto error ; } else { _isAuthed = TRUE ; } } else { MsgHeader *pAuthRes = NULL ; shardCB *pShard = sdbGetShardCB() ; UINT32 retryTimes = 0 ; MsgOpReply replyHeader ; replyHeader.contextID = -1 ; replyHeader.numReturned = 0 ; while ( TRUE ) { ++retryTimes ; rc = pShard->syncSend( pMsg, CATALOG_GROUPID, TRUE, &pAuthRes ) ; if ( SDB_OK != rc ) { rc = pShard->syncSend( pMsg, CATALOG_GROUPID, FALSE, &pAuthRes ) ; PD_RC_CHECK( rc, PDERROR, "Client[%s] failed to send auth " "req to catalog, rc: %d", clientName(), rc ) ; } if ( NULL == pAuthRes ) { rc = SDB_SYS ; PD_LOG( PDERROR, "syncsend return ok but res is NULL" ) ; goto error ; } rc = MSG_GET_INNER_REPLY_RC(pAuthRes) ; replyHeader.flags = rc ; replyHeader.startFrom = MSG_GET_INNER_REPLY_STARTFROM(pAuthRes) ; ossMemcpy( &(replyHeader.header), pAuthRes, sizeof( MsgHeader ) ) ; /// release recv msg SDB_OSS_FREE( (BYTE*)pAuthRes ) ; pAuthRes = NULL ; if ( SDB_CLS_NOT_PRIMARY == rc && retryTimes < PMD_AUTH_RETRY_TIMES ) { INT32 rcTmp = SDB_OK ; rcTmp = pShard->updatePrimaryByReply( &(replyHeader.header) ) ; if ( SDB_NET_CANNOT_CONNECT == rcTmp ) { /// the node is crashed, sleep some seconds PD_LOG( PDWARNING, "Catalog group primary node is crashed " "but other nodes not aware, sleep %d seconds", NET_NODE_FAULTUP_MIN_TIME ) ; ossSleep( NET_NODE_FAULTUP_MIN_TIME * OSS_ONE_SEC ) ; } if ( rcTmp ) { pShard->updateCatGroup( CLS_SHARD_TIMEOUT ) ; } continue ; } else if ( rc ) { PD_LOG( PDERROR, "Client[%s] authenticate failed[user: %s, " "passwd: %s], rc: %d", clientName(), user.valuestrsafe(), pass.valuestrsafe(), rc ) ; goto error ; } else { _isAuthed = TRUE ; } break ; } } done: if ( SDB_OK == rc && _isAuthed ) { _username = user.valuestrsafe() ; if ( !_username.empty() ) { _password = pass.valuestrsafe() ; } _pEDUCB->setUserInfo( _username, _password ) ; _makeName() ; CHAR szTmp[ 16 ] = { 0 } ; ossSnprintf( szTmp, sizeof(szTmp)-1, "%llu", _pEDUCB->getID() ) ; PD_AUDIT_OP( AUDIT_ACCESS, MSG_AUTH_VERIFY_REQ, AUDIT_OBJ_SESSION, szTmp, SDB_OK, "User[UserName:%s, RemoteIP:%s, RemotePort:%u, " "LocalIP:%s, LocalPort:%u] login succeed", getUsername(), getPeerIPAddr(), getPeerPort(), getLocalIPAddr(), getLocalPort() ) ; } return rc ; error: if ( SDB_AUTH_AUTHORITY_FORBIDDEN == rc ) { _pEDUCB->printInfo( EDU_INFO_ERROR, "username or passwd is wrong" ) ; } goto done ; #else _isAuthed = TRUE ; return SDB_OK ; #endif // SDB_ENGINE }