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_server_callback_proxy.h"
16 #include "dhcp_server_stub.h"
17 #include "dhcp_logger.h"
18 #include "dhcp_manager_service_ipc_interface_code.h"
19 #include "dhcp_server_death_recipient.h"
20
21 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServerStub");
22
23 namespace OHOS {
24 namespace DHCP {
DhcpServerStub()25 DhcpServerStub::DhcpServerStub() : mSingleCallback(false), callback_(nullptr)
26 {
27 InitHandleMap();
28 }
29
~DhcpServerStub()30 DhcpServerStub::~DhcpServerStub()
31 {}
32
InitHandleMap()33 void DhcpServerStub::InitHandleMap()
34 {
35 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REG_CALL_BACK)] =
36 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
37 return OnRegisterCallBack(code, data, reply, option);
38 };
39 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_START_DHCP_SERVER)] =
40 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
41 return OnStartDhcpServer(code, data, reply, option);
42 };
43 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_STOP_DHCP_SERVER)] =
44 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
45 return OnStopDhcpServer(code, data, reply, option);
46 };
47 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_RANGE)] =
48 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
49 return OnSetDhcpRange(code, data, reply, option);
50 };
51 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_NAME)] =
52 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
53 return OnSetDhcpName(code, data, reply, option);
54 };
55 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_PUT_DHCP_RANGE)] =
56 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
57 return OnPutDhcpRange(code, data, reply, option);
58 };
59 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_ALL_DHCP_RANGE)] =
60 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
61 return OnRemoveAllDhcpRange(code, data, reply, option);
62 };
63 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_DHCP_RANGE)] =
64 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
65 return OnRemoveDhcpRange(code, data, reply, option);
66 };
67 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_UPDATE_RENEW_TIME)] =
68 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
69 return OnUpdateLeasesTime(code, data, reply, option);
70 };
71 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_GET_DHCP_CLIENT_INFO)] =
72 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
73 return OnGetDhcpClientInfos(code, data, reply, option);
74 };
75 handleFuncMap[static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_STOP_SA)] =
76 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
77 return OnStopServerSa(code, data, reply, option);
78 };
79 }
80
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)81 int DhcpServerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
82 {
83 DHCP_LOGI("DhcpServerStub::OnRemoteRequest,code:%{public}u", code);
84 if (data.ReadInterfaceToken() != GetDescriptor()) {
85 DHCP_LOGE("dhcp server stub token verification error: %{public}d", code);
86 return DHCP_OPT_FAILED;
87 }
88 HandleFuncMap::iterator iter = handleFuncMap.find(code);
89 if (iter == handleFuncMap.end()) {
90 DHCP_LOGI("not find function to deal, code %{public}u", code);
91 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
92 } else {
93 int exception = data.ReadInt32();
94 DHCP_LOGI("server rec, exception:%{public}d", exception);
95 if (exception) {
96 return DHCP_OPT_FAILED;
97 }
98 return (iter->second)(code, data, reply, option);
99 }
100 }
101
IsSingleCallback() const102 bool DhcpServerStub::IsSingleCallback() const
103 {
104 return mSingleCallback;
105 }
106
SetSingleCallback(const bool isSingleCallback)107 void DhcpServerStub::SetSingleCallback(const bool isSingleCallback)
108 {
109 mSingleCallback = true;
110 }
111
OnRegisterCallBack(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)112 int DhcpServerStub::OnRegisterCallBack(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
113 {
114 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
115 sptr<IRemoteObject> remote = data.ReadRemoteObject();
116 if (remote == nullptr) {
117 DHCP_LOGE("Failed to ReadRemoteObject!");
118 return DHCP_OPT_FAILED;
119 }
120 callback_ = iface_cast<IDhcpServerCallBack>(remote);
121 if (callback_ == nullptr) {
122 callback_ = new (std::nothrow) DhcpServerCallbackProxy(remote);
123 DHCP_LOGI("create new DhcpServerCallbackProxy!");
124 }
125 DHCP_LOGI("send OnStartDhcpClient data start");
126 int pid = data.ReadInt32();
127 int tokenId = data.ReadInt32();
128 std::string ifName = data.ReadString();
129 DHCP_LOGD("%{public}s, get pid: %{public}d, tokenId: %{private}d", __func__, pid, tokenId);
130
131 ErrCode ret = RegisterDhcpServerCallBack(ifName, callback_);
132 reply.WriteInt32(0);
133 reply.WriteInt32(ret);
134 return 0;
135 }
136
OnStartDhcpServer(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)137 int DhcpServerStub::OnStartDhcpServer(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
138 {
139 DHCP_LOGI("OnStartDhcpServer\n");
140 int pid = data.ReadInt32();
141 int tokenId = data.ReadInt32();
142 std::string ifName = data.ReadString();
143 DHCP_LOGD("%{public}s, get pid: %{public}d, tokenId: %{private}d", __func__, pid, tokenId);
144
145 ErrCode ret = StartDhcpServer(ifName);
146 reply.WriteInt32(0);
147 reply.WriteInt32(ret);
148
149 return 0;
150 }
151
OnStopDhcpServer(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)152 int DhcpServerStub::OnStopDhcpServer(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
153 {
154 DHCP_LOGI("OnStopDhcpServer\n");
155 std::string ifName = data.ReadString();
156 ErrCode ret = StopDhcpServer(ifName);
157 reply.WriteInt32(0);
158 reply.WriteInt32(ret);
159
160 return 0;
161 }
162
OnStopServerSa(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)163 int DhcpServerStub::OnStopServerSa(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
164 {
165 DHCP_LOGI("OnStopServerSa");
166 ErrCode ret = StopServerSa();
167 reply.WriteInt32(0);
168 reply.WriteInt32(ret);
169 return 0;
170 }
171
OnSetDhcpName(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)172 int DhcpServerStub::OnSetDhcpName(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
173 {
174 std::string tagName = data.ReadString();
175 std::string ifname = data.ReadString();
176 DHCP_LOGI("OnSetDhcpName ifname = %{public}s\n", ifname.c_str());
177 ErrCode ret = SetDhcpName(ifname, tagName);
178 reply.WriteInt32(0);
179 reply.WriteInt32(ret);
180
181 return 0;
182 }
OnSetDhcpRange(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)183 int DhcpServerStub::OnSetDhcpRange(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
184 {
185 DHCP_LOGI("OnSetDhcpRange\n");
186 DhcpRange range;
187 range.iptype = data.ReadInt32();
188 range.leaseHours = data.ReadInt32();
189 range.strTagName = data.ReadString();
190 range.strStartip = data.ReadString();
191 range.strEndip = data.ReadString();
192 range.strSubnet = data.ReadString();
193 std::string ifname = data.ReadString();
194 ErrCode ret = SetDhcpRange(ifname, range);
195 reply.WriteInt32(0);
196 reply.WriteInt32(ret);
197
198 return 0;
199 }
OnRemoveAllDhcpRange(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)200 int DhcpServerStub::OnRemoveAllDhcpRange(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
201 {
202 DHCP_LOGI("OnRemoveAllDhcpRange\n");
203 std::string tagName = data.ReadString();
204 ErrCode ret = RemoveAllDhcpRange(tagName);
205 reply.WriteInt32(0);
206 reply.WriteInt32(ret);
207
208 return 0;
209 }
OnRemoveDhcpRange(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)210 int DhcpServerStub::OnRemoveDhcpRange(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
211 {
212 DHCP_LOGI("OnRemoveDhcpRange\n");
213 DhcpRange range;
214 range.iptype = data.ReadInt32();
215 range.leaseHours = data.ReadInt32();
216 range.strTagName = data.ReadString();
217 range.strStartip = data.ReadString();
218 range.strEndip = data.ReadString();
219 range.strSubnet = data.ReadString();
220 std::string tagName = data.ReadString();
221 ErrCode ret = RemoveDhcpRange(tagName, range);
222 reply.WriteInt32(0);
223 reply.WriteInt32(ret);
224
225 return 0;
226 }
OnGetDhcpClientInfos(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)227 int DhcpServerStub::OnGetDhcpClientInfos(uint32_t code, MessageParcel &data, MessageParcel &reply,
228 MessageOption &option)
229 {
230 DHCP_LOGI("OnGetDhcpClientInfos\n");
231 std::vector<std::string> leases;
232 std::string ifname = data.ReadString();
233 ErrCode ret = GetDhcpClientInfos(ifname, leases);
234 reply.WriteInt32(0);
235 reply.WriteInt32(ret);
236 if (ret != DHCP_E_SUCCESS) {
237 return ret;
238 }
239 int size = leases.size();
240 DHCP_LOGI("OnGetDhcpClientInfos, reply message size:%{public}d\n", size);
241 reply.WriteInt32(size);
242 for (auto str : leases) {
243 reply.WriteString(str);
244 }
245
246 return 0;
247 }
248
OnUpdateLeasesTime(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)249 int DhcpServerStub::OnUpdateLeasesTime(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
250 {
251 DHCP_LOGI("OnUpdateLeasesTime\n");
252 std::string leaseTime = data.ReadString();
253 ErrCode ret = UpdateLeasesTime(leaseTime);
254 reply.WriteInt32(0);
255 reply.WriteInt32(ret);
256
257 return 0;
258 }
259
OnPutDhcpRange(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)260 int DhcpServerStub::OnPutDhcpRange(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
261 {
262 DHCP_LOGI("OnPutDhcpRange\n");
263 DhcpRange range;
264 range.iptype = data.ReadInt32();
265 range.leaseHours = data.ReadInt32();
266 range.strTagName = data.ReadString();
267 range.strStartip = data.ReadString();
268 range.strEndip = data.ReadString();
269 range.strSubnet = data.ReadString();
270 std::string tagName = data.ReadString();
271 ErrCode ret = PutDhcpRange(tagName, range);
272 reply.WriteInt32(0);
273 reply.WriteInt32(ret);
274
275 return 0;
276 }
277 }
278 }