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 "iremote_object.h"
18 #include "message_option.h"
19 #include "message_parcel.h"
20 #include "net_manager_constants.h"
21 #include "net_manager_ext_constants.h"
22 #include "netmgr_ext_log_wrapper.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26
SetNetFirewallPolicy(const int32_t userId,const sptr<NetFirewallPolicy> & status)27 int32_t NetFirewallProxy::SetNetFirewallPolicy(const int32_t userId, const sptr<NetFirewallPolicy> &status)
28 {
29 NETMGR_EXT_LOG_I("SetNetFirewallPolicy");
30 MessageParcel data;
31 if (!data.WriteInterfaceToken(GetDescriptor())) {
32 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
33 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
34 }
35 data.WriteInt32(userId);
36 if (!status->Marshalling(data)) {
37 NETMGR_EXT_LOG_E("proxy Marshalling failed");
38 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
39 }
40 sptr<IRemoteObject> remote = Remote();
41 if (remote == nullptr) {
42 NETMGR_EXT_LOG_E("Remote is null");
43 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
44 }
45 MessageParcel reply;
46 MessageOption option;
47 int32_t ret = remote->SendRequest(static_cast<uint32_t>(SET_NET_FIREWALL_STATUS), data, reply, option);
48 if (ret != FIREWALL_SUCCESS) {
49 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
50 }
51 return ret;
52 }
53
GetNetFirewallPolicy(const int32_t userId,sptr<NetFirewallPolicy> & status)54 int32_t NetFirewallProxy::GetNetFirewallPolicy(const int32_t userId, sptr<NetFirewallPolicy> &status)
55 {
56 sptr<IRemoteObject> remote = Remote();
57 if (remote == nullptr) {
58 NETMGR_EXT_LOG_E("Remote is null");
59 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
60 }
61 MessageParcel data;
62 MessageParcel reply;
63 MessageOption option;
64 if (!data.WriteInterfaceToken(GetDescriptor())) {
65 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
66 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
67 }
68 data.WriteInt32(userId);
69 int32_t ret = remote->SendRequest(static_cast<uint32_t>(GET_NET_FIREWALL_STATUS), data, reply, option);
70 if (ret != FIREWALL_SUCCESS) {
71 NETMGR_EXT_LOG_E("GetNetFirewallPolicy proxy SendRequest failed, error code: [%{public}d]", ret);
72 return ret;
73 }
74 status = NetFirewallPolicy::Unmarshalling(reply);
75 if (status == nullptr) {
76 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
77 }
78 return FIREWALL_SUCCESS;
79 }
80
AddNetFirewallRule(const sptr<NetFirewallRule> & rule,int32_t & result)81 int32_t NetFirewallProxy::AddNetFirewallRule(const sptr<NetFirewallRule> &rule, int32_t &result)
82 {
83 NETMGR_EXT_LOG_I("AddNetFirewallRule");
84 MessageParcel data;
85 if (!data.WriteInterfaceToken(GetDescriptor())) {
86 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
87 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
88 }
89 if (!rule->Marshalling(data)) {
90 NETMGR_EXT_LOG_E("AddNetFirewallRule proxy Marshalling failed");
91 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
92 }
93 sptr<IRemoteObject> remote = Remote();
94 if (remote == nullptr) {
95 NETMGR_EXT_LOG_E("Remote is null");
96 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
97 }
98 MessageParcel reply;
99 MessageOption option;
100 int32_t ret = remote->SendRequest(static_cast<uint32_t>(ADD_NET_FIREWALL_RULE), data, reply, option);
101 if (ret != FIREWALL_SUCCESS) {
102 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
103 return ret;
104 }
105 result = reply.ReadInt32();
106 return FIREWALL_SUCCESS;
107 }
108
UpdateNetFirewallRule(const sptr<NetFirewallRule> & rule)109 int32_t NetFirewallProxy::UpdateNetFirewallRule(const sptr<NetFirewallRule> &rule)
110 {
111 NETMGR_EXT_LOG_I("UpdateNetFirewallRule");
112 MessageParcel data;
113 if (!data.WriteInterfaceToken(GetDescriptor())) {
114 NETMGR_EXT_LOG_E("UpdateNetFirewallRule WriteInterfaceToken failed");
115 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
116 }
117 if (!rule->Marshalling(data)) {
118 NETMGR_EXT_LOG_E("UpdateNetFirewallRule proxy Marshalling failed");
119 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
120 }
121 sptr<IRemoteObject> remote = Remote();
122 if (remote == nullptr) {
123 NETMGR_EXT_LOG_E("Remote is null");
124 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
125 }
126 MessageParcel reply;
127 MessageOption option;
128 int32_t ret = remote->SendRequest(static_cast<uint32_t>(UPDATE_NET_FIREWALL_RULE), data, reply, option);
129 if (ret != FIREWALL_SUCCESS) {
130 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
131 }
132 return ret;
133 }
134
DeleteNetFirewallRule(const int32_t userId,const int32_t ruleId)135 int32_t NetFirewallProxy::DeleteNetFirewallRule(const int32_t userId, const int32_t ruleId)
136 {
137 NETMGR_EXT_LOG_I("DeleteNetFirewallRule");
138 MessageParcel data;
139 if (!data.WriteInterfaceToken(GetDescriptor())) {
140 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
141 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
142 }
143 data.WriteInt32(userId);
144 data.WriteInt32(ruleId);
145 sptr<IRemoteObject> remote = Remote();
146 if (remote == nullptr) {
147 NETMGR_EXT_LOG_E("Remote is null");
148 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
149 }
150 MessageParcel reply;
151 MessageOption option;
152 int32_t ret = remote->SendRequest(static_cast<uint32_t>(DELETE_NET_FIREWALL_RULE), data, reply, option);
153 if (ret != FIREWALL_SUCCESS) {
154 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
155 }
156 return ret;
157 }
158
GetNetFirewallRules(const int32_t userId,const sptr<RequestParam> & requestParam,sptr<FirewallRulePage> & info)159 int32_t NetFirewallProxy::GetNetFirewallRules(const int32_t userId, const sptr<RequestParam> &requestParam,
160 sptr<FirewallRulePage> &info)
161 {
162 MessageParcel data;
163 if (!data.WriteInterfaceToken(GetDescriptor())) {
164 NETMGR_EXT_LOG_E("WriteInterfaceToken failed");
165 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
166 }
167 data.WriteInt32(userId);
168 if (!requestParam->Marshalling(data)) {
169 NETMGR_EXT_LOG_E("GetNetFirewallRules proxy Marshalling failed");
170 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
171 }
172 sptr<IRemoteObject> remote = Remote();
173 if (remote == nullptr) {
174 NETMGR_EXT_LOG_E("Remote is null");
175 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
176 }
177 MessageParcel reply;
178 MessageOption option;
179 int32_t ret = remote->SendRequest(static_cast<uint32_t>(GET_ALL_NET_FIREWALL_RULES), data, reply, option);
180 if (ret != FIREWALL_SUCCESS) {
181 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
182 return ret;
183 }
184 info = FirewallRulePage::Unmarshalling(reply);
185 if (info == nullptr) {
186 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
187 }
188 return FIREWALL_SUCCESS;
189 }
190
GetNetFirewallRule(const int32_t userId,const int32_t ruleId,sptr<NetFirewallRule> & rule)191 int32_t NetFirewallProxy::GetNetFirewallRule(const int32_t userId, const int32_t ruleId, sptr<NetFirewallRule> &rule)
192 {
193 MessageParcel data;
194 if (!data.WriteInterfaceToken(GetDescriptor())) {
195 NETMGR_EXT_LOG_E("GetNetFirewallRule WriteInterfaceToken failed");
196 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
197 }
198 data.WriteInt32(userId);
199 data.WriteInt32(ruleId);
200 sptr<IRemoteObject> remote = Remote();
201 if (remote == nullptr) {
202 NETMGR_EXT_LOG_E("GetNetFirewallRule Remote is null");
203 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
204 }
205 MessageParcel reply;
206 MessageOption option;
207 int32_t ret = remote->SendRequest(static_cast<uint32_t>(GET_NET_FIREWALL_RULE), data, reply, option);
208 if (ret != FIREWALL_SUCCESS) {
209 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
210 return ret;
211 }
212 rule = NetFirewallRule::Unmarshalling(reply);
213 if (rule == nullptr) {
214 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
215 }
216 return FIREWALL_SUCCESS;
217 }
218
GetInterceptRecords(const int32_t userId,const sptr<RequestParam> & requestParam,sptr<InterceptRecordPage> & info)219 int32_t NetFirewallProxy::GetInterceptRecords(const int32_t userId, const sptr<RequestParam> &requestParam,
220 sptr<InterceptRecordPage> &info)
221 {
222 MessageParcel data;
223 if (!data.WriteInterfaceToken(GetDescriptor())) {
224 NETMGR_EXT_LOG_E("GetInterceptRecords WriteInterfaceToken failed");
225 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
226 }
227 data.WriteInt32(userId);
228 if (!requestParam->Marshalling(data)) {
229 NETMGR_EXT_LOG_E("GetInterceptRecords proxy Marshalling failed");
230 return NETMANAGER_EXT_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
231 }
232 sptr<IRemoteObject> remote = Remote();
233 if (remote == nullptr) {
234 NETMGR_EXT_LOG_E("Remote is null");
235 return NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL;
236 }
237 MessageParcel reply;
238 MessageOption option;
239 int32_t ret = remote->SendRequest(static_cast<uint32_t>(GET_ALL_INTERCEPT_RECORDS), data, reply, option);
240 if (ret != FIREWALL_SUCCESS) {
241 NETMGR_EXT_LOG_E("proxy SendRequest failed, error code: [%{public}d]", ret);
242 return ret;
243 }
244 info = InterceptRecordPage::Unmarshalling(reply);
245 if (info == nullptr) {
246 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
247 }
248 return FIREWALL_SUCCESS;
249 }
250 } // namespace NetManagerStandard
251 } // namespace OHOS
252