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_policy_callback_stub.h"
17 
18 #include "net_mgr_log_wrapper.h"
19 #include "net_policy_constants.h"
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
23 static constexpr uint32_t MAX_IFACE_SIZE = 100;
24 
NetPolicyCallbackStub()25 NetPolicyCallbackStub::NetPolicyCallbackStub()
26 {
27     memberFuncMap_[static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_UID_POLICY_CHANGE)] =
28         &NetPolicyCallbackStub::OnNetUidPolicyChange;
29     memberFuncMap_[static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_UID_RULE_CHANGE)] =
30         &NetPolicyCallbackStub::OnNetUidRuleChange;
31     memberFuncMap_[static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_QUOTA_POLICY_CHANGE)] =
32         &NetPolicyCallbackStub::OnNetQuotaPolicyChange;
33     memberFuncMap_[static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_METERED_IFACES_CHANGE)] =
34         &NetPolicyCallbackStub::OnNetMeteredIfacesChange;
35     memberFuncMap_[static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_BACKGROUND_POLICY_CHANGE)] =
36         &NetPolicyCallbackStub::OnNetBackgroundPolicyChange;
37 }
38 
~NetPolicyCallbackStub()39 NetPolicyCallbackStub::~NetPolicyCallbackStub() {}
40 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int32_t NetPolicyCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
42                                                MessageOption &option)
43 {
44     std::u16string myDescriptor = NetPolicyCallbackStub::GetDescriptor();
45     std::u16string remoteDescriptor = data.ReadInterfaceToken();
46     if (myDescriptor != remoteDescriptor) {
47         NETMGR_LOG_E("Descriptor checked failed");
48         return ERR_FLATTEN_OBJECT;
49     }
50 
51     auto itFunc = memberFuncMap_.find(code);
52     if (itFunc != memberFuncMap_.end()) {
53         auto requestFunc = itFunc->second;
54         if (requestFunc != nullptr) {
55             return (this->*requestFunc)(data, reply);
56         }
57     }
58 
59     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61 
OnNetUidPolicyChange(MessageParcel & data,MessageParcel & reply)62 int32_t NetPolicyCallbackStub::OnNetUidPolicyChange(MessageParcel &data, MessageParcel &reply)
63 {
64     uint32_t uid = 0;
65     if (!data.ReadUint32(uid)) {
66         NETMGR_LOG_E("Read Uint32 data failed");
67         return NETMANAGER_ERR_READ_DATA_FAIL;
68     }
69 
70     uint32_t policy = 0;
71     if (!data.ReadUint32(policy)) {
72         NETMGR_LOG_E("Read Uint32 data failed");
73         return NETMANAGER_ERR_READ_DATA_FAIL;
74     }
75 
76     int32_t result = NetUidPolicyChange(uid, policy);
77     if (!reply.WriteInt32(result)) {
78         NETMGR_LOG_E("Write Int32 reply failed");
79         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
80     }
81 
82     return NETMANAGER_SUCCESS;
83 }
84 
NetUidRuleChange(uint32_t uid,uint32_t rule)85 int32_t NetPolicyCallbackStub::NetUidRuleChange(uint32_t uid, uint32_t rule)
86 {
87     return NETMANAGER_SUCCESS;
88 }
89 
OnNetUidRuleChange(MessageParcel & data,MessageParcel & reply)90 int32_t NetPolicyCallbackStub::OnNetUidRuleChange(MessageParcel &data, MessageParcel &reply)
91 {
92     uint32_t uid = 0;
93     if (!data.ReadUint32(uid)) {
94         NETMGR_LOG_E("Read Uint32 data failed");
95         return NETMANAGER_ERR_READ_DATA_FAIL;
96     }
97 
98     uint32_t rule = 0;
99     if (!data.ReadUint32(rule)) {
100         NETMGR_LOG_E("Read Uint32 data failed");
101         return NETMANAGER_ERR_READ_DATA_FAIL;
102     }
103 
104     int32_t result = NetUidRuleChange(uid, rule);
105     if (!reply.WriteInt32(result)) {
106         NETMGR_LOG_E("Write Int32 reply failed");
107         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
108     }
109 
110     return NETMANAGER_SUCCESS;
111 }
112 
NetUidPolicyChange(uint32_t uid,uint32_t policy)113 int32_t NetPolicyCallbackStub::NetUidPolicyChange(uint32_t uid, uint32_t policy)
114 {
115     return NETMANAGER_SUCCESS;
116 }
117 
OnNetBackgroundPolicyChange(MessageParcel & data,MessageParcel & reply)118 int32_t NetPolicyCallbackStub::OnNetBackgroundPolicyChange(MessageParcel &data, MessageParcel &reply)
119 {
120     bool isBackgroundPolicyAllow = false;
121     if (!data.ReadBool(isBackgroundPolicyAllow)) {
122         NETMGR_LOG_E("Read Bool data failed");
123         return NETMANAGER_ERR_READ_DATA_FAIL;
124     }
125 
126     int32_t result = NetBackgroundPolicyChange(isBackgroundPolicyAllow);
127     if (!reply.WriteInt32(result)) {
128         NETMGR_LOG_E("Write Int32 reply failed");
129         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
130     }
131 
132     return NETMANAGER_SUCCESS;
133 }
134 
NetBackgroundPolicyChange(bool isBackgroundPolicyAllow)135 int32_t NetPolicyCallbackStub::NetBackgroundPolicyChange(bool isBackgroundPolicyAllow)
136 {
137     return NETMANAGER_SUCCESS;
138 }
139 
OnNetQuotaPolicyChange(MessageParcel & data,MessageParcel & reply)140 int32_t NetPolicyCallbackStub::OnNetQuotaPolicyChange(MessageParcel &data, MessageParcel &reply)
141 {
142     std::vector<NetQuotaPolicy> cellularPolicies;
143     if (!NetQuotaPolicy::Unmarshalling(data, cellularPolicies)) {
144         NETMGR_LOG_E("Unmarshalling failed.");
145         return NETMANAGER_ERR_READ_DATA_FAIL;
146     }
147 
148     int32_t result = NetQuotaPolicyChange(cellularPolicies);
149     if (!reply.WriteInt32(result)) {
150         NETMGR_LOG_E("Write Int32 reply failed");
151         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
152     }
153 
154     return NETMANAGER_SUCCESS;
155 }
156 
NetQuotaPolicyChange(const std::vector<NetQuotaPolicy> & cellularPolicies)157 int32_t NetPolicyCallbackStub::NetQuotaPolicyChange(const std::vector<NetQuotaPolicy> &cellularPolicies)
158 {
159     return NETMANAGER_SUCCESS;
160 }
161 
OnNetMeteredIfacesChange(MessageParcel & data,MessageParcel & reply)162 int32_t NetPolicyCallbackStub::OnNetMeteredIfacesChange(MessageParcel &data, MessageParcel &reply)
163 {
164     uint32_t size;
165     std::vector<std::string> ifaces;
166 
167     if (!data.ReadUint32(size)) {
168         NETMGR_LOG_E("Read UInt32 data failed");
169         return NETMANAGER_ERR_READ_DATA_FAIL;
170     }
171     size = size > MAX_IFACE_SIZE ? MAX_IFACE_SIZE : size;
172     for (uint32_t i = 0; i < size; ++i) {
173         std::string iface;
174         if (!data.ReadString(iface)) {
175             NETMGR_LOG_E("Read String data failed");
176             return NETMANAGER_ERR_READ_DATA_FAIL;
177         }
178         ifaces.push_back(iface);
179     }
180     int32_t result = NetMeteredIfacesChange(ifaces);
181     if (!reply.WriteInt32(result)) {
182         NETMGR_LOG_E("Write Int32 reply failed");
183         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
184     }
185     return NETMANAGER_SUCCESS;
186 }
187 
NetMeteredIfacesChange(std::vector<std::string> & ifaces)188 int32_t NetPolicyCallbackStub::NetMeteredIfacesChange(std::vector<std::string> &ifaces)
189 {
190     return NETMANAGER_SUCCESS;
191 }
192 
OnNetStrategySwitch(MessageParcel & data,MessageParcel & reply)193 int32_t NetPolicyCallbackStub::OnNetStrategySwitch(MessageParcel &data, MessageParcel &reply)
194 {
195     std::string simId;
196     if (!data.ReadString(simId)) {
197         NETMGR_LOG_E("Read String data failed");
198         return NETMANAGER_ERR_READ_DATA_FAIL;
199     }
200 
201     bool enable = false;
202     if (!data.ReadBool(enable)) {
203         NETMGR_LOG_E("Read Bool data failed");
204         return NETMANAGER_ERR_READ_DATA_FAIL;
205     }
206 
207     int32_t result = NetStrategySwitch(simId, enable);
208     if (!reply.WriteInt32(result)) {
209         NETMGR_LOG_E("Write Int32 reply failed");
210         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
211     }
212     return NETMANAGER_SUCCESS;
213 }
214 
NetStrategySwitch(const std::string & simId,bool enable)215 int32_t NetPolicyCallbackStub::NetStrategySwitch(const std::string &simId, bool enable)
216 {
217     return NETMANAGER_SUCCESS;
218 }
219 } // namespace NetManagerStandard
220 } // namespace OHOS
221