1 /*
2 * Copyright (c) 2021-2024 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 "net_conn_service_stub.h"
17 #include "ipc_skeleton.h"
18 #include "net_conn_constants.h"
19 #include "net_conn_types.h"
20 #include "net_manager_constants.h"
21 #include "net_mgr_log_wrapper.h"
22 #include "netmanager_base_permission.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 constexpr int32_t MAX_VNIC_UID_ARRAY_SIZE = 20;
28 constexpr uint32_t MAX_IFACE_NUM = 16;
29 constexpr uint32_t MAX_NET_CAP_NUM = 32;
30 constexpr uint32_t UID_FOUNDATION = 5523;
31 const std::vector<uint32_t> SYSTEM_CODE{static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_AIRPLANE_MODE),
32 static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_GLOBAL_HTTP_PROXY),
33 static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_GLOBAL_HTTP_PROXY),
34 static_cast<uint32_t>(ConnInterfaceCode::CMD_GET_IFACENAME_IDENT_MAPS),
35 static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_FACTORYRESET_NETWORK)};
36 const std::vector<uint32_t> PERMISSION_NEED_CACHE_CODES{
37 static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GETDEFAULTNETWORK),
38 static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_HASDEFAULTNET)};
39 } // namespace
NetConnServiceStub()40 NetConnServiceStub::NetConnServiceStub()
41 {
42 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SYSTEM_READY)] = {
43 &NetConnServiceStub::OnSystemReady, {}};
44 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_CONN_CALLBACK)] = {
45 &NetConnServiceStub::OnRegisterNetConnCallback, {Permission::GET_NETWORK_INFO}};
46 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_CONN_CALLBACK_BY_SPECIFIER)] = {
47 &NetConnServiceStub::OnRegisterNetConnCallbackBySpecifier, {Permission::GET_NETWORK_INFO}};
48 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REQUEST_NET_CONNECTION)] = {
49 &NetConnServiceStub::OnRequestNetConnectionBySpecifier, {}};
50 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_UNREGISTER_NET_CONN_CALLBACK)] = {
51 &NetConnServiceStub::OnUnregisterNetConnCallback, {Permission::GET_NETWORK_INFO}};
52 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_UPDATE_NET_STATE_FOR_TEST)] = {
53 &NetConnServiceStub::OnUpdateNetStateForTest, {}};
54 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REG_NET_SUPPLIER)] = {
55 &NetConnServiceStub::OnRegisterNetSupplier, {Permission::CONNECTIVITY_INTERNAL}};
56 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_UNREG_NETWORK)] = {
57 &NetConnServiceStub::OnUnregisterNetSupplier, {Permission::CONNECTIVITY_INTERNAL}};
58 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_NET_SUPPLIER_INFO)] = {
59 &NetConnServiceStub::OnUpdateNetSupplierInfo, {Permission::CONNECTIVITY_INTERNAL}};
60 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_NET_LINK_INFO)] = {
61 &NetConnServiceStub::OnUpdateNetLinkInfo, {Permission::CONNECTIVITY_INTERNAL}};
62 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_DETECTION_RET_CALLBACK)] = {
63 &NetConnServiceStub::OnRegisterNetDetectionCallback, {}};
64 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_UNREGISTER_NET_DETECTION_RET_CALLBACK)] = {
65 &NetConnServiceStub::OnUnRegisterNetDetectionCallback, {}};
66 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_NET_DETECTION)] = {
67 &NetConnServiceStub::OnNetDetection, {Permission::GET_NETWORK_INFO, Permission::INTERNET}};
68 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_BIND_SOCKET)] = {&NetConnServiceStub::OnBindSocket,
69 {}};
70 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_SUPPLIER_CALLBACK)] = {
71 &NetConnServiceStub::OnRegisterNetSupplierCallback, {Permission::CONNECTIVITY_INTERNAL}};
72 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_AIRPLANE_MODE)] = {
73 &NetConnServiceStub::OnSetAirplaneMode, {Permission::CONNECTIVITY_INTERNAL}};
74 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_GLOBAL_HTTP_PROXY)] = {
75 &NetConnServiceStub::OnSetGlobalHttpProxy, {Permission::CONNECTIVITY_INTERNAL}};
76 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_APP_NET)] = {&NetConnServiceStub::OnSetAppNet,
77 {Permission::INTERNET}};
78 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_INTERNET_PERMISSION)] = {
79 &NetConnServiceStub::OnSetInternetPermission, {Permission::CONNECTIVITY_INTERNAL}};
80 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_INTERFACE_CALLBACK)] = {
81 &NetConnServiceStub::OnRegisterNetInterfaceCallback, {Permission::CONNECTIVITY_INTERNAL}};
82 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_ADD_NET_ROUTE)] = {
83 &NetConnServiceStub::OnAddNetworkRoute, {Permission::CONNECTIVITY_INTERNAL}};
84 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REMOVE_NET_ROUTE)] = {
85 &NetConnServiceStub::OnRemoveNetworkRoute, {Permission::CONNECTIVITY_INTERNAL}};
86 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_IS_PREFER_CELLULAR_URL)] = {
87 &NetConnServiceStub::OnIsPreferCellularUrl, {Permission::GET_NETWORK_INFO}};
88 InitAll();
89 }
90
InitAll()91 void NetConnServiceStub::InitAll()
92 {
93 InitInterfaceFuncToInterfaceMap();
94 InitResetNetFuncToInterfaceMap();
95 InitStaticArpToInterfaceMap();
96 InitQueryFuncToInterfaceMap();
97 InitVnicFuncToInterfaceMap();
98 }
99
InitInterfaceFuncToInterfaceMap()100 void NetConnServiceStub::InitInterfaceFuncToInterfaceMap()
101 {
102 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_ADD_NET_ADDRESS)] = {
103 &NetConnServiceStub::OnAddInterfaceAddress, {Permission::CONNECTIVITY_INTERNAL}};
104 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REMOVE_NET_ADDRESS)] = {
105 &NetConnServiceStub::OnDelInterfaceAddress, {Permission::CONNECTIVITY_INTERNAL}};
106 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_PREAIRPLANE_CALLBACK)] = {
107 &NetConnServiceStub::OnRegisterPreAirplaneCallback, {Permission::CONNECTIVITY_INTERNAL}};
108 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_UNREGISTER_PREAIRPLANE_CALLBACK)] = {
109 &NetConnServiceStub::OnUnregisterPreAirplaneCallback, {Permission::CONNECTIVITY_INTERNAL}};
110 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_UPDATE_SUPPLIER_SCORE)] = {
111 &NetConnServiceStub::OnUpdateSupplierScore, {Permission::CONNECTIVITY_INTERNAL}};
112 }
113
InitResetNetFuncToInterfaceMap()114 void NetConnServiceStub::InitResetNetFuncToInterfaceMap()
115 {
116 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_FACTORYRESET_NETWORK)] = {
117 &NetConnServiceStub::OnFactoryResetNetwork, {Permission::CONNECTIVITY_INTERNAL}};
118 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_FACTORYRESET_CALLBACK)] = {
119 &NetConnServiceStub::OnRegisterNetFactoryResetCallback, {Permission::CONNECTIVITY_INTERNAL}};
120 }
121
InitStaticArpToInterfaceMap()122 void NetConnServiceStub::InitStaticArpToInterfaceMap()
123 {
124 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_ADD_STATIC_ARP)] = {
125 &NetConnServiceStub::OnAddStaticArp, {Permission::CONNECTIVITY_INTERNAL}};
126 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_DEL_STATIC_ARP)] = {
127 &NetConnServiceStub::OnDelStaticArp, {Permission::CONNECTIVITY_INTERNAL}};
128 }
129
InitQueryFuncToInterfaceMap()130 void NetConnServiceStub::InitQueryFuncToInterfaceMap()
131 {
132 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_IFACE_NAMES)] = {
133 &NetConnServiceStub::OnGetIfaceNames, {}};
134 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_IFACENAME_BY_TYPE)] = {
135 &NetConnServiceStub::OnGetIfaceNameByType, {}};
136 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_GET_IFACENAME_IDENT_MAPS)] = {
137 &NetConnServiceStub::OnGetIfaceNameIdentMaps, {Permission::GET_NETWORK_INFO}};
138 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GETDEFAULTNETWORK)] = {
139 &NetConnServiceStub::OnGetDefaultNet, {Permission::GET_NETWORK_INFO}};
140 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_HASDEFAULTNET)] = {
141 &NetConnServiceStub::OnHasDefaultNet, {Permission::GET_NETWORK_INFO}};
142 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_SPECIFIC_NET)] = {
143 &NetConnServiceStub::OnGetSpecificNet, {}};
144 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_ALL_NETS)] = {&NetConnServiceStub::OnGetAllNets,
145 {Permission::GET_NETWORK_INFO}};
146 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_SPECIFIC_UID_NET)] = {
147 &NetConnServiceStub::OnGetSpecificUidNet, {}};
148 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_CONNECTION_PROPERTIES)] = {
149 &NetConnServiceStub::OnGetConnectionProperties, {Permission::GET_NETWORK_INFO}};
150 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_NET_CAPABILITIES)] = {
151 &NetConnServiceStub::OnGetNetCapabilities, {Permission::GET_NETWORK_INFO}};
152 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_ADDRESSES_BY_NAME)] = {
153 &NetConnServiceStub::OnGetAddressesByName, {Permission::INTERNET}};
154 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_ADDRESS_BY_NAME)] = {
155 &NetConnServiceStub::OnGetAddressByName, {Permission::INTERNET}};
156 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_IS_DEFAULT_NET_METERED)] = {
157 &NetConnServiceStub::OnIsDefaultNetMetered, {Permission::GET_NETWORK_INFO}};
158 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_GLOBAL_HTTP_PROXY)] = {
159 &NetConnServiceStub::OnGetGlobalHttpProxy, {}};
160 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_DEFAULT_HTTP_PROXY)] = {
161 &NetConnServiceStub::OnGetDefaultHttpProxy, {}};
162 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_NET_ID_BY_IDENTIFIER)] = {
163 &NetConnServiceStub::OnGetNetIdByIdentifier, {}};
164 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_INTERFACE_CONFIGURATION)] = {
165 &NetConnServiceStub::OnGetNetInterfaceConfiguration, {Permission::CONNECTIVITY_INTERNAL}};
166 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_SLOT_TYPE)] = {
167 &NetConnServiceStub::OnRegisterSlotType, {Permission::CONNECTIVITY_INTERNAL}};
168 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_SLOT_TYPE)] = {
169 &NetConnServiceStub::OnGetSlotType, {Permission::GET_NETWORK_INFO}};
170 }
171
InitVnicFuncToInterfaceMap()172 void NetConnServiceStub::InitVnicFuncToInterfaceMap()
173 {
174 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_ENABLE_VNIC_NET_WORK)] = {
175 &NetConnServiceStub::OnEnableVnicNetwork, {Permission::CONNECTIVITY_INTERNAL}};
176 memberFuncMap_[static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_DISABLE_VNIC_NET_WORK)] = {
177 &NetConnServiceStub::OnDisableVnicNetwork, {Permission::CONNECTIVITY_INTERNAL}};
178 }
179
~NetConnServiceStub()180 NetConnServiceStub::~NetConnServiceStub() {}
181
ToUtf8(std::u16string str16)182 std::string ToUtf8(std::u16string str16)
183 {
184 return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(str16);
185 }
186
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)187 int32_t NetConnServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
188 MessageOption &option)
189 {
190 NETMGR_LOG_D("stub call start, code = [%{public}d]", code);
191
192 std::u16string myDescripter = NetConnServiceStub::GetDescriptor();
193 std::u16string remoteDescripter = data.ReadInterfaceToken();
194 if (myDescripter != remoteDescripter) {
195 NETMGR_LOG_E("descriptor checked fail. my Descriptor: [%{public}s], remote Descriptor: [%{public}s]",
196 ToUtf8(myDescripter).c_str(), ToUtf8(remoteDescripter).c_str());
197 if (!reply.WriteInt32(NETMANAGER_ERR_DESCRIPTOR_MISMATCH)) {
198 return IPC_STUB_WRITE_PARCEL_ERR;
199 }
200 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
201 }
202
203 auto itFunc = memberFuncMap_.find(code);
204 if (itFunc == memberFuncMap_.end()) {
205 NETMGR_LOG_E("memberFuncMap not found this code! code: [%{public}d]", code);
206 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
207 }
208 auto requestFunc = itFunc->second.first;
209 if (requestFunc == nullptr) {
210 NETMGR_LOG_E("requestFunc is nullptr. code:[%{public}d]", code);
211 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
212 }
213 if (code == static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_INTERNET_PERMISSION)) {
214 // get uid should be called in this function
215 auto uid = IPCSkeleton::GetCallingUid();
216 if (uid != UID_FOUNDATION && !CheckPermission({Permission::CONNECTIVITY_INTERNAL})) {
217 if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
218 return IPC_STUB_WRITE_PARCEL_ERR;
219 }
220 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
221 }
222 }
223
224 int32_t ret = OnRequestCheck(code, itFunc->second.second);
225 if (ret == NETMANAGER_SUCCESS) {
226 ret =(this->*requestFunc)(data, reply);
227 NETMGR_LOG_D("stub call end, code = [%{public}d]", code);
228 return ret;
229 }
230 if (!reply.WriteInt32(ret)) {
231 return IPC_STUB_WRITE_PARCEL_ERR;
232 }
233 NETMGR_LOG_D("stub default case, need check");
234 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
235 }
236
OnRequestCheck(uint32_t code,const std::set<std::string> & permissions)237 int32_t NetConnServiceStub::OnRequestCheck(uint32_t code, const std::set<std::string> &permissions)
238 {
239 if (std::find(SYSTEM_CODE.begin(), SYSTEM_CODE.end(), code) != SYSTEM_CODE.end()) {
240 if (!NetManagerPermission::IsSystemCaller()) {
241 NETMGR_LOG_E("Non-system applications use system APIs.");
242 return NETMANAGER_ERR_NOT_SYSTEM_CALL;
243 }
244 }
245
246 if (std::find(PERMISSION_NEED_CACHE_CODES.begin(), PERMISSION_NEED_CACHE_CODES.end(), code) !=
247 PERMISSION_NEED_CACHE_CODES.end()) {
248 if (CheckPermissionWithCache(permissions)) {
249 return NETMANAGER_SUCCESS;
250 }
251 } else {
252 if (CheckPermission(permissions)) {
253 return NETMANAGER_SUCCESS;
254 }
255 }
256 return NETMANAGER_ERR_PERMISSION_DENIED;
257 }
258
CheckPermission(const std::set<std::string> & permissions)259 bool NetConnServiceStub::CheckPermission(const std::set<std::string> &permissions)
260 {
261 for (const auto &permission : permissions) {
262 if (!NetManagerPermission::CheckPermission(permission)) {
263 return false;
264 }
265 }
266 return true;
267 }
268
CheckPermissionWithCache(const std::set<std::string> & permissions)269 bool NetConnServiceStub::CheckPermissionWithCache(const std::set<std::string> &permissions)
270 {
271 for (const auto &permission : permissions) {
272 if (!NetManagerPermission::CheckPermissionWithCache(permission)) {
273 return false;
274 }
275 }
276 return true;
277 }
278
OnSystemReady(MessageParcel & data,MessageParcel & reply)279 int32_t NetConnServiceStub::OnSystemReady(MessageParcel &data, MessageParcel &reply)
280 {
281 int32_t ret = SystemReady();
282 if (!reply.WriteInt32(ret)) {
283 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
284 }
285
286 return NETMANAGER_SUCCESS;
287 }
288
OnSetInternetPermission(MessageParcel & data,MessageParcel & reply)289 int32_t NetConnServiceStub::OnSetInternetPermission(MessageParcel &data, MessageParcel &reply)
290 {
291 uint32_t uid;
292 if (!data.ReadUint32(uid)) {
293 return NETMANAGER_ERR_READ_DATA_FAIL;
294 }
295
296 uint8_t allow;
297 if (!data.ReadUint8(allow)) {
298 return NETMANAGER_ERR_READ_DATA_FAIL;
299 }
300
301 int32_t ret = SetInternetPermission(uid, allow);
302 if (!reply.WriteInt32(ret)) {
303 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
304 }
305
306 return NETMANAGER_SUCCESS;
307 }
308
OnEnableVnicNetwork(MessageParcel & data,MessageParcel & reply)309 int32_t NetConnServiceStub::OnEnableVnicNetwork(MessageParcel &data, MessageParcel &reply)
310 {
311 sptr<NetLinkInfo> netLinkInfo = NetLinkInfo::Unmarshalling(data);
312 if (netLinkInfo == nullptr) {
313 NETMGR_LOG_E("netLinkInfo ptr is nullptr.");
314 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
315 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
316 }
317 return NETMANAGER_ERR_LOCAL_PTR_NULL;
318 }
319 std::set<int32_t> uids;
320 int32_t size = 0;
321 int32_t uid = 0;
322 if (!data.ReadInt32(size)) {
323 return NETMANAGER_ERR_READ_DATA_FAIL;
324 }
325
326 if (size < 0 || size > MAX_VNIC_UID_ARRAY_SIZE) {
327 NETMGR_LOG_E("vnic uids size is invalid");
328 return NETMANAGER_ERR_READ_DATA_FAIL;
329 }
330
331 for (int32_t index = 0; index < size; index++) {
332 if (!data.ReadInt32(uid)) {
333 return NETMANAGER_ERR_READ_DATA_FAIL;
334 }
335 uids.insert(uid);
336 }
337 int32_t ret = EnableVnicNetwork(netLinkInfo, uids);
338 if (!reply.WriteInt32(ret)) {
339 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
340 }
341 return NETMANAGER_SUCCESS;
342 }
343
OnDisableVnicNetwork(MessageParcel & data,MessageParcel & reply)344 int32_t NetConnServiceStub::OnDisableVnicNetwork(MessageParcel &data, MessageParcel &reply)
345 {
346 int32_t ret = DisableVnicNetwork();
347 if (!reply.WriteInt32(ret)) {
348 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
349 }
350 return NETMANAGER_SUCCESS;
351 }
352
OnRegisterNetSupplier(MessageParcel & data,MessageParcel & reply)353 int32_t NetConnServiceStub::OnRegisterNetSupplier(MessageParcel &data, MessageParcel &reply)
354 {
355 NETMGR_LOG_D("stub processing");
356 NetBearType bearerType;
357 std::string ident;
358 std::set<NetCap> netCaps;
359
360 uint32_t type = 0;
361 if (!data.ReadUint32(type)) {
362 return NETMANAGER_ERR_READ_DATA_FAIL;
363 }
364 if (type > static_cast<uint32_t>(NetBearType::BEARER_DEFAULT)) {
365 return NETMANAGER_ERR_INTERNAL;
366 }
367 bearerType = static_cast<NetBearType>(type);
368
369 if (!data.ReadString(ident)) {
370 return NETMANAGER_ERR_READ_DATA_FAIL;
371 }
372 uint32_t size = 0;
373 uint32_t value = 0;
374 if (!data.ReadUint32(size)) {
375 return NETMANAGER_ERR_READ_DATA_FAIL;
376 }
377 size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
378 for (uint32_t i = 0; i < size; ++i) {
379 if (!data.ReadUint32(value)) {
380 return NETMANAGER_ERR_READ_DATA_FAIL;
381 }
382 if (value < NET_CAPABILITY_END) {
383 netCaps.insert(static_cast<NetCap>(value));
384 }
385 }
386
387 uint32_t supplierId = 0;
388 int32_t ret = RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
389 if (!reply.WriteInt32(ret)) {
390 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
391 }
392 if (ret == NETMANAGER_SUCCESS) {
393 NETMGR_LOG_D("supplierId[%{public}d].", supplierId);
394 if (!reply.WriteUint32(supplierId)) {
395 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
396 }
397 }
398 return NETMANAGER_SUCCESS;
399 }
400
OnUnregisterNetSupplier(MessageParcel & data,MessageParcel & reply)401 int32_t NetConnServiceStub::OnUnregisterNetSupplier(MessageParcel &data, MessageParcel &reply)
402 {
403 uint32_t supplierId;
404 if (!data.ReadUint32(supplierId)) {
405 return NETMANAGER_ERR_READ_DATA_FAIL;
406 }
407
408 int32_t ret = UnregisterNetSupplier(supplierId);
409 if (!reply.WriteInt32(ret)) {
410 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
411 }
412
413 return NETMANAGER_SUCCESS;
414 }
415
OnRegisterNetSupplierCallback(MessageParcel & data,MessageParcel & reply)416 int32_t NetConnServiceStub::OnRegisterNetSupplierCallback(MessageParcel &data, MessageParcel &reply)
417 {
418 uint32_t supplierId;
419 data.ReadUint32(supplierId);
420 sptr<IRemoteObject> remote = data.ReadRemoteObject();
421 if (remote == nullptr) {
422 NETMGR_LOG_E("Remote ptr is nullptr.");
423 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
424 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
425 }
426 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
427 }
428
429 sptr<INetSupplierCallback> callback = iface_cast<INetSupplierCallback>(remote);
430 if (callback == nullptr) {
431 NETMGR_LOG_E("Callback ptr is nullptr.");
432 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
433 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
434 }
435 return NETMANAGER_ERR_LOCAL_PTR_NULL;
436 }
437
438 int32_t result = RegisterNetSupplierCallback(supplierId, callback);
439 if (!reply.WriteInt32(result)) {
440 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
441 }
442 return NETMANAGER_SUCCESS;
443 }
444
OnRegisterNetConnCallback(MessageParcel & data,MessageParcel & reply)445 int32_t NetConnServiceStub::OnRegisterNetConnCallback(MessageParcel &data, MessageParcel &reply)
446 {
447 sptr<IRemoteObject> remote = data.ReadRemoteObject();
448 if (remote == nullptr) {
449 NETMGR_LOG_E("Remote ptr is nullptr.");
450 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
451 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
452 }
453 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
454 }
455
456 sptr<INetConnCallback> callback = iface_cast<INetConnCallback>(remote);
457 if (callback == nullptr) {
458 NETMGR_LOG_E("Callback ptr is nullptr.");
459 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
460 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
461 }
462 return NETMANAGER_ERR_LOCAL_PTR_NULL;
463 }
464
465 int32_t result = RegisterNetConnCallback(callback);
466 if (!reply.WriteInt32(result)) {
467 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
468 }
469 return NETMANAGER_SUCCESS;
470 }
471
OnRegisterNetConnCallbackBySpecifier(MessageParcel & data,MessageParcel & reply)472 int32_t NetConnServiceStub::OnRegisterNetConnCallbackBySpecifier(MessageParcel &data, MessageParcel &reply)
473 {
474 sptr<NetSpecifier> netSpecifier = NetSpecifier::Unmarshalling(data);
475 uint32_t timeoutMS = data.ReadUint32();
476 sptr<IRemoteObject> remote = data.ReadRemoteObject();
477 if (remote == nullptr) {
478 NETMGR_LOG_E("callback ptr is nullptr.");
479 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
480 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
481 }
482 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
483 }
484
485 sptr<INetConnCallback> callback = iface_cast<INetConnCallback>(remote);
486 if (callback == nullptr) {
487 NETMGR_LOG_E("Callback ptr is nullptr.");
488 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
489 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
490 }
491 return NETMANAGER_ERR_LOCAL_PTR_NULL;
492 }
493
494 int32_t result = RegisterNetConnCallback(netSpecifier, callback, timeoutMS);
495 if (!reply.WriteInt32(result)) {
496 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
497 }
498 return NETMANAGER_SUCCESS;
499 }
500
OnRequestNetConnectionBySpecifier(MessageParcel & data,MessageParcel & reply)501 int32_t NetConnServiceStub::OnRequestNetConnectionBySpecifier(MessageParcel &data, MessageParcel &reply)
502 {
503 sptr<NetSpecifier> netSpecifier = NetSpecifier::Unmarshalling(data);
504 uint32_t timeoutMS = data.ReadUint32();
505 sptr<IRemoteObject> remote = data.ReadRemoteObject();
506 if (remote == nullptr) {
507 NETMGR_LOG_E("Remote ptr is nullptr.");
508 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
509 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
510 }
511 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
512 }
513
514 sptr<INetConnCallback> callback = iface_cast<INetConnCallback>(remote);
515 if (callback == nullptr) {
516 NETMGR_LOG_E("Callback ptr is nullptr.");
517 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
518 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
519 }
520 return NETMANAGER_ERR_LOCAL_PTR_NULL;
521 }
522
523 int32_t result = RequestNetConnection(netSpecifier, callback, timeoutMS);
524 if (!reply.WriteInt32(result)) {
525 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
526 }
527 return NETMANAGER_SUCCESS;
528 }
529
OnUnregisterNetConnCallback(MessageParcel & data,MessageParcel & reply)530 int32_t NetConnServiceStub::OnUnregisterNetConnCallback(MessageParcel &data, MessageParcel &reply)
531 {
532 sptr<IRemoteObject> remote = data.ReadRemoteObject();
533 if (remote == nullptr) {
534 NETMGR_LOG_E("Remote ptr is nullptr.");
535 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
536 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
537 }
538 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
539 }
540
541 sptr<INetConnCallback> callback = iface_cast<INetConnCallback>(remote);
542 if (callback == nullptr) {
543 NETMGR_LOG_E("Callback ptr is nullptr.");
544 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
545 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
546 }
547 return NETMANAGER_ERR_LOCAL_PTR_NULL;
548 }
549
550 int32_t result = UnregisterNetConnCallback(callback);
551 if (!reply.WriteInt32(result)) {
552 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
553 }
554 return NETMANAGER_SUCCESS;
555 }
556
OnUpdateNetStateForTest(MessageParcel & data,MessageParcel & reply)557 int32_t NetConnServiceStub::OnUpdateNetStateForTest(MessageParcel &data, MessageParcel &reply)
558 {
559 NETMGR_LOG_D("Test NetConnServiceStub::OnUpdateNetStateForTest(), begin");
560 sptr<NetSpecifier> netSpecifier = NetSpecifier::Unmarshalling(data);
561
562 int32_t netState;
563 if (!data.ReadInt32(netState)) {
564 return NETMANAGER_ERR_READ_DATA_FAIL;
565 }
566
567 NETMGR_LOG_D("Test NetConnServiceStub::OnUpdateNetStateForTest(), netState[%{public}d]", netState);
568 int32_t result = UpdateNetStateForTest(netSpecifier, netState);
569 NETMGR_LOG_D("Test NetConnServiceStub::OnUpdateNetStateForTest(), result[%{public}d]", result);
570 if (!reply.WriteInt32(result)) {
571 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
572 }
573 return NETMANAGER_SUCCESS;
574 }
575
OnUpdateNetSupplierInfo(MessageParcel & data,MessageParcel & reply)576 int32_t NetConnServiceStub::OnUpdateNetSupplierInfo(MessageParcel &data, MessageParcel &reply)
577 {
578 NETMGR_LOG_D("OnUpdateNetSupplierInfo in.");
579 uint32_t supplierId;
580 if (!data.ReadUint32(supplierId)) {
581 NETMGR_LOG_D("fail to get supplier id.");
582 return NETMANAGER_ERR_READ_DATA_FAIL;
583 }
584
585 NETMGR_LOG_D("OnUpdateNetSupplierInfo supplierId=[%{public}d].", supplierId);
586 sptr<NetSupplierInfo> netSupplierInfo = NetSupplierInfo::Unmarshalling(data);
587 int32_t ret = UpdateNetSupplierInfo(supplierId, netSupplierInfo);
588 if (!reply.WriteInt32(ret)) {
589 NETMGR_LOG_D("fail to update net supplier info.");
590 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
591 }
592 NETMGR_LOG_D("OnUpdateNetSupplierInfo out.");
593
594 return NETMANAGER_SUCCESS;
595 }
596
OnUpdateNetLinkInfo(MessageParcel & data,MessageParcel & reply)597 int32_t NetConnServiceStub::OnUpdateNetLinkInfo(MessageParcel &data, MessageParcel &reply)
598 {
599 uint32_t supplierId;
600
601 if (!data.ReadUint32(supplierId)) {
602 return NETMANAGER_ERR_READ_DATA_FAIL;
603 }
604
605 sptr<NetLinkInfo> netLinkInfo = NetLinkInfo::Unmarshalling(data);
606
607 int32_t ret = UpdateNetLinkInfo(supplierId, netLinkInfo);
608 if (!reply.WriteInt32(ret)) {
609 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
610 }
611
612 return NETMANAGER_SUCCESS;
613 }
614
OnRegisterNetDetectionCallback(MessageParcel & data,MessageParcel & reply)615 int32_t NetConnServiceStub::OnRegisterNetDetectionCallback(MessageParcel &data, MessageParcel &reply)
616 {
617 if (!data.ContainFileDescriptors()) {
618 NETMGR_LOG_E("Execute ContainFileDescriptors failed");
619 }
620 int32_t netId = 0;
621 if (!data.ReadInt32(netId)) {
622 return NETMANAGER_ERR_READ_DATA_FAIL;
623 }
624
625 sptr<IRemoteObject> remote = data.ReadRemoteObject();
626 if (remote == nullptr) {
627 NETMGR_LOG_E("Remote ptr is nullptr.");
628 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
629 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
630 }
631 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
632 }
633
634 sptr<INetDetectionCallback> callback = iface_cast<INetDetectionCallback>(remote);
635 if (callback == nullptr) {
636 NETMGR_LOG_E("Callback ptr is nullptr.");
637 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
638 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
639 }
640 return NETMANAGER_ERR_LOCAL_PTR_NULL;
641 }
642
643 int32_t result = RegisterNetDetectionCallback(netId, callback);
644 if (!reply.WriteInt32(result)) {
645 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
646 }
647 return NETMANAGER_SUCCESS;
648 }
649
OnUnRegisterNetDetectionCallback(MessageParcel & data,MessageParcel & reply)650 int32_t NetConnServiceStub::OnUnRegisterNetDetectionCallback(MessageParcel &data, MessageParcel &reply)
651 {
652 if (!data.ContainFileDescriptors()) {
653 NETMGR_LOG_E("Execute ContainFileDescriptors failed");
654 }
655 int32_t netId = 0;
656 if (!data.ReadInt32(netId)) {
657 return NETMANAGER_ERR_READ_DATA_FAIL;
658 }
659
660 sptr<IRemoteObject> remote = data.ReadRemoteObject();
661 if (remote == nullptr) {
662 NETMGR_LOG_E("Remote ptr is nullptr.");
663 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
664 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
665 }
666 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
667 }
668
669 sptr<INetDetectionCallback> callback = iface_cast<INetDetectionCallback>(remote);
670 if (callback == nullptr) {
671 NETMGR_LOG_E("Callback ptr is nullptr.");
672 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
673 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
674 }
675 return NETMANAGER_ERR_LOCAL_PTR_NULL;
676 }
677
678 int32_t result = UnRegisterNetDetectionCallback(netId, callback);
679 if (!reply.WriteInt32(result)) {
680 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
681 }
682 return NETMANAGER_SUCCESS;
683 }
684
OnNetDetection(MessageParcel & data,MessageParcel & reply)685 int32_t NetConnServiceStub::OnNetDetection(MessageParcel &data, MessageParcel &reply)
686 {
687 int32_t netId = 0;
688 if (!data.ReadInt32(netId)) {
689 return NETMANAGER_ERR_READ_DATA_FAIL;
690 }
691 int32_t ret = NetDetection(netId);
692 if (!reply.WriteInt32(ret)) {
693 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
694 }
695 return NETMANAGER_SUCCESS;
696 }
697
OnGetIfaceNames(MessageParcel & data,MessageParcel & reply)698 int32_t NetConnServiceStub::OnGetIfaceNames(MessageParcel &data, MessageParcel &reply)
699 {
700 uint32_t netType = 0;
701 if (!data.ReadUint32(netType)) {
702 return NETMANAGER_ERR_READ_DATA_FAIL;
703 }
704 if (netType > static_cast<uint32_t>(NetBearType::BEARER_DEFAULT)) {
705 return NETMANAGER_ERR_INTERNAL;
706 }
707 NetBearType bearerType = static_cast<NetBearType>(netType);
708 std::list<std::string> ifaceNames;
709 int32_t ret = GetIfaceNames(bearerType, ifaceNames);
710 if (!reply.WriteInt32(ret)) {
711 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
712 }
713 if (ret == NETMANAGER_SUCCESS) {
714 if (!reply.WriteUint32(ifaceNames.size())) {
715 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
716 }
717
718 for (const auto &ifaceName : ifaceNames) {
719 if (!reply.WriteString(ifaceName)) {
720 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
721 }
722 }
723 }
724 return NETMANAGER_SUCCESS;
725 }
726
OnGetIfaceNameByType(MessageParcel & data,MessageParcel & reply)727 int32_t NetConnServiceStub::OnGetIfaceNameByType(MessageParcel &data, MessageParcel &reply)
728 {
729 uint32_t netType = 0;
730 if (!data.ReadUint32(netType)) {
731 return NETMANAGER_ERR_READ_DATA_FAIL;
732 }
733 if (netType > static_cast<uint32_t>(NetBearType::BEARER_DEFAULT)) {
734 return NETMANAGER_ERR_INTERNAL;
735 }
736 NetBearType bearerType = static_cast<NetBearType>(netType);
737
738 std::string ident;
739 if (!data.ReadString(ident)) {
740 return NETMANAGER_ERR_READ_DATA_FAIL;
741 }
742
743 std::string ifaceName;
744 int32_t ret = GetIfaceNameByType(bearerType, ident, ifaceName);
745 if (!reply.WriteInt32(ret)) {
746 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
747 }
748 if (ret == NETMANAGER_SUCCESS) {
749 if (!reply.WriteString(ifaceName)) {
750 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
751 }
752 }
753 return NETMANAGER_SUCCESS;
754 }
755
OnGetIfaceNameIdentMaps(MessageParcel & data,MessageParcel & reply)756 int32_t NetConnServiceStub::OnGetIfaceNameIdentMaps(MessageParcel &data, MessageParcel &reply)
757 {
758 uint32_t netType = 0;
759 if (!data.ReadUint32(netType)) {
760 return NETMANAGER_ERR_READ_DATA_FAIL;
761 }
762 if (netType > static_cast<uint32_t>(NetBearType::BEARER_DEFAULT)) {
763 return NETMANAGER_ERR_INTERNAL;
764 }
765 NetBearType bearerType = static_cast<NetBearType>(netType);
766 SafeMap<std::string, std::string> ifaceNameIdentMaps;
767 int32_t ret = GetIfaceNameIdentMaps(bearerType, ifaceNameIdentMaps);
768 if (!reply.WriteInt32(ret)) {
769 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
770 }
771 if (ret == NETMANAGER_SUCCESS) {
772 if (!reply.WriteUint32(ifaceNameIdentMaps.Size())) {
773 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
774 }
775 int32_t err = NETMANAGER_SUCCESS;
776 ifaceNameIdentMaps.Iterate([&err, &reply](const std::string &k, const std::string &v) -> void {
777 if (!reply.WriteString(k) || !reply.WriteString(v)) {
778 err = NETMANAGER_ERR_WRITE_REPLY_FAIL;
779 }
780 });
781 if (err != NETMANAGER_SUCCESS) {
782 return err;
783 }
784 }
785 return NETMANAGER_SUCCESS;
786 }
787
OnGetDefaultNet(MessageParcel & data,MessageParcel & reply)788 int32_t NetConnServiceStub::OnGetDefaultNet(MessageParcel &data, MessageParcel &reply)
789 {
790 NETMGR_LOG_D("OnGetDefaultNet Begin...");
791 int32_t netId;
792 int32_t result = GetDefaultNet(netId);
793 NETMGR_LOG_D("GetDefaultNet result is: [%{public}d]", result);
794 if (!reply.WriteInt32(result)) {
795 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
796 }
797 if (result == NETMANAGER_SUCCESS) {
798 if (!reply.WriteUint32(netId)) {
799 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
800 }
801 }
802 return NETMANAGER_SUCCESS;
803 }
804
OnHasDefaultNet(MessageParcel & data,MessageParcel & reply)805 int32_t NetConnServiceStub::OnHasDefaultNet(MessageParcel &data, MessageParcel &reply)
806 {
807 NETMGR_LOG_D("OnHasDefaultNet Begin...");
808 bool flag = false;
809 int32_t result = HasDefaultNet(flag);
810 NETMGR_LOG_D("HasDefaultNet result is: [%{public}d]", result);
811 if (!reply.WriteInt32(result)) {
812 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
813 }
814 if (result == NETMANAGER_SUCCESS) {
815 if (!reply.WriteBool(flag)) {
816 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
817 }
818 }
819 return NETMANAGER_SUCCESS;
820 }
821
OnGetSpecificNet(MessageParcel & data,MessageParcel & reply)822 int32_t NetConnServiceStub::OnGetSpecificNet(MessageParcel &data, MessageParcel &reply)
823 {
824 uint32_t type;
825 if (!data.ReadUint32(type)) {
826 return NETMANAGER_ERR_READ_DATA_FAIL;
827 }
828
829 if (type > static_cast<uint32_t>(NetBearType::BEARER_DEFAULT)) {
830 return NETMANAGER_ERR_INTERNAL;
831 }
832
833 NetBearType bearerType = static_cast<NetBearType>(type);
834
835 NETMGR_LOG_D("stub execute GetSpecificNet");
836 std::list<int32_t> netIdList;
837 int32_t ret = GetSpecificNet(bearerType, netIdList);
838 if (!reply.WriteInt32(ret)) {
839 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
840 }
841 if (ret == NETMANAGER_SUCCESS) {
842 uint32_t size = static_cast<uint32_t>(netIdList.size());
843 size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size;
844 if (!reply.WriteUint32(size)) {
845 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
846 }
847
848 uint32_t index = 0;
849 for (auto p = netIdList.begin(); p != netIdList.end(); ++p) {
850 if (++index > MAX_IFACE_NUM) {
851 break;
852 }
853 if (!reply.WriteInt32(*p)) {
854 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
855 }
856 }
857 }
858 return NETMANAGER_SUCCESS;
859 }
860
OnGetAllNets(MessageParcel & data,MessageParcel & reply)861 int32_t NetConnServiceStub::OnGetAllNets(MessageParcel &data, MessageParcel &reply)
862 {
863 NETMGR_LOG_D("stub execute GetAllNets");
864 std::list<int32_t> netIdList;
865 int32_t ret = GetAllNets(netIdList);
866 if (!reply.WriteInt32(ret)) {
867 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
868 }
869 if (ret == NETMANAGER_SUCCESS) {
870 uint32_t size = static_cast<uint32_t>(netIdList.size());
871 if (!reply.WriteUint32(size)) {
872 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
873 }
874
875 for (auto p = netIdList.begin(); p != netIdList.end(); ++p) {
876 if (!reply.WriteInt32(*p)) {
877 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
878 }
879 }
880 }
881 return NETMANAGER_SUCCESS;
882 }
883
OnGetSpecificUidNet(MessageParcel & data,MessageParcel & reply)884 int32_t NetConnServiceStub::OnGetSpecificUidNet(MessageParcel &data, MessageParcel &reply)
885 {
886 int32_t uid = 0;
887 if (!data.ReadInt32(uid)) {
888 return NETMANAGER_ERR_READ_DATA_FAIL;
889 }
890 NETMGR_LOG_D("stub execute GetSpecificUidNet");
891
892 int32_t netId = 0;
893 int32_t ret = GetSpecificUidNet(uid, netId);
894 if (!reply.WriteInt32(ret)) {
895 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
896 }
897 if (ret == NETMANAGER_SUCCESS) {
898 if (!reply.WriteInt32(netId)) {
899 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
900 }
901 }
902 return NETMANAGER_SUCCESS;
903 }
904
OnGetConnectionProperties(MessageParcel & data,MessageParcel & reply)905 int32_t NetConnServiceStub::OnGetConnectionProperties(MessageParcel &data, MessageParcel &reply)
906 {
907 int32_t netId = 0;
908 if (!data.ReadInt32(netId)) {
909 return NETMANAGER_ERR_READ_DATA_FAIL;
910 }
911
912 NETMGR_LOG_D("stub execute GetConnectionProperties");
913 NetLinkInfo info;
914 int32_t ret = GetConnectionProperties(netId, info);
915 if (!reply.WriteInt32(ret)) {
916 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
917 }
918 if (ret == NETMANAGER_SUCCESS) {
919 sptr<NetLinkInfo> netLinkInfo_ptr = new (std::nothrow) NetLinkInfo(info);
920 if (!NetLinkInfo::Marshalling(reply, netLinkInfo_ptr)) {
921 NETMGR_LOG_E("proxy Marshalling failed");
922 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
923 }
924 }
925 return NETMANAGER_SUCCESS;
926 }
927
OnGetNetCapabilities(MessageParcel & data,MessageParcel & reply)928 int32_t NetConnServiceStub::OnGetNetCapabilities(MessageParcel &data, MessageParcel &reply)
929 {
930 int32_t netId = 0;
931 if (!data.ReadInt32(netId)) {
932 return NETMANAGER_ERR_READ_DATA_FAIL;
933 }
934
935 NETMGR_LOG_D("stub execute GetNetCapabilities");
936
937 NetAllCapabilities netAllCap;
938 int32_t ret = GetNetCapabilities(netId, netAllCap);
939 if (!reply.WriteInt32(ret)) {
940 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
941 }
942 if (ret == NETMANAGER_SUCCESS) {
943 if (!reply.WriteUint32(netAllCap.linkUpBandwidthKbps_) ||
944 !reply.WriteUint32(netAllCap.linkDownBandwidthKbps_)) {
945 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
946 }
947 uint32_t size = netAllCap.netCaps_.size();
948 size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
949 if (!reply.WriteUint32(size)) {
950 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
951 }
952 uint32_t index = 0;
953 for (auto netCap : netAllCap.netCaps_) {
954 if (++index > MAX_NET_CAP_NUM) {
955 break;
956 }
957 if (!reply.WriteUint32(static_cast<uint32_t>(netCap))) {
958 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
959 }
960 }
961
962 size = netAllCap.bearerTypes_.size();
963 size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
964 if (!reply.WriteUint32(size)) {
965 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
966 }
967 index = 0;
968 for (auto bearerType : netAllCap.bearerTypes_) {
969 if (++index > MAX_NET_CAP_NUM) {
970 break;
971 }
972 if (!reply.WriteUint32(static_cast<uint32_t>(bearerType))) {
973 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
974 }
975 }
976 }
977 return NETMANAGER_SUCCESS;
978 }
979
OnGetAddressesByName(MessageParcel & data,MessageParcel & reply)980 int32_t NetConnServiceStub::OnGetAddressesByName(MessageParcel &data, MessageParcel &reply)
981 {
982 std::string host;
983 if (!data.ReadString(host)) {
984 return NETMANAGER_ERR_READ_DATA_FAIL;
985 }
986 int32_t netId;
987 if (!data.ReadInt32(netId)) {
988 return NETMANAGER_ERR_READ_DATA_FAIL;
989 }
990 NETMGR_LOG_D("stub execute GetAddressesByName");
991 std::vector<INetAddr> addrList;
992 int32_t ret = GetAddressesByName(host, netId, addrList);
993 if (!reply.WriteInt32(ret)) {
994 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
995 }
996 if (ret == NETMANAGER_SUCCESS) {
997 uint32_t size = static_cast<uint32_t>(addrList.size());
998 size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size;
999 if (!reply.WriteUint32(size)) {
1000 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1001 }
1002 uint32_t index = 0;
1003 for (auto p = addrList.begin(); p != addrList.end(); ++p) {
1004 if (++index > MAX_IFACE_NUM) {
1005 break;
1006 }
1007 sptr<INetAddr> netaddr_ptr = (std::make_unique<INetAddr>(*p)).release();
1008 if (!INetAddr::Marshalling(reply, netaddr_ptr)) {
1009 NETMGR_LOG_E("proxy Marshalling failed");
1010 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1011 }
1012 }
1013 }
1014 return NETMANAGER_SUCCESS;
1015 }
1016
OnGetAddressByName(MessageParcel & data,MessageParcel & reply)1017 int32_t NetConnServiceStub::OnGetAddressByName(MessageParcel &data, MessageParcel &reply)
1018 {
1019 std::string host;
1020 if (!data.ReadString(host)) {
1021 return NETMANAGER_ERR_READ_DATA_FAIL;
1022 }
1023 int32_t netId;
1024 if (!data.ReadInt32(netId)) {
1025 return NETMANAGER_ERR_READ_DATA_FAIL;
1026 }
1027 NETMGR_LOG_D("stub execute GetAddressByName");
1028 INetAddr addr;
1029 int32_t ret = GetAddressByName(host, netId, addr);
1030 if (!reply.WriteInt32(ret)) {
1031 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1032 }
1033 if (ret == NETMANAGER_SUCCESS) {
1034 sptr<INetAddr> netaddr_ptr = (std::make_unique<INetAddr>(addr)).release();
1035 if (!INetAddr::Marshalling(reply, netaddr_ptr)) {
1036 NETMGR_LOG_E("proxy Marshalling failed");
1037 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1038 }
1039 }
1040 return NETMANAGER_SUCCESS;
1041 }
1042
OnBindSocket(MessageParcel & data,MessageParcel & reply)1043 int32_t NetConnServiceStub::OnBindSocket(MessageParcel &data, MessageParcel &reply)
1044 {
1045 int32_t socketFd;
1046 if (!data.ReadInt32(socketFd)) {
1047 return NETMANAGER_ERR_READ_DATA_FAIL;
1048 }
1049 int32_t netId;
1050 if (!data.ReadInt32(netId)) {
1051 return NETMANAGER_ERR_READ_DATA_FAIL;
1052 }
1053 NETMGR_LOG_D("stub execute BindSocket");
1054
1055 int32_t ret = BindSocket(socketFd, netId);
1056 if (!reply.WriteInt32(ret)) {
1057 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1058 }
1059 return NETMANAGER_SUCCESS;
1060 }
1061
OnSetAirplaneMode(MessageParcel & data,MessageParcel & reply)1062 int32_t NetConnServiceStub::OnSetAirplaneMode(MessageParcel &data, MessageParcel &reply)
1063 {
1064 bool state = false;
1065 if (!data.ReadBool(state)) {
1066 return NETMANAGER_ERR_READ_DATA_FAIL;
1067 }
1068 int32_t ret = SetAirplaneMode(state);
1069 if (!reply.WriteInt32(ret)) {
1070 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1071 }
1072 return NETMANAGER_SUCCESS;
1073 }
1074
OnIsDefaultNetMetered(MessageParcel & data,MessageParcel & reply)1075 int32_t NetConnServiceStub::OnIsDefaultNetMetered(MessageParcel &data, MessageParcel &reply)
1076 {
1077 NETMGR_LOG_D("stub execute IsDefaultNetMetered");
1078 bool flag = false;
1079 int32_t result = IsDefaultNetMetered(flag);
1080 if (!reply.WriteInt32(result)) {
1081 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1082 }
1083 if (result == NETMANAGER_SUCCESS) {
1084 if (!reply.WriteBool(flag)) {
1085 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1086 }
1087 }
1088 return NETMANAGER_SUCCESS;
1089 }
1090
OnSetGlobalHttpProxy(MessageParcel & data,MessageParcel & reply)1091 int32_t NetConnServiceStub::OnSetGlobalHttpProxy(MessageParcel &data, MessageParcel &reply)
1092 {
1093 NETMGR_LOG_D("stub execute SetGlobalHttpProxy");
1094
1095 HttpProxy httpProxy;
1096 if (!HttpProxy::Unmarshalling(data, httpProxy)) {
1097 return ERR_FLATTEN_OBJECT;
1098 }
1099
1100 int32_t ret = SetGlobalHttpProxy(httpProxy);
1101 if (!reply.WriteInt32(ret)) {
1102 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1103 }
1104 return NETMANAGER_SUCCESS;
1105 }
1106
OnGetGlobalHttpProxy(MessageParcel & data,MessageParcel & reply)1107 int32_t NetConnServiceStub::OnGetGlobalHttpProxy(MessageParcel &data, MessageParcel &reply)
1108 {
1109 HttpProxy httpProxy;
1110 int32_t result = GetGlobalHttpProxy(httpProxy);
1111 if (!reply.WriteInt32(result)) {
1112 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1113 }
1114
1115 if (result != NETMANAGER_SUCCESS) {
1116 return result;
1117 }
1118
1119 if (!httpProxy.Marshalling(reply)) {
1120 return ERR_FLATTEN_OBJECT;
1121 }
1122 return NETMANAGER_SUCCESS;
1123 }
1124
OnGetDefaultHttpProxy(MessageParcel & data,MessageParcel & reply)1125 int32_t NetConnServiceStub::OnGetDefaultHttpProxy(MessageParcel &data, MessageParcel &reply)
1126 {
1127 NETMGR_LOG_D("stub execute OnGetDefaultHttpProxy");
1128 int32_t bindNetId = 0;
1129 if (!data.ReadInt32(bindNetId)) {
1130 return NETMANAGER_ERR_READ_DATA_FAIL;
1131 }
1132 HttpProxy httpProxy;
1133 int32_t result = GetDefaultHttpProxy(bindNetId, httpProxy);
1134 if (!reply.WriteInt32(result)) {
1135 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1136 }
1137
1138 if (result != NETMANAGER_SUCCESS) {
1139 return result;
1140 }
1141
1142 if (!httpProxy.Marshalling(reply)) {
1143 return ERR_FLATTEN_OBJECT;
1144 }
1145 return NETMANAGER_SUCCESS;
1146 }
1147
OnGetNetIdByIdentifier(MessageParcel & data,MessageParcel & reply)1148 int32_t NetConnServiceStub::OnGetNetIdByIdentifier(MessageParcel &data, MessageParcel &reply)
1149 {
1150 NETMGR_LOG_D("stub execute OnGetNetIdByIdentifier");
1151 std::string ident;
1152 if (!data.ReadString(ident)) {
1153 return NETMANAGER_ERR_READ_DATA_FAIL;
1154 }
1155
1156 std::list<int32_t> netIdList;
1157 int32_t ret = GetNetIdByIdentifier(ident, netIdList);
1158 if (!reply.WriteInt32(ret)) {
1159 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1160 }
1161
1162 if (ret == NETMANAGER_SUCCESS) {
1163 uint32_t size = static_cast<uint32_t>(netIdList.size());
1164 if (!reply.WriteUint32(size)) {
1165 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1166 }
1167 for (auto p = netIdList.begin(); p != netIdList.end(); ++p) {
1168 if (!reply.WriteInt32(*p)) {
1169 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1170 }
1171 }
1172 }
1173 return NETMANAGER_SUCCESS;
1174 }
1175
OnSetAppNet(MessageParcel & data,MessageParcel & reply)1176 int32_t NetConnServiceStub::OnSetAppNet(MessageParcel &data, MessageParcel &reply)
1177 {
1178 int32_t netId = 0;
1179 if (!data.ReadInt32(netId)) {
1180 return NETMANAGER_ERR_READ_DATA_FAIL;
1181 }
1182 int ret = SetAppNet(netId);
1183 if (!reply.WriteInt32(ret)) {
1184 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1185 }
1186 return NETMANAGER_SUCCESS;
1187 }
1188
OnRegisterNetInterfaceCallback(MessageParcel & data,MessageParcel & reply)1189 int32_t NetConnServiceStub::OnRegisterNetInterfaceCallback(MessageParcel &data, MessageParcel &reply)
1190 {
1191 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1192 if (remote == nullptr) {
1193 NETMGR_LOG_E("Remote ptr is nullptr.");
1194 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
1195 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1196 }
1197 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1198 }
1199
1200 sptr<INetInterfaceStateCallback> callback = iface_cast<INetInterfaceStateCallback>(remote);
1201 int32_t ret = RegisterNetInterfaceCallback(callback);
1202 if (!reply.WriteInt32(ret)) {
1203 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1204 }
1205 return NETMANAGER_SUCCESS;
1206 }
1207
OnGetNetInterfaceConfiguration(MessageParcel & data,MessageParcel & reply)1208 int32_t NetConnServiceStub::OnGetNetInterfaceConfiguration(MessageParcel &data, MessageParcel &reply)
1209 {
1210 std::string iface;
1211 if (!data.ReadString(iface)) {
1212 return NETMANAGER_ERR_READ_DATA_FAIL;
1213 }
1214
1215 NetInterfaceConfiguration config;
1216 int32_t ret = GetNetInterfaceConfiguration(iface, config);
1217 if (!reply.WriteInt32(ret)) {
1218 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1219 }
1220
1221 if (ret == NETMANAGER_SUCCESS) {
1222 if (!config.Marshalling(reply)) {
1223 return ERR_FLATTEN_OBJECT;
1224 }
1225 }
1226 return NETMANAGER_SUCCESS;
1227 }
1228
OnAddNetworkRoute(MessageParcel & data,MessageParcel & reply)1229 int32_t NetConnServiceStub::OnAddNetworkRoute(MessageParcel &data, MessageParcel &reply)
1230 {
1231 int32_t netId = 0;
1232 if (!data.ReadInt32(netId)) {
1233 return NETMANAGER_ERR_READ_DATA_FAIL;
1234 }
1235
1236 std::string ifName = "";
1237 if (!data.ReadString(ifName)) {
1238 return NETMANAGER_ERR_READ_DATA_FAIL;
1239 }
1240
1241 std::string destination = "";
1242 if (!data.ReadString(destination)) {
1243 return NETMANAGER_ERR_READ_DATA_FAIL;
1244 }
1245
1246 std::string nextHop = "";
1247 if (!data.ReadString(nextHop)) {
1248 return NETMANAGER_ERR_READ_DATA_FAIL;
1249 }
1250
1251 int32_t ret = AddNetworkRoute(netId, ifName, destination, nextHop);
1252 if (!reply.WriteInt32(ret)) {
1253 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1254 }
1255
1256 return NETMANAGER_SUCCESS;
1257 }
1258
1259
OnRemoveNetworkRoute(MessageParcel & data,MessageParcel & reply)1260 int32_t NetConnServiceStub::OnRemoveNetworkRoute(MessageParcel &data, MessageParcel &reply)
1261 {
1262 int32_t netId = 0;
1263 if (!data.ReadInt32(netId)) {
1264 return NETMANAGER_ERR_READ_DATA_FAIL;
1265 }
1266
1267 std::string ifName = "";
1268 if (!data.ReadString(ifName)) {
1269 return NETMANAGER_ERR_READ_DATA_FAIL;
1270 }
1271
1272 std::string destination = "";
1273 if (!data.ReadString(destination)) {
1274 return NETMANAGER_ERR_READ_DATA_FAIL;
1275 }
1276
1277 std::string nextHop = "";
1278 if (!data.ReadString(nextHop)) {
1279 return NETMANAGER_ERR_READ_DATA_FAIL;
1280 }
1281
1282 int32_t ret = RemoveNetworkRoute(netId, ifName, destination, nextHop);
1283 if (!reply.WriteInt32(ret)) {
1284 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1285 }
1286
1287 return NETMANAGER_SUCCESS;
1288 }
1289
OnAddInterfaceAddress(MessageParcel & data,MessageParcel & reply)1290 int32_t NetConnServiceStub::OnAddInterfaceAddress(MessageParcel &data, MessageParcel &reply)
1291 {
1292 std::string ifName = "";
1293 if (!data.ReadString(ifName)) {
1294 return NETMANAGER_ERR_READ_DATA_FAIL;
1295 }
1296
1297 std::string ipAddr = "";
1298 if (!data.ReadString(ipAddr)) {
1299 return NETMANAGER_ERR_READ_DATA_FAIL;
1300 }
1301
1302 int32_t prefixLength = 0;
1303 if (!data.ReadInt32(prefixLength)) {
1304 return NETMANAGER_ERR_READ_DATA_FAIL;
1305 }
1306
1307 int32_t ret = AddInterfaceAddress(ifName, ipAddr, prefixLength);
1308 if (!reply.WriteInt32(ret)) {
1309 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1310 }
1311
1312 return NETMANAGER_SUCCESS;
1313 }
1314
OnDelInterfaceAddress(MessageParcel & data,MessageParcel & reply)1315 int32_t NetConnServiceStub::OnDelInterfaceAddress(MessageParcel &data, MessageParcel &reply)
1316 {
1317 std::string ifName = "";
1318 if (!data.ReadString(ifName)) {
1319 return NETMANAGER_ERR_READ_DATA_FAIL;
1320 }
1321
1322 std::string ipAddr = "";
1323 if (!data.ReadString(ipAddr)) {
1324 return NETMANAGER_ERR_READ_DATA_FAIL;
1325 }
1326
1327 int32_t prefixLength = 0;
1328 if (!data.ReadInt32(prefixLength)) {
1329 return NETMANAGER_ERR_READ_DATA_FAIL;
1330 }
1331
1332 int32_t ret = DelInterfaceAddress(ifName, ipAddr, prefixLength);
1333 if (!reply.WriteInt32(ret)) {
1334 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1335 }
1336
1337 return NETMANAGER_SUCCESS;
1338 }
1339
OnAddStaticArp(MessageParcel & data,MessageParcel & reply)1340 int32_t NetConnServiceStub::OnAddStaticArp(MessageParcel &data, MessageParcel &reply)
1341 {
1342 std::string ipAddr = "";
1343 if (!data.ReadString(ipAddr)) {
1344 return NETMANAGER_ERR_READ_DATA_FAIL;
1345 }
1346
1347 std::string macAddr = "";
1348 if (!data.ReadString(macAddr)) {
1349 return NETMANAGER_ERR_READ_DATA_FAIL;
1350 }
1351
1352 std::string ifName = "";
1353 if (!data.ReadString(ifName)) {
1354 return NETMANAGER_ERR_READ_DATA_FAIL;
1355 }
1356
1357 int32_t ret = AddStaticArp(ipAddr, macAddr, ifName);
1358 if (!reply.WriteInt32(ret)) {
1359 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1360 }
1361
1362 return NETMANAGER_SUCCESS;
1363 }
1364
OnDelStaticArp(MessageParcel & data,MessageParcel & reply)1365 int32_t NetConnServiceStub::OnDelStaticArp(MessageParcel &data, MessageParcel &reply)
1366 {
1367 std::string ipAddr = "";
1368 if (!data.ReadString(ipAddr)) {
1369 return NETMANAGER_ERR_READ_DATA_FAIL;
1370 }
1371
1372 std::string macAddr = "";
1373 if (!data.ReadString(macAddr)) {
1374 return NETMANAGER_ERR_READ_DATA_FAIL;
1375 }
1376
1377 std::string ifName = "";
1378 if (!data.ReadString(ifName)) {
1379 return NETMANAGER_ERR_READ_DATA_FAIL;
1380 }
1381
1382 int32_t ret = DelStaticArp(ipAddr, macAddr, ifName);
1383 if (!reply.WriteInt32(ret)) {
1384 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1385 }
1386
1387 return NETMANAGER_SUCCESS;
1388 }
1389
OnRegisterSlotType(MessageParcel & data,MessageParcel & reply)1390 int32_t NetConnServiceStub::OnRegisterSlotType(MessageParcel &data, MessageParcel &reply)
1391 {
1392 uint32_t supplierId = 0;
1393 if (!data.ReadUint32(supplierId)) {
1394 return NETMANAGER_ERR_READ_DATA_FAIL;
1395 }
1396
1397 int32_t type = 0;
1398 if (!data.ReadInt32(type)) {
1399 return NETMANAGER_ERR_READ_DATA_FAIL;
1400 }
1401
1402 int32_t ret = RegisterSlotType(supplierId, type);
1403 if (!reply.WriteInt32(ret)) {
1404 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1405 }
1406
1407 return NETMANAGER_SUCCESS;
1408 }
1409
OnGetSlotType(MessageParcel & data,MessageParcel & reply)1410 int32_t NetConnServiceStub::OnGetSlotType(MessageParcel &data, MessageParcel &reply)
1411 {
1412 std::string type = "";
1413 int32_t ret = GetSlotType(type);
1414 if (!reply.WriteInt32(ret)) {
1415 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1416 }
1417 if (!reply.WriteString(type)) {
1418 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1419 }
1420 return NETMANAGER_SUCCESS;
1421 }
1422
OnFactoryResetNetwork(MessageParcel & data,MessageParcel & reply)1423 int32_t NetConnServiceStub::OnFactoryResetNetwork(MessageParcel &data, MessageParcel &reply)
1424 {
1425 int32_t ret = FactoryResetNetwork();
1426 if (!reply.WriteInt32(ret)) {
1427 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1428 }
1429
1430 return NETMANAGER_SUCCESS;
1431 }
1432
OnRegisterNetFactoryResetCallback(MessageParcel & data,MessageParcel & reply)1433 int32_t NetConnServiceStub::OnRegisterNetFactoryResetCallback(MessageParcel &data, MessageParcel &reply)
1434 {
1435 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1436 if (remote == nullptr) {
1437 NETMGR_LOG_E("Remote ptr is nullptr.");
1438 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
1439 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1440 }
1441 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1442 }
1443
1444 sptr<INetFactoryResetCallback> callback = iface_cast<INetFactoryResetCallback>(remote);
1445 if (callback == nullptr) {
1446 NETMGR_LOG_E("Callback ptr is nullptr.");
1447 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
1448 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1449 }
1450 return NETMANAGER_ERR_LOCAL_PTR_NULL;
1451 }
1452
1453 int32_t result = RegisterNetFactoryResetCallback(callback);
1454 if (!reply.WriteInt32(result)) {
1455 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1456 }
1457
1458 return NETMANAGER_SUCCESS;
1459 }
1460
OnIsPreferCellularUrl(MessageParcel & data,MessageParcel & reply)1461 int32_t NetConnServiceStub::OnIsPreferCellularUrl(MessageParcel &data, MessageParcel &reply)
1462 {
1463 std::string url;
1464 if (!data.ReadString(url)) {
1465 return NETMANAGER_ERR_READ_DATA_FAIL;
1466 }
1467 bool preferCellular = false;
1468 int32_t ret = IsPreferCellularUrl(url, preferCellular);
1469 if (!reply.WriteInt32(ret)) {
1470 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1471 }
1472 if (!reply.WriteBool(preferCellular)) {
1473 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1474 }
1475
1476 return NETMANAGER_SUCCESS;
1477 }
1478
OnUpdateSupplierScore(MessageParcel & data,MessageParcel & reply)1479 int32_t NetConnServiceStub::OnUpdateSupplierScore(MessageParcel &data, MessageParcel &reply)
1480 {
1481 uint32_t type = 0;
1482 uint32_t detectionStatus = 0;
1483 uint32_t supplierId;
1484 if (!data.ReadUint32(type)) {
1485 return NETMANAGER_ERR_READ_DATA_FAIL;
1486 }
1487 if (!data.ReadUint32(detectionStatus)) {
1488 return NETMANAGER_ERR_READ_DATA_FAIL;
1489 }
1490 if (type > static_cast<uint32_t>(NetBearType::BEARER_DEFAULT)) {
1491 return NETMANAGER_ERR_INTERNAL;
1492 }
1493 if (!data.ReadUint32(supplierId)) {
1494 return NETMANAGER_ERR_READ_DATA_FAIL;
1495 }
1496 NetBearType bearerType = static_cast<NetBearType>(type);
1497 int32_t ret = UpdateSupplierScore(bearerType, detectionStatus, supplierId);
1498 if (!reply.WriteInt32(ret)) {
1499 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1500 }
1501 if (ret == NETMANAGER_SUCCESS) {
1502 NETMGR_LOG_D("supplierId[%{public}d].", supplierId);
1503 if (!reply.WriteUint32(supplierId)) {
1504 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1505 }
1506 }
1507 return NETMANAGER_SUCCESS;
1508 }
1509
OnRegisterPreAirplaneCallback(MessageParcel & data,MessageParcel & reply)1510 int32_t NetConnServiceStub::OnRegisterPreAirplaneCallback(MessageParcel &data, MessageParcel &reply)
1511 {
1512 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1513 if (remote == nullptr) {
1514 NETMGR_LOG_E("Remote ptr is nullptr.");
1515 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
1516 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1517 }
1518 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1519 }
1520
1521 sptr<IPreAirplaneCallback> callback = iface_cast<IPreAirplaneCallback>(remote);
1522 if (callback == nullptr) {
1523 NETMGR_LOG_E("Callback ptr is nullptr.");
1524 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
1525 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1526 }
1527 return NETMANAGER_ERR_LOCAL_PTR_NULL;
1528 }
1529
1530 int32_t result = RegisterPreAirplaneCallback(callback);
1531 if (!reply.WriteInt32(result)) {
1532 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1533 }
1534
1535 return NETMANAGER_SUCCESS;
1536 }
1537
OnUnregisterPreAirplaneCallback(MessageParcel & data,MessageParcel & reply)1538 int32_t NetConnServiceStub::OnUnregisterPreAirplaneCallback(MessageParcel &data, MessageParcel &reply)
1539 {
1540 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1541 if (remote == nullptr) {
1542 NETMGR_LOG_E("Remote ptr is nullptr.");
1543 if (!reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL)) {
1544 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1545 }
1546 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1547 }
1548
1549 sptr<IPreAirplaneCallback> callback = iface_cast<IPreAirplaneCallback>(remote);
1550 if (callback == nullptr) {
1551 NETMGR_LOG_E("Callback ptr is nullptr.");
1552 if (!reply.WriteInt32(NETMANAGER_ERR_LOCAL_PTR_NULL)) {
1553 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1554 }
1555 return NETMANAGER_ERR_LOCAL_PTR_NULL;
1556 }
1557
1558 int32_t result = UnregisterPreAirplaneCallback(callback);
1559 if (!reply.WriteInt32(result)) {
1560 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
1561 }
1562
1563 return NETMANAGER_SUCCESS;
1564 }
1565 } // namespace NetManagerStandard
1566 } // namespace OHOS
1567