1 /*
2 * Copyright (c) 2022-2024 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
16 #include "ethernet_management.h"
17
18 #include <regex>
19 #include <thread>
20 #include <pthread.h>
21 #include <unistd.h>
22 #include <sys/ioctl.h>
23 #include <vector>
24
25 #include "net_manager_constants.h"
26 #include "netmgr_ext_log_wrapper.h"
27 #include "netsys_controller.h"
28 #include "securec.h"
29
30 namespace OHOS {
31 namespace NetManagerStandard {
32 const std::string IFACE_MATCH = "eth\\d";
33 constexpr const char *IFACE_LINK_UP = "up";
34 constexpr const char *IFACE_RUNNING = "running";
35 constexpr int SLEEP_TIME_S = 2;
36 constexpr uint32_t INDEX_ONE = 1;
37 constexpr uint32_t INDEX_TWO = 2;
38 constexpr uint32_t INDEX_THREE = 3;
39 constexpr uint32_t INDEX_FOUR = 4;
40 constexpr uint32_t INDEX_FIVE = 5;
41 constexpr uint32_t BUFFER_SIZE = 64;
OnDhcpSuccess(EthernetDhcpCallback::DhcpResult &dhcpResult)42 int32_t EthernetManagement::EhternetDhcpNotifyCallback::OnDhcpSuccess(EthernetDhcpCallback::DhcpResult &dhcpResult)
43 {
44 ethernetManagement_.UpdateDevInterfaceLinkInfo(dhcpResult);
45 return 0;
46 }
47
OnInterfaceAddressUpdated(const std::string &, const std::string &, int, int)48 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceAddressUpdated(const std::string &,
49 const std::string &, int, int)
50 {
51 return 0;
52 }
53
OnInterfaceAddressRemoved(const std::string &, const std::string &, int, int)54 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceAddressRemoved(const std::string &,
55 const std::string &, int, int)
56 {
57 return 0;
58 }
59
OnInterfaceAdded(const std::string &iface)60 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceAdded(const std::string &iface)
61 {
62 std::regex re(IFACE_MATCH);
63 if (std::regex_search(iface, re)) {
64 ethernetManagement_.DevInterfaceAdd(iface);
65 if (NetsysController::GetInstance().SetInterfaceUp(iface) != ERR_NONE) {
66 NETMGR_EXT_LOG_E("Iface[%{public}s] added set up fail!", iface.c_str());
67 }
68 }
69 return 0;
70 }
71
OnInterfaceRemoved(const std::string &iface)72 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceRemoved(const std::string &iface)
73 {
74 std::regex re(IFACE_MATCH);
75 if (std::regex_search(iface, re)) {
76 ethernetManagement_.DevInterfaceRemove(iface);
77 if (NetsysController::GetInstance().SetInterfaceDown(iface) != ERR_NONE) {
78 NETMGR_EXT_LOG_E("Iface[%{public}s] added set down fail!", iface.c_str());
79 }
80 }
81 return 0;
82 }
83
OnInterfaceChanged(const std::string &, bool)84 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceChanged(const std::string &, bool)
85 {
86 return 0;
87 }
88
OnInterfaceLinkStateChanged(const std::string &ifName, bool up)89 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
90 {
91 auto startTime = std::chrono::steady_clock::now();
92 std::regex re(IFACE_MATCH);
93 if (std::regex_search(ifName, re)) {
94 ethernetManagement_.UpdateInterfaceState(ifName, up);
95 }
96 auto endTime = std::chrono::steady_clock::now();
97 auto durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime);
98 NETMGR_EXT_LOG_I("OnInterfaceLinkStateChanged iface[%{public}s] up[%{public}d], cost=%{public}lld",
99 ifName.c_str(), up, durationNs.count());
100 return 0;
101 }
102
OnRouteChanged(bool, const std::string &, const std::string &, const std::string &)103 int32_t EthernetManagement::DevInterfaceStateCallback::OnRouteChanged(bool, const std::string &, const std::string &,
104 const std::string &)
105 {
106 return 0;
107 }
108
OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult)109 int32_t EthernetManagement::DevInterfaceStateCallback::OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult)
110 {
111 return 0;
112 }
113
OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface)114 int32_t EthernetManagement::DevInterfaceStateCallback::OnBandwidthReachedLimit(const std::string &limitName,
115 const std::string &iface)
116 {
117 return 0;
118 }
119
GetInstance()120 EthernetManagement &EthernetManagement::GetInstance()
121 {
122 static EthernetManagement gInstance;
123 return gInstance;
124 }
125
EthernetManagement()126 EthernetManagement::EthernetManagement()
127 {
128 ethDhcpController_ = std::make_unique<EthernetDhcpController>();
129 ethDhcpNotifyCallback_ = new (std::nothrow) EhternetDhcpNotifyCallback(*this);
130 if (ethDhcpNotifyCallback_ != nullptr) {
131 ethDhcpController_->RegisterDhcpCallback(ethDhcpNotifyCallback_);
132 }
133
134 ethDevInterfaceStateCallback_ = new (std::nothrow) DevInterfaceStateCallback(*this);
135 ethConfiguration_ = std::make_unique<EthernetConfiguration>();
136 ethConfiguration_->ReadSystemConfiguration(devCaps_, devCfgs_);
137 ethLanManageMent_ = std::make_unique<EthernetLanManagement>();
138 }
139
140 EthernetManagement::~EthernetManagement() = default;
141
UpdateInterfaceState(const std::string &dev, bool up)142 void EthernetManagement::UpdateInterfaceState(const std::string &dev, bool up)
143 {
144 NETMGR_EXT_LOG_D("EthernetManagement UpdateInterfaceState dev[%{public}s] up[%{public}d]", dev.c_str(), up);
145 std::unique_lock<std::mutex> lock(mutex_);
146 auto fit = devs_.find(dev);
147 if (fit == devs_.end()) {
148 return;
149 }
150 sptr<DevInterfaceState> devState = fit->second;
151 if (devState == nullptr) {
152 NETMGR_EXT_LOG_E("devState is nullptr");
153 return;
154 }
155 devState->SetLinkUp(up);
156 IPSetMode mode = devState->GetIPSetMode();
157 bool dhcpReqState = devState->GetDhcpReqState();
158 NETMGR_EXT_LOG_D("EthernetManagement UpdateInterfaceState mode[%{public}d] dhcpReqState[%{public}d]",
159 static_cast<int32_t>(mode), dhcpReqState);
160 if (up) {
161 if (!devState->IsLanIface()) {
162 devState->RemoteUpdateNetSupplierInfo();
163 }
164 if ((mode == DHCP || mode == LAN_DHCP) && !dhcpReqState) {
165 StartDhcpClient(dev, devState);
166 } else {
167 if (devState->IsLanIface()) {
168 ethLanManageMent_->UpdateLanLinkInfo(devState);
169 } else {
170 devState->RemoteUpdateNetLinkInfo();
171 }
172 }
173 } else {
174 if ((mode == DHCP || mode == LAN_DHCP) && dhcpReqState) {
175 StopDhcpClient(dev, devState);
176 }
177 if (devState->IsLanIface()) {
178 ethLanManageMent_->ReleaseLanNetLink(devState);
179 } else {
180 devState->RemoteUpdateNetSupplierInfo();
181 }
182 netLinkConfigs_[dev] = nullptr;
183 }
184 }
185
GetMacAddress(std::vector<MacAddressInfo> &macAddrList)186 int32_t EthernetManagement::GetMacAddress(std::vector<MacAddressInfo> &macAddrList)
187 {
188 std::regex re(IFACE_MATCH);
189 std::vector<std::string> ifaceLists = NetsysController::GetInstance().InterfaceGetList();
190 if (ifaceLists.empty()) {
191 NETMGR_EXT_LOG_E("EthernetManagement iface list is empty");
192 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
193 }
194 for (const auto &iface : ifaceLists) {
195 if (std::regex_search(iface, re)) {
196 MacAddressInfo macAddressInfo;
197 auto spMacAddr = GetMacAddr(iface);
198 if (spMacAddr.c_str() == nullptr) {
199 NETMGR_EXT_LOG_E("The iface[%{public}s] device does not find MAC address", iface.c_str());
200 continue;
201 }
202 macAddressInfo.iface_ = iface;
203 macAddressInfo.macAddress_ = spMacAddr;
204 macAddrList.push_back(macAddressInfo);
205 }
206 }
207 if (macAddrList.size() == 0) {
208 NETMGR_EXT_LOG_E("EthernetManagement mac address list is empty");
209 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
210 }
211 return NETMANAGER_EXT_SUCCESS;
212 }
213
GetMacAddr(const std::string &iface)214 std::string EthernetManagement::GetMacAddr(const std::string &iface)
215 {
216 NETMGR_EXT_LOG_D("GetMacAddr when iface is [%{public}s]", iface.c_str());
217 std::string macAddr;
218
219 int fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
220 struct ifreq ifr = {};
221 strncpy_s(ifr.ifr_name, IFNAMSIZ, iface.c_str(), iface.length());
222
223 if (ioctl(fd, SIOCGIFHWADDR, &ifr) != -1) {
224 macAddr = HwAddrToStr(ifr.ifr_hwaddr.sa_data);
225 }
226 close(fd);
227 return macAddr;
228 }
229
HwAddrToStr(char *hwaddr)230 std::string EthernetManagement::HwAddrToStr(char *hwaddr)
231 {
232 char buf[BUFFER_SIZE] = {'\0'};
233 if (hwaddr == nullptr) {
234 NETMGR_EXT_LOG_E("hwaddr is nullptr");
235 return "";
236 }
237 errno_t result =
238 sprintf_s(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x", hwaddr[0], hwaddr[INDEX_ONE],
239 hwaddr[INDEX_TWO], hwaddr[INDEX_THREE], hwaddr[INDEX_FOUR],
240 hwaddr[INDEX_FIVE]);
241 if (result < 0) {
242 NETMGR_EXT_LOG_E("[hwAddrToStr] result error : [%{public}d]", result);
243 return "";
244 }
245 return std::string(buf);
246 }
247
UpdateDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> cfg)248 int32_t EthernetManagement::UpdateDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> cfg)
249 {
250 if (cfg == nullptr) {
251 NETMGR_EXT_LOG_E("cfg is nullptr");
252 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
253 }
254 std::unique_lock<std::mutex> lock(mutex_);
255 auto fit = devs_.find(iface);
256 if (fit == devs_.end() || fit->second == nullptr) {
257 NETMGR_EXT_LOG_E("The iface[%{public}s] device or device information does not exist", iface.c_str());
258 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
259 }
260 if (!fit->second->GetLinkUp()) {
261 NETMGR_EXT_LOG_E("The iface[%{public}s] device is unlink", iface.c_str());
262 return ETHERNET_ERR_DEVICE_NOT_LINK;
263 }
264 if (!ModeInputCheck(fit->second->GetIfcfg()->mode_, cfg->mode_)) {
265 NETMGR_EXT_LOG_E("The iface[%{public}s] device can not exchange between WAN and LAN", iface.c_str());
266 return NETMANAGER_ERR_INVALID_PARAMETER;
267 }
268 if (!ethConfiguration_->WriteUserConfiguration(iface, cfg)) {
269 NETMGR_EXT_LOG_E("EthernetManagement write user configurations error!");
270 return ETHERNET_ERR_USER_CONIFGURATION_WRITE_FAIL;
271 }
272 if (fit->second->GetIfcfg()->mode_ != cfg->mode_) {
273 if (cfg->mode_ == DHCP || cfg->mode_ == LAN_DHCP) {
274 StartDhcpClient(iface, fit->second);
275 } else {
276 StopDhcpClient(iface, fit->second);
277 netLinkConfigs_[iface] = nullptr;
278 }
279 } else if (cfg->mode_ == DHCP) {
280 fit->second->UpdateNetHttpProxy(cfg->httpProxy_);
281 }
282 if (fit->second->IsLanIface()) {
283 ethLanManageMent_->GetOldLinkInfo(fit->second);
284 fit->second->SetLancfg(cfg);
285 ethLanManageMent_->UpdateLanLinkInfo(fit->second);
286 } else {
287 fit->second->SetIfcfg(cfg);
288 }
289 devCfgs_[iface] = cfg;
290 return NETMANAGER_EXT_SUCCESS;
291 }
292
UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult &dhcpResult)293 int32_t EthernetManagement::UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult &dhcpResult)
294 {
295 NETMGR_EXT_LOG_D("EthernetManagement::UpdateDevInterfaceLinkInfo");
296 std::lock_guard<std::mutex> locker(mutex_);
297 auto fit = devs_.find(dhcpResult.iface);
298 if (fit == devs_.end() || fit->second == nullptr) {
299 NETMGR_EXT_LOG_E("The iface[%{public}s] device or device information does not exist", dhcpResult.iface.c_str());
300 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
301 }
302 if (!fit->second->GetLinkUp()) {
303 NETMGR_EXT_LOG_E("The iface[%{public}s] The device is not turned on", dhcpResult.iface.c_str());
304 return ETHERNET_ERR_DEVICE_NOT_LINK;
305 }
306
307 IPSetMode mode = fit->second->GetIPSetMode();
308 if (mode == IPSetMode::STATIC || mode == IPSetMode::LAN_STATIC) {
309 NETMGR_EXT_LOG_E("The iface[%{public}s] set mode is STATIC now", dhcpResult.iface.c_str());
310 return ETHERNET_ERR_DEVICE_NOT_LINK;
311 }
312
313 auto &config = netLinkConfigs_[dhcpResult.iface];
314 if (config == nullptr) {
315 config = new (std::nothrow) StaticConfiguration();
316 if (config == nullptr) {
317 NETMGR_EXT_LOG_E("Iface:%{public}s's link info config is nullptr", dhcpResult.iface.c_str());
318 }
319 }
320
321 if (!ethConfiguration_->ConvertToConfiguration(dhcpResult, config)) {
322 NETMGR_EXT_LOG_E("EthernetManagement dhcp convert to configurations error!");
323 return ETHERNET_ERR_CONVERT_CONFIGURATINO_FAIL;
324 }
325 if (fit->second->IsLanIface()) {
326 ethLanManageMent_->GetOldLinkInfo(fit->second);
327 fit->second->UpdateLanLinkInfo(config);
328 ethLanManageMent_->UpdateLanLinkInfo(fit->second);
329 } else {
330 fit->second->UpdateLinkInfo(config);
331 fit->second->RemoteUpdateNetLinkInfo();
332 }
333 return NETMANAGER_EXT_SUCCESS;
334 }
335
GetDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig)336 int32_t EthernetManagement::GetDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig)
337 {
338 std::unique_lock<std::mutex> lock(mutex_);
339 auto fit = devs_.find(iface);
340 if (fit == devs_.end() || fit->second == nullptr) {
341 NETMGR_EXT_LOG_E("The iface[%{public}s] device does not exist", iface.c_str());
342 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
343 }
344 if (!fit->second->GetLinkUp()) {
345 ifaceConfig = fit->second->GetIfcfg();
346 return NETMANAGER_EXT_SUCCESS;
347 }
348 auto temp = ethConfiguration_->MakeInterfaceConfiguration(fit->second->GetIfcfg(), fit->second->GetLinkInfo());
349 if (temp == nullptr) {
350 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
351 }
352 *ifaceConfig = *temp;
353 return NETMANAGER_EXT_SUCCESS;
354 }
355
IsIfaceActive(const std::string &iface, int32_t &activeStatus)356 int32_t EthernetManagement::IsIfaceActive(const std::string &iface, int32_t &activeStatus)
357 {
358 std::unique_lock<std::mutex> lock(mutex_);
359 auto fit = devs_.find(iface);
360 if (fit == devs_.end() || fit->second == nullptr) {
361 NETMGR_EXT_LOG_E("The iface[%{public}s] device does not exist", iface.c_str());
362 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
363 }
364 activeStatus = static_cast<int32_t>(fit->second->GetLinkUp());
365 return NETMANAGER_EXT_SUCCESS;
366 }
367
GetAllActiveIfaces(std::vector<std::string> &activeIfaces)368 int32_t EthernetManagement::GetAllActiveIfaces(std::vector<std::string> &activeIfaces)
369 {
370 std::unique_lock<std::mutex> lock(mutex_);
371 for (auto it = devs_.begin(); it != devs_.end(); ++it) {
372 if (it->second->GetLinkUp()) {
373 activeIfaces.push_back(it->first);
374 }
375 }
376 return NETMANAGER_EXT_SUCCESS;
377 }
378
ResetFactory()379 int32_t EthernetManagement::ResetFactory()
380 {
381 if (!ethConfiguration_->ClearAllUserConfiguration()) {
382 NETMGR_EXT_LOG_E("Failed to ResetFactory!");
383 return ETHERNET_ERR_USER_CONIFGURATION_CLEAR_FAIL;
384 }
385 NETMGR_EXT_LOG_I("Success to ResetFactory!");
386 return NETMANAGER_EXT_SUCCESS;
387 }
388
Init()389 void EthernetManagement::Init()
390 {
391 static const unsigned int SLEEP_TIME = 4;
392 std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIME));
393 if (ethDevInterfaceStateCallback_ != nullptr) {
394 NetsysController::GetInstance().RegisterCallback(ethDevInterfaceStateCallback_);
395 }
396 std::regex re(IFACE_MATCH);
397 std::vector<std::string> ifaces = NetsysController::GetInstance().InterfaceGetList();
398 if (ifaces.empty()) {
399 NETMGR_EXT_LOG_E("EthernetManagement link list is empty");
400 return;
401 }
402 NETMGR_EXT_LOG_D("EthernetManagement devs size[%{public}zd]", ifaces.size());
403 if (!ethConfiguration_->ReadUserConfiguration(devCfgs_)) {
404 NETMGR_EXT_LOG_E("EthernetManagement read user configurations error! ");
405 return;
406 }
407 for (const auto &devName : ifaces) {
408 NETMGR_EXT_LOG_D("EthernetManagement devName[%{public}s]", devName.c_str());
409 if (!std::regex_search(devName, re)) {
410 continue;
411 }
412 DevInterfaceAdd(devName);
413 }
414 std::thread t(&EthernetManagement::StartSetDevUpThd, &EthernetManagement::GetInstance());
415 std::string threadName = "SetDevUpThd";
416 pthread_setname_np(t.native_handle(), threadName.c_str());
417 t.detach();
418 NETMGR_EXT_LOG_D("EthernetManagement devs_ size[%{public}zd", devs_.size());
419 }
420
StartSetDevUpThd()421 void EthernetManagement::StartSetDevUpThd()
422 {
423 NETMGR_EXT_LOG_D("EthernetManagement StartSetDevUpThd in.");
424 std::map<std::string, sptr<DevInterfaceState>> tempDevMap;
425 {
426 std::unique_lock<std::mutex> lock(mutex_);
427 tempDevMap = devs_;
428 }
429
430 for (auto &dev : tempDevMap) {
431 std::string devName = dev.first;
432 if (IsIfaceLinkUp(devName)) {
433 continue;
434 }
435 while (true) {
436 if (NetsysController::GetInstance().SetInterfaceUp(devName) != ERR_NONE) {
437 sleep(SLEEP_TIME_S);
438 continue;
439 }
440 break;
441 }
442 }
443 }
444
IsIfaceLinkUp(const std::string &iface)445 bool EthernetManagement::IsIfaceLinkUp(const std::string &iface)
446 {
447 OHOS::nmd::InterfaceConfigurationParcel config;
448 config.ifName = iface;
449 if (NetsysController::GetInstance().GetInterfaceConfig(config) != ERR_NONE) {
450 return false;
451 }
452 if (std::find(config.flags.begin(), config.flags.end(), IFACE_LINK_UP) == config.flags.end() ||
453 std::find(config.flags.begin(), config.flags.end(), IFACE_RUNNING) == config.flags.end()) {
454 return false;
455 }
456 UpdateInterfaceState(iface, true);
457 return true;
458 }
459
StartDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState)460 void EthernetManagement::StartDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState)
461 {
462 NETMGR_EXT_LOG_D("EthernetManagement StartDhcpClient[%{public}s]", dev.c_str());
463 ethDhcpController_->StartClient(dev, true);
464 devState->SetDhcpReqState(true);
465 }
466
StopDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState)467 void EthernetManagement::StopDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState)
468 {
469 NETMGR_EXT_LOG_D("EthernetManagement StopDhcpClient[%{public}s]", dev.c_str());
470 ethDhcpController_->StopClient(dev, true);
471 devState->SetDhcpReqState(false);
472 }
473
DevInterfaceAdd(const std::string &devName)474 void EthernetManagement::DevInterfaceAdd(const std::string &devName)
475 {
476 NETMGR_EXT_LOG_D("Interface name:[%{public}s] add.", devName.c_str());
477 std::unique_lock<std::mutex> lock(mutex_);
478 auto fitDev = devs_.find(devName);
479 if (fitDev != devs_.end()) {
480 NETMGR_EXT_LOG_E("Interface name:[%{public}s] has added.", devName.c_str());
481 return;
482 }
483 sptr<DevInterfaceState> devState = new (std::nothrow) DevInterfaceState();
484 if (devState == nullptr) {
485 NETMGR_EXT_LOG_E("devState is nullptr");
486 return;
487 }
488 ethConfiguration_->ReadSystemConfiguration(devCaps_, devCfgs_);
489 devs_.insert(std::make_pair(devName, devState));
490 devState->SetDevName(devName);
491 auto fitCfg = devCfgs_.find(devName);
492 if (fitCfg != devCfgs_.end()) {
493 if (fitCfg->second->mode_ == LAN_STATIC || fitCfg->second->mode_ == LAN_DHCP) {
494 NETMGR_EXT_LOG_D("Lan Interface name:[%{public}s] add, mode [%{public}d]",
495 devName.c_str(), fitCfg->second->mode_);
496 devState->SetLancfg(fitCfg->second);
497 ethLanManageMent_->UpdateLanLinkInfo(devState);
498 return;
499 }
500 devState->RemoteRegisterNetSupplier();
501 devState->SetIfcfg(fitCfg->second);
502 } else {
503 sptr<InterfaceConfiguration> ifCfg = new (std::nothrow) InterfaceConfiguration();
504 if (ifCfg == nullptr) {
505 NETMGR_EXT_LOG_E("ifCfg is nullptr");
506 return;
507 }
508 ifCfg->mode_ = DHCP;
509 devState->RemoteRegisterNetSupplier();
510 devState->SetIfcfg(ifCfg);
511 }
512 auto fitCap = devCaps_.find(devName);
513 if (fitCap != devCaps_.end()) {
514 devState->SetNetCaps(fitCap->second);
515 }
516 }
517
DevInterfaceRemove(const std::string &devName)518 void EthernetManagement::DevInterfaceRemove(const std::string &devName)
519 {
520 NETMGR_EXT_LOG_D("Interface name:[%{public}s] remove.", devName.c_str());
521 std::unique_lock<std::mutex> lock(mutex_);
522 auto fitDev = devs_.find(devName);
523 if (fitDev != devs_.end()) {
524 if (fitDev->second != nullptr) {
525 fitDev->second->RemoteUnregisterNetSupplier();
526 }
527 devs_.erase(fitDev);
528 }
529 }
530
GetDumpInfo(std::string &info)531 void EthernetManagement::GetDumpInfo(std::string &info)
532 {
533 std::for_each(devs_.begin(), devs_.end(), [&info](const auto &dev) { dev.second->GetDumpInfo(info); });
534 }
535
ModeInputCheck(IPSetMode origin, IPSetMode input)536 bool EthernetManagement::ModeInputCheck(IPSetMode origin, IPSetMode input)
537 {
538 if (origin == STATIC || origin == DHCP) {
539 if (input == LAN_STATIC || input == LAN_DHCP) {
540 return false;
541 }
542 } else if (origin == LAN_STATIC || origin == LAN_DHCP) {
543 if (input == STATIC || input == DHCP) {
544 return false;
545 }
546 }
547 return true;
548 }
549 } // namespace NetManagerStandard
550 } // namespace OHOS
551