1518678f8Sopenharmony_ci/*
2518678f8Sopenharmony_ci * Copyright (C) 2021-2023 Huawei Device Co., Ltd.
3518678f8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4518678f8Sopenharmony_ci * you may not use this file except in compliance with the License.
5518678f8Sopenharmony_ci * You may obtain a copy of the License at
6518678f8Sopenharmony_ci *
7518678f8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8518678f8Sopenharmony_ci *
9518678f8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10518678f8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11518678f8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12518678f8Sopenharmony_ci * See the License for the specific language governing permissions and
13518678f8Sopenharmony_ci * limitations under the License.
14518678f8Sopenharmony_ci */
15518678f8Sopenharmony_ci#include "dhcp_server_proxy.h"
16518678f8Sopenharmony_ci#include "dhcp_manager_service_ipc_interface_code.h"
17518678f8Sopenharmony_ci#include "dhcp_server_callback_stub.h"
18518678f8Sopenharmony_ci#include "dhcp_c_utils.h"
19518678f8Sopenharmony_ci#include "dhcp_errcode.h"
20518678f8Sopenharmony_ci#include "dhcp_logger.h"
21518678f8Sopenharmony_ci
22518678f8Sopenharmony_ciDEFINE_DHCPLOG_DHCP_LABEL("DhcpServerProxy");
23518678f8Sopenharmony_ci
24518678f8Sopenharmony_cinamespace OHOS {
25518678f8Sopenharmony_cinamespace DHCP {
26518678f8Sopenharmony_ciconstexpr int MAX_SIZE = 512;
27518678f8Sopenharmony_ci
28518678f8Sopenharmony_cistatic sptr<DhcpServreCallBackStub> g_dhcpServerCallBackStub =
29518678f8Sopenharmony_ci    sptr<DhcpServreCallBackStub>(new (std::nothrow)DhcpServreCallBackStub());
30518678f8Sopenharmony_ci
31518678f8Sopenharmony_ciDhcpServerProxy::DhcpServerProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IDhcpServer>(impl),
32518678f8Sopenharmony_ci    remote_(nullptr), mRemoteDied(false), deathRecipient_(nullptr)
33518678f8Sopenharmony_ci{
34518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
35518678f8Sopenharmony_ci    if (impl) {
36518678f8Sopenharmony_ci        if (!impl->IsProxyObject()) {
37518678f8Sopenharmony_ci            DHCP_LOGW("not proxy object!");
38518678f8Sopenharmony_ci            return;
39518678f8Sopenharmony_ci        }
40518678f8Sopenharmony_ci        deathRecipient_ = new (std::nothrow)DhcpServerDeathRecipient(*this);
41518678f8Sopenharmony_ci        if (deathRecipient_ == nullptr) {
42518678f8Sopenharmony_ci            DHCP_LOGW("deathRecipient_ is nullptr!");
43518678f8Sopenharmony_ci        }
44518678f8Sopenharmony_ci        if (!impl->AddDeathRecipient(deathRecipient_)) {
45518678f8Sopenharmony_ci            DHCP_LOGW("AddDeathRecipient failed!");
46518678f8Sopenharmony_ci            return;
47518678f8Sopenharmony_ci        }
48518678f8Sopenharmony_ci        remote_ = impl;
49518678f8Sopenharmony_ci        DHCP_LOGI("AddDeathRecipient success! ");
50518678f8Sopenharmony_ci    }
51518678f8Sopenharmony_ci}
52518678f8Sopenharmony_ci
53518678f8Sopenharmony_ciDhcpServerProxy::~DhcpServerProxy()
54518678f8Sopenharmony_ci{
55518678f8Sopenharmony_ci    DHCP_LOGI("enter ~DhcpServerProxy!");
56518678f8Sopenharmony_ci    RemoveDeathRecipient();
57518678f8Sopenharmony_ci}
58518678f8Sopenharmony_ci
59518678f8Sopenharmony_civoid DhcpServerProxy::RemoveDeathRecipient(void)
60518678f8Sopenharmony_ci{
61518678f8Sopenharmony_ci    DHCP_LOGI("enter RemoveDeathRecipient!");
62518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
63518678f8Sopenharmony_ci    if (remote_ == nullptr) {
64518678f8Sopenharmony_ci        DHCP_LOGI("remote_ is nullptr!");
65518678f8Sopenharmony_ci        return;
66518678f8Sopenharmony_ci    }
67518678f8Sopenharmony_ci    if (deathRecipient_ == nullptr) {
68518678f8Sopenharmony_ci        DHCP_LOGI("deathRecipient_ is nullptr!");
69518678f8Sopenharmony_ci        return;
70518678f8Sopenharmony_ci    }
71518678f8Sopenharmony_ci    remote_->RemoveDeathRecipient(deathRecipient_);
72518678f8Sopenharmony_ci    remote_ = nullptr;
73518678f8Sopenharmony_ci}
74518678f8Sopenharmony_ci
75518678f8Sopenharmony_civoid DhcpServerProxy::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
76518678f8Sopenharmony_ci{
77518678f8Sopenharmony_ci    DHCP_LOGI("Remote service is died! remoteObject: %{private}p", &remoteObject);
78518678f8Sopenharmony_ci    mRemoteDied = true;
79518678f8Sopenharmony_ci    RemoveDeathRecipient();
80518678f8Sopenharmony_ci    if (g_dhcpServerCallBackStub == nullptr) {
81518678f8Sopenharmony_ci        DHCP_LOGE("g_dhcpServerCallBackStub is nullptr");
82518678f8Sopenharmony_ci        return;
83518678f8Sopenharmony_ci    }
84518678f8Sopenharmony_ci    if (g_dhcpServerCallBackStub != nullptr) {
85518678f8Sopenharmony_ci        g_dhcpServerCallBackStub->SetRemoteDied(true);
86518678f8Sopenharmony_ci    }
87518678f8Sopenharmony_ci}
88518678f8Sopenharmony_ci
89518678f8Sopenharmony_cibool DhcpServerProxy::IsRemoteDied(void)
90518678f8Sopenharmony_ci{
91518678f8Sopenharmony_ci    if (mRemoteDied) {
92518678f8Sopenharmony_ci        DHCP_LOGI("IsRemoteDied! remote is died now!");
93518678f8Sopenharmony_ci    }
94518678f8Sopenharmony_ci    return mRemoteDied;
95518678f8Sopenharmony_ci}
96518678f8Sopenharmony_ci
97518678f8Sopenharmony_ciErrCode DhcpServerProxy::RegisterDhcpServerCallBack(const std::string& ifname,
98518678f8Sopenharmony_ci    const sptr<IDhcpServerCallBack> &callback)
99518678f8Sopenharmony_ci{
100518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy enter RegisterDhcpServerCallBack mRemoteDied:%{public}d", mRemoteDied);
101518678f8Sopenharmony_ci    if (mRemoteDied) {
102518678f8Sopenharmony_ci        DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
103518678f8Sopenharmony_ci        return DHCP_E_FAILED;
104518678f8Sopenharmony_ci    }
105518678f8Sopenharmony_ci    MessageParcel data, reply;
106518678f8Sopenharmony_ci    MessageOption option;
107518678f8Sopenharmony_ci    if (!data.WriteInterfaceToken(GetDescriptor())) {
108518678f8Sopenharmony_ci        DHCP_LOGE("Write interface token error: %{public}s", __func__);
109518678f8Sopenharmony_ci        return DHCP_E_FAILED;
110518678f8Sopenharmony_ci    }
111518678f8Sopenharmony_ci    data.WriteInt32(0);
112518678f8Sopenharmony_ci    if (g_dhcpServerCallBackStub == nullptr) {
113518678f8Sopenharmony_ci        DHCP_LOGE("g_dhcpServerCallBackStub is nullptr");
114518678f8Sopenharmony_ci        return DHCP_E_FAILED;
115518678f8Sopenharmony_ci    }
116518678f8Sopenharmony_ci    g_dhcpServerCallBackStub->RegisterCallBack(callback);
117518678f8Sopenharmony_ci
118518678f8Sopenharmony_ci    if (!data.WriteRemoteObject(g_dhcpServerCallBackStub->AsObject())) {
119518678f8Sopenharmony_ci        DHCP_LOGE("DhcpServerProxy::RegisterCallBack WriteRemoteObject failed!");
120518678f8Sopenharmony_ci        return DHCP_E_FAILED;
121518678f8Sopenharmony_ci    }
122518678f8Sopenharmony_ci
123518678f8Sopenharmony_ci    int pid = GetCallingPid();
124518678f8Sopenharmony_ci    int tokenId = GetCallingTokenId();
125518678f8Sopenharmony_ci    data.WriteInt32(pid);
126518678f8Sopenharmony_ci    data.WriteInt32(tokenId);
127518678f8Sopenharmony_ci    data.WriteString(ifname);
128518678f8Sopenharmony_ci    DHCP_LOGI("%{public}s, calling uid:%{public}d, pid:%{public}d, ifname:%{public}s",
129518678f8Sopenharmony_ci        __func__, GetCallingUid(), pid, ifname.c_str());
130518678f8Sopenharmony_ci    int error = Remote()->SendRequest(static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REG_CALL_BACK),
131518678f8Sopenharmony_ci        data, reply, option);
132518678f8Sopenharmony_ci    if (error != ERR_NONE) {
133518678f8Sopenharmony_ci        DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
134518678f8Sopenharmony_ci            static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REG_CALL_BACK), error);
135518678f8Sopenharmony_ci        return DHCP_E_FAILED;
136518678f8Sopenharmony_ci    }
137518678f8Sopenharmony_ci    int exception = reply.ReadInt32();
138518678f8Sopenharmony_ci    if (exception) {
139518678f8Sopenharmony_ci        DHCP_LOGE("exception failed, exception:%{public}d", exception);
140518678f8Sopenharmony_ci        return DHCP_E_FAILED;
141518678f8Sopenharmony_ci    }
142518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy RegisterDhcpServerCallBack ok, exception:%{public}d", exception);
143518678f8Sopenharmony_ci    return DHCP_E_SUCCESS;
144518678f8Sopenharmony_ci}
145518678f8Sopenharmony_ci
146518678f8Sopenharmony_ciErrCode DhcpServerProxy::StartDhcpServer(const std::string& ifname)
147518678f8Sopenharmony_ci{
148518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy enter StartDhcpServer mRemoteDied:%{public}d", mRemoteDied);
149518678f8Sopenharmony_ci    if (mRemoteDied) {
150518678f8Sopenharmony_ci        DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
151518678f8Sopenharmony_ci        return DHCP_E_FAILED;
152518678f8Sopenharmony_ci    }
153518678f8Sopenharmony_ci    MessageParcel data, reply;
154518678f8Sopenharmony_ci    MessageOption option;
155518678f8Sopenharmony_ci    if (!data.WriteInterfaceToken(GetDescriptor())) {
156518678f8Sopenharmony_ci        DHCP_LOGE("Write interface token error: %{public}s", __func__);
157518678f8Sopenharmony_ci        return DHCP_E_FAILED;
158518678f8Sopenharmony_ci    }
159518678f8Sopenharmony_ci    data.WriteInt32(0);
160518678f8Sopenharmony_ci
161518678f8Sopenharmony_ci    int pid = GetCallingPid();
162518678f8Sopenharmony_ci    int tokenId = GetCallingTokenId();
163518678f8Sopenharmony_ci    data.WriteInt32(pid);
164518678f8Sopenharmony_ci    data.WriteInt32(tokenId);
165518678f8Sopenharmony_ci    data.WriteString(ifname);
166518678f8Sopenharmony_ci    DHCP_LOGI("%{public}s, calling uid:%{public}d, pid:%{public}d, ifname:%{public}s",
167518678f8Sopenharmony_ci        __func__, GetCallingUid(), pid, ifname.c_str());
168518678f8Sopenharmony_ci    int error = Remote()->SendRequest(
169518678f8Sopenharmony_ci        static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_START_DHCP_SERVER), data, reply, option);
170518678f8Sopenharmony_ci    if (error != ERR_NONE) {
171518678f8Sopenharmony_ci        DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
172518678f8Sopenharmony_ci            static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_START_DHCP_SERVER), error);
173518678f8Sopenharmony_ci        return DHCP_E_FAILED;
174518678f8Sopenharmony_ci    }
175518678f8Sopenharmony_ci    int exception = reply.ReadInt32();
176518678f8Sopenharmony_ci    if (exception) {
177518678f8Sopenharmony_ci        DHCP_LOGE("exception failed, exception:%{public}d", exception);
178518678f8Sopenharmony_ci        return DHCP_E_FAILED;
179518678f8Sopenharmony_ci    }
180518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy StartDhcpServer ok, exception:%{public}d", exception);
181518678f8Sopenharmony_ci    return DHCP_E_SUCCESS;
182518678f8Sopenharmony_ci}
183518678f8Sopenharmony_ci
184518678f8Sopenharmony_ciErrCode DhcpServerProxy::SetDhcpRange(const std::string& ifname, const DhcpRange& range)
185518678f8Sopenharmony_ci{
186518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy enter SetDhcpRange mRemoteDied:%{public}d", mRemoteDied);
187518678f8Sopenharmony_ci    if (mRemoteDied) {
188518678f8Sopenharmony_ci        DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
189518678f8Sopenharmony_ci        return DHCP_E_FAILED;
190518678f8Sopenharmony_ci    }
191518678f8Sopenharmony_ci    MessageParcel data, reply;
192518678f8Sopenharmony_ci    MessageOption option;
193518678f8Sopenharmony_ci    if (!data.WriteInterfaceToken(GetDescriptor())) {
194518678f8Sopenharmony_ci        DHCP_LOGE("Write interface token error: %{public}s", __func__);
195518678f8Sopenharmony_ci        return DHCP_E_FAILED;
196518678f8Sopenharmony_ci    }
197518678f8Sopenharmony_ci    data.WriteInt32(0);
198518678f8Sopenharmony_ci
199518678f8Sopenharmony_ci    data.WriteInt32(range.iptype);
200518678f8Sopenharmony_ci    data.WriteInt32(range.leaseHours);
201518678f8Sopenharmony_ci    data.WriteString(range.strTagName);
202518678f8Sopenharmony_ci    data.WriteString(range.strStartip);
203518678f8Sopenharmony_ci    data.WriteString(range.strEndip);
204518678f8Sopenharmony_ci    data.WriteString(range.strSubnet);
205518678f8Sopenharmony_ci    data.WriteString(ifname);
206518678f8Sopenharmony_ci    DHCP_LOGI("%{public}s, LINE :%{public}d ifname:%{public}s iptype %{public}d leaseHours %{public}d"
207518678f8Sopenharmony_ci        "TagName:%{public}s Startip:%{public}s strEndip:%{public}s strSubnet:%{public}s",
208518678f8Sopenharmony_ci        __func__, __LINE__, ifname.c_str(), range.iptype, range.leaseHours, range.strTagName.c_str(),
209518678f8Sopenharmony_ci        range.strStartip.c_str(), range.strEndip.c_str(), range.strSubnet.c_str());
210518678f8Sopenharmony_ci    int error = Remote()->SendRequest(
211518678f8Sopenharmony_ci        static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_RANGE), data, reply, option);
212518678f8Sopenharmony_ci    if (error != ERR_NONE) {
213518678f8Sopenharmony_ci        DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
214518678f8Sopenharmony_ci            static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_RANGE), error);
215518678f8Sopenharmony_ci        return DHCP_E_FAILED;
216518678f8Sopenharmony_ci    }
217518678f8Sopenharmony_ci    int exception = reply.ReadInt32();
218518678f8Sopenharmony_ci    if (exception) {
219518678f8Sopenharmony_ci        DHCP_LOGE("exception failed, exception:%{public}d", exception);
220518678f8Sopenharmony_ci        return DHCP_E_FAILED;
221518678f8Sopenharmony_ci    }
222518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy SetDhcpRange ok, exception:%{public}d", exception);
223518678f8Sopenharmony_ci    return DHCP_E_SUCCESS;
224518678f8Sopenharmony_ci}
225518678f8Sopenharmony_ci
226518678f8Sopenharmony_ciErrCode DhcpServerProxy::SetDhcpName(const std::string& ifname, const std::string& tagName)
227518678f8Sopenharmony_ci{
228518678f8Sopenharmony_ci
229518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy enter SetDhcpName mRemoteDied:%{public}d", mRemoteDied);
230518678f8Sopenharmony_ci    if (mRemoteDied) {
231518678f8Sopenharmony_ci        DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
232518678f8Sopenharmony_ci        return DHCP_E_FAILED;
233518678f8Sopenharmony_ci    }
234518678f8Sopenharmony_ci    MessageParcel data, reply;
235518678f8Sopenharmony_ci    MessageOption option;
236518678f8Sopenharmony_ci    if (!data.WriteInterfaceToken(GetDescriptor())) {
237518678f8Sopenharmony_ci        DHCP_LOGE("Write interface token error: %{public}s", __func__);
238518678f8Sopenharmony_ci        return DHCP_E_FAILED;
239518678f8Sopenharmony_ci    }
240518678f8Sopenharmony_ci    data.WriteInt32(0);
241518678f8Sopenharmony_ci
242518678f8Sopenharmony_ci    data.WriteString(tagName);
243518678f8Sopenharmony_ci    data.WriteString(ifname);
244518678f8Sopenharmony_ci     DHCP_LOGI("%{public}s, LINE :%{public}d ifname:%{public}s tagName %{public}s", __func__, __LINE__, ifname.c_str(),
245518678f8Sopenharmony_ci        tagName.c_str());
246518678f8Sopenharmony_ci    int error = Remote()->SendRequest(
247518678f8Sopenharmony_ci        static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_NAME), data, reply, option);
248518678f8Sopenharmony_ci    if (error != ERR_NONE) {
249518678f8Sopenharmony_ci        DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
250518678f8Sopenharmony_ci            static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_SET_DHCP_NAME), error);
251518678f8Sopenharmony_ci        return DHCP_E_FAILED;
252518678f8Sopenharmony_ci    }
253518678f8Sopenharmony_ci    int exception = reply.ReadInt32();
254518678f8Sopenharmony_ci    if (exception) {
255518678f8Sopenharmony_ci        DHCP_LOGE("exception failed, exception:%{public}d", exception);
256518678f8Sopenharmony_ci        return DHCP_E_FAILED;
257518678f8Sopenharmony_ci    }
258518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy SetDhcpName ok, exception:%{public}d", exception);
259518678f8Sopenharmony_ci    return DHCP_E_SUCCESS;
260518678f8Sopenharmony_ci}
261518678f8Sopenharmony_ciErrCode DhcpServerProxy::PutDhcpRange(const std::string& tagName, const DhcpRange& range)
262518678f8Sopenharmony_ci{
263518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy enter PutDhcpRange mRemoteDied:%{public}d", mRemoteDied);
264518678f8Sopenharmony_ci    if (mRemoteDied) {
265518678f8Sopenharmony_ci        DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
266518678f8Sopenharmony_ci        return DHCP_E_FAILED;
267518678f8Sopenharmony_ci    }
268518678f8Sopenharmony_ci    MessageParcel data, reply;
269518678f8Sopenharmony_ci    MessageOption option;
270518678f8Sopenharmony_ci    if (!data.WriteInterfaceToken(GetDescriptor())) {
271518678f8Sopenharmony_ci        DHCP_LOGE("Write interface token error: %{public}s", __func__);
272518678f8Sopenharmony_ci        return DHCP_E_FAILED;
273518678f8Sopenharmony_ci    }
274518678f8Sopenharmony_ci    data.WriteInt32(0);
275518678f8Sopenharmony_ci    data.WriteInt32(range.iptype);
276518678f8Sopenharmony_ci    data.WriteInt32(range.leaseHours);
277518678f8Sopenharmony_ci    data.WriteString(range.strTagName);
278518678f8Sopenharmony_ci    data.WriteString(range.strStartip);
279518678f8Sopenharmony_ci    data.WriteString(range.strEndip);
280518678f8Sopenharmony_ci    data.WriteString(range.strSubnet);
281518678f8Sopenharmony_ci    data.WriteString(tagName);
282518678f8Sopenharmony_ci     DHCP_LOGI("%{public}s, LINE :%{public}d tagName:%{public}s iptype %{public}d  leaseHours %{public}d"
283518678f8Sopenharmony_ci        "strTagName:%{public}s strStartip:%{public}s strEndip:%{public}s strSubnet:%{public}s",
284518678f8Sopenharmony_ci        __func__, __LINE__, tagName.c_str(), range.iptype, range.leaseHours, range.strTagName.c_str(),
285518678f8Sopenharmony_ci        range.strStartip.c_str(), range.strEndip.c_str(), range.strSubnet.c_str());
286518678f8Sopenharmony_ci    int error = Remote()->SendRequest(
287518678f8Sopenharmony_ci        static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_PUT_DHCP_RANGE), data, reply, option);
288518678f8Sopenharmony_ci    if (error != ERR_NONE) {
289518678f8Sopenharmony_ci        DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
290518678f8Sopenharmony_ci            static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_PUT_DHCP_RANGE), error);
291518678f8Sopenharmony_ci        return DHCP_E_FAILED;
292518678f8Sopenharmony_ci    }
293518678f8Sopenharmony_ci    int exception = reply.ReadInt32();
294518678f8Sopenharmony_ci    if (exception) {
295518678f8Sopenharmony_ci        DHCP_LOGE("exception failed, exception:%{public}d", exception);
296518678f8Sopenharmony_ci        return DHCP_E_FAILED;
297518678f8Sopenharmony_ci    }
298518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy SetDhcpRange ok, exception:%{public}d", exception);
299518678f8Sopenharmony_ci    return DHCP_E_SUCCESS;
300518678f8Sopenharmony_ci}
301518678f8Sopenharmony_ci
302518678f8Sopenharmony_ciErrCode DhcpServerProxy::RemoveAllDhcpRange(const std::string& tagName)
303518678f8Sopenharmony_ci{
304518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy enter RemoveAllDhcpRange mRemoteDied:%{public}d", mRemoteDied);
305518678f8Sopenharmony_ci    if (mRemoteDied) {
306518678f8Sopenharmony_ci        DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
307518678f8Sopenharmony_ci        return DHCP_E_FAILED;
308518678f8Sopenharmony_ci    }
309518678f8Sopenharmony_ci    MessageParcel data, reply;
310518678f8Sopenharmony_ci    MessageOption option;
311518678f8Sopenharmony_ci    if (!data.WriteInterfaceToken(GetDescriptor())) {
312518678f8Sopenharmony_ci        DHCP_LOGE("Write interface token error: %{public}s", __func__);
313518678f8Sopenharmony_ci        return DHCP_E_FAILED;
314518678f8Sopenharmony_ci    }
315518678f8Sopenharmony_ci    data.WriteInt32(0);
316518678f8Sopenharmony_ci
317518678f8Sopenharmony_ci    data.WriteString(tagName);
318518678f8Sopenharmony_ci    DHCP_LOGI("%{public}s, calling tagName:%{public}s", __func__, tagName.c_str());
319518678f8Sopenharmony_ci    int error = Remote()->SendRequest(
320518678f8Sopenharmony_ci        static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_ALL_DHCP_RANGE), data, reply, option);
321518678f8Sopenharmony_ci    if (error != ERR_NONE) {
322518678f8Sopenharmony_ci        DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
323518678f8Sopenharmony_ci            static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_ALL_DHCP_RANGE), error);
324518678f8Sopenharmony_ci        return DHCP_E_FAILED;
325518678f8Sopenharmony_ci    }
326518678f8Sopenharmony_ci    int exception = reply.ReadInt32();
327518678f8Sopenharmony_ci    if (exception) {
328518678f8Sopenharmony_ci        DHCP_LOGE("exception failed, exception:%{public}d", exception);
329518678f8Sopenharmony_ci        return DHCP_E_FAILED;
330518678f8Sopenharmony_ci    }
331518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy RemoveAllDhcpRange ok, exception:%{public}d", exception);
332518678f8Sopenharmony_ci    return DHCP_E_SUCCESS;
333518678f8Sopenharmony_ci}
334518678f8Sopenharmony_ci
335518678f8Sopenharmony_ciErrCode DhcpServerProxy::UpdateLeasesTime(const std::string& leaseTime)
336518678f8Sopenharmony_ci{
337518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy enter UpdateLeasesTime mRemoteDied:%{public}d", mRemoteDied);
338518678f8Sopenharmony_ci    if (mRemoteDied) {
339518678f8Sopenharmony_ci        DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
340518678f8Sopenharmony_ci        return DHCP_E_FAILED;
341518678f8Sopenharmony_ci    }
342518678f8Sopenharmony_ci    MessageParcel data, reply;
343518678f8Sopenharmony_ci    MessageOption option;
344518678f8Sopenharmony_ci    if (!data.WriteInterfaceToken(GetDescriptor())) {
345518678f8Sopenharmony_ci        DHCP_LOGE("Write interface token error: %{public}s", __func__);
346518678f8Sopenharmony_ci        return DHCP_E_FAILED;
347518678f8Sopenharmony_ci    }
348518678f8Sopenharmony_ci    data.WriteInt32(0);
349518678f8Sopenharmony_ci
350518678f8Sopenharmony_ci    data.WriteString(leaseTime);
351518678f8Sopenharmony_ci    DHCP_LOGI("%{public}s, calling  leaseTime:%{public}s", __func__, leaseTime.c_str());
352518678f8Sopenharmony_ci    int error = Remote()->SendRequest(
353518678f8Sopenharmony_ci        static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_UPDATE_RENEW_TIME), data, reply, option);
354518678f8Sopenharmony_ci    if (error != ERR_NONE) {
355518678f8Sopenharmony_ci        DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
356518678f8Sopenharmony_ci            static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_UPDATE_RENEW_TIME), error);
357518678f8Sopenharmony_ci        return DHCP_E_FAILED;
358518678f8Sopenharmony_ci    }
359518678f8Sopenharmony_ci    int exception = reply.ReadInt32();
360518678f8Sopenharmony_ci    if (exception) {
361518678f8Sopenharmony_ci        DHCP_LOGE("exception failed, exception:%{public}d", exception);
362518678f8Sopenharmony_ci        return DHCP_E_FAILED;
363518678f8Sopenharmony_ci    }
364518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy UpdateLeasesTime ok, exception:%{public}d", exception);
365518678f8Sopenharmony_ci    return DHCP_E_SUCCESS;
366518678f8Sopenharmony_ci}
367518678f8Sopenharmony_ci
368518678f8Sopenharmony_ciErrCode DhcpServerProxy::StopDhcpServer(const std::string& ifname)
369518678f8Sopenharmony_ci{
370518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy enter StopDhcpServer mRemoteDied:%{public}d", mRemoteDied);
371518678f8Sopenharmony_ci    if (mRemoteDied) {
372518678f8Sopenharmony_ci        DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
373518678f8Sopenharmony_ci        return DHCP_E_FAILED;
374518678f8Sopenharmony_ci    }
375518678f8Sopenharmony_ci    MessageParcel data, reply;
376518678f8Sopenharmony_ci    MessageOption option;
377518678f8Sopenharmony_ci    if (!data.WriteInterfaceToken(GetDescriptor())) {
378518678f8Sopenharmony_ci        DHCP_LOGE("Write interface token error: %{public}s", __func__);
379518678f8Sopenharmony_ci        return DHCP_E_FAILED;
380518678f8Sopenharmony_ci    }
381518678f8Sopenharmony_ci    data.WriteInt32(0);
382518678f8Sopenharmony_ci    data.WriteString(ifname);
383518678f8Sopenharmony_ci    DHCP_LOGI("%{public}s, calling tagName:%{public}s", __func__, ifname.c_str());
384518678f8Sopenharmony_ci    int error = Remote()->SendRequest(
385518678f8Sopenharmony_ci        static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_STOP_DHCP_SERVER), data, reply, option);
386518678f8Sopenharmony_ci    if (error != ERR_NONE) {
387518678f8Sopenharmony_ci        DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
388518678f8Sopenharmony_ci            static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_STOP_DHCP_SERVER), error);
389518678f8Sopenharmony_ci        return DHCP_E_FAILED;
390518678f8Sopenharmony_ci    }
391518678f8Sopenharmony_ci    int exception = reply.ReadInt32();
392518678f8Sopenharmony_ci    if (exception) {
393518678f8Sopenharmony_ci        DHCP_LOGE("exception failed, exception:%{public}d", exception);
394518678f8Sopenharmony_ci        return DHCP_E_FAILED;
395518678f8Sopenharmony_ci    }
396518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy StopDhcpServer ok, exception:%{public}d", exception);
397518678f8Sopenharmony_ci    return DHCP_E_SUCCESS;
398518678f8Sopenharmony_ci}
399518678f8Sopenharmony_ciErrCode DhcpServerProxy::RemoveDhcpRange(const std::string& tagName, const DhcpRange& range)
400518678f8Sopenharmony_ci{
401518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy enter RemoveDhcpRange mRemoteDied:%{public}d", mRemoteDied);
402518678f8Sopenharmony_ci    if (mRemoteDied) {
403518678f8Sopenharmony_ci        DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
404518678f8Sopenharmony_ci        return DHCP_E_FAILED;
405518678f8Sopenharmony_ci    }
406518678f8Sopenharmony_ci    MessageParcel data, reply;
407518678f8Sopenharmony_ci    MessageOption option;
408518678f8Sopenharmony_ci    if (!data.WriteInterfaceToken(GetDescriptor())) {
409518678f8Sopenharmony_ci        DHCP_LOGE("Write interface token error: %{public}s", __func__);
410518678f8Sopenharmony_ci        return DHCP_E_FAILED;
411518678f8Sopenharmony_ci    }
412518678f8Sopenharmony_ci    data.WriteInt32(0);
413518678f8Sopenharmony_ci    data.WriteInt32(range.iptype);
414518678f8Sopenharmony_ci    data.WriteInt32(range.leaseHours);
415518678f8Sopenharmony_ci    data.WriteString(range.strTagName);
416518678f8Sopenharmony_ci    data.WriteString(range.strStartip);
417518678f8Sopenharmony_ci    data.WriteString(range.strEndip);
418518678f8Sopenharmony_ci    data.WriteString(range.strSubnet);
419518678f8Sopenharmony_ci    data.WriteString(tagName);
420518678f8Sopenharmony_ci     DHCP_LOGI("%{public}s, LINE :%{public}d ifname:%{public}s iptype %{public}d leaseHours %{public}d"
421518678f8Sopenharmony_ci        "strTagName:%{public}s strStartip:%{public}s strEndip:%{public}s strSubnet:%{public}s",
422518678f8Sopenharmony_ci        __func__, __LINE__, tagName.c_str(), range.iptype, range.leaseHours, range.strTagName.c_str(),
423518678f8Sopenharmony_ci        range.strStartip.c_str(), range.strEndip.c_str(), range.strSubnet.c_str());
424518678f8Sopenharmony_ci    int error = Remote()->SendRequest(
425518678f8Sopenharmony_ci        static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_DHCP_RANGE), data, reply, option);
426518678f8Sopenharmony_ci    if (error != ERR_NONE) {
427518678f8Sopenharmony_ci        DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
428518678f8Sopenharmony_ci            static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REMOVE_DHCP_RANGE), error);
429518678f8Sopenharmony_ci        return DHCP_E_FAILED;
430518678f8Sopenharmony_ci    }
431518678f8Sopenharmony_ci    int exception = reply.ReadInt32();
432518678f8Sopenharmony_ci    if (exception) {
433518678f8Sopenharmony_ci        DHCP_LOGE("exception failed, exception:%{public}d", exception);
434518678f8Sopenharmony_ci        return DHCP_E_FAILED;
435518678f8Sopenharmony_ci    }
436518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy SetDhcpRange ok, exception:%{public}d", exception);
437518678f8Sopenharmony_ci    return DHCP_E_SUCCESS;
438518678f8Sopenharmony_ci}
439518678f8Sopenharmony_ciErrCode DhcpServerProxy::GetDhcpClientInfos(const std::string& ifname, std::vector<std::string>& dhcpClientInfo)
440518678f8Sopenharmony_ci{
441518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy enter GetDhcpClientInfos mRemoteDied:%{public}d", mRemoteDied);
442518678f8Sopenharmony_ci    if (mRemoteDied) {
443518678f8Sopenharmony_ci        DHCP_LOGE("failed to `%{public}s`,remote service is died!", __func__);
444518678f8Sopenharmony_ci        return DHCP_E_FAILED;
445518678f8Sopenharmony_ci    }
446518678f8Sopenharmony_ci    MessageParcel data, reply;
447518678f8Sopenharmony_ci    MessageOption option;
448518678f8Sopenharmony_ci    if (!data.WriteInterfaceToken(GetDescriptor())) {
449518678f8Sopenharmony_ci        DHCP_LOGE("Write interface token error: %{public}s", __func__);
450518678f8Sopenharmony_ci        return DHCP_E_FAILED;
451518678f8Sopenharmony_ci    }
452518678f8Sopenharmony_ci    data.WriteInt32(0);
453518678f8Sopenharmony_ci    data.WriteString(ifname);
454518678f8Sopenharmony_ci    DHCP_LOGI("%{public}s, LINE :%{public}d %{public}s", __func__, __LINE__, ifname.c_str());
455518678f8Sopenharmony_ci    int error = Remote()->SendRequest(
456518678f8Sopenharmony_ci        static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_GET_DHCP_CLIENT_INFO), data, reply, option);
457518678f8Sopenharmony_ci    if (error != ERR_NONE) {
458518678f8Sopenharmony_ci        DHCP_LOGE("Set Attr(%{public}d) failed, code is %{public}d",
459518678f8Sopenharmony_ci            static_cast<int32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_GET_DHCP_CLIENT_INFO), error);
460518678f8Sopenharmony_ci        return DHCP_E_FAILED;
461518678f8Sopenharmony_ci    }
462518678f8Sopenharmony_ci    int exception = reply.ReadInt32();
463518678f8Sopenharmony_ci    if (exception) {
464518678f8Sopenharmony_ci        DHCP_LOGE("exception failed, exception:%{public}d", exception);
465518678f8Sopenharmony_ci        return DHCP_E_FAILED;
466518678f8Sopenharmony_ci    }
467518678f8Sopenharmony_ci    int ret = reply.ReadInt32();
468518678f8Sopenharmony_ci    if (ret == DHCP_E_SUCCESS) {
469518678f8Sopenharmony_ci        int tmpsize = reply.ReadInt32();
470518678f8Sopenharmony_ci        DHCP_LOGI("DhcpServerProxy GetDhcpClientInfos ok, exception:%{public}d, reply data size:%{public}d", exception,
471518678f8Sopenharmony_ci            tmpsize);
472518678f8Sopenharmony_ci        if (tmpsize > MAX_SIZE) {
473518678f8Sopenharmony_ci            DHCP_LOGE("GetDhcpClientInfos tmpsize error: %{public}d", tmpsize);
474518678f8Sopenharmony_ci            return DHCP_E_FAILED;
475518678f8Sopenharmony_ci        }
476518678f8Sopenharmony_ci        for (int i = 0; i < tmpsize; i++) {
477518678f8Sopenharmony_ci            std::string str = reply.ReadString();
478518678f8Sopenharmony_ci            dhcpClientInfo.push_back(str);
479518678f8Sopenharmony_ci        }
480518678f8Sopenharmony_ci    }
481518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerProxy GetDhcpClientInfos 1");
482518678f8Sopenharmony_ci    return DHCP_E_SUCCESS;
483518678f8Sopenharmony_ci}
484518678f8Sopenharmony_ci}  // namespace DHCP
485518678f8Sopenharmony_ci}  // namespace OHOS
486