• 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 "netmanager_base_permission.h"
17 #include "net_conn_constants.h"
18 #include "net_conn_service_stub.h"
19 #include "net_conn_types.h"
20 #include "net_manager_constants.h"
21 #include "net_mgr_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace NetManagerStandard {
25 static constexpr uint32_t MAX_IFACE_NUM = 16;
26 static constexpr uint32_t MAX_NET_CAP_NUM = 32;
27 
NetConnServiceStub()28 NetConnServiceStub::NetConnServiceStub()
29 {
30     memberFuncMap_[CMD_NM_SYSTEM_READY] = {&NetConnServiceStub::OnSystemReady, {}};
31     memberFuncMap_[CMD_NM_REGISTER_NET_CONN_CALLBACK] = {&NetConnServiceStub::OnRegisterNetConnCallback,
32                                                          {Permission::GET_NETWORK_INFO}};
33     memberFuncMap_[CMD_NM_REGISTER_NET_CONN_CALLBACK_BY_SPECIFIER] = {
34         &NetConnServiceStub::OnRegisterNetConnCallbackBySpecifier, {Permission::GET_NETWORK_INFO}};
35     memberFuncMap_[CMD_NM_UNREGISTER_NET_CONN_CALLBACK] = {&NetConnServiceStub::OnUnregisterNetConnCallback,
36                                                            {Permission::GET_NETWORK_INFO}};
37     memberFuncMap_[CMD_NM_UPDATE_NET_STATE_FOR_TEST] = {&NetConnServiceStub::OnUpdateNetStateForTest, {}};
38     memberFuncMap_[CMD_NM_REG_NET_SUPPLIER] = {&NetConnServiceStub::OnRegisterNetSupplier, {}};
39     memberFuncMap_[CMD_NM_UNREG_NETWORK] = {&NetConnServiceStub::OnUnregisterNetSupplier, {}};
40     memberFuncMap_[CMD_NM_SET_NET_SUPPLIER_INFO] = {&NetConnServiceStub::OnUpdateNetSupplierInfo, {}};
41     memberFuncMap_[CMD_NM_SET_NET_LINK_INFO] = {&NetConnServiceStub::OnUpdateNetLinkInfo, {}};
42     memberFuncMap_[CMD_NM_REGISTER_NET_DETECTION_RET_CALLBACK] = {&NetConnServiceStub::OnRegisterNetDetectionCallback,
43                                                                   {}};
44     memberFuncMap_[CMD_NM_UNREGISTER_NET_DETECTION_RET_CALLBACK] = {
45         &NetConnServiceStub::OnUnRegisterNetDetectionCallback, {}};
46     memberFuncMap_[CMD_NM_NET_DETECTION] = {&NetConnServiceStub::OnNetDetection,
47                                             {Permission::GET_NETWORK_INFO, Permission::INTERNET}};
48     memberFuncMap_[CMD_NM_GET_IFACE_NAMES] = {&NetConnServiceStub::OnGetIfaceNames, {}};
49     memberFuncMap_[CMD_NM_GET_IFACENAME_BY_TYPE] = {&NetConnServiceStub::OnGetIfaceNameByType, {}};
50     memberFuncMap_[CMD_NM_GETDEFAULTNETWORK] = {&NetConnServiceStub::OnGetDefaultNet, {Permission::GET_NETWORK_INFO}};
51     memberFuncMap_[CMD_NM_HASDEFAULTNET] = {&NetConnServiceStub::OnHasDefaultNet, {Permission::GET_NETWORK_INFO}};
52     memberFuncMap_[CMD_NM_GET_SPECIFIC_NET] = {&NetConnServiceStub::OnGetSpecificNet, {}};
53     memberFuncMap_[CMD_NM_GET_ALL_NETS] = {&NetConnServiceStub::OnGetAllNets, {Permission::GET_NETWORK_INFO}};
54     memberFuncMap_[CMD_NM_GET_SPECIFIC_UID_NET] = {&NetConnServiceStub::OnGetSpecificUidNet, {}};
55     memberFuncMap_[CMD_NM_GET_CONNECTION_PROPERTIES] = {&NetConnServiceStub::OnGetConnectionProperties,
56                                                         {Permission::GET_NETWORK_INFO}};
57     memberFuncMap_[CMD_NM_GET_NET_CAPABILITIES] = {&NetConnServiceStub::OnGetNetCapabilities,
58                                                    {Permission::GET_NETWORK_INFO}};
59     memberFuncMap_[CMD_NM_GET_ADDRESSES_BY_NAME] = {&NetConnServiceStub::OnGetAddressesByName,
60                                                     {Permission::INTERNET}};
61     memberFuncMap_[CMD_NM_GET_ADDRESS_BY_NAME] = {&NetConnServiceStub::OnGetAddressByName,
62                                                   {Permission::INTERNET}};
63     memberFuncMap_[CMD_NM_BIND_SOCKET] = {&NetConnServiceStub::OnBindSocket, {}};
64     memberFuncMap_[CMD_NM_REGISTER_NET_SUPPLIER_CALLBACK] = {&NetConnServiceStub::OnRegisterNetSupplierCallback, {}};
65     memberFuncMap_[CMD_NM_SET_AIRPLANE_MODE] = {&NetConnServiceStub::OnSetAirplaneMode,
66                                                 {Permission::CONNECTIVITY_INTERNAL}};
67     memberFuncMap_[CMD_NM_IS_DEFAULT_NET_METERED] = {&NetConnServiceStub::OnIsDefaultNetMetered,
68                                                      {Permission::GET_NETWORK_INFO}};
69     memberFuncMap_[CMD_NM_SET_HTTP_PROXY] = {&NetConnServiceStub::OnSetGlobalHttpProxy,
70                                              {Permission::CONNECTIVITY_INTERNAL}};
71     memberFuncMap_[CMD_NM_GET_HTTP_PROXY] = {&NetConnServiceStub::OnGetGlobalHttpProxy, {}};
72     memberFuncMap_[CMD_NM_GET_NET_ID_BY_IDENTIFIER] = {&NetConnServiceStub::OnGetNetIdByIdentifier, {}};
73     memberFuncMap_[CMD_NM_SET_APP_NET] = {&NetConnServiceStub::OnSetAppNet, {Permission::INTERNET}};
74 }
75 
~NetConnServiceStub()76 NetConnServiceStub::~NetConnServiceStub() {}
77 
ToUtf8(std::u16string str16)78 std::string ToUtf8(std::u16string str16)
79 {
80     return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(str16);
81 }
82 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)83 int32_t NetConnServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
84                                             MessageOption &option)
85 {
86     NETMGR_LOG_D("stub call start, code = [%{public}d]", code);
87 
88     std::u16string myDescripter = NetConnServiceStub::GetDescriptor();
89     std::u16string remoteDescripter = data.ReadInterfaceToken();
90     NETMGR_LOG_D("myDescripter[%{public}s], remoteDescripter[%{public}s]", ToUtf8(myDescripter).c_str(),
91                  ToUtf8(remoteDescripter).c_str());
92     if (myDescripter != remoteDescripter) {
93         NETMGR_LOG_E("descriptor checked fail");
94         if (!reply.WriteInt32(NETMANAGER_ERR_DESCRIPTOR_MISMATCH)) {
95             return IPC_STUB_WRITE_PARCEL_ERR;
96         }
97         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
98     }
99 
100     auto itFunc = memberFuncMap_.find(code);
101     if (itFunc == memberFuncMap_.end()) {
102         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
103     }
104     auto requestFunc = itFunc->second.first;
105     if (requestFunc == nullptr) {
106         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
107     }
108 
109     if (code != CMD_NM_GETDEFAULTNETWORK && CheckPermission(itFunc->second.second)) {
110         return (this->*requestFunc)(data, reply);
111     }
112     if (code == CMD_NM_GETDEFAULTNETWORK && CheckPermissionWithCache(itFunc->second.second)) {
113         return (this->*requestFunc)(data, reply);
114     }
115 
116     if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
117         return IPC_STUB_WRITE_PARCEL_ERR;
118     }
119     NETMGR_LOG_D("stub default case, need check");
120     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
121 }
122 
CheckPermission(const std::set<std::string> & permissions)123 bool NetConnServiceStub::CheckPermission(const std::set<std::string> &permissions)
124 {
125     for (const auto &permission : permissions) {
126         NETMGR_LOG_I("liyufan stub default permission, need check %{public}s",permission.c_str());
127         if (!NetManagerPermission::CheckPermission(permission)) {
128             return false;
129         }
130     }
131     return true;
132 }
133 
CheckPermissionWithCache(const std::set<std::string> & permissions)134 bool NetConnServiceStub::CheckPermissionWithCache(const std::set<std::string> &permissions)
135 {
136     for (const auto &permission : permissions) {
137         if (!NetManagerPermission::CheckPermissionWithCache(permission)) {
138             return false;
139         }
140     }
141     return true;
142 }
143 
OnSystemReady(MessageParcel & data,MessageParcel & reply)144 int32_t NetConnServiceStub::OnSystemReady(MessageParcel &data, MessageParcel &reply)
145 {
146     SystemReady();
147     return 0;
148 }
149 
OnRegisterNetSupplier(MessageParcel & data,MessageParcel & reply)150 int32_t NetConnServiceStub::OnRegisterNetSupplier(MessageParcel &data, MessageParcel &reply)
151 {
152     NETMGR_LOG_D("stub processing");
153     NetBearType bearerType;
154     std::string ident;
155     std::set<NetCap> netCaps;
156 
157     uint32_t type = 0;
158     if (!data.ReadUint32(type)) {
159         return NETMANAGER_ERR_READ_DATA_FAIL;
160     }
161     bearerType = static_cast<NetBearType>(type);
162 
163     if (!data.ReadString(ident)) {
164         return NETMANAGER_ERR_READ_DATA_FAIL;
165     }
166     uint32_t size = 0;
167     uint32_t value = 0;
168     if (!data.ReadUint32(size)) {
169         return NETMANAGER_ERR_READ_DATA_FAIL;
170     }
171     size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
172     for (uint32_t i = 0; i < size; ++i) {
173         if (!data.ReadUint32(value)) {
174             return NETMANAGER_ERR_READ_DATA_FAIL;
175         }
176         netCaps.insert(static_cast<NetCap>(value));
177     }
178 
179     uint32_t supplierId = 0;
180     int32_t ret = RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
181     if (!reply.WriteInt32(ret)) {
182         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
183     }
184     if (ret == NETMANAGER_SUCCESS) {
185         NETMGR_LOG_E("supplierId[%{public}d].", supplierId);
186         if (!reply.WriteUint32(supplierId)) {
187             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
188         }
189     }
190     return NETMANAGER_SUCCESS;
191 }
192 
OnUnregisterNetSupplier(MessageParcel & data,MessageParcel & reply)193 int32_t NetConnServiceStub::OnUnregisterNetSupplier(MessageParcel &data, MessageParcel &reply)
194 {
195     uint32_t supplierId;
196     if (!data.ReadUint32(supplierId)) {
197         return NETMANAGER_ERR_READ_DATA_FAIL;
198     }
199 
200     int32_t ret = UnregisterNetSupplier(supplierId);
201     if (!reply.WriteInt32(ret)) {
202         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
203     }
204 
205     return NETMANAGER_SUCCESS;
206 }
207 
OnRegisterNetSupplierCallback(MessageParcel & data,MessageParcel & reply)208 int32_t NetConnServiceStub::OnRegisterNetSupplierCallback(MessageParcel &data, MessageParcel &reply)
209 {
210     int32_t result = NETMANAGER_SUCCESS;
211     uint32_t supplierId;
212     data.ReadUint32(supplierId);
213     sptr<IRemoteObject> remote = data.ReadRemoteObject();
214     if (remote == nullptr) {
215         NETMGR_LOG_E("Callback ptr is nullptr.");
216         result = NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
217         reply.WriteInt32(result);
218         return result;
219     }
220 
221     sptr<INetSupplierCallback> callback = iface_cast<INetSupplierCallback>(remote);
222     if (callback == nullptr) {
223         result = NETMANAGER_ERR_LOCAL_PTR_NULL;
224         reply.WriteInt32(result);
225         return result;
226     }
227 
228     result = RegisterNetSupplierCallback(supplierId, callback);
229     reply.WriteInt32(result);
230     return result;
231 }
232 
OnRegisterNetConnCallback(MessageParcel & data,MessageParcel & reply)233 int32_t NetConnServiceStub::OnRegisterNetConnCallback(MessageParcel &data, MessageParcel &reply)
234 {
235     int32_t result = NETMANAGER_SUCCESS;
236     sptr<IRemoteObject> remote = data.ReadRemoteObject();
237     if (remote == nullptr) {
238         NETMGR_LOG_E("Callback ptr is nullptr.");
239         result = NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
240         reply.WriteInt32(result);
241         return result;
242     }
243 
244     sptr<INetConnCallback> callback = iface_cast<INetConnCallback>(remote);
245     if (callback == nullptr) {
246         result = NETMANAGER_ERR_LOCAL_PTR_NULL;
247         reply.WriteInt32(result);
248         return result;
249     }
250 
251     result = RegisterNetConnCallback(callback);
252     reply.WriteInt32(result);
253     return result;
254 }
255 
OnRegisterNetConnCallbackBySpecifier(MessageParcel & data,MessageParcel & reply)256 int32_t NetConnServiceStub::OnRegisterNetConnCallbackBySpecifier(MessageParcel &data, MessageParcel &reply)
257 {
258     sptr<NetSpecifier> netSpecifier = NetSpecifier::Unmarshalling(data);
259     uint32_t timeoutMS = data.ReadUint32();
260     int32_t result = NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
261     sptr<IRemoteObject> remote = data.ReadRemoteObject();
262     if (remote == nullptr) {
263         NETMGR_LOG_E("callback ptr is nullptr.");
264         reply.WriteInt32(result);
265         return result;
266     }
267 
268     sptr<INetConnCallback> callback = iface_cast<INetConnCallback>(remote);
269     if (callback == nullptr) {
270         result = NETMANAGER_ERR_LOCAL_PTR_NULL;
271         reply.WriteInt32(result);
272         return result;
273     }
274 
275     result = RegisterNetConnCallback(netSpecifier, callback, timeoutMS);
276     reply.WriteInt32(result);
277     return result;
278 }
279 
OnUnregisterNetConnCallback(MessageParcel & data,MessageParcel & reply)280 int32_t NetConnServiceStub::OnUnregisterNetConnCallback(MessageParcel &data, MessageParcel &reply)
281 {
282     int32_t result = NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
283     sptr<IRemoteObject> remote = data.ReadRemoteObject();
284     if (remote == nullptr) {
285         NETMGR_LOG_E("callback ptr is nullptr.");
286         reply.WriteInt32(result);
287         return result;
288     }
289 
290     sptr<INetConnCallback> callback = iface_cast<INetConnCallback>(remote);
291     if (callback == nullptr) {
292         result = NETMANAGER_ERR_LOCAL_PTR_NULL;
293         reply.WriteInt32(result);
294         return result;
295     }
296 
297     result = UnregisterNetConnCallback(callback);
298     reply.WriteInt32(result);
299     return result;
300 }
301 
OnUpdateNetStateForTest(MessageParcel & data,MessageParcel & reply)302 int32_t NetConnServiceStub::OnUpdateNetStateForTest(MessageParcel &data, MessageParcel &reply)
303 {
304     NETMGR_LOG_I("Test NetConnServiceStub::OnUpdateNetStateForTest(), begin");
305     sptr<NetSpecifier> netSpecifier = NetSpecifier::Unmarshalling(data);
306 
307     int32_t netState;
308     if (!data.ReadInt32(netState)) {
309         return NETMANAGER_ERR_READ_DATA_FAIL;
310     }
311 
312     NETMGR_LOG_I("Test NetConnServiceStub::OnUpdateNetStateForTest(), netState[%{public}d]", netState);
313     int32_t result = UpdateNetStateForTest(netSpecifier, netState);
314     NETMGR_LOG_I("Test NetConnServiceStub::OnUpdateNetStateForTest(), result[%{public}d]", result);
315     reply.WriteInt32(result);
316     return result;
317 }
318 
OnUpdateNetSupplierInfo(MessageParcel & data,MessageParcel & reply)319 int32_t NetConnServiceStub::OnUpdateNetSupplierInfo(MessageParcel &data, MessageParcel &reply)
320 {
321     NETMGR_LOG_D("OnUpdateNetSupplierInfo in.");
322     uint32_t supplierId;
323     if (!data.ReadUint32(supplierId)) {
324         NETMGR_LOG_D("fail to get supplier id.");
325         return NETMANAGER_ERR_READ_DATA_FAIL;
326     }
327 
328     NETMGR_LOG_D("OnUpdateNetSupplierInfo supplierId=[%{public}d].", supplierId);
329     sptr<NetSupplierInfo> netSupplierInfo = NetSupplierInfo::Unmarshalling(data);
330     int32_t ret = UpdateNetSupplierInfo(supplierId, netSupplierInfo);
331     if (!reply.WriteInt32(ret)) {
332         NETMGR_LOG_D("fail to update net supplier info.");
333         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
334     }
335     NETMGR_LOG_D("OnUpdateNetSupplierInfo out.");
336 
337     return NETMANAGER_SUCCESS;
338 }
339 
OnUpdateNetLinkInfo(MessageParcel & data,MessageParcel & reply)340 int32_t NetConnServiceStub::OnUpdateNetLinkInfo(MessageParcel &data, MessageParcel &reply)
341 {
342     uint32_t supplierId;
343 
344     if (!data.ReadUint32(supplierId)) {
345         return NETMANAGER_ERR_READ_DATA_FAIL;
346     }
347 
348     sptr<NetLinkInfo> netLinkInfo = NetLinkInfo::Unmarshalling(data);
349 
350     int32_t ret = UpdateNetLinkInfo(supplierId, netLinkInfo);
351     if (!reply.WriteInt32(ret)) {
352         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
353     }
354 
355     return NETMANAGER_SUCCESS;
356 }
357 
OnRegisterNetDetectionCallback(MessageParcel & data,MessageParcel & reply)358 int32_t NetConnServiceStub::OnRegisterNetDetectionCallback(MessageParcel &data, MessageParcel &reply)
359 {
360     if (!data.ContainFileDescriptors()) {
361         NETMGR_LOG_E("Execute ContainFileDescriptors failed");
362     }
363     int32_t netId = 0;
364     if (!data.ReadInt32(netId)) {
365         return NETMANAGER_ERR_READ_DATA_FAIL;
366     }
367 
368     int32_t result = NETMANAGER_SUCCESS;
369     sptr<IRemoteObject> remote = data.ReadRemoteObject();
370     if (remote == nullptr) {
371         NETMGR_LOG_E("Callback ptr is nullptr.");
372         result = NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
373         reply.WriteInt32(result);
374         return result;
375     }
376 
377     sptr<INetDetectionCallback> callback = iface_cast<INetDetectionCallback>(remote);
378     if (callback == nullptr) {
379         result = NETMANAGER_ERR_LOCAL_PTR_NULL;
380         reply.WriteInt32(result);
381         return result;
382     }
383 
384     result = RegisterNetDetectionCallback(netId, callback);
385     reply.WriteInt32(result);
386     return result;
387 }
388 
OnUnRegisterNetDetectionCallback(MessageParcel & data,MessageParcel & reply)389 int32_t NetConnServiceStub::OnUnRegisterNetDetectionCallback(MessageParcel &data, MessageParcel &reply)
390 {
391     if (!data.ContainFileDescriptors()) {
392         NETMGR_LOG_E("Execute ContainFileDescriptors failed");
393     }
394     int32_t netId = 0;
395     if (!data.ReadInt32(netId)) {
396         return NETMANAGER_ERR_READ_DATA_FAIL;
397     }
398 
399     int32_t result = NETMANAGER_SUCCESS;
400     sptr<IRemoteObject> remote = data.ReadRemoteObject();
401     if (remote == nullptr) {
402         NETMGR_LOG_E("Callback ptr is nullptr.");
403         result = NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
404         reply.WriteInt32(result);
405         return result;
406     }
407 
408     sptr<INetDetectionCallback> callback = iface_cast<INetDetectionCallback>(remote);
409     if (callback == nullptr) {
410         result = NETMANAGER_ERR_LOCAL_PTR_NULL;
411         reply.WriteInt32(result);
412         return result;
413     }
414 
415     result = UnRegisterNetDetectionCallback(netId, callback);
416     reply.WriteInt32(result);
417     return result;
418 }
419 
OnNetDetection(MessageParcel & data,MessageParcel & reply)420 int32_t NetConnServiceStub::OnNetDetection(MessageParcel &data, MessageParcel &reply)
421 {
422     int32_t netId = 0;
423     if (!data.ReadInt32(netId)) {
424         return NETMANAGER_ERR_READ_DATA_FAIL;
425     }
426     int32_t ret = NetDetection(netId);
427     if (!reply.WriteInt32(ret)) {
428         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
429     }
430     return NETMANAGER_SUCCESS;
431 }
432 
OnGetIfaceNames(MessageParcel & data,MessageParcel & reply)433 int32_t NetConnServiceStub::OnGetIfaceNames(MessageParcel &data, MessageParcel &reply)
434 {
435     uint32_t netType = 0;
436     if (!data.ReadUint32(netType)) {
437         return NETMANAGER_ERR_READ_DATA_FAIL;
438     }
439     NetBearType bearerType = static_cast<NetBearType>(netType);
440     std::list<std::string> ifaceNames;
441     int32_t ret = GetIfaceNames(bearerType, ifaceNames);
442     if (!reply.WriteInt32(ret)) {
443         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
444     }
445     if (ret == NETMANAGER_SUCCESS) {
446         if (!reply.WriteUint32(ifaceNames.size())) {
447             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
448         }
449 
450         for (const auto &ifaceName : ifaceNames) {
451             if (!reply.WriteString(ifaceName)) {
452                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
453             }
454         }
455     }
456     return ret;
457 }
458 
OnGetIfaceNameByType(MessageParcel & data,MessageParcel & reply)459 int32_t NetConnServiceStub::OnGetIfaceNameByType(MessageParcel &data, MessageParcel &reply)
460 {
461     uint32_t netType = 0;
462     if (!data.ReadUint32(netType)) {
463         return NETMANAGER_ERR_READ_DATA_FAIL;
464     }
465     NetBearType bearerType = static_cast<NetBearType>(netType);
466 
467     std::string ident;
468     if (!data.ReadString(ident)) {
469         return NETMANAGER_ERR_READ_DATA_FAIL;
470     }
471 
472     std::string ifaceName;
473     int32_t ret = GetIfaceNameByType(bearerType, ident, ifaceName);
474     if (!reply.WriteInt32(ret)) {
475         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
476     }
477     if (ret == NETMANAGER_SUCCESS) {
478         if (!reply.WriteString(ifaceName)) {
479             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
480         }
481     }
482     return ret;
483 }
484 
OnGetDefaultNet(MessageParcel & data,MessageParcel & reply)485 int32_t NetConnServiceStub::OnGetDefaultNet(MessageParcel &data, MessageParcel &reply)
486 {
487     NETMGR_LOG_D("OnGetDefaultNet Begin...");
488     int32_t netId;
489     int32_t result = GetDefaultNet(netId);
490     NETMGR_LOG_D("GetDefaultNet result is: [%{public}d]", result);
491     if (!reply.WriteInt32(result)) {
492         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
493     }
494     if (result == NETMANAGER_SUCCESS) {
495         if (!reply.WriteUint32(netId)) {
496             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
497         }
498     }
499     return NETMANAGER_SUCCESS;
500 }
501 
OnHasDefaultNet(MessageParcel & data,MessageParcel & reply)502 int32_t NetConnServiceStub::OnHasDefaultNet(MessageParcel &data, MessageParcel &reply)
503 {
504     NETMGR_LOG_D("OnHasDefaultNet Begin...");
505     bool flag = false;
506     int32_t result = HasDefaultNet(flag);
507     NETMGR_LOG_D("HasDefaultNet result is: [%{public}d]", result);
508     if (!reply.WriteInt32(result)) {
509         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
510     }
511     if (result == NETMANAGER_SUCCESS) {
512         if (!reply.WriteBool(flag)) {
513             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
514         }
515     }
516     return NETMANAGER_SUCCESS;
517 }
518 
OnGetSpecificNet(MessageParcel & data,MessageParcel & reply)519 int32_t NetConnServiceStub::OnGetSpecificNet(MessageParcel &data, MessageParcel &reply)
520 {
521     uint32_t type;
522     if (!data.ReadUint32(type)) {
523         return NETMANAGER_ERR_READ_DATA_FAIL;
524     }
525     NetBearType bearerType = static_cast<NetBearType>(type);
526 
527     NETMGR_LOG_D("stub execute GetSpecificNet");
528     std::list<int32_t> netIdList;
529     int32_t ret = GetSpecificNet(bearerType, netIdList);
530     if (!reply.WriteInt32(ret)) {
531         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
532     }
533     if (ret == NETMANAGER_SUCCESS) {
534         int32_t size = static_cast<int32_t>(netIdList.size());
535         size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size;
536         if (!reply.WriteInt32(size)) {
537             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
538         }
539 
540         int32_t index = 0;
541         for (auto p = netIdList.begin(); p != netIdList.end(); ++p) {
542             if (++index > MAX_IFACE_NUM) {
543                 break;
544             }
545             if (!reply.WriteInt32(*p)) {
546                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
547             }
548         }
549     }
550     return ret;
551 }
552 
OnGetAllNets(MessageParcel & data,MessageParcel & reply)553 int32_t NetConnServiceStub::OnGetAllNets(MessageParcel &data, MessageParcel &reply)
554 {
555     NETMGR_LOG_D("stub execute GetAllNets");
556     std::list<int32_t> netIdList;
557     int32_t ret = GetAllNets(netIdList);
558     if (!reply.WriteInt32(ret)) {
559         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
560     }
561     if (ret == NETMANAGER_SUCCESS) {
562         int32_t size = static_cast<int32_t>(netIdList.size());
563         if (!reply.WriteInt32(size)) {
564             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
565         }
566 
567         for (auto p = netIdList.begin(); p != netIdList.end(); ++p) {
568             if (!reply.WriteInt32(*p)) {
569                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
570             }
571         }
572     }
573     return ret;
574 }
575 
OnGetSpecificUidNet(MessageParcel & data,MessageParcel & reply)576 int32_t NetConnServiceStub::OnGetSpecificUidNet(MessageParcel &data, MessageParcel &reply)
577 {
578     int32_t uid = 0;
579     if (!data.ReadInt32(uid)) {
580         return NETMANAGER_ERR_READ_DATA_FAIL;
581     }
582     NETMGR_LOG_D("stub execute GetSpecificUidNet");
583 
584     int32_t netId = 0;
585     int32_t ret = GetSpecificUidNet(uid, netId);
586     if (!reply.WriteInt32(ret)) {
587         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
588     }
589     if (ret == NETMANAGER_SUCCESS) {
590         if (!reply.WriteInt32(netId)) {
591             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
592         }
593     }
594     return ret;
595 }
596 
OnGetConnectionProperties(MessageParcel & data,MessageParcel & reply)597 int32_t NetConnServiceStub::OnGetConnectionProperties(MessageParcel &data, MessageParcel &reply)
598 {
599     int32_t netId = 0;
600     if (!data.ReadInt32(netId)) {
601         return NETMANAGER_ERR_READ_DATA_FAIL;
602     }
603 
604     NETMGR_LOG_D("stub execute GetConnectionProperties");
605     NetLinkInfo info;
606     int32_t ret = GetConnectionProperties(netId, info);
607     if (!reply.WriteInt32(ret)) {
608         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
609     }
610     if (ret == NETMANAGER_SUCCESS) {
611         sptr<NetLinkInfo> netLinkInfo_ptr = new (std::nothrow) NetLinkInfo(info);
612         if (!NetLinkInfo::Marshalling(reply, netLinkInfo_ptr)) {
613             NETMGR_LOG_E("proxy Marshalling failed");
614             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
615         }
616     }
617     return ret;
618 }
619 
OnGetNetCapabilities(MessageParcel & data,MessageParcel & reply)620 int32_t NetConnServiceStub::OnGetNetCapabilities(MessageParcel &data, MessageParcel &reply)
621 {
622     int32_t netId = 0;
623     if (!data.ReadInt32(netId)) {
624         return NETMANAGER_ERR_READ_DATA_FAIL;
625     }
626 
627     NETMGR_LOG_D("stub execute GetNetCapabilities");
628 
629     NetAllCapabilities netAllCap;
630     int32_t ret = GetNetCapabilities(netId, netAllCap);
631     if (!reply.WriteInt32(ret)) {
632         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
633     }
634     if (ret == NETMANAGER_SUCCESS) {
635         if (!reply.WriteUint32(netAllCap.linkUpBandwidthKbps_) ||
636             !reply.WriteUint32(netAllCap.linkDownBandwidthKbps_)) {
637             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
638         }
639         uint32_t size = netAllCap.netCaps_.size();
640         size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
641         if (!reply.WriteUint32(size)) {
642             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
643         }
644         int32_t index = 0;
645         for (auto netCap : netAllCap.netCaps_) {
646             if (++index > MAX_NET_CAP_NUM) {
647                 break;
648             }
649             if (!reply.WriteUint32(static_cast<uint32_t>(netCap))) {
650                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
651             }
652         }
653 
654         size = netAllCap.bearerTypes_.size();
655         size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
656         if (!reply.WriteUint32(size)) {
657             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
658         }
659         index = 0;
660         for (auto bearerType : netAllCap.bearerTypes_) {
661             if (++index > MAX_NET_CAP_NUM) {
662                 break;
663             }
664             if (!reply.WriteUint32(static_cast<uint32_t>(bearerType))) {
665                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
666             }
667         }
668     }
669     return ret;
670 }
671 
OnGetAddressesByName(MessageParcel & data,MessageParcel & reply)672 int32_t NetConnServiceStub::OnGetAddressesByName(MessageParcel &data, MessageParcel &reply)
673 {
674     std::string host;
675     if (!data.ReadString(host)) {
676         return NETMANAGER_ERR_READ_DATA_FAIL;
677     }
678     int32_t netId;
679     if (!data.ReadInt32(netId)) {
680         return NETMANAGER_ERR_READ_DATA_FAIL;
681     }
682     NETMGR_LOG_D("stub execute GetAddressesByName");
683     std::vector<INetAddr> addrList;
684     int32_t ret = GetAddressesByName(host, netId, addrList);
685     if (!reply.WriteInt32(ret)) {
686         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
687     }
688     if (ret == NETMANAGER_SUCCESS) {
689         int32_t size = static_cast<int32_t>(addrList.size());
690         size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size;
691         if (!reply.WriteInt32(size)) {
692             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
693         }
694         int32_t index = 0;
695         for (auto p = addrList.begin(); p != addrList.end(); ++p) {
696             if (++index > MAX_IFACE_NUM) {
697                 break;
698             }
699             sptr<INetAddr> netaddr_ptr = (std::make_unique<INetAddr>(*p)).release();
700             if (!INetAddr::Marshalling(reply, netaddr_ptr)) {
701                 NETMGR_LOG_E("proxy Marshalling failed");
702                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
703             }
704         }
705     }
706     return ret;
707 }
708 
OnGetAddressByName(MessageParcel & data,MessageParcel & reply)709 int32_t NetConnServiceStub::OnGetAddressByName(MessageParcel &data, MessageParcel &reply)
710 {
711     std::string host;
712     if (!data.ReadString(host)) {
713         return NETMANAGER_ERR_READ_DATA_FAIL;
714     }
715     int32_t netId;
716     if (!data.ReadInt32(netId)) {
717         return NETMANAGER_ERR_READ_DATA_FAIL;
718     }
719     NETMGR_LOG_D("stub execute GetAddressByName");
720     INetAddr addr;
721     int32_t ret = GetAddressByName(host, netId, addr);
722     if (!reply.WriteInt32(ret)) {
723         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
724     }
725     if (ret == NETMANAGER_SUCCESS) {
726         sptr<INetAddr> netaddr_ptr = (std::make_unique<INetAddr>(addr)).release();
727         if (!INetAddr::Marshalling(reply, netaddr_ptr)) {
728             NETMGR_LOG_E("proxy Marshalling failed");
729             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
730         }
731     }
732     return ret;
733 }
734 
OnBindSocket(MessageParcel & data,MessageParcel & reply)735 int32_t NetConnServiceStub::OnBindSocket(MessageParcel &data, MessageParcel &reply)
736 {
737     int32_t socket_fd;
738     if (!data.ReadInt32(socket_fd)) {
739         return NETMANAGER_ERR_READ_DATA_FAIL;
740     }
741     int32_t netId;
742     if (!data.ReadInt32(netId)) {
743         return NETMANAGER_ERR_READ_DATA_FAIL;
744     }
745     NETMGR_LOG_D("stub execute BindSocket");
746 
747     int32_t ret = BindSocket(socket_fd, netId);
748     if (!reply.WriteInt32(ret)) {
749         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
750     }
751     return ret;
752 }
753 
OnSetAirplaneMode(MessageParcel & data,MessageParcel & reply)754 int32_t NetConnServiceStub::OnSetAirplaneMode(MessageParcel &data, MessageParcel &reply)
755 {
756     bool state = false;
757     if (!data.ReadBool(state)) {
758         return NETMANAGER_ERR_READ_DATA_FAIL;
759     }
760     int32_t ret = SetAirplaneMode(state);
761     if (!reply.WriteInt32(ret)) {
762         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
763     }
764     return ret;
765 }
766 
OnIsDefaultNetMetered(MessageParcel & data,MessageParcel & reply)767 int32_t NetConnServiceStub::OnIsDefaultNetMetered(MessageParcel &data, MessageParcel &reply)
768 {
769     NETMGR_LOG_D("stub execute IsDefaultNetMetered");
770     bool flag = false;
771     int32_t result = IsDefaultNetMetered(flag);
772     if (!reply.WriteInt32(result)) {
773         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
774     }
775     if (result == NETMANAGER_SUCCESS) {
776         if (!reply.WriteBool(flag)) {
777             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
778         }
779     }
780     return NETMANAGER_SUCCESS;
781 }
782 
OnSetGlobalHttpProxy(MessageParcel & data,MessageParcel & reply)783 int32_t NetConnServiceStub::OnSetGlobalHttpProxy(MessageParcel &data, MessageParcel &reply)
784 {
785     NETMGR_LOG_D("stub execute SetGlobalHttpProxy");
786 
787     HttpProxy httpProxy;
788     if (!HttpProxy::Unmarshalling(data, httpProxy)) {
789         return NETMANAGER_ERR_READ_DATA_FAIL;
790     }
791     int32_t ret = SetGlobalHttpProxy(httpProxy);
792     if (!reply.WriteInt32(ret)) {
793         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
794     }
795     return ret;
796 }
797 
OnGetGlobalHttpProxy(MessageParcel & data,MessageParcel & reply)798 int32_t NetConnServiceStub::OnGetGlobalHttpProxy(MessageParcel &data, MessageParcel &reply)
799 {
800     HttpProxy httpProxy;
801     int32_t result = GetGlobalHttpProxy(httpProxy);
802     if (!reply.WriteInt32(result)) {
803         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
804     }
805 
806     if (result != NETMANAGER_SUCCESS) {
807         return result;
808     }
809 
810     if (!httpProxy.Marshalling(reply)) {
811         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
812     }
813     return NETMANAGER_SUCCESS;
814 }
815 
OnGetNetIdByIdentifier(MessageParcel & data,MessageParcel & reply)816 int32_t NetConnServiceStub::OnGetNetIdByIdentifier(MessageParcel &data, MessageParcel &reply)
817 {
818     NETMGR_LOG_D("stub execute OnGetNetIdByIdentifier");
819     std::string ident;
820     if (!data.ReadString(ident)) {
821         return NETMANAGER_ERR_READ_DATA_FAIL;
822     }
823 
824     int32_t netId = 0;
825     int32_t ret = GetNetIdByIdentifier(ident, netId);
826     if (!reply.WriteInt32(ret)) {
827         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
828     }
829 
830     if (ret == NETMANAGER_SUCCESS) {
831         if (!reply.WriteInt32(netId)) {
832             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
833         }
834     }
835     return ret;
836 }
837 
OnSetAppNet(MessageParcel & data,MessageParcel & reply)838 int32_t NetConnServiceStub::OnSetAppNet(MessageParcel &data, MessageParcel &reply)
839 {
840     int32_t netId = 0;
841     if (!data.ReadInt32(netId)) {
842         return NETMANAGER_ERR_READ_DATA_FAIL;
843     }
844     int ret = SetAppNet(netId);
845     if (!reply.WriteInt32(ret)) {
846         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
847     }
848     return ret;
849 }
850 } // namespace NetManagerStandard
851 } // namespace OHOS
852