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 #include "dhcp_client_stub_lite.h"
16 #include "dhcp_client_callback_proxy.h"
17 #include "dhcp_manager_service_ipc_interface_code.h"
18 #include "dhcp_sdk_define.h"
19 #ifndef OHOS_EUPDATER
20 #include "ipc_skeleton.h"
21 #include "rpc_errno.h"
22 #endif
23 #include "dhcp_client_state_machine.h"
24 #include "dhcp_logger.h"
25 #include "dhcp_errcode.h"
26
27 #ifndef OHOS_EUPDATER
28 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientStub");
29 #endif
30 namespace OHOS {
31 namespace DHCP {
DhcpClientStub()32 DhcpClientStub::DhcpClientStub()
33 {
34 #ifndef OHOS_EUPDATER
35 InitHandleMap();
36 #endif
37 }
38
~DhcpClientStub()39 DhcpClientStub::~DhcpClientStub()
40 {}
41
42 #ifndef OHOS_EUPDATER
InitHandleMap()43 void DhcpClientStub::InitHandleMap()
44 {
45 handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK)] = &DhcpClientStub::OnRegisterCallBack;
46 handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT)] = &DhcpClientStub::OnStartDhcpClient;
47 handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT)] = &DhcpClientStub::OnStopDhcpClient;
48 handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_DEAL_DHCP_CACHE)] =
49 &DhcpClientStub::OnDealWifiDhcpCache;
50 }
51
OnRemoteRequest(uint32_t code,IpcIo * req,IpcIo * reply)52 int DhcpClientStub::OnRemoteRequest(uint32_t code, IpcIo *req, IpcIo *reply)
53 {
54 DHCP_LOGI("run: %{public}s code: %{public}u L1", __func__, code);
55 if (req == nullptr || reply == nullptr) {
56 DHCP_LOGD("req:%{public}d, reply:%{public}d", req == nullptr, reply == nullptr);
57 return DHCP_OPT_FAILED;
58 }
59
60 DHCP_LOGI("run ReadInterfaceToken L1 code %{public}u", code);
61 size_t length;
62 uint16_t* interfaceRead = nullptr;
63 interfaceRead = ReadInterfaceToken(req, &length);
64 for (size_t i = 0; i < length; i++) {
65 if (i >= DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH || interfaceRead[i] != DECLARE_INTERFACE_DESCRIPTOR_L1[i]) {
66 DHCP_LOGE("Sta stub token verification error: %{public}d", code);
67 return DHCP_OPT_FAILED;
68 }
69 }
70
71 int exception = 0;
72 (void)ReadInt32(req, &exception);
73 if (exception) {
74 (void)WriteInt32(reply, 0);
75 (void)WriteInt32(reply, DHCP_OPT_NOT_SUPPORTED);
76 return DHCP_OPT_FAILED;
77 }
78
79 HandleFuncMap::iterator iter = handleFuncMap.find(code);
80 if (iter == handleFuncMap.end()) {
81 DHCP_LOGE("not find function to deal, code %{public}u", code);
82 (void)WriteInt32(reply, 0);
83 (void)WriteInt32(reply, DHCP_OPT_NOT_SUPPORTED);
84 } else {
85 (this->*(iter->second))(code, req, reply);
86 }
87 return DHCP_OPT_SUCCESS;
88 }
89
OnRegisterCallBack(uint32_t code,IpcIo * req,IpcIo * reply)90 int DhcpClientStub::OnRegisterCallBack(uint32_t code, IpcIo *req, IpcIo *reply)
91 {
92 DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
93 ErrCode ret = DHCP_E_FAILED;
94 SvcIdentity sid;
95 bool readSid = ReadRemoteObject(req, &sid);
96 if (!readSid) {
97 DHCP_LOGE("read SvcIdentity failed");
98 (void)WriteInt32(reply, 0);
99 (void)WriteInt32(reply, ret);
100 return DHCP_OPT_FAILED;
101 }
102
103 std::shared_ptr<IDhcpClientCallBack> callback_ = std::make_shared<DhcpClientCallbackProxy>(&sid);
104 DHCP_LOGI("create new DhcpClientCallbackProxy!");
105
106 size_t readLen;
107 std::string ifname = (char *)ReadString(req, &readLen);
108 DHCP_LOGI("ifname:%{public}s", ifname.c_str());
109
110 ret = RegisterDhcpClientCallBack(ifname, callback_);
111 (void)WriteInt32(reply, 0);
112 (void)WriteInt32(reply, ret);
113 return DHCP_OPT_SUCCESS;
114 }
115
OnStartDhcpClient(uint32_t code,IpcIo * req,IpcIo * reply)116 int DhcpClientStub::OnStartDhcpClient(uint32_t code, IpcIo *req, IpcIo *reply)
117 {
118 DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
119 ErrCode ret = DHCP_E_FAILED;
120 SvcIdentity sid;
121 bool readSid = ReadRemoteObject(req, &sid);
122 if (!readSid) {
123 DHCP_LOGE("read SvcIdentity failed");
124 (void)WriteInt32(reply, 0);
125 (void)WriteInt32(reply, ret);
126 return DHCP_OPT_FAILED;
127 }
128
129 size_t readLen;
130 RouterConfig config;
131 bool bIpv6;
132 std::string ifname = (char *)ReadString(req, &readLen);
133 std::string bssid = (char *)ReadString(req, &readLen);
134 (void)ReadBool(req, &config.prohibitUseCacheIp);
135 (void)ReadBool(req, &config.bIpv6);
136 (void)ReadBool(req, &config.bSpecificNetwork);
137 (void)ReadBool(req, &config.isStaticIpv4);
138 (void)ReadBool(req, &config.bIpv4);
139 DHCP_LOGI("ifname:%{public}s prohibitUseCacheIp:%{public}d, bIpv6:%{public}d, bSpecificNetwork:%{public}d",
140 ifname.c_str(), config.prohibitUseCacheIp, config.bIpv6, config.bSpecificNetwork);
141
142 ret = StartDhcpClient(config);
143 (void)WriteInt32(reply, 0);
144 (void)WriteInt32(reply, ret);
145 return DHCP_OPT_SUCCESS;
146 }
147
OnStopDhcpClient(uint32_t code,IpcIo * req,IpcIo * reply)148 int DhcpClientStub::OnStopDhcpClient(uint32_t code, IpcIo *req, IpcIo *reply)
149 {
150 DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
151 ErrCode ret = DHCP_E_FAILED;
152 SvcIdentity sid;
153 bool readSid = ReadRemoteObject(req, &sid);
154 if (!readSid) {
155 DHCP_LOGE("read SvcIdentity failed");
156 (void)WriteInt32(reply, 0);
157 (void)WriteInt32(reply, ret);
158 return DHCP_OPT_FAILED;
159 }
160
161 size_t readLen;
162 bool bIpv6;
163 std::string ifname = (char *)ReadString(req, &readLen);
164 (void)ReadBool(req, &bIpv6);
165 bool bIpv4 = true;
166 (void)ReadBool(req, &bIpv4);
167 DHCP_LOGI("ifname:%{public}s bIpv6:%{public}d bIpv4:%{public}d", ifname.c_str(), bIpv6, bIpv4);
168
169 ret = StopDhcpClient(ifname, bIpv6, bIpv4);
170 (void)WriteInt32(reply, 0);
171 (void)WriteInt32(reply, ret);
172 return 0;
173 }
174
OnDealWifiDhcpCache(uint32_t code,IpcIo * req,IpcIo * reply)175 int DhcpClientStub::OnDealWifiDhcpCache(uint32_t code, IpcIo *req, IpcIo *reply)
176 {
177 DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
178 ErrCode ret = DHCP_E_FAILED;
179 SvcIdentity sid;
180 bool readSid = ReadRemoteObject(req, &sid);
181 if (!readSid) {
182 DHCP_LOGE("read SvcIdentity failed");
183 (void)WriteInt32(reply, 0);
184 (void)WriteInt32(reply, ret);
185 return DHCP_OPT_FAILED;
186 }
187 int32_t cmd = 0;
188 (void)ReadInt32(req, &cmd);
189 size_t readLen;
190 IpCacheInfo ipCacheInfo;
191 ipCacheInfo.ssid = (char *)ReadString(req, &readLen);
192 ipCacheInfo.bssid = (char *)ReadString(req, &readLen);
193 ret = DealWifiDhcpCache(cmd, ipCacheInfo);
194 (void)WriteInt32(reply, 0);
195 (void)WriteInt32(reply, ret);
196 return 0;
197 }
198 #endif
199 }
200 }
201