diff --git a/services/implementation/include/authentication_v2/auth_manager.h b/services/implementation/include/authentication_v2/auth_manager.h index f0cb9b39df20a62970951e641dc277237f01d039..5ceca27479311390a09406e811d624ce8d72c829 100644 --- a/services/implementation/include/authentication_v2/auth_manager.h +++ b/services/implementation/include/authentication_v2/auth_manager.h @@ -135,7 +135,6 @@ private: void ParseJsonObject(const JsonObject &jsonObject); void GetAuthParam(const std::string &sessionName, int32_t authType, const std::string &deviceId, const std::string &extra); - std::string GetBundleName(const JsonObject &jsonObject); void SetAuthType(int32_t authType); bool IsAuthTypeSupported(const int32_t &authType); bool IsAuthCodeReady(const std::string &sessionName); diff --git a/services/implementation/include/authentication_v2/dm_auth_manager_base.h b/services/implementation/include/authentication_v2/dm_auth_manager_base.h index c196aada9dec3b38f4aa12027a38f22058b293bf..2ceff22d20a5c9dc8f805652e4d53202bea1c077 100644 --- a/services/implementation/include/authentication_v2/dm_auth_manager_base.h +++ b/services/implementation/include/authentication_v2/dm_auth_manager_base.h @@ -96,7 +96,6 @@ extern const int32_t HML_SESSION_TIMEOUT; extern const int32_t SESSION_HEARTBEAT_TIMEOUT; extern const int32_t PIN_AUTH_TIMEOUT; extern const int32_t EVENT_TIMEOUT; -extern const int32_t WAIT_TIMEOUT; extern const int32_t DM_AUTH_TYPE_MAX; extern const int32_t DM_AUTH_TYPE_MIN; diff --git a/services/implementation/include/authentication_v2/dm_auth_state_machine.h b/services/implementation/include/authentication_v2/dm_auth_state_machine.h index ed71148367aba8ad7be25927a1446e8eae1550ab..03a0d83dd1b808e5f8878c60f3665d0225bcbd54 100644 --- a/services/implementation/include/authentication_v2/dm_auth_state_machine.h +++ b/services/implementation/include/authentication_v2/dm_auth_state_machine.h @@ -114,7 +114,6 @@ private: std::condition_variable stateCv_; std::mutex eventMutex_; std::condition_variable eventCv_; - bool eventCvReady_{false}; // Direction of authentication DmAuthDirection direction_; diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 32347ab91c9f22d57e8ce6965d43310e2038ee4a..99fccbb45e5be82ed8c61b70d84b3dae3607c818 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -243,6 +243,10 @@ private: void CheckIsLastLnnAcl(DistributedDeviceProfile::AccessControlProfile profile, DistributedDeviceProfile::AccessControlProfile delProfile, DmOfflineParam &lnnAclParam, bool &isLastLnnAcl, const std::string &localUdid); + void BindTargetImpl(uint64_t tokenId, const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam); + int32_t SetBundleNameAndTokenIdToExtra(const std::map &bindParam, + std::map &bindParamTmp, uint64_t tokenId); private: std::shared_ptr authMgr_; // Old protocol only std::mutex authMgrMtx_; @@ -263,7 +267,7 @@ private: std::map deviceId2SessionIdMap_; std::map> sessionsMap_; // sessionId corresponds to the session object std::map deviceIdMutexMap_; // Lock corresponding to the device ID - std::mutex mapMutex_; // sessionsMap_的锁 + std::mutex mapMutex_; // sessionsMap_ lock std::map sessionEnableCvMap_; // Condition variable corresponding to the session std::map sessionEnableMutexMap_; // Lock corresponding to the session std::map sessionEnableCvReadyMap_; // Condition variable ready flag diff --git a/services/implementation/src/authentication_v2/auth_manager.cpp b/services/implementation/src/authentication_v2/auth_manager.cpp index 662df9c9717a4c72ca4230e6cf130c74845a4783..db40a057756d3d51dfa0c489d3a5b72d52a792ca 100644 --- a/services/implementation/src/authentication_v2/auth_manager.cpp +++ b/services/implementation/src/authentication_v2/auth_manager.cpp @@ -405,23 +405,16 @@ void AuthManager::ParseHmlInfoInJsonObject(const JsonObject &jsonObject) return; } -std::string AuthManager::GetBundleName(const JsonObject &jsonObject) -{ - if (!jsonObject.IsDiscarded() && jsonObject[BUNDLE_NAME_KEY].IsString()) { - return jsonObject[BUNDLE_NAME_KEY].Get(); - } - bool isSystemSA = false; - std::string bundleName; - AppManager::GetInstance().GetCallerName(isSystemSA, bundleName); - return bundleName; -} - void AuthManager::ParseJsonObject(const JsonObject &jsonObject) { if (jsonObject.IsDiscarded()) { return; } + if (jsonObject[TAG_TOKENID].IsNumberInteger()) { + context_->accesser.tokenId = jsonObject[TAG_TOKENID].Get(); + } + if (jsonObject[APP_OPERATION_KEY].IsString()) { context_->appOperation = jsonObject[APP_OPERATION_KEY].Get(); } @@ -437,9 +430,11 @@ void AuthManager::ParseJsonObject(const JsonObject &jsonObject) context_->connDelayCloseTime = GetCloseSessionDelaySeconds(delaySecondsStr); } - context_->accesser.bundleName = GetBundleName(jsonObject); - bindParam_[BUNDLE_NAME_KEY] = context_->accesser.bundleName; - context_->accessee.bundleName = context_->accesser.bundleName; + if (jsonObject[BUNDLE_NAME_KEY].IsString()) { + context_->accesser.bundleName = jsonObject[BUNDLE_NAME_KEY].Get(); + bindParam_[BUNDLE_NAME_KEY] = context_->accesser.bundleName; + context_->accessee.bundleName = context_->accesser.bundleName; + } if (jsonObject[TAG_PEER_BUNDLE_NAME].IsString() && !jsonObject[TAG_PEER_BUNDLE_NAME].Get().empty()) { context_->accessee.bundleName = jsonObject[TAG_PEER_BUNDLE_NAME].Get(); @@ -553,9 +548,6 @@ void AuthManager::GetAuthParam(const std::string &pkgName, int32_t authType, context_->accesser.deviceName = context_->listener->GetLocalDisplayDeviceNameForPrivacy(); context_->accesser.deviceType = context_->softbusConnector->GetLocalDeviceTypeId(); context_->accesser.isOnline = false; - uint32_t callingTokenId = 0; - MultipleUserConnector::GetCallingTokenId(callingTokenId); - context_->accesser.tokenId = static_cast(callingTokenId); context_->accessee.deviceId = deviceId; context_->accessee.addr = deviceId; diff --git a/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp b/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp index b57e00daa894d89d525a7c21185ae328343605ae..49eb28e1781bf68134cc0407b4bae2ae8c0d4130 100644 --- a/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp @@ -97,7 +97,6 @@ const int32_t HML_SESSION_TIMEOUT = 10; const int32_t SESSION_HEARTBEAT_TIMEOUT = 50; const int32_t PIN_AUTH_TIMEOUT = 60; const int32_t EVENT_TIMEOUT = 5000; // 5000 ms -const int32_t WAIT_TIMEOUT = 2000; // 2000 ms int32_t AuthManagerBase::AuthenticateDevice(const std::string &pkgName, int32_t authType, diff --git a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp index ebee651ec9b548cacefec9fd2a9c478f9503694f..0b78e02844b0af864626637f3cae97edbdf67a79 100644 --- a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp @@ -1000,7 +1000,7 @@ int32_t DmAuthMessageProcessor::ParseNegotiateMessage( if (jsonObject[TAG_BUNDLE_NAME_V2].IsString()) { context->accesser.bundleName = jsonObject[TAG_BUNDLE_NAME_V2].Get(); } - + if (jsonObject[TAG_EXTRA_INFO].IsString()) { context->accesser.extraInfo = jsonObject[TAG_EXTRA_INFO].Get(); } @@ -1269,7 +1269,14 @@ int32_t DmAuthMessageProcessor::CreateMessageForwardUltrasonicNegotiate(std::sha void DmAuthMessageProcessor::CreateAndSendMsg(DmMessageType msgType, std::shared_ptr context) { auto message = CreateMessage(msgType, context); - context->softbusConnector->GetSoftbusSession()->SendData(context->sessionId, message); + int32_t ret = context->softbusConnector->GetSoftbusSession()->SendData(context->sessionId, message); + if (ret != DM_OK) { + if (context->direction == DM_AUTH_SOURCE) { + context->authStateMachine->TransitionTo(std::make_shared()); + } else { + context->authStateMachine->TransitionTo(std::make_shared()); + } + } } std::string DmAuthMessageProcessor::CompressSyncMsg(std::string &inputStr) diff --git a/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp b/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp index 8265cbe402385e5039829a55813fca898be60b37..fea4121847bb63a0245eee0c5ca6d1c5c0744134 100644 --- a/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp @@ -262,18 +262,9 @@ DmEventType DmAuthStateMachine::WaitExpectEvent(DmEventType eventType) std::unique_lock lock(eventMutex_); auto startTime = std::chrono::high_resolution_clock::now(); while (running_.load()) { - if (eventCv_.wait_for(lock, std::chrono::milliseconds(WAIT_TIMEOUT), [&] { - return !running_.load() || !eventQueue_.empty() || eventCvReady_; - })) { - eventCvReady_ = false; - LOGI("DmAuthStateMachine: WaitExpectEvent wait successful."); - } else { - if (!eventCvReady_) { - LOGE("DmAuthStateMachine: WaitExpectEvent wait timeout."); - return DmEventType::ON_TIMEOUT; - } - eventCvReady_ = false; - } + eventCv_.wait(lock, [&] { + return !running_.load() || !eventQueue_.empty(); + }); if (!running_.load()) { return DmEventType::ON_FAIL; } @@ -305,7 +296,6 @@ void DmAuthStateMachine::NotifyEventFinish(DmEventType eventType) { std::unique_lock lock(eventMutex_); eventQueue_.push(eventType); - eventCvReady_ = true; } eventCv_.notify_one(); if (eventType == DmEventType::ON_FAIL) { diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index ff7e0ef1761fbbae9ac0c926100a2861e031e1fe..e51bfed3118b44aa69f5220da842684e034b4567 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -717,11 +717,11 @@ int DeviceManagerServiceImpl::OnSessionOpened(int sessionId, int result) { std::lock_guard lock(sessionEnableMutexMap_[sessionId]); if (result == 0) { - sessionEnableCvReadyMap_[sessionId] = true; LOGE("OnSessionOpened successful, sessionId: %{public}d", sessionId); } else { LOGE("OnSessionOpened failed, sessionId: %{public}d, res: %{public}d", sessionId, result); } + sessionEnableCvReadyMap_[sessionId] = true; sessionEnableCvMap_[sessionId].notify_all(); } std::string peerUdid = ""; @@ -1009,6 +1009,9 @@ void DeviceManagerServiceImpl::OnBytesReceived(int sessionId, const void *data, if (authMgr == nullptr) { return; } + if (msgType == MSG_TYPE_REQ_ACL_NEGOTIATE || msgType == MSG_TYPE_RESP_ACL_NEGOTIATE) { + curSession->version_ = DM_CURRENT_VERSION; + } } else { /**         Monitor old messages on ports 80/90 @@ -1424,14 +1427,10 @@ std::shared_ptr DeviceManagerServiceImpl::GetOrCreateSession(const std: } std::unique_lock cvLock(sessionEnableMutexMap_[sessionId]); - sessionEnableCvReadyMap_[sessionId] = false; - if (sessionEnableCvMap_[sessionId].wait_for(cvLock, std::chrono::milliseconds(WAIT_TIMEOUT), - [&] { return sessionEnableCvReadyMap_[sessionId]; })) { - LOGI("session enable, sessionId: %{public}d.", sessionId); - } else { - LOGE("wait session enable timeout or enable fail, sessionId: %{public}d.", sessionId); - return nullptr; - } + sessionEnableCvMap_[sessionId].wait(cvLock, [&] { + return sessionEnableCvReadyMap_[sessionId]; + }); + instance = std::make_shared(sessionId, deviceId); deviceId2SessionIdMap_[deviceId] = sessionId; sessionsMap_[sessionId] = instance; @@ -1533,14 +1532,9 @@ int32_t DeviceManagerServiceImpl::ParseConnectAddr(const PeerTargetId &targetId, return DM_OK; } -int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, - const std::map &bindParam) +void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::string &pkgName, + const PeerTargetId &targetId, const std::map &bindParam) { - if (pkgName.empty()) { - LOGE("BindTarget failed, pkgName is empty."); - return ERR_DM_INPUT_PARA_INVALID; - } - std::string deviceId = ""; PeerTargetId targetIdTmp = const_cast(targetId); if (ParseConnectAddr(targetId, deviceId, bindParam) == DM_OK) { @@ -1548,14 +1542,14 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P } else { if (targetId.deviceId.empty()) { LOGE("DeviceManagerServiceImpl::BindTarget failed, ParseConnectAddr failed."); - return ERR_DM_INPUT_PARA_INVALID; + return; } } // Created only at the source end. The same target device will not be created repeatedly with the new protocol. std::shared_ptr curSession = GetOrCreateSession(targetIdTmp.deviceId, bindParam); if (curSession == nullptr) { LOGE("Failed to create the session. Target deviceId: %{public}s.", targetIdTmp.deviceId.c_str()); - return ERR_DM_AUTH_OPEN_SESSION_FAILED; + return; } // Logical session random number @@ -1565,16 +1559,17 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P logicalSessionId = GenerateRandNum(sessionId); if (curSession->logicalSessionSet_.find(logicalSessionId) != curSession->logicalSessionSet_.end()) { LOGE("Failed to create the logical session."); - return ERR_DM_LOGIC_SESSION_CREATE_FAILED; + softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); + return; } } // Create on the src end. - uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); int32_t ret = InitAndRegisterAuthMgr(true, tokenId, curSession, logicalSessionId); if (ret != DM_OK) { LOGE("InitAndRegisterAuthMgr failed, ret %{public}d.", ret); - return ret; + softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); + return; } curSession->logicalSessionSet_.insert(logicalSessionId); curSession->logicalSessionCnt_.fetch_add(1); @@ -1583,14 +1578,66 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P auto authMgr = GetAuthMgrByTokenId(tokenId); if (authMgr == nullptr) { - return ERR_DM_POINT_NULL; + CleanAuthMgrByLogicalSessionId(logicalSessionId); + return; } authMgr->SetBindTargetParams(targetId); if ((ret = authMgr->BindTarget(pkgName, targetIdTmp, bindParam, sessionId, logicalSessionId)) != DM_OK) { LOGE("authMgr BindTarget failed, ret %{public}d.", ret); CleanAuthMgrByLogicalSessionId(logicalSessionId); } - return ret; + LOGI("DeviceManagerServiceImpl BindTargetImpl thread end, tokenId: %{public}" PRId64 ".", tokenId); + return; +} + +int32_t DeviceManagerServiceImpl::SetBundleNameAndTokenIdToExtra(const std::map &bindParam, + std::map &bindParamTmp, uint64_t tokenId) +{ + bindParamTmp = const_cast&>(bindParam); + JsonObject jsonObject = GetExtraJsonObject(bindParamTmp); + if (jsonObject.IsDiscarded()) { + return ERR_DM_INPUT_PARA_INVALID; + } + jsonObject[TAG_TOKENID] = tokenId; + if (!jsonObject[BUNDLE_NAME_KEY].IsString()) { + bool isSystemSA = false; + std::string bundleName; + AppManager::GetInstance().GetCallerName(isSystemSA, bundleName); + jsonObject[BUNDLE_NAME_KEY] = bundleName; + } + + auto iter = bindParamTmp.find(PARAM_KEY_BIND_EXTRA_DATA); + if (iter != bindParamTmp.end()) { + iter->second = jsonObject.Dump(); + } else { + ParseMapFromJsonString(jsonObject.Dump(), bindParamTmp); + } + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam) +{ + if (pkgName.empty()) { + LOGE("BindTarget failed, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); + std::map bindParamTmp; + if (SetBundleNameAndTokenIdToExtra(bindParam, bindParamTmp, tokenId) != DM_OK) { + LOGE("BindTarget failed, SetBundleNameAndTokenIdToExtra failed."); + return ERR_DM_INPUT_PARA_INVALID; + } + if (authMgrMap_.find(tokenId) == authMgrMap_.end()) { + std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, + targetId, bindParamTmp); + newThread.detach(); + } else { + LOGE("BindTarget failed, this device is being bound. Please try again later."); + return ERR_DM_AUTH_BUSINESS_BUSY; + } + return DM_OK; } int32_t DeviceManagerServiceImpl::DpAclAdd(const std::string &udid)