1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "dns_resolver_client.h"
17
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20
21 #include "net_mgr_log_wrapper.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
DnsResolverClient()25 DnsResolverClient::DnsResolverClient() : dnsResolverService_(nullptr), deathRecipient_(nullptr) {}
26
~DnsResolverClient()27 DnsResolverClient::~DnsResolverClient() {}
28
GetAddressesByName(const std::string & hostName,std::vector<INetAddr> & addrInfo)29 int32_t DnsResolverClient::GetAddressesByName(const std::string &hostName, std::vector<INetAddr> &addrInfo)
30 {
31 sptr<IDnsResolverService> proxy = GetProxy();
32 if (proxy == nullptr) {
33 NETMGR_LOG_E("proxy is nullptr");
34 return IPC_PROXY_ERR;
35 }
36 return proxy->GetAddressesByName(hostName, addrInfo);
37 }
38
GetProxy()39 sptr<IDnsResolverService> DnsResolverClient::GetProxy()
40 {
41 std::lock_guard lock(mutex_);
42 if (dnsResolverService_) {
43 NETMGR_LOG_D("get proxy is ok");
44 return dnsResolverService_;
45 }
46 NETMGR_LOG_D("execute GetSystemAbilityManager");
47 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48 if (sam == nullptr) {
49 NETMGR_LOG_E("GetProxy, get SystemAbilityManager failed");
50 return nullptr;
51 }
52 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_DNS_MANAGER_SYS_ABILITY_ID);
53 if (remote == nullptr) {
54 NETMGR_LOG_E("get Remote service failed");
55 return nullptr;
56 }
57 deathRecipient_ = (std::make_unique<DnsResolverDeathRecipient>(*this)).release();
58 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
59 NETMGR_LOG_E("add death recipient failed");
60 return nullptr;
61 }
62 dnsResolverService_ = iface_cast<IDnsResolverService>(remote);
63 if (dnsResolverService_ == nullptr) {
64 NETMGR_LOG_E("get Remote service proxy failed");
65 return nullptr;
66 }
67 return dnsResolverService_;
68 }
69
OnRemoteDied(const wptr<IRemoteObject> & remote)70 void DnsResolverClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
71 {
72 NETMGR_LOG_D("on remote died");
73 if (remote == nullptr) {
74 NETMGR_LOG_E("remote object is nullptr");
75 return;
76 }
77 std::lock_guard lock(mutex_);
78 if (dnsResolverService_ == nullptr) {
79 NETMGR_LOG_E("dnsResolverService_ is nullptr");
80 return;
81 }
82 sptr<IRemoteObject> local = dnsResolverService_->AsObject();
83 if (local != remote.promote()) {
84 NETMGR_LOG_E("proxy and stub is not same remote object");
85 return;
86 }
87 local->RemoveDeathRecipient(deathRecipient_);
88 dnsResolverService_ = nullptr;
89 }
90 } // namespace NetManagerStandard
91 } // namespace OHOS