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