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_SET_CONFIG)] =
49 &DhcpClientStub::OnSetConfiguration;
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 bool bIpv6;
131 std::string ifname = (char *)ReadString(req, &readLen);
132 (void)ReadBool(req, &bIpv6);
133 DHCP_LOGI("ifname:%{public}s bIpv6:%{public}d", ifname.c_str(), bIpv6);
134
135 ret = StartDhcpClient(ifname, bIpv6);
136 (void)WriteInt32(reply, 0);
137 (void)WriteInt32(reply, ret);
138 return DHCP_OPT_SUCCESS;
139 }
140
OnSetConfiguration(uint32_t code,IpcIo * req,IpcIo * reply)141 int DhcpClientStub::OnSetConfiguration(uint32_t code, IpcIo *req, IpcIo *reply)
142 {
143 DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
144 ErrCode ret = DHCP_E_FAILED;
145 SvcIdentity sid;
146 bool readSid = ReadRemoteObject(req, &sid);
147 if (!readSid) {
148 DHCP_LOGE("read SvcIdentity failed");
149 (void)WriteInt32(reply, 0);
150 (void)WriteInt32(reply, ret);
151 return DHCP_OPT_FAILED;
152 }
153
154 size_t readLen;
155 std::string ifname = (char *)ReadString(req, &readLen);
156 RouterConfig config;
157 config.bssid = (char *)ReadString(req, &readLen);
158 DHCP_LOGI("ifname:%{public}s", ifname.c_str());
159
160 ret = SetConfiguration(ifname, config);
161 (void)WriteInt32(reply, 0);
162 (void)WriteInt32(reply, ret);
163 return DHCP_OPT_SUCCESS;
164 }
OnStopDhcpClient(uint32_t code,IpcIo * req,IpcIo * reply)165 int DhcpClientStub::OnStopDhcpClient(uint32_t code, IpcIo *req, IpcIo *reply)
166 {
167 DHCP_LOGI("run %{public}s code %{public}u", __func__, code);
168 ErrCode ret = DHCP_E_FAILED;
169 SvcIdentity sid;
170 bool readSid = ReadRemoteObject(req, &sid);
171 if (!readSid) {
172 DHCP_LOGE("read SvcIdentity failed");
173 (void)WriteInt32(reply, 0);
174 (void)WriteInt32(reply, ret);
175 return DHCP_OPT_FAILED;
176 }
177
178 size_t readLen;
179 bool bIpv6;
180 std::string ifname = (char *)ReadString(req, &readLen);
181 (void)ReadBool(req, &bIpv6);
182 DHCP_LOGI("ifname:%{public}s bIpv6:%{public}d", ifname.c_str(), bIpv6);
183
184 ret = StopDhcpClient(ifname, bIpv6);
185 (void)WriteInt32(reply, 0);
186 (void)WriteInt32(reply, ret);
187 return 0;
188 }
189 #endif
190 }
191 }
192