1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "netfirewall_proxy.h"
17 #include "hilog/log.h"
18 #include "iremote_object.h"
19 #include "message_option.h"
20 #include "message_parcel.h"
21 #include "net_manager_constants.h"
22 #include "net_manager_ext_constants.h"
23 #include "netmgr_ext_log_wrapper.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27
SetNetFirewallPolicy(const int32_t userId,const sptr<NetFirewallPolicy> & status)28 int32_t NetFirewallProxy::SetNetFirewallPolicy(const int32_t userId, const sptr<NetFirewallPolicy> &status)
29 {
30 NETMGR_EXT_LOG_I("SetNetFirewallPolicy");
31 MessageParcel data;
32 if (!data.WriteInterfaceToken(GetDescriptor())) {
33 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
34 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
35 }
36 data.WriteInt32(userId);
37 if (!status->Marshalling(data)) {
38 NETMGR_EXT_LOG_E("proxy Marshalling failed");
39 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
40 }
41 sptr<IRemoteObject> remote = Remote();
42 if (remote == nullptr) {
43 NETMGR_EXT_LOG_E("Remote is null");
44 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
45 }
46 MessageParcel reply;
47 MessageOption option;
48 int32_t ret = remote->SendRequest(static_cast<uint32_t>(SET_NET_FIREWALL_STATUS), data, reply, option);
49 if (ret != FIREWALL_SUCCESS) {
50 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
51 }
52 return ret;
53 }
54
GetNetFirewallPolicy(const int32_t userId,sptr<NetFirewallPolicy> & status)55 int32_t NetFirewallProxy::GetNetFirewallPolicy(const int32_t userId, sptr<NetFirewallPolicy> &status)
56 {
57 sptr<IRemoteObject> remote = Remote();
58 if (remote == nullptr) {
59 NETMGR_EXT_LOG_E("Remote is null");
60 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
61 }
62 MessageParcel data;
63 MessageParcel reply;
64 MessageOption option;
65 if (!data.WriteInterfaceToken(GetDescriptor())) {
66 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
67 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
68 }
69 data.WriteInt32(userId);
70 int32_t ret = remote->SendRequest(static_cast<uint32_t>(GET_NET_FIREWALL_STATUS), data, reply, option);
71 if (ret != FIREWALL_SUCCESS) {
72 NETMGR_EXT_LOG_E("GetNetFirewallPolicy proxy SendRequest failed, error code: [%{public}d]", ret);
73 return ret;
74 }
75 status = NetFirewallPolicy::Unmarshalling(reply);
76 if (status == nullptr) {
77 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
78 }
79 return FIREWALL_SUCCESS;
80 }
81
AddNetFirewallRule(const sptr<NetFirewallRule> & rule,int32_t & result)82 int32_t NetFirewallProxy::AddNetFirewallRule(const sptr<NetFirewallRule> &rule, int32_t &result)
83 {
84 NETMGR_EXT_LOG_I("AddNetFirewallRule");
85 MessageParcel data;
86 if (!data.WriteInterfaceToken(GetDescriptor())) {
87 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
88 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
89 }
90 if (!rule->Marshalling(data)) {
91 NETMGR_EXT_LOG_E("AddNetFirewallRule proxy Marshalling failed");
92 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
93 }
94 sptr<IRemoteObject> remote = Remote();
95 if (remote == nullptr) {
96 NETMGR_EXT_LOG_E("Remote is null");
97 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
98 }
99 MessageParcel reply;
100 MessageOption option;
101 int32_t ret = remote->SendRequest(static_cast<uint32_t>(ADD_NET_FIREWALL_RULE), data, reply, option);
102 if (ret != FIREWALL_SUCCESS) {
103 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
104 return ret;
105 }
106 result = reply.ReadInt32();
107 return FIREWALL_SUCCESS;
108 }
109
UpdateNetFirewallRule(const sptr<NetFirewallRule> & rule)110 int32_t NetFirewallProxy::UpdateNetFirewallRule(const sptr<NetFirewallRule> &rule)
111 {
112 NETMGR_EXT_LOG_I("UpdateNetFirewallRule");
113 MessageParcel data;
114 if (!data.WriteInterfaceToken(GetDescriptor())) {
115 NETMGR_EXT_LOG_E("UpdateNetFirewallRule WriteInterfaceToken failed");
116 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
117 }
118 if (!rule->Marshalling(data)) {
119 NETMGR_EXT_LOG_E("UpdateNetFirewallRule proxy Marshalling failed");
120 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
121 }
122 sptr<IRemoteObject> remote = Remote();
123 if (remote == nullptr) {
124 NETMGR_EXT_LOG_E("Remote is null");
125 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
126 }
127 MessageParcel reply;
128 MessageOption option;
129 int32_t ret = remote->SendRequest(static_cast<uint32_t>(UPDATE_NET_FIREWALL_RULE), data, reply, option);
130 if (ret != FIREWALL_SUCCESS) {
131 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
132 }
133 return ret;
134 }
135
DeleteNetFirewallRule(const int32_t userId,const int32_t ruleId)136 int32_t NetFirewallProxy::DeleteNetFirewallRule(const int32_t userId, const int32_t ruleId)
137 {
138 NETMGR_EXT_LOG_I("DeleteNetFirewallRule");
139 MessageParcel data;
140 if (!data.WriteInterfaceToken(GetDescriptor())) {
141 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
142 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
143 }
144 data.WriteInt32(userId);
145 data.WriteInt32(ruleId);
146 sptr<IRemoteObject> remote = Remote();
147 if (remote == nullptr) {
148 NETMGR_EXT_LOG_E("Remote is null");
149 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
150 }
151 MessageParcel reply;
152 MessageOption option;
153 int32_t ret = remote->SendRequest(static_cast<uint32_t>(DELETE_NET_FIREWALL_RULE), data, reply, option);
154 if (ret != FIREWALL_SUCCESS) {
155 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
156 }
157 return ret;
158 }
159
GetNetFirewallRules(const int32_t userId,const sptr<RequestParam> & requestParam,sptr<FirewallRulePage> & info)160 int32_t NetFirewallProxy::GetNetFirewallRules(const int32_t userId, const sptr<RequestParam> &requestParam,
161 sptr<FirewallRulePage> &info)
162 {
163 MessageParcel data;
164 if (!data.WriteInterfaceToken(GetDescriptor())) {
165 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
166 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
167 }
168 data.WriteInt32(userId);
169 if (!requestParam->Marshalling(data)) {
170 NETMGR_EXT_LOG_E("GetNetFirewallRules proxy Marshalling failed");
171 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
172 }
173 sptr<IRemoteObject> remote = Remote();
174 if (remote == nullptr) {
175 NETMGR_EXT_LOG_E("Remote is null");
176 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
177 }
178 MessageParcel reply;
179 MessageOption option;
180 int32_t ret = remote->SendRequest(static_cast<uint32_t>(GET_ALL_NET_FIREWALL_RULES), data, reply, option);
181 if (ret != FIREWALL_SUCCESS) {
182 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
183 return ret;
184 }
185 info = FirewallRulePage::Unmarshalling(reply);
186 if (info == nullptr) {
187 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
188 }
189 return FIREWALL_SUCCESS;
190 }
191
GetNetFirewallRule(const int32_t userId,const int32_t ruleId,sptr<NetFirewallRule> & rule)192 int32_t NetFirewallProxy::GetNetFirewallRule(const int32_t userId, const int32_t ruleId, sptr<NetFirewallRule> &rule)
193 {
194 MessageParcel data;
195 if (!data.WriteInterfaceToken(GetDescriptor())) {
196 NETMGR_EXT_LOG_E("GetNetFirewallRule WriteInterfaceToken failed");
197 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
198 }
199 data.WriteInt32(userId);
200 data.WriteInt32(ruleId);
201 sptr<IRemoteObject> remote = Remote();
202 if (remote == nullptr) {
203 NETMGR_EXT_LOG_E("GetNetFirewallRule Remote is null");
204 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
205 }
206 MessageParcel reply;
207 MessageOption option;
208 int32_t ret = remote->SendRequest(static_cast<uint32_t>(GET_NET_FIREWALL_RULE), data, reply, option);
209 if (ret != FIREWALL_SUCCESS) {
210 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
211 return ret;
212 }
213 rule = NetFirewallRule::Unmarshalling(reply);
214 if (rule == nullptr) {
215 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
216 }
217 return FIREWALL_SUCCESS;
218 }
219
GetInterceptRecords(const int32_t userId,const sptr<RequestParam> & requestParam,sptr<InterceptRecordPage> & info)220 int32_t NetFirewallProxy::GetInterceptRecords(const int32_t userId, const sptr<RequestParam> &requestParam,
221 sptr<InterceptRecordPage> &info)
222 {
223 MessageParcel data;
224 if (!data.WriteInterfaceToken(GetDescriptor())) {
225 NETMGR_EXT_LOG_E("GetInterceptRecords WriteInterfaceToken failed");
226 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
227 }
228 data.WriteInt32(userId);
229 if (!requestParam->Marshalling(data)) {
230 NETMGR_EXT_LOG_E("GetInterceptRecords proxy Marshalling failed");
231 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
232 }
233 sptr<IRemoteObject> remote = Remote();
234 if (remote == nullptr) {
235 NETMGR_EXT_LOG_E("Remote is null");
236 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
237 }
238 MessageParcel reply;
239 MessageOption option;
240 int32_t ret = remote->SendRequest(static_cast<uint32_t>(GET_ALL_INTERCEPT_RECORDS), data, reply, option);
241 if (ret != FIREWALL_SUCCESS) {
242 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
243 return ret;
244 }
245 info = InterceptRecordPage::Unmarshalling(reply);
246 if (info == nullptr) {
247 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
248 }
249 return FIREWALL_SUCCESS;
250 }
251 } // namespace NetManagerStandard
252 } // namespace OHOS
253