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 #include "dhcp_server_proxy.h"
16 #include "dhcp_manager_service_ipc_interface_code.h"
17 #include "dhcp_server_callback_stub.h"
18 #include "dhcp_c_utils.h"
19 #include "dhcp_errcode.h"
20 #include "dhcp_logger.h"
21 #include "dhcp_sdk_define.h"
22
23 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServerProxyLite");
24
25 namespace OHOS {
26 namespace DHCP {
27
28 static SvcIdentity g_sid;
29 static IpcObjectStub g_objStub;
30 static DhcpServreCallBackStub g_dhcpServerCallBackStub;
31
ParseDhcpClientInfos(IpcIo * reply,std::vector<std::string> & infos)32 static ErrCode ParseDhcpClientInfos(IpcIo *reply, std::vector<std::string> &infos)
33 {
34 int tmpsize = 0;
35 (void)ReadInt32(reply, &tmpsize);
36 unsigned int readLen;
37 for (int i = 0; i < tmpsize; ++i) {
38 std::string str = (char *)ReadString(reply, &readLen);
39 infos.emplace_back(str);
40 }
41 return DHCP_E_SUCCESS;
42 }
43
IpcCallback(void * owner,int code,IpcIo * reply)44 static int IpcCallback(void *owner, int code, IpcIo *reply)
45 {
46 if (code != 0 || owner == nullptr || reply == nullptr) {
47 DHCP_LOGE("Callback error, code:%{public}d, owner:%{public}d, reply:%{public}d",
48 code, owner == nullptr, reply == nullptr);
49 return DHCP_E_FAILED;
50 }
51
52 struct IpcOwner *data = (struct IpcOwner *)owner;
53 (void)ReadInt32(reply, &data->exception);
54 (void)ReadInt32(reply, &data->retCode);
55 if (data->exception != 0 || data->retCode != DHCP_E_SUCCESS || data->variable == nullptr) {
56 return DHCP_E_FAILED;
57 }
58
59 switch (data->funcId) {
60 case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_GET_DHCP_CLIENT_INFO): {
61 data->retCode = ParseDhcpClientInfos(reply, *((std::vector<std::string> *)data->variable));
62 break;
63 }
64 default:
65 break;
66 }
67 return DHCP_E_SUCCESS;
68 }
69
AsyncCallback(uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option)70 static int AsyncCallback(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
71 {
72 if (data == nullptr) {
73 DHCP_LOGE("AsyncCallback error, data is null");
74 return DHCP_E_FAILED;
75 }
76 return g_dhcpServerCallBackStub.OnRemoteRequest(code, data);
77 }
78
OnRemoteSrvDied(void * arg)79 static void OnRemoteSrvDied(void *arg)
80 {
81 DHCP_LOGE("%{public}s called.", __func__);
82 DhcpServerProxy *client = DhcpServerProxy::GetInstance();
83 if (client != nullptr) {
84 client->OnRemoteDied();
85 }
86 return;
87 }
88
89 DhcpServerProxy *DhcpServerProxy::g_instance = nullptr;
DhcpServerProxy()90 DhcpServerProxy::DhcpServerProxy() : remote_(nullptr), remoteDied_(false)
91 {}
92
~DhcpServerProxy()93 DhcpServerProxy::~DhcpServerProxy()
94 {}
95
GetInstance(void)96 DhcpServerProxy *DhcpServerProxy::GetInstance(void)
97 {
98 if (g_instance != nullptr) {
99 return g_instance;
100 }
101
102 DhcpServerProxy *tempInstance = new(std::nothrow) DhcpServerProxy();
103 g_instance = tempInstance;
104 return g_instance;
105 }
106
ReleaseInstance(void)107 void DhcpServerProxy::ReleaseInstance(void)
108 {
109 if (g_instance != nullptr) {
110 delete g_instance;
111 g_instance = nullptr;
112 }
113 }
114
Init(void)115 ErrCode DhcpServerProxy::Init(void)
116 {
117 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(DHCP_SERVICE_LITE, DHCP_FEATRUE_SERVER);
118 if (iUnknown == nullptr) {
119 DHCP_LOGE("GetFeatureApi failed.");
120 return DHCP_E_FAILED;
121 }
122 IClientProxy *proxy = nullptr;
123 int result = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, reinterpret_cast<void **>(&proxy));
124 if (result != 0) {
125 DHCP_LOGE("QueryInterface failed.");
126 return DHCP_E_FAILED;
127 }
128 remote_ = proxy;
129
130 // Register SA Death Callback
131 uint32_t deadId = 0;
132 svcIdentity_ = SAMGR_GetRemoteIdentity(DHCP_SERVICE_LITE, DHCP_FEATRUE_SERVER);
133 result = AddDeathRecipient(svcIdentity_, OnRemoteSrvDied, nullptr, &deadId);
134 if (result != 0) {
135 DHCP_LOGE("Register SA Death Callback failed, errorCode[%d]", result);
136 }
137 return DHCP_E_SUCCESS;
138 }
139
IsRemoteDied(void)140 bool DhcpServerProxy::IsRemoteDied(void)
141 {
142 if (remoteDied_) {
143 DHCP_LOGI("IsRemoteDied! remote is died now!");
144 }
145 return remoteDied_;
146 }
147
OnRemoteDied(void)148 void WifiScanProxy::OnRemoteDied(void)
149 {
150 DHCP_LOGW("Remote service is died!");
151 remoteDied_ = true;
152 g_dhcpServerCallBackStub.SetRemoteDied(true);
153 }
154
RegisterDhcpServerCallBack(const std::string & ifname,const std::shared_ptr<IDhcpServerCallBack> & callback)155 ErrCode DhcpServerProxy::RegisterDhcpServerCallBack(const std::string& ifname,
156 const std::shared_ptr<IDhcpServerCallBack> &callback)
157 {
158 if (remoteDied_ || remote_ == nullptr) {
159 DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
160 __func__, remoteDied_, remote_ == nullptr);
161 return DHCP_E_FAILED;
162 }
163 DHCP_LOGD("RegisterCallBack start!");
164 g_objStub.func = AsyncCallback;
165 g_objStub.args = nullptr;
166 g_objStub.isRemote = false;
167
168 g_sid.handle = IPC_INVALID_HANDLE;
169 g_sid.token = SERVICE_TYPE_ANONYMOUS;
170 g_sid.cookie = (uintptr_t)&g_objStub;
171
172 IpcIo request;
173 char data[IPC_DATA_SIZE_SMALL];
174 struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
175
176 IpcIoInit(&request, data, IPC_DATA_SIZE_SMALL, MAX_IPC_OBJ_COUNT);
177 if (!WriteInterfaceToken(&request, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
178 DHCP_LOGE("Write interface token error: %{public}s", __func__);
179 return DHCP_E_FAILED;
180 }
181 (void)WriteInt32(&request, 0);
182 (void)WriteRemoteObject(&request, &g_sid);
183 (void)WriteString(&request, ifname.c_str());
184 owner.funcId = static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REG_CALL_BACK);
185 int error = remote_->Invoke(remote_,
186 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REG_CALL_BACK),
187 &request, &owner, IpcCallback);
188 if (error != DHCP_E_SUCCESS) {
189 DHCP_LOGE("RegisterCallBack failed, error code is %{public}d", error);
190 return DHCP_E_FAILED;
191 }
192 DHCP_LOGD("RegisterCallBack is finished: result=%{public}d", owner.exception);
193 if (owner.exception) {
194 return DHCP_E_FAILED;
195 }
196 g_dhcpServerCallBackStub.RegisterCallBack(callback);
197 return DHCP_E_SUCCESS;
198 }
199
StartDhcpServer(const std::string & ifname)200 ErrCode DhcpServerProxy::StartDhcpServer(const std::string& ifname)
201 {
202 DHCP_LOGI("DhcpServerProxy enter StartDhcpServer mRemoteDied:%{public}d", mRemoteDied);
203 if (remoteDied_ || remote_ == nullptr) {
204 DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
205 __func__, remoteDied_, remote_ == nullptr);
206 return DHCP_E_FAILED;
207 }
208
209 IpcIo request;
210 char data[IPC_DATA_SIZE_MID];
211 struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
212
213 IpcIoInit(&request, data, IPC_DATA_SIZE_MID, MAX_IPC_OBJ_COUNT);
214 if (!WriteInterfaceToken(&request, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
215 DHCP_LOGE("Write interface token error: %{public}s", __func__);
216 return DHCP_E_FAILED;
217 }
218 (void)WriteInt32(&request, 0);
219 (void)WriteString(&request, ifname.c_str());
220
221 owner.funcId = static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_START_DHCP_SERVER);
222 int error = remote_->Invoke(remote_,
223 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_START_DHCP_SERVER),
224 &request, &owner, IpcCallback);
225 if (error != DHCP_E_SUCCESS) {
226 DHCP_LOGW("Set Attr(%{public}d) failed",
227 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_START_DHCP_SERVER));
228 return DHCP_E_FAILED;
229 }
230
231 if (owner.exception) {
232 return DHCP_E_FAILED;
233 }
234
235 return ErrCode(owner.retCode);
236 }
237
StopDhcpServer(const std::string & ifname)238 ErrCode DhcpServerProxy::StopDhcpServer(const std::string& ifname)
239 {
240 DHCP_LOGI("DhcpServerProxy enter StopDhcpServer mRemoteDied:%{public}d", mRemoteDied);
241 if (remoteDied_ || remote_ == nullptr) {
242 DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
243 __func__, remoteDied_, remote_ == nullptr);
244 return DHCP_E_FAILED;
245 }
246
247 IpcIo request;
248 char data[IPC_DATA_SIZE_MID];
249 struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
250
251 IpcIoInit(&request, data, IPC_DATA_SIZE_MID, MAX_IPC_OBJ_COUNT);
252 if (!WriteInterfaceToken(&request, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
253 DHCP_LOGE("Write interface token error: %{public}s", __func__);
254 return DHCP_E_FAILED;
255 }
256 (void)WriteInt32(&request, 0);
257 (void)WriteString(&request, ifname.c_str());
258
259 owner.funcId = static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_STOP_DHCP_SERVER);
260 int error = remote_->Invoke(remote_,
261 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_STOP_DHCP_SERVER),
262 &request, &owner, IpcCallback);
263 if (error != DHCP_E_SUCCESS) {
264 DHCP_LOGW("Set Attr(%{public}d) failed",
265 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_STOP_DHCP_SERVER));
266 return DHCP_E_FAILED;
267 }
268
269 if (owner.exception) {
270 return DHCP_E_FAILED;
271 }
272
273 return ErrCode(owner.retCode);
274 }
275
SetDhcpRange(const std::string & ifname,const DhcpRange & range)276 ErrCode DhcpServerProxy::SetDhcpRange(const std::string& ifname, const DhcpRange& range)
277 {
278 DHCP_LOGI("DhcpServerProxy enter SetDhcpRange mRemoteDied:%{public}d", mRemoteDied);
279 DHCP_LOGI("DhcpServerProxy enter StopDhcpServer mRemoteDied:%{public}d", mRemoteDied);
280 if (remoteDied_ || remote_ == nullptr) {
281 DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
282 __func__, remoteDied_, remote_ == nullptr);
283 return DHCP_E_FAILED;
284 }
285
286 IpcIo request;
287 char data[IPC_DATA_SIZE_MID];
288 struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
289
290 IpcIoInit(&request, data, IPC_DATA_SIZE_MID, MAX_IPC_OBJ_COUNT);
291 if (!WriteInterfaceToken(&request, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
292 DHCP_LOGE("Write interface token error: %{public}s", __func__);
293 return DHCP_E_FAILED;
294 }
295 (void)WriteInt32(&request, 0);
296 (void)WriteInt32(&request, range.iptype);
297 (void)WriteInt32(&request, range.leaseHours);
298 (void)WriteString(&request, range.strTagName.c_str());
299 (void)WriteString(&request, range.strStartip.c_str());
300 (void)WriteString(&request, range.strEndip.c_str());
301 (void)WriteString(&request, range.strSubnet.c_str());
302 (void)WriteString(&request, ifname.c_str());
303
304 owner.funcId = static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_RANGE);
305 int error = remote_->Invoke(remote_,
306 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_RANGE),
307 &request, &owner, IpcCallback);
308 if (error != DHCP_E_SUCCESS) {
309 DHCP_LOGW("Set Attr(%{public}d) failed",
310 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_RANGE));
311 return DHCP_E_FAILED;
312 }
313
314 if (owner.exception) {
315 return DHCP_E_FAILED;
316 }
317
318 return ErrCode(owner.retCode);
319 }
320
SetDhcpName(const std::string & ifname,const std::string & tagName)321 ErrCode DhcpServerProxy::SetDhcpName(const std::string& ifname, const std::string& tagName)
322 {
323
324 DHCP_LOGI("DhcpServerProxy enter SetDhcpName mRemoteDied:%{public}d", mRemoteDied);
325 if (remoteDied_ || remote_ == nullptr) {
326 DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
327 __func__, remoteDied_, remote_ == nullptr);
328 return DHCP_E_FAILED;
329 }
330
331 IpcIo request;
332 char data[IPC_DATA_SIZE_MID];
333 struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
334
335 IpcIoInit(&request, data, IPC_DATA_SIZE_MID, MAX_IPC_OBJ_COUNT);
336 if (!WriteInterfaceToken(&request, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
337 DHCP_LOGE("Write interface token error: %{public}s", __func__);
338 return DHCP_E_FAILED;
339 }
340 (void)WriteInt32(&request, 0);
341 (void)WriteString(&request, tagName.c_str());
342 (void)WriteString(&request, ifname.c_str());
343
344 owner.funcId = static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_NAME);
345 int error = remote_->Invoke(remote_,
346 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_NAME),
347 &request, &owner, IpcCallback);
348 if (error != DHCP_E_SUCCESS) {
349 DHCP_LOGW("Set Attr(%{public}d) failed",
350 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_NAME));
351 return DHCP_E_FAILED;
352 }
353
354 if (owner.exception) {
355 return DHCP_E_FAILED;
356 }
357
358 return ErrCode(owner.retCode);
359 }
360
PutDhcpRange(const std::string & tagName,const DhcpRange & range)361 ErrCode DhcpServerProxy::PutDhcpRange(const std::string& tagName, const DhcpRange& range)
362 {
363 DHCP_LOGI("DhcpServerProxy enter PutDhcpRange mRemoteDied:%{public}d", mRemoteDied);
364 if (remoteDied_ || remote_ == nullptr) {
365 DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
366 __func__, remoteDied_, remote_ == nullptr);
367 return DHCP_E_FAILED;
368 }
369
370 IpcIo request;
371 char data[IPC_DATA_SIZE_MID];
372 struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
373
374 IpcIoInit(&request, data, IPC_DATA_SIZE_MID, MAX_IPC_OBJ_COUNT);
375 if (!WriteInterfaceToken(&request, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
376 DHCP_LOGE("Write interface token error: %{public}s", __func__);
377 return DHCP_E_FAILED;
378 }
379 (void)WriteInt32(&request, 0);
380 (void)WriteInt32(&request, range.iptype);
381 (void)WriteInt32(&request, range.leaseHours);
382 (void)WriteString(&request, range.strTagName.c_str());
383 (void)WriteString(&request, range.strStartip.c_str());
384 (void)WriteString(&request, range.strEndip.c_str());
385 (void)WriteString(&request, range.strSubnet.c_str());
386 (void)WriteString(&request, tagName.c_str());
387
388 owner.funcId = static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_PUT_DHCP_RANGE);
389 int error = remote_->Invoke(remote_,
390 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_PUT_DHCP_RANGE),
391 &request, &owner, IpcCallback);
392 if (error != DHCP_E_SUCCESS) {
393 DHCP_LOGW("Set Attr(%{public}d) failed",
394 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_PUT_DHCP_RANGE));
395 return DHCP_E_FAILED;
396 }
397
398 if (owner.exception) {
399 return DHCP_E_FAILED;
400 }
401
402 return ErrCode(owner.retCode);
403 }
404
RemoveAllDhcpRange(const std::string & tagName)405 ErrCode DhcpServerProxy::RemoveAllDhcpRange(const std::string& tagName)
406 {
407 DHCP_LOGI("DhcpServerProxy enter RemoveAllDhcpRange mRemoteDied:%{public}d", mRemoteDied);
408 if (remoteDied_ || remote_ == nullptr) {
409 DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
410 __func__, remoteDied_, remote_ == nullptr);
411 return DHCP_E_FAILED;
412 }
413
414 IpcIo request;
415 char data[IPC_DATA_SIZE_MID];
416 struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
417
418 IpcIoInit(&request, data, IPC_DATA_SIZE_MID, MAX_IPC_OBJ_COUNT);
419 if (!WriteInterfaceToken(&request, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
420 DHCP_LOGE("Write interface token error: %{public}s", __func__);
421 return DHCP_E_FAILED;
422 }
423 (void)WriteInt32(&request, 0);
424 (void)WriteString(&request, tagName.c_str());
425
426 owner.funcId = static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_ALL_DHCP_RANGE);
427 int error = remote_->Invoke(remote_,
428 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_ALL_DHCP_RANGE),
429 &request, &owner, IpcCallback);
430 if (error != DHCP_E_SUCCESS) {
431 DHCP_LOGW("Set Attr(%{public}d) failed",
432 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_ALL_DHCP_RANGE));
433 return DHCP_E_FAILED;
434 }
435
436 if (owner.exception) {
437 return DHCP_E_FAILED;
438 }
439
440 return ErrCode(owner.retCode);
441 }
442
UpdateLeasesTime(const std::string & leaseTime)443 ErrCode DhcpServerProxy::UpdateLeasesTime(const std::string& leaseTime)
444 {
445 DHCP_LOGI("DhcpServerProxy enter UpdateLeasesTime mRemoteDied:%{public}d", mRemoteDied);
446 if (remoteDied_ || remote_ == nullptr) {
447 DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
448 __func__, remoteDied_, remote_ == nullptr);
449 return DHCP_E_FAILED;
450 }
451
452 IpcIo request;
453 char data[IPC_DATA_SIZE_MID];
454 struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
455
456 IpcIoInit(&request, data, IPC_DATA_SIZE_MID, MAX_IPC_OBJ_COUNT);
457 if (!WriteInterfaceToken(&request, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
458 DHCP_LOGE("Write interface token error: %{public}s", __func__);
459 return DHCP_E_FAILED;
460 }
461 (void)WriteInt32(&request, 0);
462 (void)WriteString(&request, leaseTime.c_str());
463
464 owner.funcId = static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_UPDATE_RENEW_TIME);
465 int error = remote_->Invoke(remote_,
466 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_UPDATE_RENEW_TIME),
467 &request, &owner, IpcCallback);
468 if (error != DHCP_E_SUCCESS) {
469 DHCP_LOGW("Set Attr(%{public}d) failed",
470 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_UPDATE_RENEW_TIME));
471 return DHCP_E_FAILED;
472 }
473
474 if (owner.exception) {
475 return DHCP_E_FAILED;
476 }
477
478 return ErrCode(owner.retCode);
479 }
480
RemoveDhcpRange(const std::string & tagName,const DhcpRange & range)481 ErrCode DhcpServerProxy::RemoveDhcpRange(const std::string& tagName, const DhcpRange& range)
482 {
483 DHCP_LOGI("DhcpServerProxy enter RemoveDhcpRange mRemoteDied:%{public}d", mRemoteDied);
484 if (remoteDied_ || remote_ == nullptr) {
485 DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
486 __func__, remoteDied_, remote_ == nullptr);
487 return DHCP_E_FAILED;
488 }
489
490 IpcIo request;
491 char data[IPC_DATA_SIZE_MID];
492 struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
493
494 IpcIoInit(&request, data, IPC_DATA_SIZE_MID, MAX_IPC_OBJ_COUNT);
495 if (!WriteInterfaceToken(&request, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
496 DHCP_LOGE("Write interface token error: %{public}s", __func__);
497 return DHCP_E_FAILED;
498 }
499 (void)WriteInt32(&request, 0);
500 (void)WriteInt32(&request, range.iptype);
501 (void)WriteInt32(&request, range.leaseHours);
502 (void)WriteString(&request, range.strTagName.c_str());
503 (void)WriteString(&request, range.strStartip.c_str());
504 (void)WriteString(&request, range.strEndip.c_str());
505 (void)WriteString(&request, range.strSubnet.c_str());
506 (void)WriteString(&request, tagName.c_str());
507
508 owner.funcId = static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_DHCP_RANGE);
509 int error = remote_->Invoke(remote_,
510 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_DHCP_RANGE),
511 &request, &owner, IpcCallback);
512 if (error != DHCP_E_SUCCESS) {
513 DHCP_LOGW("Set Attr(%{public}d) failed",
514 static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_DHCP_RANGE));
515 return DHCP_E_FAILED;
516 }
517
518 if (owner.exception) {
519 return DHCP_E_FAILED;
520 }
521
522 return ErrCode(owner.retCode);
523 }
524
GetDhcpClientInfos(const std::string & ifname,std::vector<std::string> & dhcpClientInfo)525 ErrCode DhcpServerProxy::GetDhcpClientInfos(const std::string& ifname, std::vector<std::string>& dhcpClientInfo)
526 {
527 DHCP_LOGI("DhcpServerProxy enter GetDhcpClientInfos mRemoteDied:%{public}d", mRemoteDied);
528 if (remoteDied_ || remote_ == nullptr) {
529 DHCP_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
530 __func__, remoteDied_, remote_ == nullptr);
531 return DHCP_E_FAILED;
532 }
533
534 IpcIo request;
535 char data[IPC_DATA_SIZE_SMALL];
536 struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
537
538 IpcIoInit(&request, data, IPC_DATA_SIZE_SMALL, MAX_IPC_OBJ_COUNT);
539 if (!WriteInterfaceToken(&request, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
540 DHCP_LOGE("Write interface token error: %{public}s", __func__);
541 return DHCP_E_FAILED;
542 }
543 (void)WriteInt32(&request, 0);
544 WriteString(&request, ifname.c_str());
545 owner.variable = &dhcpClientInfo;
546 owner.funcId = static_cast<int32_t>(ScanInterfaceCode::DHCP_SERVER_SVR_CMD_GET_DHCP_CLIENT_INFO);
547 int error = remote_->Invoke(remote_,
548 static_cast<int32_t>(ScanInterfaceCode::DHCP_SERVER_SVR_CMD_GET_DHCP_CLIENT_INFO),
549 &request, &owner, IpcCallback);
550 if (error != DHCP_E_SUCCESS) {
551 DHCP_LOGW("Set Attr(%{public}d) failed",
552 static_cast<int32_t>(ScanInterfaceCode::DHCP_SERVER_SVR_CMD_GET_DHCP_CLIENT_INFO));
553 return DHCP_E_FAILED;
554 }
555
556 if (owner.exception) {
557 return DHCP_E_FAILED;
558 }
559 return ErrCode(owner.retCode);
560 }
561 } // namespace DHCP
562 } // namespace OHOS