• 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 "ethernet_service_stub.h"
17 
18 #include "net_manager_constants.h"
19 #include "i_ethernet_service.h"
20 #include "interface_configuration.h"
21 #include "interface_type.h"
22 #include "ipc_object_stub.h"
23 #include "message_parcel.h"
24 #include "net_manager_constants.h"
25 #include "net_manager_ext_constants.h"
26 #include "netmgr_ext_log_wrapper.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 namespace {
31 constexpr uint32_t MAX_SIZE = 16;
32 }
33 
EthernetServiceStub()34 EthernetServiceStub::EthernetServiceStub()
35 {
36     memberFuncMap_[CMD_SET_IF_CFG] = &EthernetServiceStub::OnSetIfaceConfig;
37     memberFuncMap_[CMD_GET_IF_CFG] = &EthernetServiceStub::OnGetIfaceConfig;
38     memberFuncMap_[CMD_IS_ACTIVATE] = &EthernetServiceStub::OnIsIfaceActive;
39     memberFuncMap_[CMD_GET_ACTIVATE_INTERFACE] = &EthernetServiceStub::OnGetAllActiveIfaces;
40     memberFuncMap_[CMD_RESET_FACTORY] = &EthernetServiceStub::OnResetFactory;
41     memberFuncMap_[CMD_SET_INTERFACE_UP] = &EthernetServiceStub::OnSetInterfaceUp;
42     memberFuncMap_[CMD_SET_INTERFACE_DOWN] = &EthernetServiceStub::OnSetInterfaceDown;
43     memberFuncMap_[CMD_GET_INTERFACE_CONFIG] = &EthernetServiceStub::OnGetInterfaceConfig;
44 }
45 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)46 int32_t EthernetServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
47                                              MessageOption &option)
48 {
49     NETMGR_EXT_LOG_D("stub call start, code = [%{public}d]", code);
50 
51     std::u16string myDescripter = EthernetServiceStub::GetDescriptor();
52     std::u16string remoteDescripter = data.ReadInterfaceToken();
53     if (myDescripter != remoteDescripter) {
54         NETMGR_EXT_LOG_E("descriptor checked fail");
55         return NETMANAGER_EXT_ERR_DESCRIPTOR_MISMATCH;
56     }
57     auto itFunc = memberFuncMap_.find(code);
58     if (itFunc != memberFuncMap_.end()) {
59         auto requestFunc = itFunc->second;
60         if (requestFunc != nullptr) {
61             return (this->*requestFunc)(data, reply);
62         }
63     }
64     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
65 }
66 
OnSetIfaceConfig(MessageParcel & data,MessageParcel & reply)67 int32_t EthernetServiceStub::OnSetIfaceConfig(MessageParcel &data, MessageParcel &reply)
68 {
69     std::string iface;
70     if (!data.ReadString(iface)) {
71         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
72     }
73     sptr<InterfaceConfiguration> ic = InterfaceConfiguration::Unmarshalling(data);
74     if (ic == nullptr) {
75         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
76     }
77     return SetIfaceConfig(iface, ic);
78 }
79 
OnGetIfaceConfig(MessageParcel & data,MessageParcel & reply)80 int32_t EthernetServiceStub::OnGetIfaceConfig(MessageParcel &data, MessageParcel &reply)
81 {
82     std::string iface;
83     if (!data.ReadString(iface)) {
84         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
85     }
86     sptr<InterfaceConfiguration> ifaceConfig = new (std::nothrow) InterfaceConfiguration();
87     int32_t ret = GetIfaceConfig(iface, ifaceConfig);
88     if (ret == NETMANAGER_EXT_SUCCESS && ifaceConfig != nullptr) {
89         if (!reply.WriteInt32(GET_CFG_SUC)) {
90             NETMGR_EXT_LOG_E("write failed");
91             return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
92         }
93         if (!ifaceConfig->Marshalling(reply)) {
94             NETMGR_EXT_LOG_E("proxy Marshalling failed");
95             return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
96         }
97     }
98     return ret;
99 }
100 
OnIsIfaceActive(MessageParcel & data,MessageParcel & reply)101 int32_t EthernetServiceStub::OnIsIfaceActive(MessageParcel &data, MessageParcel &reply)
102 {
103     std::string iface;
104     if (!data.ReadString(iface)) {
105         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
106     }
107     int32_t activeStatus = 0;
108     int32_t ret = IsIfaceActive(iface, activeStatus);
109     if (ret == NETMANAGER_EXT_SUCCESS) {
110         if (!reply.WriteUint32(activeStatus)) {
111             return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
112         }
113     }
114     return ret;
115 }
116 
OnGetAllActiveIfaces(MessageParcel & data,MessageParcel & reply)117 int32_t EthernetServiceStub::OnGetAllActiveIfaces(MessageParcel &data, MessageParcel &reply)
118 {
119     std::vector<std::string> ifaces;
120     int32_t ret = GetAllActiveIfaces(ifaces);
121     NETMGR_EXT_LOG_E("ret %{public}d", ret);
122     if (ret != NETMANAGER_EXT_SUCCESS || ifaces.size() == 0) {
123         NETMGR_EXT_LOG_E("get all active ifaces failed");
124         return ret;
125     }
126     if (ifaces.size() > MAX_SIZE) {
127         NETMGR_EXT_LOG_E("ifaces size is too large");
128         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
129     }
130     if (!reply.WriteUint32(ifaces.size())) {
131         NETMGR_EXT_LOG_E("iface size write failed");
132         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
133     }
134     for (auto iface : ifaces) {
135         if (!reply.WriteString(iface)) {
136             NETMGR_EXT_LOG_E("iface write failed");
137             return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
138         }
139     }
140     return NETMANAGER_EXT_SUCCESS;
141 }
142 
OnResetFactory(MessageParcel & data,MessageParcel & reply)143 int32_t EthernetServiceStub::OnResetFactory(MessageParcel &data, MessageParcel &reply)
144 {
145     return ResetFactory();
146 }
147 
OnSetInterfaceUp(MessageParcel & data,MessageParcel & reply)148 int32_t EthernetServiceStub::OnSetInterfaceUp(MessageParcel &data, MessageParcel &reply)
149 {
150     std::string iface;
151     if (!data.ReadString(iface)) {
152         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
153     }
154     return SetInterfaceUp(iface);
155 }
156 
OnSetInterfaceDown(MessageParcel & data,MessageParcel & reply)157 int32_t EthernetServiceStub::OnSetInterfaceDown(MessageParcel &data, MessageParcel &reply)
158 {
159     std::string iface;
160     if (!data.ReadString(iface)) {
161         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
162     }
163     return SetInterfaceDown(iface);
164 }
165 
OnGetInterfaceConfig(MessageParcel & data,MessageParcel & reply)166 int32_t EthernetServiceStub::OnGetInterfaceConfig(MessageParcel &data, MessageParcel &reply)
167 {
168     std::string iface;
169     if (!data.ReadString(iface)) {
170         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
171     }
172     OHOS::nmd::InterfaceConfigurationParcel cfg;
173     int32_t result = GetInterfaceConfig(iface, cfg);
174     if (!result) {
175         NETMGR_EXT_LOG_E("GetInterfaceConfig is error");
176         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
177     }
178     reply.WriteString(cfg.ifName);
179     reply.WriteString(cfg.hwAddr);
180     reply.WriteString(cfg.ipv4Addr);
181     reply.WriteInt32(cfg.prefixLength);
182     if (cfg.flags.size() > MAX_SIZE) {
183         NETMGR_EXT_LOG_E("cfg flags size is too large");
184         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
185     }
186     reply.WriteInt32(static_cast<int32_t>(cfg.flags.size()));
187     for (auto flag : cfg.flags) {
188         reply.WriteString(flag);
189     }
190     reply.WriteInt32(result);
191     return NETMANAGER_EXT_SUCCESS;
192 }
193 } // namespace NetManagerStandard
194 } // namespace OHOS