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     std::string macAddr;
217 
218     int fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
219     struct ifreq ifr = {};
220     strncpy_s(ifr.ifr_name, IFNAMSIZ, iface.c_str(), iface.length());
221 
222     if (ioctl(fd, SIOCGIFHWADDR, &ifr) != -1) {
223         macAddr = HwAddrToStr(ifr.ifr_hwaddr.sa_data);
224     }
225     close(fd);
226     return macAddr;
227 }
228 
HwAddrToStr(char * hwaddr)229 std::string EthernetManagement::HwAddrToStr(char *hwaddr)
230 {
231     char buf[BUFFER_SIZE] = {'\0'};
232     if (hwaddr == nullptr) {
233         NETMGR_EXT_LOG_E("hwaddr is nullptr");
234         return "";
235     }
236     errno_t result =
237         sprintf_s(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x", hwaddr[0], hwaddr[INDEX_ONE],
238             hwaddr[INDEX_TWO], hwaddr[INDEX_THREE], hwaddr[INDEX_FOUR],
239             hwaddr[INDEX_FIVE]);
240     if (result < 0) {
241         NETMGR_EXT_LOG_E("[hwAddrToStr] result error : [%{public}d]", result);
242         return "";
243     }
244     return std::string(buf);
245 }
246 
UpdateDevInterfaceCfg(const std::string & iface,sptr<InterfaceConfiguration> cfg)247 int32_t EthernetManagement::UpdateDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> cfg)
248 {
249     if (cfg == nullptr) {
250         NETMGR_EXT_LOG_E("cfg is nullptr");
251         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
252     }
253     std::unique_lock<std::mutex> lock(mutex_);
254     auto fit = devs_.find(iface);
255     if (fit == devs_.end() || fit->second == nullptr) {
256         NETMGR_EXT_LOG_E("The iface[%{public}s] device or device information does not exist", iface.c_str());
257         return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
258     }
259     if (!fit->second->GetLinkUp()) {
260         NETMGR_EXT_LOG_E("The iface[%{public}s] device is unlink", iface.c_str());
261         return ETHERNET_ERR_DEVICE_NOT_LINK;
262     }
263     if (!ModeInputCheck(fit->second->GetIfcfg()->mode_, cfg->mode_)) {
264         NETMGR_EXT_LOG_E("The iface[%{public}s] device can not exchange between WAN and LAN", iface.c_str());
265         return NETMANAGER_ERR_INVALID_PARAMETER;
266     }
267     if (!ethConfiguration_->WriteUserConfiguration(iface, cfg)) {
268         NETMGR_EXT_LOG_E("EthernetManagement write user configurations error!");
269         return ETHERNET_ERR_USER_CONIFGURATION_WRITE_FAIL;
270     }
271     if (fit->second->GetIfcfg()->mode_ != cfg->mode_) {
272         if (cfg->mode_ == DHCP || cfg->mode_ == LAN_DHCP) {
273             StartDhcpClient(iface, fit->second);
274         } else {
275             StopDhcpClient(iface, fit->second);
276             netLinkConfigs_[iface] = nullptr;
277         }
278     } else if (cfg->mode_ == DHCP) {
279         fit->second->UpdateNetHttpProxy(cfg->httpProxy_);
280     }
281     if (fit->second->IsLanIface()) {
282         ethLanManageMent_->GetOldLinkInfo(fit->second);
283         fit->second->SetLancfg(cfg);
284         ethLanManageMent_->UpdateLanLinkInfo(fit->second);
285     } else {
286         fit->second->SetIfcfg(cfg);
287     }
288     devCfgs_[iface] = cfg;
289     return NETMANAGER_EXT_SUCCESS;
290 }
291 
UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult & dhcpResult)292 int32_t EthernetManagement::UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult &dhcpResult)
293 {
294     NETMGR_EXT_LOG_D("EthernetManagement::UpdateDevInterfaceLinkInfo");
295     std::lock_guard<std::mutex> locker(mutex_);
296     auto fit = devs_.find(dhcpResult.iface);
297     if (fit == devs_.end() || fit->second == nullptr) {
298         NETMGR_EXT_LOG_E("The iface[%{public}s] device or device information does not exist", dhcpResult.iface.c_str());
299         return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
300     }
301     if (!fit->second->GetLinkUp()) {
302         NETMGR_EXT_LOG_E("The iface[%{public}s] The device is not turned on", dhcpResult.iface.c_str());
303         return ETHERNET_ERR_DEVICE_NOT_LINK;
304     }
305 
306     IPSetMode mode = fit->second->GetIPSetMode();
307     if (mode == IPSetMode::STATIC || mode == IPSetMode::LAN_STATIC) {
308         NETMGR_EXT_LOG_E("The iface[%{public}s] set mode is STATIC now", dhcpResult.iface.c_str());
309         return ETHERNET_ERR_DEVICE_NOT_LINK;
310     }
311 
312     auto &config = netLinkConfigs_[dhcpResult.iface];
313     if (config == nullptr) {
314         config = new (std::nothrow) StaticConfiguration();
315         if (config == nullptr) {
316             NETMGR_EXT_LOG_E("Iface:%{public}s's link info config is nullptr", dhcpResult.iface.c_str());
317         }
318     }
319 
320     if (!ethConfiguration_->ConvertToConfiguration(dhcpResult, config)) {
321         NETMGR_EXT_LOG_E("EthernetManagement dhcp convert to configurations error!");
322         return ETHERNET_ERR_CONVERT_CONFIGURATINO_FAIL;
323     }
324     if (fit->second->IsLanIface()) {
325         ethLanManageMent_->GetOldLinkInfo(fit->second);
326         fit->second->UpdateLanLinkInfo(config);
327         ethLanManageMent_->UpdateLanLinkInfo(fit->second);
328     } else {
329         fit->second->UpdateLinkInfo(config);
330         fit->second->RemoteUpdateNetLinkInfo();
331     }
332     return NETMANAGER_EXT_SUCCESS;
333 }
334 
GetDevInterfaceCfg(const std::string & iface,sptr<InterfaceConfiguration> & ifaceConfig)335 int32_t EthernetManagement::GetDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig)
336 {
337     std::unique_lock<std::mutex> lock(mutex_);
338     auto fit = devs_.find(iface);
339     if (fit == devs_.end() || fit->second == nullptr) {
340         NETMGR_EXT_LOG_E("The iface[%{public}s] device does not exist", iface.c_str());
341         return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
342     }
343     if (!fit->second->GetLinkUp()) {
344         ifaceConfig = fit->second->GetIfcfg();
345         return NETMANAGER_EXT_SUCCESS;
346     }
347     auto temp = ethConfiguration_->MakeInterfaceConfiguration(fit->second->GetIfcfg(), fit->second->GetLinkInfo());
348     if (temp == nullptr) {
349         return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
350     }
351     *ifaceConfig = *temp;
352     return NETMANAGER_EXT_SUCCESS;
353 }
354 
IsIfaceActive(const std::string & iface,int32_t & activeStatus)355 int32_t EthernetManagement::IsIfaceActive(const std::string &iface, int32_t &activeStatus)
356 {
357     std::unique_lock<std::mutex> lock(mutex_);
358     auto fit = devs_.find(iface);
359     if (fit == devs_.end() || fit->second == nullptr) {
360         NETMGR_EXT_LOG_E("The iface[%{public}s] device does not exist", iface.c_str());
361         return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
362     }
363     activeStatus = static_cast<int32_t>(fit->second->GetLinkUp());
364     return NETMANAGER_EXT_SUCCESS;
365 }
366 
GetAllActiveIfaces(std::vector<std::string> & activeIfaces)367 int32_t EthernetManagement::GetAllActiveIfaces(std::vector<std::string> &activeIfaces)
368 {
369     std::unique_lock<std::mutex> lock(mutex_);
370     for (auto it = devs_.begin(); it != devs_.end(); ++it) {
371         if (it->second->GetLinkUp()) {
372             activeIfaces.push_back(it->first);
373         }
374     }
375     return NETMANAGER_EXT_SUCCESS;
376 }
377 
ResetFactory()378 int32_t EthernetManagement::ResetFactory()
379 {
380     if (!ethConfiguration_->ClearAllUserConfiguration()) {
381         NETMGR_EXT_LOG_E("Failed to ResetFactory!");
382         return ETHERNET_ERR_USER_CONIFGURATION_CLEAR_FAIL;
383     }
384     NETMGR_EXT_LOG_I("Success to ResetFactory!");
385     return NETMANAGER_EXT_SUCCESS;
386 }
387 
Init()388 void EthernetManagement::Init()
389 {
390     static const unsigned int SLEEP_TIME = 4;
391     std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIME));
392     if (ethDevInterfaceStateCallback_ != nullptr) {
393         NetsysController::GetInstance().RegisterCallback(ethDevInterfaceStateCallback_);
394     }
395     std::regex re(IFACE_MATCH);
396     std::vector<std::string> ifaces = NetsysController::GetInstance().InterfaceGetList();
397     if (ifaces.empty()) {
398         NETMGR_EXT_LOG_E("EthernetManagement link list is empty");
399         return;
400     }
401     NETMGR_EXT_LOG_D("EthernetManagement devs size[%{public}zd]", ifaces.size());
402     if (!ethConfiguration_->ReadUserConfiguration(devCfgs_)) {
403         NETMGR_EXT_LOG_E("EthernetManagement read user configurations error! ");
404         return;
405     }
406     for (const auto &devName : ifaces) {
407         NETMGR_EXT_LOG_D("EthernetManagement devName[%{public}s]", devName.c_str());
408         if (!std::regex_search(devName, re)) {
409             continue;
410         }
411         DevInterfaceAdd(devName);
412     }
413     std::thread t(&EthernetManagement::StartSetDevUpThd, &EthernetManagement::GetInstance());
414     std::string threadName = "SetDevUpThd";
415     pthread_setname_np(t.native_handle(), threadName.c_str());
416     t.detach();
417     NETMGR_EXT_LOG_D("EthernetManagement devs_ size[%{public}zd", devs_.size());
418 }
419 
StartSetDevUpThd()420 void EthernetManagement::StartSetDevUpThd()
421 {
422     NETMGR_EXT_LOG_D("EthernetManagement StartSetDevUpThd in.");
423     std::map<std::string, sptr<DevInterfaceState>> tempDevMap;
424     {
425         std::unique_lock<std::mutex> lock(mutex_);
426         tempDevMap = devs_;
427     }
428 
429     for (auto &dev : tempDevMap) {
430         std::string devName = dev.first;
431         if (IsIfaceLinkUp(devName)) {
432             continue;
433         }
434         while (true) {
435             if (NetsysController::GetInstance().SetInterfaceUp(devName) != ERR_NONE) {
436                 sleep(SLEEP_TIME_S);
437                 continue;
438             }
439             break;
440         }
441     }
442 }
443 
IsIfaceLinkUp(const std::string & iface)444 bool EthernetManagement::IsIfaceLinkUp(const std::string &iface)
445 {
446     OHOS::nmd::InterfaceConfigurationParcel config;
447     config.ifName = iface;
448     if (NetsysController::GetInstance().GetInterfaceConfig(config) != ERR_NONE) {
449         return false;
450     }
451     if (std::find(config.flags.begin(), config.flags.end(), IFACE_LINK_UP) == config.flags.end() ||
452         std::find(config.flags.begin(), config.flags.end(), IFACE_RUNNING) == config.flags.end()) {
453         return false;
454     }
455     UpdateInterfaceState(iface, true);
456     return true;
457 }
458 
StartDhcpClient(const std::string & dev,sptr<DevInterfaceState> & devState)459 void EthernetManagement::StartDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState)
460 {
461     NETMGR_EXT_LOG_D("EthernetManagement StartDhcpClient[%{public}s]", dev.c_str());
462     ethDhcpController_->StartClient(dev, true);
463     devState->SetDhcpReqState(true);
464 }
465 
StopDhcpClient(const std::string & dev,sptr<DevInterfaceState> & devState)466 void EthernetManagement::StopDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState)
467 {
468     NETMGR_EXT_LOG_D("EthernetManagement StopDhcpClient[%{public}s]", dev.c_str());
469     ethDhcpController_->StopClient(dev, true);
470     devState->SetDhcpReqState(false);
471 }
472 
DevInterfaceAdd(const std::string & devName)473 void EthernetManagement::DevInterfaceAdd(const std::string &devName)
474 {
475     NETMGR_EXT_LOG_D("Interface name:[%{public}s] add.", devName.c_str());
476     std::unique_lock<std::mutex> lock(mutex_);
477     auto fitDev = devs_.find(devName);
478     if (fitDev != devs_.end()) {
479         NETMGR_EXT_LOG_E("Interface name:[%{public}s] has added.", devName.c_str());
480         return;
481     }
482     sptr<DevInterfaceState> devState = new (std::nothrow) DevInterfaceState();
483     if (devState == nullptr) {
484         NETMGR_EXT_LOG_E("devState is nullptr");
485         return;
486     }
487     ethConfiguration_->ReadSystemConfiguration(devCaps_, devCfgs_);
488     devs_.insert(std::make_pair(devName, devState));
489     devState->SetDevName(devName);
490     auto fitCfg = devCfgs_.find(devName);
491     if (fitCfg != devCfgs_.end()) {
492         if (fitCfg->second->mode_ == LAN_STATIC || fitCfg->second->mode_ == LAN_DHCP) {
493             NETMGR_EXT_LOG_D("Lan Interface name:[%{public}s] add, mode [%{public}d]",
494                              devName.c_str(), fitCfg->second->mode_);
495             devState->SetLancfg(fitCfg->second);
496             ethLanManageMent_->UpdateLanLinkInfo(devState);
497             return;
498         }
499         devState->RemoteRegisterNetSupplier();
500         devState->SetIfcfg(fitCfg->second);
501     } else {
502         sptr<InterfaceConfiguration> ifCfg = new (std::nothrow) InterfaceConfiguration();
503         if (ifCfg == nullptr) {
504             NETMGR_EXT_LOG_E("ifCfg is nullptr");
505             return;
506         }
507         ifCfg->mode_ = DHCP;
508         devState->RemoteRegisterNetSupplier();
509         devState->SetIfcfg(ifCfg);
510     }
511     auto fitCap = devCaps_.find(devName);
512     if (fitCap != devCaps_.end()) {
513         devState->SetNetCaps(fitCap->second);
514     }
515 }
516 
DevInterfaceRemove(const std::string & devName)517 void EthernetManagement::DevInterfaceRemove(const std::string &devName)
518 {
519     NETMGR_EXT_LOG_D("Interface name:[%{public}s] remove.", devName.c_str());
520     std::unique_lock<std::mutex> lock(mutex_);
521     auto fitDev = devs_.find(devName);
522     if (fitDev != devs_.end()) {
523         if (fitDev->second != nullptr) {
524             fitDev->second->RemoteUnregisterNetSupplier();
525         }
526         devs_.erase(fitDev);
527     }
528 }
529 
GetDumpInfo(std::string & info)530 void EthernetManagement::GetDumpInfo(std::string &info)
531 {
532     std::for_each(devs_.begin(), devs_.end(), [&info](const auto &dev) { dev.second->GetDumpInfo(info); });
533 }
534 
ModeInputCheck(IPSetMode origin,IPSetMode input)535 bool EthernetManagement::ModeInputCheck(IPSetMode origin, IPSetMode input)
536 {
537     if (origin == STATIC || origin == DHCP) {
538         if (input == LAN_STATIC || input == LAN_DHCP) {
539             return false;
540         }
541     } else if (origin == LAN_STATIC || origin == LAN_DHCP) {
542         if (input == STATIC || input == DHCP) {
543             return false;
544         }
545     }
546     return true;
547 }
548 } // namespace NetManagerStandard
549 } // namespace OHOS
550