/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "net_conn_client.h" #include "iservice_registry.h" #include "system_ability_definition.h" #include "fwmark_client.h" #include "netsys_sock_client.h" #include "net_conn_service_proxy.h" #include "net_manager_constants.h" #include "net_mgr_log_wrapper.h" #include "net_supplier_callback_stub.h" static constexpr const int32_t MIN_VALID_NETID = 100; namespace OHOS { namespace NetManagerStandard { NetConnClient::NetConnClient() : NetConnService_(nullptr), deathRecipient_(nullptr) {} NetConnClient::~NetConnClient() {} int32_t NetConnClient::SystemReady() { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->SystemReady(); } int32_t NetConnClient::RegisterNetSupplier(NetBearType bearerType, const std::string &ident, const std::set &netCaps, uint32_t &supplierId) { NETMGR_LOG_D("RegisterNetSupplier client in."); sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->RegisterNetSupplier(bearerType, ident, netCaps, supplierId); } int32_t NetConnClient::UnregisterNetSupplier(uint32_t supplierId) { NETMGR_LOG_D("UnregisterNetSupplier client in."); sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->UnregisterNetSupplier(supplierId); } int32_t NetConnClient::RegisterNetSupplierCallback(uint32_t supplierId, const sptr &callback) { NETMGR_LOG_D("RegisterNetSupplierCallback client in."); sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } sptr ptr = std::make_unique().release(); ptr->RegisterSupplierCallbackImpl(callback); netSupplierCallback_[supplierId] = ptr; return proxy->RegisterNetSupplierCallback(supplierId, ptr); } int32_t NetConnClient::RegisterNetConnCallback(const sptr &callback) { NETMGR_LOG_D("RegisterNetConnCallback client in."); sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("The parameter of proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->RegisterNetConnCallback(callback); } int32_t NetConnClient::RegisterNetConnCallback(const sptr &netSpecifier, const sptr &callback, const uint32_t &timeoutMS) { NETMGR_LOG_D("RegisterNetConnCallback with timeout client in."); if (netSpecifier == nullptr || !netSpecifier->SpecifierIsValid()) { NETMGR_LOG_E("The parameter of netSpecifier is invalid"); return NETMANAGER_ERR_PARAMETER_ERROR; } sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("The parameter of proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->RegisterNetConnCallback(netSpecifier, callback, timeoutMS); } int32_t NetConnClient::UnregisterNetConnCallback(const sptr &callback) { NETMGR_LOG_D("UnregisterNetConnCallback client in."); sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->UnregisterNetConnCallback(callback); } int32_t NetConnClient::UpdateNetSupplierInfo(uint32_t supplierId, const sptr &netSupplierInfo) { NETMGR_LOG_D("UpdateNetSupplierInfo client in."); sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->UpdateNetSupplierInfo(supplierId, netSupplierInfo); } int32_t NetConnClient::UpdateNetLinkInfo(uint32_t supplierId, const sptr &netLinkInfo) { NETMGR_LOG_D("UpdateNetLinkInfo client in."); sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->UpdateNetLinkInfo(supplierId, netLinkInfo); } int32_t NetConnClient::GetDefaultNet(NetHandle &netHandle) { NETMGR_LOG_D("GetDefaultNet client in."); sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } int32_t netId = 0; int32_t result = proxy->GetDefaultNet(netId); if (result != NETMANAGER_SUCCESS) { NETMGR_LOG_D("fail to get default net."); return result; } netHandle.SetNetId(netId); NETMGR_LOG_D("GetDefaultNet client out."); return NETMANAGER_SUCCESS; } int32_t NetConnClient::HasDefaultNet(bool &flag) { NETMGR_LOG_D("HasDefaultNet client in."); sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->HasDefaultNet(flag); } int32_t NetConnClient::GetAllNets(std::list> &netList) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } std::list netIdList; int32_t result = proxy->GetAllNets(netIdList); if (result != NETMANAGER_SUCCESS) { return result; } std::list::iterator iter; for (iter = netIdList.begin(); iter != netIdList.end(); ++iter) { sptr netHandle = std::make_unique(*iter).release(); if (netHandle != nullptr) { netList.push_back(netHandle); } } return NETMANAGER_SUCCESS; } int32_t NetConnClient::GetConnectionProperties(const NetHandle &netHandle, NetLinkInfo &info) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->GetConnectionProperties(netHandle.GetNetId(), info); } int32_t NetConnClient::GetNetCapabilities(const NetHandle &netHandle, NetAllCapabilities &netAllCap) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->GetNetCapabilities(netHandle.GetNetId(), netAllCap); } int32_t NetConnClient::GetAddressesByName(const std::string &host, int32_t netId, std::vector &addrList) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->GetAddressesByName(host, netId, addrList); } int32_t NetConnClient::GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->GetAddressByName(host, netId, addr); } int32_t NetConnClient::BindSocket(int32_t socket_fd, int32_t netId) { if (netId < MIN_VALID_NETID) { return NET_CONN_ERR_INVALID_NETWORK; } std::shared_ptr fwmarkClient_ = std::make_shared(); if (fwmarkClient_ == nullptr) { NETMGR_LOG_E("fwmarkClient_ is nullptr"); return NETMANAGER_ERR_PARAMETER_ERROR; } fwmarkClient_->BindSocket(socket_fd, netId); return NETMANAGER_SUCCESS; } int32_t NetConnClient::NetDetection(const NetHandle &netHandle) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->NetDetection(netHandle.GetNetId()); } sptr NetConnClient::GetProxy() { std::lock_guard lock(mutex_); if (NetConnService_) { NETMGR_LOG_D("get proxy is ok"); return NetConnService_; } NETMGR_LOG_D("execute GetSystemAbilityManager"); sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (sam == nullptr) { NETMGR_LOG_E("GetProxy(), get SystemAbilityManager failed"); return nullptr; } sptr remote = sam->CheckSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID); if (remote == nullptr) { NETMGR_LOG_E("get Remote service failed"); return nullptr; } deathRecipient_ = (std::make_unique(*this)).release(); if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) { NETMGR_LOG_E("add death recipient failed"); return nullptr; } NetConnService_ = iface_cast(remote); if (NetConnService_ == nullptr) { NETMGR_LOG_E("get Remote service proxy failed"); return nullptr; } return NetConnService_; } int32_t NetConnClient::SetAirplaneMode(bool state) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->SetAirplaneMode(state); } void NetConnClient::OnRemoteDied(const wptr &remote) { NETMGR_LOG_D("on remote died"); if (remote == nullptr) { NETMGR_LOG_E("remote object is nullptr"); return; } std::lock_guard lock(mutex_); if (NetConnService_ == nullptr) { NETMGR_LOG_E("NetConnService_ is nullptr"); return; } sptr local = NetConnService_->AsObject(); if (local != remote.promote()) { NETMGR_LOG_E("proxy and stub is not same remote object"); return; } local->RemoveDeathRecipient(deathRecipient_); NetConnService_ = nullptr; } int32_t NetConnClient::IsDefaultNetMetered(bool &isMetered) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->IsDefaultNetMetered(isMetered); } int32_t NetConnClient::SetGlobalHttpProxy(const HttpProxy &httpProxy) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->SetGlobalHttpProxy(httpProxy); } int32_t NetConnClient::GetGlobalHttpProxy(HttpProxy &httpProxy) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->GetGlobalHttpProxy(httpProxy); } int32_t NetConnClient::GetNetIdByIdentifier(const std::string &ident, int32_t &netId) { sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } return proxy->GetNetIdByIdentifier(ident, netId); } int32_t NetConnClient::SetAppNet(int32_t netId) { if (netId < MIN_VALID_NETID && netId != 0) { return NET_CONN_ERR_INVALID_NETWORK; } sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_LOG_E("proxy is nullptr"); return NETMANAGER_ERR_GET_PROXY_FAIL; } int32_t ret = proxy->SetAppNet(netId); if (ret != NETMANAGER_SUCCESS) { return ret; } SetNetForApp(netId); return NETMANAGER_SUCCESS; } int32_t NetConnClient::GetAppNet(int32_t &netId) { netId = GetNetForApp(); return NETMANAGER_SUCCESS; } } // namespace NetManagerStandard } // namespace OHOS