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_event.h"
16518678f8Sopenharmony_ci#include "dhcp_logger.h"
17518678f8Sopenharmony_ci
18518678f8Sopenharmony_ciDEFINE_DHCPLOG_DHCP_LABEL("DhcpSAEvent");
19518678f8Sopenharmony_ci
20518678f8Sopenharmony_ciDhcpClientCallBack::DhcpClientCallBack()
21518678f8Sopenharmony_ci{
22518678f8Sopenharmony_ci    DHCP_LOGI("DhcpClientCallBack");
23518678f8Sopenharmony_ci}
24518678f8Sopenharmony_ci
25518678f8Sopenharmony_ciDhcpClientCallBack::~DhcpClientCallBack()
26518678f8Sopenharmony_ci{
27518678f8Sopenharmony_ci    DHCP_LOGI("~DhcpClientCallBack");
28518678f8Sopenharmony_ci}
29518678f8Sopenharmony_ci
30518678f8Sopenharmony_civoid DhcpClientCallBack::ResultInfoCopy(DhcpResult &dhcpResult, OHOS::DHCP::DhcpResult& result)
31518678f8Sopenharmony_ci{
32518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptClientId, DHCP_MAX_FILE_BYTES, result.strYourCli.c_str()) != EOK) {
33518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptClientId strcpy_s failed!");
34518678f8Sopenharmony_ci    }
35518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptServerId, DHCP_MAX_FILE_BYTES, result.strServer.c_str()) != EOK) {
36518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptServerId strcpy_s failed!");
37518678f8Sopenharmony_ci    }
38518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptSubnet, DHCP_MAX_FILE_BYTES, result.strSubnet.c_str()) != EOK) {
39518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptSubnet strcpy_s failed!");
40518678f8Sopenharmony_ci    }
41518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptDns1, DHCP_MAX_FILE_BYTES, result.strDns1.c_str()) != EOK) {
42518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptDns1 strcpy_s failed!");
43518678f8Sopenharmony_ci    }
44518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptDns2, DHCP_MAX_FILE_BYTES, result.strDns2.c_str()) != EOK) {
45518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptDns2 strcpy_s failed!");
46518678f8Sopenharmony_ci    }
47518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptRouter1, DHCP_MAX_FILE_BYTES, result.strRouter1.c_str()) != EOK) {
48518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptRouter1 strcpy_s failed!");
49518678f8Sopenharmony_ci    }
50518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptRouter2, DHCP_MAX_FILE_BYTES, result.strRouter2.c_str()) != EOK) {
51518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptRouter2 strcpy_s failed!");
52518678f8Sopenharmony_ci    }
53518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptVendor, DHCP_MAX_FILE_BYTES, result.strVendor.c_str()) != EOK) {
54518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptVendor strcpy_s failed!");
55518678f8Sopenharmony_ci    }
56518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptLinkIpv6Addr, DHCP_MAX_FILE_BYTES, result.strLinkIpv6Addr.c_str()) != EOK) {
57518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptLinkIpv6Addr strcpy_s failed!");
58518678f8Sopenharmony_ci    }
59518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptRandIpv6Addr, DHCP_MAX_FILE_BYTES, result.strRandIpv6Addr.c_str()) != EOK) {
60518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptRandIpv6Addr strcpy_s failed!");
61518678f8Sopenharmony_ci    }
62518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptLocalAddr1, DHCP_MAX_FILE_BYTES, result.strLocalAddr1.c_str()) != EOK) {
63518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptLocalAddr1 strcpy_s failed!");
64518678f8Sopenharmony_ci    }
65518678f8Sopenharmony_ci    if (strcpy_s(dhcpResult.strOptLocalAddr2, DHCP_MAX_FILE_BYTES, result.strLocalAddr2.c_str()) != EOK) {
66518678f8Sopenharmony_ci        DHCP_LOGE("ResultInfoCopy strOptLocalAddr2 strcpy_s failed!");
67518678f8Sopenharmony_ci    }
68518678f8Sopenharmony_ci    for (size_t i = 0; i < result.vectorDnsAddr.size(); i++) {
69518678f8Sopenharmony_ci        if (i >= DHCP_DNS_MAX_NUMBER) {
70518678f8Sopenharmony_ci            DHCP_LOGE("ResultInfoCopy break, i:%{public}zu, dns max number:%{public}d", i, DHCP_DNS_MAX_NUMBER);
71518678f8Sopenharmony_ci            break;
72518678f8Sopenharmony_ci        }
73518678f8Sopenharmony_ci        if (strncpy_s(dhcpResult.dnsList.dnsAddr[i], DHCP_DNS_DATA_MAX_LEN, result.vectorDnsAddr[i].c_str(),
74518678f8Sopenharmony_ci            result.vectorDnsAddr[i].length()) != EOK) {
75518678f8Sopenharmony_ci            DHCP_LOGE("ResultInfoCopy, strncpy_s failed, i:%{public}zu", i);
76518678f8Sopenharmony_ci        } else {
77518678f8Sopenharmony_ci            dhcpResult.dnsList.dnsNumber++;
78518678f8Sopenharmony_ci        }
79518678f8Sopenharmony_ci    }
80518678f8Sopenharmony_ci}
81518678f8Sopenharmony_ci
82518678f8Sopenharmony_civoid DhcpClientCallBack::OnIpSuccessChanged(int status, const std::string& ifname, OHOS::DHCP::DhcpResult& result)
83518678f8Sopenharmony_ci    __attribute__((no_sanitize("cfi")))
84518678f8Sopenharmony_ci{
85518678f8Sopenharmony_ci    DHCP_LOGI("OnIpSuccessChanged status:%{public}d,ifname:%{public}s", status, ifname.c_str());
86518678f8Sopenharmony_ci    DhcpResult dhcpResult;
87518678f8Sopenharmony_ci    dhcpResult.iptype = result.iptype;
88518678f8Sopenharmony_ci    dhcpResult.isOptSuc = result.isOptSuc;
89518678f8Sopenharmony_ci    dhcpResult.uOptLeasetime = result.uLeaseTime;
90518678f8Sopenharmony_ci    dhcpResult.uAddTime = result.uAddTime;
91518678f8Sopenharmony_ci    dhcpResult.uGetTime = result.uGetTime;
92518678f8Sopenharmony_ci    ResultInfoCopy(dhcpResult, result);
93518678f8Sopenharmony_ci    DHCP_LOGI("ResultInfoCopy dstDnsNumber:%{public}u srcDnsNumber:%{public}zu", dhcpResult.dnsList.dnsNumber,
94518678f8Sopenharmony_ci        result.vectorDnsAddr.size());
95518678f8Sopenharmony_ci
96518678f8Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(callBackMutex);
97518678f8Sopenharmony_ci    auto iter = mapClientCallBack.find(ifname);
98518678f8Sopenharmony_ci    if ((iter != mapClientCallBack.end()) && (iter->second != nullptr) &&
99518678f8Sopenharmony_ci        (iter->second->OnIpSuccessChanged != nullptr)) {
100518678f8Sopenharmony_ci        DHCP_LOGI("OnIpSuccessChanged callbackEvent status:%{public}d", status);
101518678f8Sopenharmony_ci        iter->second->OnIpSuccessChanged(status, ifname.c_str(), &dhcpResult);
102518678f8Sopenharmony_ci    } else {
103518678f8Sopenharmony_ci        DHCP_LOGE("OnIpSuccessChanged callbackEvent failed!");
104518678f8Sopenharmony_ci    }
105518678f8Sopenharmony_ci}
106518678f8Sopenharmony_ci
107518678f8Sopenharmony_civoid DhcpClientCallBack::OnIpFailChanged(int status, const std::string& ifname, const std::string& reason)
108518678f8Sopenharmony_ci    __attribute__((no_sanitize("cfi")))
109518678f8Sopenharmony_ci{
110518678f8Sopenharmony_ci    DHCP_LOGI("OnIpFailChanged status:%{public}d, ifname:%{public}s, reason:%{public}s", status, ifname.c_str(),
111518678f8Sopenharmony_ci        reason.c_str());
112518678f8Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(callBackMutex);
113518678f8Sopenharmony_ci    auto iter = mapClientCallBack.find(ifname);
114518678f8Sopenharmony_ci    if ((iter != mapClientCallBack.end()) && (iter->second != nullptr) && (iter->second->OnIpFailChanged != nullptr)) {
115518678f8Sopenharmony_ci        DHCP_LOGI("OnIpFailChanged callbackEvent status:%{public}d", status);
116518678f8Sopenharmony_ci        iter->second->OnIpFailChanged(status, ifname.c_str(), reason.c_str());
117518678f8Sopenharmony_ci    } else {
118518678f8Sopenharmony_ci        DHCP_LOGE("OnIpFailChanged callbackEvent failed!");
119518678f8Sopenharmony_ci    }
120518678f8Sopenharmony_ci}
121518678f8Sopenharmony_ci#ifndef OHOS_ARCH_LITE
122518678f8Sopenharmony_civoid DhcpClientCallBack::OnDhcpOfferReport(int status, const std::string& ifname, OHOS::DHCP::DhcpResult& result)
123518678f8Sopenharmony_ci{
124518678f8Sopenharmony_ci    DHCP_LOGI("OnDhcpOfferReport status:%{public}d,ifname:%{public}s", status, ifname.c_str());
125518678f8Sopenharmony_ci    DhcpResult dhcpResult;
126518678f8Sopenharmony_ci    dhcpResult.iptype = result.iptype;
127518678f8Sopenharmony_ci    dhcpResult.isOptSuc = result.isOptSuc;
128518678f8Sopenharmony_ci    dhcpResult.uOptLeasetime = result.uLeaseTime;
129518678f8Sopenharmony_ci    dhcpResult.uAddTime = result.uAddTime;
130518678f8Sopenharmony_ci    dhcpResult.uGetTime = result.uGetTime;
131518678f8Sopenharmony_ci    ResultInfoCopy(dhcpResult, result);
132518678f8Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(mapReportMutex_);
133518678f8Sopenharmony_ci    auto iter = mapDhcpClientReport_.find(ifname);
134518678f8Sopenharmony_ci    if ((iter != mapDhcpClientReport_.end()) && (iter->second != nullptr) &&
135518678f8Sopenharmony_ci        (iter->second->OnDhcpClientReport != nullptr)) {
136518678f8Sopenharmony_ci        iter->second->OnDhcpClientReport(status, ifname.c_str(), &dhcpResult);
137518678f8Sopenharmony_ci    } else {
138518678f8Sopenharmony_ci        DHCP_LOGE("OnDhcpOfferReport callbackEvent failed!");
139518678f8Sopenharmony_ci    }
140518678f8Sopenharmony_ci}
141518678f8Sopenharmony_ci
142518678f8Sopenharmony_ciOHOS::sptr<OHOS::IRemoteObject> DhcpClientCallBack::AsObject()
143518678f8Sopenharmony_ci{
144518678f8Sopenharmony_ci    DHCP_LOGI("DhcpClientCallBack AsObject!");
145518678f8Sopenharmony_ci    return nullptr;
146518678f8Sopenharmony_ci}
147518678f8Sopenharmony_ci#endif
148518678f8Sopenharmony_civoid DhcpClientCallBack::RegisterCallBack(const std::string& ifname, const ClientCallBack *event)
149518678f8Sopenharmony_ci{
150518678f8Sopenharmony_ci    if (event == nullptr) {
151518678f8Sopenharmony_ci        DHCP_LOGE("DhcpClientCallBack event is nullptr!");
152518678f8Sopenharmony_ci        return;
153518678f8Sopenharmony_ci    }
154518678f8Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(callBackMutex);
155518678f8Sopenharmony_ci    auto iter = mapClientCallBack.find(ifname);
156518678f8Sopenharmony_ci    if (iter != mapClientCallBack.end()) {
157518678f8Sopenharmony_ci        iter->second = event;
158518678f8Sopenharmony_ci        DHCP_LOGI("Client RegisterCallBack update event, ifname:%{public}s", ifname.c_str());
159518678f8Sopenharmony_ci    } else {
160518678f8Sopenharmony_ci        mapClientCallBack.emplace(std::make_pair(ifname, event));
161518678f8Sopenharmony_ci        DHCP_LOGI("Client RegisterCallBack add event, ifname:%{public}s", ifname.c_str());
162518678f8Sopenharmony_ci    }
163518678f8Sopenharmony_ci}
164518678f8Sopenharmony_ci
165518678f8Sopenharmony_cistatic bool MatchSubStr(const std::string &key, const std::string &pattern)
166518678f8Sopenharmony_ci{
167518678f8Sopenharmony_ci    return key.find(pattern) != std::string::npos;
168518678f8Sopenharmony_ci}
169518678f8Sopenharmony_ci
170518678f8Sopenharmony_civoid DhcpClientCallBack::UnRegisterCallBack(const std::string& ifname)
171518678f8Sopenharmony_ci{
172518678f8Sopenharmony_ci    if (ifname.empty()) {
173518678f8Sopenharmony_ci        DHCP_LOGE("Client UnRegisterCallBack ifname is empty!");
174518678f8Sopenharmony_ci        return;
175518678f8Sopenharmony_ci    }
176518678f8Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(callBackMutex);
177518678f8Sopenharmony_ci    auto iter = mapClientCallBack.find(ifname);
178518678f8Sopenharmony_ci    if (iter != mapClientCallBack.end()) {
179518678f8Sopenharmony_ci        mapClientCallBack.erase(iter);
180518678f8Sopenharmony_ci        DHCP_LOGI("Client UnRegisterCallBack erase ifname:%{public}s", ifname.c_str());
181518678f8Sopenharmony_ci    } else {
182518678f8Sopenharmony_ci        auto it = std::find_if(mapClientCallBack.begin(), mapClientCallBack.end(),
183518678f8Sopenharmony_ci            [&](const auto &kv) { return MatchSubStr(kv.first, ifname); });
184518678f8Sopenharmony_ci        if (it != mapClientCallBack.end()) {
185518678f8Sopenharmony_ci            DHCP_LOGI("Client UnRegisterCallBack find match ifname:%{public}s key:%{public}s",
186518678f8Sopenharmony_ci                ifname.c_str(), it->first.c_str());
187518678f8Sopenharmony_ci            mapClientCallBack.erase(it);
188518678f8Sopenharmony_ci        } else {
189518678f8Sopenharmony_ci            DHCP_LOGI("Client UnRegisterCallBack no match ifname:%{public}s", ifname.c_str());
190518678f8Sopenharmony_ci        }
191518678f8Sopenharmony_ci    }
192518678f8Sopenharmony_ci}
193518678f8Sopenharmony_ci
194518678f8Sopenharmony_civoid DhcpClientCallBack::RegisterDhcpClientReportCallBack(const std::string& ifname, const DhcpClientReport *event)
195518678f8Sopenharmony_ci{
196518678f8Sopenharmony_ci    if (event == nullptr) {
197518678f8Sopenharmony_ci        DHCP_LOGE("RegisterDhcpClientReportCallBack event is nullptr!");
198518678f8Sopenharmony_ci        return;
199518678f8Sopenharmony_ci    }
200518678f8Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(mapReportMutex_);
201518678f8Sopenharmony_ci    auto iter = mapDhcpClientReport_.find(ifname);
202518678f8Sopenharmony_ci    if (iter != mapDhcpClientReport_.end()) {
203518678f8Sopenharmony_ci        iter->second = event;
204518678f8Sopenharmony_ci        DHCP_LOGI("RegisterDhcpClientReportCallBack update event, ifname:%{public}s", ifname.c_str());
205518678f8Sopenharmony_ci    } else {
206518678f8Sopenharmony_ci        mapDhcpClientReport_.emplace(std::make_pair(ifname, event));
207518678f8Sopenharmony_ci        DHCP_LOGI("RegisterDhcpClientReportCallBack add event, ifname:%{public}s", ifname.c_str());
208518678f8Sopenharmony_ci    }
209518678f8Sopenharmony_ci}
210518678f8Sopenharmony_ci
211518678f8Sopenharmony_ciDhcpServerCallBack::DhcpServerCallBack()
212518678f8Sopenharmony_ci{
213518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerCallBack");
214518678f8Sopenharmony_ci}
215518678f8Sopenharmony_ci
216518678f8Sopenharmony_ciDhcpServerCallBack::~DhcpServerCallBack()
217518678f8Sopenharmony_ci{
218518678f8Sopenharmony_ci    DHCP_LOGI("~DhcpServerCallBack");
219518678f8Sopenharmony_ci}
220518678f8Sopenharmony_ci
221518678f8Sopenharmony_civoid DhcpServerCallBack::OnServerStatusChanged(int status)
222518678f8Sopenharmony_ci{
223518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerCallBack OnServerStatusChanged status:%{public}d", status);
224518678f8Sopenharmony_ci}
225518678f8Sopenharmony_ci
226518678f8Sopenharmony_civoid DhcpServerCallBack::OnServerLeasesChanged(const std::string& ifname, std::vector<std::string>& leases)
227518678f8Sopenharmony_ci{
228518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerCallBack OnServerLeasesChanged ifname:%{public}s", ifname.c_str());
229518678f8Sopenharmony_ci}
230518678f8Sopenharmony_ci
231518678f8Sopenharmony_civoid DhcpServerCallBack::OnServerSerExitChanged(const std::string& ifname)
232518678f8Sopenharmony_ci{
233518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerCallBack OnServerSerExitChanged ifname:%{public}s", ifname.c_str());
234518678f8Sopenharmony_ci}
235518678f8Sopenharmony_ci
236518678f8Sopenharmony_civoid DhcpServerCallBack::OnServerSuccess(const std::string& ifname, std::vector<DhcpStationInfo>& stationInfos)
237518678f8Sopenharmony_ci{
238518678f8Sopenharmony_ci    size_t size = stationInfos.size();
239518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerCallBack OnServerSuccess ifname:%{public}s size:%{public}zu", ifname.c_str(), size);
240518678f8Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(callBackServerMutex);
241518678f8Sopenharmony_ci    auto iter = mapServerCallBack.find(ifname);
242518678f8Sopenharmony_ci    if ((iter != mapServerCallBack.end()) && (iter->second != nullptr) && (iter->second->OnServerSuccess != nullptr) &&
243518678f8Sopenharmony_ci        (size > 0)) {
244518678f8Sopenharmony_ci        DhcpStationInfo *infos = (struct DhcpStationInfo*)malloc(size * sizeof(DhcpStationInfo));
245518678f8Sopenharmony_ci        if (infos == nullptr) {
246518678f8Sopenharmony_ci            DHCP_LOGE("malloc failed");
247518678f8Sopenharmony_ci            return;
248518678f8Sopenharmony_ci        }
249518678f8Sopenharmony_ci        for (size_t i = 0; i < size; i++) {
250518678f8Sopenharmony_ci            if (strcpy_s(infos[i].ipAddr, sizeof(infos[i].ipAddr), stationInfos[i].ipAddr) != EOK) {
251518678f8Sopenharmony_ci                DHCP_LOGE("OnServerSuccess strcpy_s ipAddr failed!");
252518678f8Sopenharmony_ci                free(infos);
253518678f8Sopenharmony_ci                infos = nullptr;
254518678f8Sopenharmony_ci                return;
255518678f8Sopenharmony_ci            }
256518678f8Sopenharmony_ci            if (strcpy_s(infos[i].macAddr, sizeof(infos[i].macAddr), stationInfos[i].macAddr) != EOK) {
257518678f8Sopenharmony_ci                DHCP_LOGE("OnServerSuccess strcpy_s macAddr failed!");
258518678f8Sopenharmony_ci                free(infos);
259518678f8Sopenharmony_ci                infos = nullptr;
260518678f8Sopenharmony_ci                return;
261518678f8Sopenharmony_ci            }
262518678f8Sopenharmony_ci            if (strcpy_s(infos[i].deviceName, sizeof(infos[i].deviceName), stationInfos[i].deviceName) != EOK) {
263518678f8Sopenharmony_ci                DHCP_LOGE("OnServerSuccess strcpy_s deviceName failed!");
264518678f8Sopenharmony_ci                free(infos);
265518678f8Sopenharmony_ci                infos = nullptr;
266518678f8Sopenharmony_ci                return;
267518678f8Sopenharmony_ci            }
268518678f8Sopenharmony_ci        }
269518678f8Sopenharmony_ci        iter->second->OnServerSuccess(ifname.c_str(), infos, size);
270518678f8Sopenharmony_ci        free(infos);
271518678f8Sopenharmony_ci        infos = nullptr;
272518678f8Sopenharmony_ci        DHCP_LOGI("OnServerSuccess callbackEvent ok!");
273518678f8Sopenharmony_ci    } else {
274518678f8Sopenharmony_ci        DHCP_LOGE("OnServerSuccess not find callback!");
275518678f8Sopenharmony_ci    }
276518678f8Sopenharmony_ci}
277518678f8Sopenharmony_ci
278518678f8Sopenharmony_ci#ifndef OHOS_ARCH_LITE
279518678f8Sopenharmony_ciOHOS::sptr<OHOS::IRemoteObject> DhcpServerCallBack::AsObject()
280518678f8Sopenharmony_ci{
281518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerCallBack AsObject!");
282518678f8Sopenharmony_ci    return nullptr;
283518678f8Sopenharmony_ci}
284518678f8Sopenharmony_ci#endif
285518678f8Sopenharmony_civoid DhcpServerCallBack::RegisterCallBack(const std::string& ifname, const ServerCallBack *event)
286518678f8Sopenharmony_ci{
287518678f8Sopenharmony_ci    if (event == nullptr) {
288518678f8Sopenharmony_ci        DHCP_LOGE("DhcpServerCallBack event is nullptr!");
289518678f8Sopenharmony_ci        return;
290518678f8Sopenharmony_ci    }
291518678f8Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(callBackServerMutex);
292518678f8Sopenharmony_ci    auto iter = mapServerCallBack.find(ifname);
293518678f8Sopenharmony_ci    if (iter != mapServerCallBack.end()) {
294518678f8Sopenharmony_ci        iter->second = event;
295518678f8Sopenharmony_ci        DHCP_LOGI("Server RegisterCallBack update event, ifname:%{public}s", ifname.c_str());
296518678f8Sopenharmony_ci    } else {
297518678f8Sopenharmony_ci        mapServerCallBack.emplace(std::make_pair(ifname, event));
298518678f8Sopenharmony_ci        DHCP_LOGI("Server RegisterCallBack add event, ifname:%{public}s", ifname.c_str());
299518678f8Sopenharmony_ci    }
300518678f8Sopenharmony_ci}
301518678f8Sopenharmony_ci
302518678f8Sopenharmony_civoid DhcpServerCallBack::UnRegisterCallBack(const std::string& ifname)
303518678f8Sopenharmony_ci{
304518678f8Sopenharmony_ci    if (ifname.empty()) {
305518678f8Sopenharmony_ci        DHCP_LOGE("Server UnRegisterCallBack ifname is empty!");
306518678f8Sopenharmony_ci        return;
307518678f8Sopenharmony_ci    }
308518678f8Sopenharmony_ci    std::lock_guard<std::mutex> autoLock(callBackServerMutex);
309518678f8Sopenharmony_ci    auto iter = mapServerCallBack.find(ifname);
310518678f8Sopenharmony_ci    if (iter != mapServerCallBack.end()) {
311518678f8Sopenharmony_ci        mapServerCallBack.erase(iter);
312518678f8Sopenharmony_ci        DHCP_LOGI("Server UnRegisterCallBack erase ifname:%{public}s", ifname.c_str());
313518678f8Sopenharmony_ci    } else {
314518678f8Sopenharmony_ci        DHCP_LOGI("Server UnRegisterCallBack not find, ifname:%{public}s", ifname.c_str());
315518678f8Sopenharmony_ci    }
316518678f8Sopenharmony_ci}
317