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