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_service_stub.h"
17 #include "net_mgr_log_wrapper.h"
18 #include "dns_resolver_constants.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
DnsResolverServiceStub()22 DnsResolverServiceStub::DnsResolverServiceStub()
23 {
24 memberFuncMap_[CMD_GET_ADDR_BY_NAME] = &DnsResolverServiceStub::OnGetAddressesByName;
25 memberFuncMap_[CMD_GET_ADDR_INFO] = &DnsResolverServiceStub::OnGetAddrInfo;
26 memberFuncMap_[CMD_CRT_NETWORK_CACHE] = &DnsResolverServiceStub::OnCreateNetworkCache;
27 memberFuncMap_[CMD_DEL_NETWORK_CACHE] = &DnsResolverServiceStub::OnDestroyNetworkCache;
28 memberFuncMap_[CMD_FLS_NETWORK_CACHE] = &DnsResolverServiceStub::OnFlushNetworkCache;
29 memberFuncMap_[CMD_SET_RESOLVER_CONFIG] = &DnsResolverServiceStub::OnSetResolverConfig;
30 memberFuncMap_[CMD_GET_RESOLVER_INFO] = &DnsResolverServiceStub::OnGetResolverInfo;
31 }
32
~DnsResolverServiceStub()33 DnsResolverServiceStub::~DnsResolverServiceStub() {}
34
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int32_t DnsResolverServiceStub::OnRemoteRequest(
36 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
37 {
38 NETMGR_LOG_D("stub call start, code = [%{public}d]", code);
39
40 std::u16string myDescripter = DnsResolverServiceStub::GetDescriptor();
41 std::u16string remoteDescripter = data.ReadInterfaceToken();
42 if (myDescripter != remoteDescripter) {
43 NETMGR_LOG_E("descriptor checked fail");
44 return NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
45 }
46 auto itFunc = memberFuncMap_.find(code);
47 if (itFunc != memberFuncMap_.end()) {
48 auto requestFunc = itFunc->second;
49 if (requestFunc != nullptr) {
50 return (this->*requestFunc)(data, reply);
51 }
52 }
53
54 NETMGR_LOG_D("stub default case, need check");
55 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
56 }
57
OnGetAddressesByName(MessageParcel & data,MessageParcel & reply)58 int32_t DnsResolverServiceStub::OnGetAddressesByName(MessageParcel &data, MessageParcel &reply)
59 {
60 std::string hostName;
61 if (!data.ReadString(hostName)) {
62 return NETMANAGER_ERR_READ_DATA_FAIL;
63 }
64 std::vector<INetAddr> addrInfo;
65 int32_t ret = GetAddressesByName(hostName, addrInfo);
66 if (!reply.WriteInt32(addrInfo.size())) {
67 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
68 }
69 for (auto s : addrInfo) {
70 if (!s.Marshalling(reply)) {
71 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
72 }
73 }
74 if (!reply.WriteInt32(ret)) {
75 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
76 }
77 return NETMANAGER_SUCCESS;
78 }
79
OnGetAddrInfo(MessageParcel & data,MessageParcel & reply)80 int32_t DnsResolverServiceStub::OnGetAddrInfo(MessageParcel &data, MessageParcel &reply)
81 {
82 std::string hostname;
83 std::string server;
84 if (!data.ReadString(hostname)) {
85 return NETMANAGER_ERR_READ_DATA_FAIL;
86 }
87 if (!data.ReadString(server)) {
88 return NETMANAGER_ERR_READ_DATA_FAIL;
89 }
90 sptr<DnsAddrInfo> hints = DnsAddrInfo::Unmarshalling(data);
91 std::vector<sptr<DnsAddrInfo>> dnsAddrInfo;
92 int32_t ret = GetAddrInfo(hostname, server, hints, dnsAddrInfo);
93
94 int32_t vsize = static_cast<int32_t>(dnsAddrInfo.size());
95 if (!reply.WriteInt32(vsize)) {
96 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
97 }
98 for (std::vector<sptr<DnsAddrInfo>>::iterator it = dnsAddrInfo.begin(); it != dnsAddrInfo.end(); ++it) {
99 (*it)->Marshalling(reply);
100 }
101 if (!reply.WriteInt32(ret)) {
102 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
103 }
104 return NETMANAGER_SUCCESS;
105 }
106
OnCreateNetworkCache(MessageParcel & data,MessageParcel & reply)107 int32_t DnsResolverServiceStub::OnCreateNetworkCache(MessageParcel &data, MessageParcel &reply)
108 {
109 int32_t netId = 0;
110 if (!data.ReadInt32(netId)) {
111 return NETMANAGER_ERR_READ_DATA_FAIL;
112 }
113 int32_t ret = CreateNetworkCache(netId);
114 if (!reply.WriteInt32(ret)) {
115 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
116 }
117 return NETMANAGER_SUCCESS;
118 }
119
OnDestroyNetworkCache(MessageParcel & data,MessageParcel & reply)120 int32_t DnsResolverServiceStub::OnDestroyNetworkCache(MessageParcel &data, MessageParcel &reply)
121 {
122 int32_t netId = 0;
123 if (!data.ReadInt32(netId)) {
124 return NETMANAGER_ERR_READ_DATA_FAIL;
125 }
126 int32_t ret = DestroyNetworkCache(netId);
127 if (!reply.WriteInt32(ret)) {
128 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
129 }
130 return NETMANAGER_SUCCESS;
131 }
132
OnFlushNetworkCache(MessageParcel & data,MessageParcel & reply)133 int32_t DnsResolverServiceStub::OnFlushNetworkCache(MessageParcel &data, MessageParcel &reply)
134 {
135 int32_t netId = 0;
136 if (!data.ReadInt32(netId)) {
137 return NETMANAGER_ERR_READ_DATA_FAIL;
138 }
139 int32_t ret = FlushNetworkCache(netId);
140 if (!reply.WriteInt32(ret)) {
141 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
142 }
143 return NETMANAGER_SUCCESS;
144 }
145
OnSetResolverConfig(MessageParcel & data,MessageParcel & reply)146 int32_t DnsResolverServiceStub::OnSetResolverConfig(MessageParcel &data, MessageParcel &reply)
147 {
148 int32_t netId = 0;
149 uint16_t baseTimeoutMsec = 0;
150 uint8_t retryCount = 0;
151 std::vector<std::string> servers;
152 std::vector<std::string> domains;
153
154 if (!data.ReadInt32(netId)) {
155 return NETMANAGER_ERR_READ_DATA_FAIL;
156 }
157 if (!data.ReadUint16(baseTimeoutMsec)) {
158 return NETMANAGER_ERR_READ_DATA_FAIL;
159 }
160 if (!data.ReadUint8(retryCount)) {
161 return NETMANAGER_ERR_READ_DATA_FAIL;
162 }
163 int32_t size;
164 if (!data.ReadInt32(size)) {
165 return NETMANAGER_ERR_READ_DATA_FAIL;
166 }
167
168 std::string s;
169 for (int32_t i = 0; i < size; ++i) {
170 std::string().swap(s);
171 if (!data.ReadString(s)) {
172 return NETMANAGER_ERR_READ_DATA_FAIL;
173 }
174 servers.push_back(s);
175 }
176
177 if (!data.ReadInt32(size)) {
178 return NETMANAGER_ERR_READ_DATA_FAIL;
179 }
180
181 for (int32_t i = 0; i < size; ++i) {
182 std::string().swap(s);
183 if (!data.ReadString(s)) {
184 return NETMANAGER_ERR_READ_DATA_FAIL;
185 }
186 domains.push_back(s);
187 }
188 int32_t ret = SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
189 if (!reply.WriteInt32(ret)) {
190 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
191 }
192 return NETMANAGER_SUCCESS;
193 }
194
OnGetResolverInfo(MessageParcel & data,MessageParcel & reply)195 int32_t DnsResolverServiceStub::OnGetResolverInfo(MessageParcel &data, MessageParcel &reply)
196 {
197 int32_t netId = 0;
198 std::vector<std::string> servers;
199 std::vector<std::string> domains;
200 uint16_t baseTimeoutMsec = 0;
201 uint8_t retryCount = 0;
202 if (!data.ReadInt32(netId)) {
203 return NETMANAGER_ERR_READ_DATA_FAIL;
204 }
205 int32_t ret = GetResolverInfo(netId, servers, domains, baseTimeoutMsec, retryCount);
206
207 int32_t vsize = static_cast<int32_t>(servers.size());
208 if (!reply.WriteInt32(vsize)) {
209 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
210 }
211 for (std::vector<std::string>::iterator it = servers.begin(); it != servers.end(); ++it) {
212 if (!reply.WriteString(*it)) {
213 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
214 }
215 }
216
217 vsize = static_cast<int32_t>(domains.size());
218 if (!reply.WriteInt32(vsize)) {
219 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
220 }
221 for (std::vector<std::string>::iterator it = domains.begin(); it != domains.end(); ++it) {
222 if (!reply.WriteString(*it)) {
223 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
224 }
225 }
226
227 if (!reply.WriteUint16(baseTimeoutMsec)) {
228 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
229 }
230
231 if (!reply.WriteUint8(retryCount)) {
232 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
233 }
234
235 if (!reply.WriteInt32(ret)) {
236 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
237 }
238 return NETMANAGER_SUCCESS;
239 }
240 } // namespace NetManagerStandard
241 } // namespace OHOS