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 "ethernet_service_stub.h"
17
18 #include "i_ethernet_service.h"
19 #include "interface_configuration.h"
20 #include "interface_type.h"
21 #include "ipc_object_stub.h"
22 #include "message_parcel.h"
23 #include "net_manager_constants.h"
24 #include "net_manager_ext_constants.h"
25 #include "netmgr_ext_log_wrapper.h"
26
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace {
30 constexpr uint32_t MAX_SIZE = 16;
31 constexpr uint32_t MAX_IFACE_NAME_LEN = 13;
32 constexpr uint32_t MAX_MAC_ADDR_LEN = 17;
33 constexpr uint32_t MAX_IPV4_ADDR_LEN = 15;
34 constexpr uint32_t MAX_PRE_LEN = 128;
35 } // namespace
36
EthernetServiceStub()37 EthernetServiceStub::EthernetServiceStub()
38 {
39 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_SET_IF_CFG)] =
40 &EthernetServiceStub::OnSetIfaceConfig;
41 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_GET_IF_CFG)] =
42 &EthernetServiceStub::OnGetIfaceConfig;
43 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_IS_ACTIVATE)] =
44 &EthernetServiceStub::OnIsIfaceActive;
45 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_GET_ACTIVATE_INTERFACE)] =
46 &EthernetServiceStub::OnGetAllActiveIfaces;
47 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_RESET_FACTORY)] =
48 &EthernetServiceStub::OnResetFactory;
49 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_REGISTER_INTERFACE_CB)] =
50 &EthernetServiceStub::OnRegisterIfacesStateChanged;
51 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_UNREGISTER_INTERFACE_CB)] =
52 &EthernetServiceStub::OnUnregisterIfacesStateChanged;
53 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_SET_INTERFACE_UP)] =
54 &EthernetServiceStub::OnSetInterfaceUp;
55 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_SET_INTERFACE_DOWN)] =
56 &EthernetServiceStub::OnSetInterfaceDown;
57 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_GET_INTERFACE_CONFIG)] =
58 &EthernetServiceStub::OnGetInterfaceConfig;
59 memberFuncMap_[static_cast<uint32_t>(EthernetInterfaceCode::CMD_SET_INTERFACE_CONFIG)] =
60 &EthernetServiceStub::OnSetInterfaceConfig;
61 }
62
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)63 int32_t EthernetServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
64 MessageOption &option)
65 {
66 NETMGR_EXT_LOG_D("stub call start, code = [%{public}d]", code);
67
68 std::u16string myDescripter = EthernetServiceStub::GetDescriptor();
69 std::u16string remoteDescripter = data.ReadInterfaceToken();
70 if (myDescripter != remoteDescripter) {
71 NETMGR_EXT_LOG_E("descriptor checked fail");
72 return NETMANAGER_EXT_ERR_DESCRIPTOR_MISMATCH;
73 }
74 auto itFunc = memberFuncMap_.find(code);
75 if (itFunc != memberFuncMap_.end()) {
76 auto requestFunc = itFunc->second;
77 if (requestFunc != nullptr) {
78 return (this->*requestFunc)(data, reply);
79 }
80 }
81 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
82 }
83
OnSetIfaceConfig(MessageParcel & data,MessageParcel & reply)84 int32_t EthernetServiceStub::OnSetIfaceConfig(MessageParcel &data, MessageParcel &reply)
85 {
86 std::string iface;
87 if (!data.ReadString(iface)) {
88 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
89 }
90 sptr<InterfaceConfiguration> ic = InterfaceConfiguration::Unmarshalling(data);
91 if (ic == nullptr) {
92 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
93 }
94 return SetIfaceConfig(iface, ic);
95 }
96
OnGetIfaceConfig(MessageParcel & data,MessageParcel & reply)97 int32_t EthernetServiceStub::OnGetIfaceConfig(MessageParcel &data, MessageParcel &reply)
98 {
99 std::string iface;
100 if (!data.ReadString(iface)) {
101 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
102 }
103 sptr<InterfaceConfiguration> ifaceConfig = new (std::nothrow) InterfaceConfiguration();
104 int32_t ret = GetIfaceConfig(iface, ifaceConfig);
105 if (ret == NETMANAGER_EXT_SUCCESS && ifaceConfig != nullptr) {
106 if (!reply.WriteInt32(GET_CFG_SUC)) {
107 NETMGR_EXT_LOG_E("write failed");
108 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
109 }
110 if (!ifaceConfig->Marshalling(reply)) {
111 NETMGR_EXT_LOG_E("proxy Marshalling failed");
112 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
113 }
114 }
115 return ret;
116 }
117
OnIsIfaceActive(MessageParcel & data,MessageParcel & reply)118 int32_t EthernetServiceStub::OnIsIfaceActive(MessageParcel &data, MessageParcel &reply)
119 {
120 std::string iface;
121 if (!data.ReadString(iface)) {
122 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
123 }
124 int32_t activeStatus = 0;
125 int32_t ret = IsIfaceActive(iface, activeStatus);
126 if (ret == NETMANAGER_EXT_SUCCESS) {
127 if (!reply.WriteUint32(activeStatus)) {
128 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
129 }
130 }
131 return ret;
132 }
133
OnGetAllActiveIfaces(MessageParcel & data,MessageParcel & reply)134 int32_t EthernetServiceStub::OnGetAllActiveIfaces(MessageParcel &data, MessageParcel &reply)
135 {
136 std::vector<std::string> ifaces;
137 int32_t ret = GetAllActiveIfaces(ifaces);
138 NETMGR_EXT_LOG_E("ret %{public}d", ret);
139 if (ret != NETMANAGER_EXT_SUCCESS || ifaces.size() == 0) {
140 NETMGR_EXT_LOG_E("get all active ifaces failed");
141 return ret;
142 }
143 if (ifaces.size() > MAX_SIZE) {
144 NETMGR_EXT_LOG_E("ifaces size is too large");
145 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
146 }
147 if (!reply.WriteUint32(ifaces.size())) {
148 NETMGR_EXT_LOG_E("iface size write failed");
149 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
150 }
151 for (auto iface : ifaces) {
152 if (!reply.WriteString(iface)) {
153 NETMGR_EXT_LOG_E("iface write failed");
154 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
155 }
156 }
157 return NETMANAGER_EXT_SUCCESS;
158 }
159
OnResetFactory(MessageParcel & data,MessageParcel & reply)160 int32_t EthernetServiceStub::OnResetFactory(MessageParcel &data, MessageParcel &reply)
161 {
162 return ResetFactory();
163 }
164
OnRegisterIfacesStateChanged(MessageParcel & data,MessageParcel & reply)165 int32_t EthernetServiceStub::OnRegisterIfacesStateChanged(MessageParcel &data, MessageParcel &reply)
166 {
167 sptr<InterfaceStateCallback> callback = iface_cast<InterfaceStateCallback>(data.ReadRemoteObject());
168 if (callback == nullptr) {
169 NETMGR_EXT_LOG_E("callback is null.");
170 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
171 }
172
173 int32_t ret = RegisterIfacesStateChanged(callback);
174 if (!reply.WriteInt32(ret)) {
175 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
176 }
177 return NETMANAGER_EXT_SUCCESS;
178 }
179
OnUnregisterIfacesStateChanged(MessageParcel & data,MessageParcel & reply)180 int32_t EthernetServiceStub::OnUnregisterIfacesStateChanged(MessageParcel &data, MessageParcel &reply)
181 {
182 sptr<InterfaceStateCallback> callback = iface_cast<InterfaceStateCallback>(data.ReadRemoteObject());
183 if (callback == nullptr) {
184 NETMGR_EXT_LOG_E("callback is null.");
185 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
186 }
187
188 int32_t ret = UnregisterIfacesStateChanged(callback);
189 if (!reply.WriteInt32(ret)) {
190 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
191 }
192 return NETMANAGER_EXT_SUCCESS;
193 }
194
OnSetInterfaceUp(MessageParcel & data,MessageParcel & reply)195 int32_t EthernetServiceStub::OnSetInterfaceUp(MessageParcel &data, MessageParcel &reply)
196 {
197 std::string iface;
198 if (!data.ReadString(iface)) {
199 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
200 }
201 return SetInterfaceUp(iface);
202 }
203
OnSetInterfaceDown(MessageParcel & data,MessageParcel & reply)204 int32_t EthernetServiceStub::OnSetInterfaceDown(MessageParcel &data, MessageParcel &reply)
205 {
206 std::string iface;
207 if (!data.ReadString(iface)) {
208 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
209 }
210 return SetInterfaceDown(iface);
211 }
212
OnGetInterfaceConfig(MessageParcel & data,MessageParcel & reply)213 int32_t EthernetServiceStub::OnGetInterfaceConfig(MessageParcel &data, MessageParcel &reply)
214 {
215 std::string iface;
216 if (!data.ReadString(iface)) {
217 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
218 }
219 OHOS::nmd::InterfaceConfigurationParcel cfg;
220 int32_t result = GetInterfaceConfig(iface, cfg);
221 if (result != ERR_NONE) {
222 NETMGR_EXT_LOG_E("GetInterfaceConfig is error");
223 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
224 }
225 reply.WriteString(cfg.ifName);
226 reply.WriteString(cfg.hwAddr);
227 reply.WriteString(cfg.ipv4Addr);
228 reply.WriteInt32(cfg.prefixLength);
229 if (cfg.flags.size() > MAX_SIZE) {
230 NETMGR_EXT_LOG_E("cfg flags size is too large");
231 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
232 }
233 reply.WriteInt32(static_cast<int32_t>(cfg.flags.size()));
234 for (auto flag : cfg.flags) {
235 reply.WriteString(flag);
236 }
237 reply.WriteInt32(result);
238 return NETMANAGER_EXT_SUCCESS;
239 }
240
OnSetInterfaceConfig(MessageParcel & data,MessageParcel & reply)241 int32_t EthernetServiceStub::OnSetInterfaceConfig(MessageParcel &data, MessageParcel &reply)
242 {
243 std::string iface = data.ReadString();
244
245 OHOS::nmd::InterfaceConfigurationParcel cfg;
246 cfg.ifName = data.ReadString();
247 if (cfg.ifName.size() > MAX_IFACE_NAME_LEN || cfg.ifName.size() == 0) {
248 NETMGR_EXT_LOG_E("ifName=[%{public}s] is too long", cfg.ifName.c_str());
249 return NETMANAGER_EXT_ERR_INVALID_PARAMETER;
250 }
251 cfg.hwAddr = data.ReadString();
252 if (cfg.hwAddr.size() > MAX_MAC_ADDR_LEN) {
253 NETMGR_EXT_LOG_E("hwAddr=[%{public}s] is too long", cfg.hwAddr.c_str());
254 return NETMANAGER_EXT_ERR_INVALID_PARAMETER;
255 }
256 cfg.ipv4Addr = data.ReadString();
257 if (cfg.ipv4Addr.size() > MAX_IPV4_ADDR_LEN) {
258 NETMGR_EXT_LOG_E("ipv4Addr=[%{public}s] is too long", cfg.ipv4Addr.c_str());
259 return NETMANAGER_EXT_ERR_INVALID_PARAMETER;
260 }
261 cfg.prefixLength = data.ReadInt32();
262 if (cfg.prefixLength > static_cast<int32_t>(MAX_PRE_LEN)) {
263 NETMGR_EXT_LOG_E("prefixLength=[%{public}d] is too large", cfg.prefixLength);
264 return NETMANAGER_EXT_ERR_INVALID_PARAMETER;
265 }
266
267 uint32_t vecSize = data.ReadUint32();
268 if (vecSize == 0 || vecSize > MAX_SIZE) {
269 NETMGR_EXT_LOG_E("flags size=[%{public}u] is 0 or too large", vecSize);
270 return NETMANAGER_EXT_ERR_INVALID_PARAMETER;
271 }
272 for (uint32_t idx = 0; idx < vecSize; idx++) {
273 cfg.flags.push_back(data.ReadString());
274 }
275
276 int32_t result = SetInterfaceConfig(iface, cfg);
277 if (result != ERR_NONE) {
278 NETMGR_EXT_LOG_E("Set interface config failed");
279 return NETMANAGER_EXT_ERR_OPERATION_FAILED;
280 }
281 return NETMANAGER_EXT_SUCCESS;
282 }
283 } // namespace NetManagerStandard
284 } // namespace OHOS
285