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