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