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_event.h"
16 #include "dhcp_logger.h"
17 
18 DEFINE_DHCPLOG_DHCP_LABEL("DhcpSAEvent");
19 
DhcpClientCallBack()20 DhcpClientCallBack::DhcpClientCallBack()
21 {
22     DHCP_LOGI("DhcpClientCallBack");
23 }
24 
~DhcpClientCallBack()25 DhcpClientCallBack::~DhcpClientCallBack()
26 {
27     DHCP_LOGI("~DhcpClientCallBack");
28 }
29 
ResultInfoCopy(DhcpResult & dhcpResult,OHOS::DHCP::DhcpResult & result)30 void DhcpClientCallBack::ResultInfoCopy(DhcpResult &dhcpResult, OHOS::DHCP::DhcpResult& result)
31 {
32     if (strcpy_s(dhcpResult.strOptClientId, DHCP_MAX_FILE_BYTES, result.strYourCli.c_str()) != EOK) {
33         DHCP_LOGE("ResultInfoCopy strOptClientId strcpy_s failed!");
34     }
35     if (strcpy_s(dhcpResult.strOptServerId, DHCP_MAX_FILE_BYTES, result.strServer.c_str()) != EOK) {
36         DHCP_LOGE("ResultInfoCopy strOptServerId strcpy_s failed!");
37     }
38     if (strcpy_s(dhcpResult.strOptSubnet, DHCP_MAX_FILE_BYTES, result.strSubnet.c_str()) != EOK) {
39         DHCP_LOGE("ResultInfoCopy strOptSubnet strcpy_s failed!");
40     }
41     if (strcpy_s(dhcpResult.strOptDns1, DHCP_MAX_FILE_BYTES, result.strDns1.c_str()) != EOK) {
42         DHCP_LOGE("ResultInfoCopy strOptDns1 strcpy_s failed!");
43     }
44     if (strcpy_s(dhcpResult.strOptDns2, DHCP_MAX_FILE_BYTES, result.strDns2.c_str()) != EOK) {
45         DHCP_LOGE("ResultInfoCopy strOptDns2 strcpy_s failed!");
46     }
47     if (strcpy_s(dhcpResult.strOptRouter1, DHCP_MAX_FILE_BYTES, result.strRouter1.c_str()) != EOK) {
48         DHCP_LOGE("ResultInfoCopy strOptRouter1 strcpy_s failed!");
49     }
50     if (strcpy_s(dhcpResult.strOptRouter2, DHCP_MAX_FILE_BYTES, result.strRouter2.c_str()) != EOK) {
51         DHCP_LOGE("ResultInfoCopy strOptRouter2 strcpy_s failed!");
52     }
53     if (strcpy_s(dhcpResult.strOptVendor, DHCP_MAX_FILE_BYTES, result.strVendor.c_str()) != EOK) {
54         DHCP_LOGE("ResultInfoCopy strOptVendor strcpy_s failed!");
55     }
56     if (strcpy_s(dhcpResult.strOptLinkIpv6Addr, DHCP_MAX_FILE_BYTES, result.strLinkIpv6Addr.c_str()) != EOK) {
57         DHCP_LOGE("ResultInfoCopy strOptLinkIpv6Addr strcpy_s failed!");
58     }
59     if (strcpy_s(dhcpResult.strOptRandIpv6Addr, DHCP_MAX_FILE_BYTES, result.strRandIpv6Addr.c_str()) != EOK) {
60         DHCP_LOGE("ResultInfoCopy strOptRandIpv6Addr strcpy_s failed!");
61     }
62     if (strcpy_s(dhcpResult.strOptLocalAddr1, DHCP_MAX_FILE_BYTES, result.strLocalAddr1.c_str()) != EOK) {
63         DHCP_LOGE("ResultInfoCopy strOptLocalAddr1 strcpy_s failed!");
64     }
65     if (strcpy_s(dhcpResult.strOptLocalAddr2, DHCP_MAX_FILE_BYTES, result.strLocalAddr2.c_str()) != EOK) {
66         DHCP_LOGE("ResultInfoCopy strOptLocalAddr2 strcpy_s failed!");
67     }
68     for (size_t i = 0; i < result.vectorDnsAddr.size(); i++) {
69         if (i >= DHCP_DNS_MAX_NUMBER) {
70             DHCP_LOGE("ResultInfoCopy break, i:%{public}zu, dns max number:%{public}d", i, DHCP_DNS_MAX_NUMBER);
71             break;
72         }
73         if (strncpy_s(dhcpResult.dnsList.dnsAddr[i], DHCP_DNS_DATA_MAX_LEN, result.vectorDnsAddr[i].c_str(),
74             result.vectorDnsAddr[i].length()) != EOK) {
75             DHCP_LOGE("ResultInfoCopy, strncpy_s failed, i:%{public}zu", i);
76         } else {
77             dhcpResult.dnsList.dnsNumber++;
78         }
79     }
80 }
81 
OnIpSuccessChanged(int status,const std::string & ifname,OHOS::DHCP::DhcpResult & result)82 void DhcpClientCallBack::OnIpSuccessChanged(int status, const std::string& ifname, OHOS::DHCP::DhcpResult& result)
83     __attribute__((no_sanitize("cfi")))
84 {
85     DHCP_LOGI("OnIpSuccessChanged status:%{public}d,ifname:%{public}s", status, ifname.c_str());
86     DhcpResult dhcpResult;
87     dhcpResult.iptype = result.iptype;
88     dhcpResult.isOptSuc = result.isOptSuc;
89     dhcpResult.uOptLeasetime = result.uLeaseTime;
90     dhcpResult.uAddTime = result.uAddTime;
91     dhcpResult.uGetTime = result.uGetTime;
92     ResultInfoCopy(dhcpResult, result);
93     DHCP_LOGI("ResultInfoCopy dstDnsNumber:%{public}u srcDnsNumber:%{public}zu", dhcpResult.dnsList.dnsNumber,
94         result.vectorDnsAddr.size());
95 
96     std::lock_guard<std::mutex> autoLock(callBackMutex);
97     auto iter = mapClientCallBack.find(ifname);
98     if ((iter != mapClientCallBack.end()) && (iter->second != nullptr) &&
99         (iter->second->OnIpSuccessChanged != nullptr)) {
100         DHCP_LOGI("OnIpSuccessChanged callbackEvent status:%{public}d", status);
101         iter->second->OnIpSuccessChanged(status, ifname.c_str(), &dhcpResult);
102     } else {
103         DHCP_LOGE("OnIpSuccessChanged callbackEvent failed!");
104     }
105 }
106 
OnIpFailChanged(int status,const std::string & ifname,const std::string & reason)107 void DhcpClientCallBack::OnIpFailChanged(int status, const std::string& ifname, const std::string& reason)
108     __attribute__((no_sanitize("cfi")))
109 {
110     DHCP_LOGI("OnIpFailChanged status:%{public}d, ifname:%{public}s, reason:%{public}s", status, ifname.c_str(),
111         reason.c_str());
112     std::lock_guard<std::mutex> autoLock(callBackMutex);
113     auto iter = mapClientCallBack.find(ifname);
114     if ((iter != mapClientCallBack.end()) && (iter->second != nullptr) && (iter->second->OnIpFailChanged != nullptr)) {
115         DHCP_LOGI("OnIpFailChanged callbackEvent status:%{public}d", status);
116         iter->second->OnIpFailChanged(status, ifname.c_str(), reason.c_str());
117     } else {
118         DHCP_LOGE("OnIpFailChanged callbackEvent failed!");
119     }
120 }
121 #ifndef OHOS_ARCH_LITE
OnDhcpOfferReport(int status,const std::string & ifname,OHOS::DHCP::DhcpResult & result)122 void DhcpClientCallBack::OnDhcpOfferReport(int status, const std::string& ifname, OHOS::DHCP::DhcpResult& result)
123 {
124     DHCP_LOGI("OnDhcpOfferReport status:%{public}d,ifname:%{public}s", status, ifname.c_str());
125     DhcpResult dhcpResult;
126     dhcpResult.iptype = result.iptype;
127     dhcpResult.isOptSuc = result.isOptSuc;
128     dhcpResult.uOptLeasetime = result.uLeaseTime;
129     dhcpResult.uAddTime = result.uAddTime;
130     dhcpResult.uGetTime = result.uGetTime;
131     ResultInfoCopy(dhcpResult, result);
132     std::lock_guard<std::mutex> autoLock(mapReportMutex_);
133     auto iter = mapDhcpClientReport_.find(ifname);
134     if ((iter != mapDhcpClientReport_.end()) && (iter->second != nullptr) &&
135         (iter->second->OnDhcpClientReport != nullptr)) {
136         iter->second->OnDhcpClientReport(status, ifname.c_str(), &dhcpResult);
137     } else {
138         DHCP_LOGE("OnDhcpOfferReport callbackEvent failed!");
139     }
140 }
141 
AsObject()142 OHOS::sptr<OHOS::IRemoteObject> DhcpClientCallBack::AsObject()
143 {
144     DHCP_LOGI("DhcpClientCallBack AsObject!");
145     return nullptr;
146 }
147 #endif
RegisterCallBack(const std::string & ifname,const ClientCallBack * event)148 void DhcpClientCallBack::RegisterCallBack(const std::string& ifname, const ClientCallBack *event)
149 {
150     if (event == nullptr) {
151         DHCP_LOGE("DhcpClientCallBack event is nullptr!");
152         return;
153     }
154     std::lock_guard<std::mutex> autoLock(callBackMutex);
155     auto iter = mapClientCallBack.find(ifname);
156     if (iter != mapClientCallBack.end()) {
157         iter->second = event;
158         DHCP_LOGI("Client RegisterCallBack update event, ifname:%{public}s", ifname.c_str());
159     } else {
160         mapClientCallBack.emplace(std::make_pair(ifname, event));
161         DHCP_LOGI("Client RegisterCallBack add event, ifname:%{public}s", ifname.c_str());
162     }
163 }
164 
UnRegisterCallBack(const std::string & ifname)165 void DhcpClientCallBack::UnRegisterCallBack(const std::string& ifname)
166 {
167     if (ifname.empty()) {
168         DHCP_LOGE("Client UnRegisterCallBack ifname is empty!");
169         return;
170     }
171     std::lock_guard<std::mutex> autoLock(callBackMutex);
172     auto iter = mapClientCallBack.find(ifname);
173     if (iter != mapClientCallBack.end()) {
174         mapClientCallBack.erase(iter);
175         DHCP_LOGI("Client UnRegisterCallBack erase ifname:%{public}s", ifname.c_str());
176     } else {
177         DHCP_LOGI("Client UnRegisterCallBack not find, ifname:%{public}s", ifname.c_str());
178     }
179 }
180 
RegisterDhcpClientReportCallBack(const std::string & ifname,const DhcpClientReport * event)181 void DhcpClientCallBack::RegisterDhcpClientReportCallBack(const std::string& ifname, const DhcpClientReport *event)
182 {
183     if (event == nullptr) {
184         DHCP_LOGE("RegisterDhcpClientReportCallBack event is nullptr!");
185         return;
186     }
187     std::lock_guard<std::mutex> autoLock(mapReportMutex_);
188     auto iter = mapDhcpClientReport_.find(ifname);
189     if (iter != mapDhcpClientReport_.end()) {
190         iter->second = event;
191         DHCP_LOGI("RegisterDhcpClientReportCallBack update event, ifname:%{public}s", ifname.c_str());
192     } else {
193         mapDhcpClientReport_.emplace(std::make_pair(ifname, event));
194         DHCP_LOGI("RegisterDhcpClientReportCallBack add event, ifname:%{public}s", ifname.c_str());
195     }
196 }
197 
DhcpServerCallBack()198 DhcpServerCallBack::DhcpServerCallBack()
199 {
200     DHCP_LOGI("DhcpServerCallBack");
201 }
202 
~DhcpServerCallBack()203 DhcpServerCallBack::~DhcpServerCallBack()
204 {
205     DHCP_LOGI("~DhcpServerCallBack");
206 }
207 
OnServerStatusChanged(int status)208 void DhcpServerCallBack::OnServerStatusChanged(int status)
209 {
210     DHCP_LOGI("DhcpServerCallBack OnServerStatusChanged status:%{public}d", status);
211 }
212 
OnServerLeasesChanged(const std::string & ifname,std::vector<std::string> & leases)213 void DhcpServerCallBack::OnServerLeasesChanged(const std::string& ifname, std::vector<std::string>& leases)
214 {
215     DHCP_LOGI("DhcpServerCallBack OnServerLeasesChanged ifname:%{public}s", ifname.c_str());
216 }
217 
OnServerSerExitChanged(const std::string & ifname)218 void DhcpServerCallBack::OnServerSerExitChanged(const std::string& ifname)
219 {
220     DHCP_LOGI("DhcpServerCallBack OnServerSerExitChanged ifname:%{public}s", ifname.c_str());
221 }
222 
OnServerSuccess(const std::string & ifname,std::vector<DhcpStationInfo> & stationInfos)223 void DhcpServerCallBack::OnServerSuccess(const std::string& ifname, std::vector<DhcpStationInfo>& stationInfos)
224 {
225     size_t size = stationInfos.size();
226     DHCP_LOGI("DhcpServerCallBack OnServerSuccess ifname:%{public}s size:%{public}zu", ifname.c_str(), size);
227     std::lock_guard<std::mutex> autoLock(callBackServerMutex);
228     auto iter = mapServerCallBack.find(ifname);
229     if ((iter != mapServerCallBack.end()) && (iter->second != nullptr) && (iter->second->OnServerSuccess != nullptr) &&
230         (size > 0)) {
231         DhcpStationInfo *infos = (struct DhcpStationInfo*)malloc(size * sizeof(DhcpStationInfo));
232         if (infos == nullptr) {
233             DHCP_LOGE("malloc failed");
234             return;
235         }
236         for (size_t i = 0; i < size; i++) {
237             if (strcpy_s(infos[i].ipAddr, sizeof(infos[i].ipAddr), stationInfos[i].ipAddr) != EOK) {
238                 DHCP_LOGE("OnServerSuccess strcpy_s ipAddr failed!");
239                 free(infos);
240                 infos = nullptr;
241                 return;
242             }
243             if (strcpy_s(infos[i].macAddr, sizeof(infos[i].macAddr), stationInfos[i].macAddr) != EOK) {
244                 DHCP_LOGE("OnServerSuccess strcpy_s macAddr failed!");
245                 free(infos);
246                 infos = nullptr;
247                 return;
248             }
249             if (strcpy_s(infos[i].deviceName, sizeof(infos[i].deviceName), stationInfos[i].deviceName) != EOK) {
250                 DHCP_LOGE("OnServerSuccess strcpy_s deviceName failed!");
251                 free(infos);
252                 infos = nullptr;
253                 return;
254             }
255         }
256         iter->second->OnServerSuccess(ifname.c_str(), infos, size);
257         free(infos);
258         infos = nullptr;
259         DHCP_LOGI("OnServerSuccess callbackEvent ok!");
260     } else {
261         DHCP_LOGE("OnServerSuccess not find callback!");
262     }
263 }
264 
265 #ifndef OHOS_ARCH_LITE
AsObject()266 OHOS::sptr<OHOS::IRemoteObject> DhcpServerCallBack::AsObject()
267 {
268     DHCP_LOGI("DhcpServerCallBack AsObject!");
269     return nullptr;
270 }
271 #endif
RegisterCallBack(const std::string & ifname,const ServerCallBack * event)272 void DhcpServerCallBack::RegisterCallBack(const std::string& ifname, const ServerCallBack *event)
273 {
274     if (event == nullptr) {
275         DHCP_LOGE("DhcpServerCallBack event is nullptr!");
276         return;
277     }
278     std::lock_guard<std::mutex> autoLock(callBackServerMutex);
279     auto iter = mapServerCallBack.find(ifname);
280     if (iter != mapServerCallBack.end()) {
281         iter->second = event;
282         DHCP_LOGI("Server RegisterCallBack update event, ifname:%{public}s", ifname.c_str());
283     } else {
284         mapServerCallBack.emplace(std::make_pair(ifname, event));
285         DHCP_LOGI("Server RegisterCallBack add event, ifname:%{public}s", ifname.c_str());
286     }
287 }
288 
UnRegisterCallBack(const std::string & ifname)289 void DhcpServerCallBack::UnRegisterCallBack(const std::string& ifname)
290 {
291     if (ifname.empty()) {
292         DHCP_LOGE("Server UnRegisterCallBack ifname is empty!");
293         return;
294     }
295     std::lock_guard<std::mutex> autoLock(callBackServerMutex);
296     auto iter = mapServerCallBack.find(ifname);
297     if (iter != mapServerCallBack.end()) {
298         mapServerCallBack.erase(iter);
299         DHCP_LOGI("Server UnRegisterCallBack erase ifname:%{public}s", ifname.c_str());
300     } else {
301         DHCP_LOGI("Server UnRegisterCallBack not find, ifname:%{public}s", ifname.c_str());
302     }
303 }
304