• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "ethernet_configuration.h"
16 
17 #include <arpa/inet.h>
18 #include <cerrno>
19 #include <cstdlib>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <fstream>
23 #include <limits>
24 #include <sstream>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <regex>
29 
30 #include "net_manager_constants.h"
31 #include "netmanager_base_common_utils.h"
32 #include "netmgr_ext_log_wrapper.h"
33 #include "route.h"
34 #include "securec.h"
35 
36 namespace OHOS {
37 namespace NetManagerStandard {
38 namespace {
39 const std::string IFACE_MATCH = "eth\\d";
40 const std::string CONFIG_KEY_ETH_COMPONENT_FLAG = "config_ethernet_interfaces";
41 const std::string CONFIG_KEY_ETH_IFACE = "iface";
42 const std::string CONFIG_KEY_ETH_CAPS = "caps";
43 const std::string CONFIG_KEY_ETH_IP = "ip";
44 const std::string CONFIG_KEY_ETH_GATEWAY = "gateway";
45 const std::string CONFIG_KEY_ETH_DNS = "dns";
46 const std::string CONFIG_KEY_ETH_NETMASK = "netmask";
47 const std::string CONFIG_KEY_ETH_ROUTE = "route";
48 const std::string CONFIG_KEY_ETH_ROUTE_MASK = "routemask";
49 constexpr int32_t MKDIR_ERR = -1;
50 constexpr int32_t USER_PATH_LEN = 25;
51 constexpr const char *FILE_OBLIQUE_LINE = "/";
52 constexpr const char *KEY_DEVICE = "DEVICE=";
53 constexpr const char *KEY_BOOTPROTO = "BOOTPROTO=";
54 constexpr const char *KEY_STATIC = "STATIC";
55 constexpr const char *KEY_DHCP = "DHCP";
56 constexpr const char *KEY_IPADDR = "IPADDR=";
57 constexpr const char *KEY_NETMASK = "NETMASK=";
58 constexpr const char *KEY_GATEWAY = "GATEWAY=";
59 constexpr const char *KEY_ROUTE = "ROUTE=";
60 constexpr const char *KEY_ROUTE_NETMASK = "ROUTE_NETMASK=";
61 constexpr const char *KEY_DNS = "DNS=";
62 constexpr const char *WRAP = "\n";
63 constexpr const char *DNS_SEPARATOR = ",";
64 constexpr const char *DEFAULT_NET_ADDR = "0.0.0.0";
65 constexpr const char *EMPTY_NET_ADDR = "*";
66 } // namespace
67 
EthernetConfiguration()68 EthernetConfiguration::EthernetConfiguration()
69 {
70     CreateDir(USER_CONFIG_DIR);
71 }
72 
ReadSysteamConfiguration(std::map<std::string,std::set<NetCap>> & devCaps,std::map<std::string,sptr<InterfaceConfiguration>> & devCfgs)73 bool EthernetConfiguration::ReadSysteamConfiguration(std::map<std::string, std::set<NetCap>> &devCaps,
74                                                      std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs)
75 {
76     const auto &jsonStr = ReadJsonFile(NETWORK_CONFIG_PATH);
77     if (jsonStr.length() == 0) {
78         NETMGR_EXT_LOG_E("ReadConfigData config file is return empty!");
79         return false;
80     }
81     const auto &jsonCfg = nlohmann::json::parse(jsonStr);
82     if (jsonCfg.find(CONFIG_KEY_ETH_COMPONENT_FLAG) == jsonCfg.end()) {
83         NETMGR_EXT_LOG_E("ReadConfigData not find network_ethernet_component!");
84         return false;
85     }
86     const auto &arrIface = jsonCfg.at(CONFIG_KEY_ETH_COMPONENT_FLAG);
87     NETMGR_EXT_LOG_D("read ConfigData ethValue:%{public}s", arrIface.dump().c_str());
88     for (const auto &item : arrIface) {
89         const auto &iface = item[CONFIG_KEY_ETH_IFACE].get<std::string>();
90         const auto &caps = item.at(CONFIG_KEY_ETH_CAPS).get<std::set<NetCap>>();
91         if (!caps.empty()) {
92             devCaps[iface] = caps;
93         }
94         const auto &fit = devCfgs.find(iface);
95         if (fit != devCfgs.end()) {
96             NETMGR_EXT_LOG_E("The iface=%{public}s device have set!", fit->first.c_str());
97             continue;
98         }
99         sptr<InterfaceConfiguration> config = ConvertJsonToConfiguration(item);
100         if (config == nullptr) {
101             NETMGR_EXT_LOG_E("config is nullptr");
102             return false;
103         }
104         std::regex re(IFACE_MATCH);
105         if (!item[CONFIG_KEY_ETH_IP].empty() && std::regex_search(iface, re)) {
106             devCfgs[iface] = config;
107         }
108     }
109     return true;
110 }
111 
ConvertJsonToConfiguration(const nlohmann::json & jsonData)112 sptr<InterfaceConfiguration> EthernetConfiguration::ConvertJsonToConfiguration(const nlohmann::json &jsonData)
113 {
114     sptr<InterfaceConfiguration> config = new (std::nothrow) InterfaceConfiguration();
115     if (config == nullptr) {
116         NETMGR_EXT_LOG_E("config is nullptr");
117         return nullptr;
118     }
119     config->mode_ = STATIC;
120     config->ipStatic_.ipAddr_.address_ = jsonData[CONFIG_KEY_ETH_IP];
121     config->ipStatic_.ipAddr_.netMask_ = jsonData[CONFIG_KEY_ETH_NETMASK];
122     config->ipStatic_.ipAddr_.family_ = static_cast<uint8_t>(CommonUtils::GetAddrFamily(jsonData[CONFIG_KEY_ETH_IP]));
123     int prefixLen = CommonUtils::GetMaskLength(jsonData[CONFIG_KEY_ETH_NETMASK]);
124     if (config->ipStatic_.ipAddr_.family_ == AF_INET) {
125         config->ipStatic_.ipAddr_.prefixlen_ = prefixLen;
126     }
127     config->ipStatic_.netMask_.address_ = jsonData[CONFIG_KEY_ETH_NETMASK];
128     config->ipStatic_.gateway_.address_ = jsonData[CONFIG_KEY_ETH_GATEWAY];
129     config->ipStatic_.gateway_.family_ =
130         static_cast<uint8_t>(CommonUtils::GetAddrFamily(jsonData[CONFIG_KEY_ETH_GATEWAY]));
131     if (config->ipStatic_.gateway_.family_ == AF_INET) {
132         config->ipStatic_.gateway_.prefixlen_ = prefixLen;
133     }
134     config->ipStatic_.route_.address_ = jsonData[CONFIG_KEY_ETH_ROUTE];
135     int routePrefixLen = 0;
136     if (!jsonData[CONFIG_KEY_ETH_ROUTE_MASK].empty()) {
137         routePrefixLen = CommonUtils::GetMaskLength(jsonData[CONFIG_KEY_ETH_ROUTE_MASK]);
138     }
139     config->ipStatic_.route_.family_ =
140         static_cast<uint8_t>(CommonUtils::GetAddrFamily(jsonData[CONFIG_KEY_ETH_ROUTE]));
141     if (config->ipStatic_.route_.family_ == AF_INET) {
142         config->ipStatic_.route_.prefixlen_ = routePrefixLen;
143     }
144     std::vector<std::string> servers = CommonUtils::Split(jsonData[CONFIG_KEY_ETH_DNS], DNS_SEPARATOR);
145     for (const auto &dns : servers) {
146         INetAddr addr;
147         addr.address_ = dns;
148         config->ipStatic_.dnsServers_.push_back(addr);
149     }
150     return config;
151 }
152 
ReadUserConfiguration(std::map<std::string,sptr<InterfaceConfiguration>> & devCfgs)153 bool EthernetConfiguration::ReadUserConfiguration(std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs)
154 {
155     DIR *dir = nullptr;
156     dirent *ptr = nullptr;
157     if ((dir = opendir(USER_CONFIG_DIR)) == nullptr) {
158         NETMGR_EXT_LOG_E("Read user configuration open dir error dir=[%{public}s]", USER_CONFIG_DIR);
159         return false;
160     }
161     std::string iface;
162     while ((ptr = readdir(dir)) != nullptr) {
163         if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
164             continue;
165         }
166         if (ptr->d_type == DT_REG) {
167             std::string filePath = std::string(USER_CONFIG_DIR) + FILE_OBLIQUE_LINE + ptr->d_name;
168             std::string fileContent;
169             if (!ReadFile(filePath, fileContent)) {
170                 continue;
171             }
172             std::string().swap(iface);
173             sptr<InterfaceConfiguration> cfg = new (std::nothrow) InterfaceConfiguration();
174             if (cfg == nullptr) {
175                 NETMGR_EXT_LOG_E("cfg new failed for devname[%{public}s]", iface.c_str());
176                 continue;
177             }
178             ParserFileConfig(fileContent, iface, cfg);
179             std::regex re(IFACE_MATCH);
180             if (!iface.empty() && std::regex_search(iface, re)) {
181                 NETMGR_EXT_LOG_D("ReadFileList devname[%{public}s]", iface.c_str());
182                 devCfgs[iface] = cfg;
183             }
184         }
185     }
186     closedir(dir);
187     return true;
188 }
189 
WriteUserConfiguration(const std::string & iface,sptr<InterfaceConfiguration> & cfg)190 bool EthernetConfiguration::WriteUserConfiguration(const std::string &iface, sptr<InterfaceConfiguration> &cfg)
191 {
192     if (cfg == nullptr) {
193         NETMGR_EXT_LOG_E("cfg is nullptr");
194         return false;
195     }
196     if (!CreateDir(USER_CONFIG_DIR)) {
197         NETMGR_EXT_LOG_E("create dir failed");
198         return false;
199     }
200     if (cfg->mode_ == STATIC) {
201         int prefixlen = 0;
202         cfg->ipStatic_.ipAddr_.family_ =
203             static_cast<uint8_t>(CommonUtils::GetAddrFamily(cfg->ipStatic_.ipAddr_.address_));
204         if (cfg->ipStatic_.ipAddr_.family_ == AF_INET) {
205             if (cfg->ipStatic_.netMask_.address_.empty()) {
206                 prefixlen = CommonUtils::GetMaskLength(cfg->ipStatic_.ipAddr_.netMask_);
207             } else {
208                 prefixlen = CommonUtils::GetMaskLength(cfg->ipStatic_.netMask_.address_);
209             }
210         }
211         cfg->ipStatic_.ipAddr_.prefixlen_ = prefixlen;
212         cfg->ipStatic_.gateway_.family_ =
213             static_cast<uint8_t>(CommonUtils::GetAddrFamily(cfg->ipStatic_.gateway_.address_));
214         cfg->ipStatic_.gateway_.prefixlen_ = prefixlen;
215         cfg->ipStatic_.route_.family_ =
216             static_cast<uint8_t>(CommonUtils::GetAddrFamily(cfg->ipStatic_.route_.address_));
217         int routePrefixLen = 0;
218         if (!cfg->ipStatic_.route_.netMask_.empty()) {
219             routePrefixLen = CommonUtils::GetMaskLength(cfg->ipStatic_.route_.netMask_);
220         }
221         cfg->ipStatic_.route_.prefixlen_ = routePrefixLen;
222     }
223     std::string fileContent;
224     std::string filePath = std::string(USER_CONFIG_DIR) + FILE_OBLIQUE_LINE + iface;
225     GenCfgContent(iface, cfg, fileContent);
226     return WriteFile(filePath, fileContent);
227 }
228 
ClearAllUserConfiguration()229 bool EthernetConfiguration::ClearAllUserConfiguration()
230 {
231     return DelDir(USER_CONFIG_DIR);
232 }
233 
ConvertToConfiguration(const EthernetDhcpCallback::DhcpResult & dhcpResult,sptr<StaticConfiguration> & config)234 bool EthernetConfiguration::ConvertToConfiguration(const EthernetDhcpCallback::DhcpResult &dhcpResult,
235                                                    sptr<StaticConfiguration> &config)
236 {
237     if (config == nullptr) {
238         NETMGR_EXT_LOG_E("Error ConvertToIpConfiguration config is null");
239         return false;
240     }
241     const auto &emPrefixlen = 0;
242     unsigned int prefixlen = 0;
243     config->ipAddr_.address_ = dhcpResult.ipAddr;
244     config->netMask_.address_ = dhcpResult.subNet;
245     config->ipAddr_.family_ = static_cast<uint8_t>(CommonUtils::GetAddrFamily(dhcpResult.ipAddr));
246     if (config->ipAddr_.family_ == AF_INET) {
247         config->ipAddr_.prefixlen_ = static_cast<uint8_t>(CommonUtils::GetMaskLength(dhcpResult.subNet));
248     }
249     prefixlen = config->ipAddr_.prefixlen_;
250     config->gateway_.address_ = dhcpResult.gateWay;
251     config->gateway_.family_ = static_cast<uint8_t>(CommonUtils::GetAddrFamily(dhcpResult.gateWay));
252     config->gateway_.prefixlen_ = prefixlen;
253     if (dhcpResult.gateWay != dhcpResult.route1) {
254         if (dhcpResult.route1 == EMPTY_NET_ADDR) {
255             config->route_.address_ = DEFAULT_NET_ADDR;
256             config->route_.prefixlen_ = emPrefixlen;
257         } else {
258             config->route_.address_ = dhcpResult.route1;
259             config->route_.prefixlen_ = prefixlen;
260         }
261     }
262     if (dhcpResult.gateWay != dhcpResult.route2) {
263         if (dhcpResult.route2 == EMPTY_NET_ADDR) {
264             config->route_.address_ = DEFAULT_NET_ADDR;
265             config->route_.prefixlen_ = emPrefixlen;
266         } else {
267             config->route_.address_ = dhcpResult.route2;
268             config->route_.prefixlen_ = prefixlen;
269         }
270     }
271     config->route_.family_ = static_cast<uint8_t>(CommonUtils::GetAddrFamily(config->route_.address_));
272     INetAddr dnsNet1;
273     dnsNet1.address_ = dhcpResult.dns1;
274     INetAddr dnsNet2;
275     dnsNet2.address_ = dhcpResult.dns2;
276     config->dnsServers_.push_back(dnsNet1);
277     config->dnsServers_.push_back(dnsNet2);
278     return true;
279 }
280 
MakeInterfaceConfiguration(const sptr<InterfaceConfiguration> & devCfg,const sptr<NetLinkInfo> & devLinkInfo)281 sptr<InterfaceConfiguration> EthernetConfiguration::MakeInterfaceConfiguration(
282     const sptr<InterfaceConfiguration> &devCfg, const sptr<NetLinkInfo> &devLinkInfo)
283 {
284     if (devCfg == nullptr || devLinkInfo == nullptr) {
285         NETMGR_EXT_LOG_E("param is nullptr");
286         return nullptr;
287     }
288     sptr<InterfaceConfiguration> cfg = new (std::nothrow) InterfaceConfiguration();
289     if (cfg == nullptr) {
290         NETMGR_EXT_LOG_E("cfg new failed");
291         return nullptr;
292     }
293     cfg->mode_ = devCfg->mode_;
294     INetAddr addrNet = devLinkInfo->netAddrList_.back();
295     Route route = devLinkInfo->routeList_.front();
296     Route routeLocal = devLinkInfo->routeList_.back();
297     cfg->ipStatic_.ipAddr_.address_ = addrNet.address_;
298     cfg->ipStatic_.route_.address_ = route.destination_.address_;
299     cfg->ipStatic_.gateway_.address_ = route.gateway_.address_;
300     cfg->ipStatic_.netMask_.address_ = CommonUtils::GetMaskByLength(routeLocal.destination_.prefixlen_);
301     cfg->ipStatic_.domain_ = devLinkInfo->domain_;
302     for (const auto addr : devLinkInfo->dnsList_) {
303         cfg->ipStatic_.dnsServers_.push_back(addr);
304     }
305     return cfg;
306 }
307 
ReadJsonFile(const std::string & filePath)308 std::string EthernetConfiguration::ReadJsonFile(const std::string &filePath)
309 {
310     std::ifstream infile;
311     std::string strLine;
312     std::string strAll;
313     infile.open(filePath);
314     if (!infile.is_open()) {
315         NETMGR_EXT_LOG_E("ReadJsonFile filePath failed");
316         return strAll;
317     }
318     while (getline(infile, strLine)) {
319         strAll.append(strLine);
320     }
321     infile.close();
322     return strAll;
323 }
324 
IsDirExist(const std::string & dirPath)325 bool EthernetConfiguration::IsDirExist(const std::string &dirPath)
326 {
327     struct stat status;
328     if (dirPath.empty()) {
329         return false;
330     }
331     return (stat(dirPath.c_str(), &status) == 0);
332 }
333 
CreateDir(const std::string & dirPath)334 bool EthernetConfiguration::CreateDir(const std::string &dirPath)
335 {
336     if (IsDirExist(dirPath)) {
337         return true;
338     }
339     if (mkdir(dirPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == MKDIR_ERR) {
340         NETMGR_EXT_LOG_E("mkdir failed %{public}d: %{public}s", errno, strerror(errno));
341         return false;
342     }
343     return true;
344 }
345 
DelDir(const std::string & dirPath)346 bool EthernetConfiguration::DelDir(const std::string &dirPath)
347 {
348     DIR *dir = nullptr;
349     dirent *entry = nullptr;
350     struct stat statbuf;
351     if ((dir = opendir(dirPath.c_str())) == nullptr) {
352         NETMGR_EXT_LOG_E("EthernetConfiguration DelDir open user dir failed!");
353         return false;
354     }
355     while ((entry = readdir(dir)) != nullptr) {
356         std::string filePath = dirPath + FILE_OBLIQUE_LINE + entry->d_name;
357         lstat(filePath.c_str(), &statbuf);
358         if (S_ISREG(statbuf.st_mode)) {
359             remove(filePath.c_str());
360         }
361     }
362     closedir(dir);
363     sync();
364     return rmdir(dirPath.c_str()) >= 0;
365 }
366 
IsFileExist(const std::string & filePath,std::string & realPath)367 bool EthernetConfiguration::IsFileExist(const std::string &filePath, std::string &realPath)
368 {
369     char tmpPath[PATH_MAX] = {0};
370     if (!realpath(filePath.c_str(), tmpPath)) {
371         NETMGR_EXT_LOG_E("file name is error");
372         return false;
373     }
374     if (strncmp(tmpPath, USER_CONFIG_DIR, USER_PATH_LEN) != 0) {
375         NETMGR_EXT_LOG_E("file path is error");
376         return false;
377     }
378     realPath = tmpPath;
379     return true;
380 }
381 
ReadFile(const std::string & filePath,std::string & fileContent)382 bool EthernetConfiguration::ReadFile(const std::string &filePath, std::string &fileContent)
383 {
384     std::unique_lock<std::mutex> lock(mutex_);
385     if (filePath.empty()) {
386         NETMGR_EXT_LOG_E("filePath empty.");
387         return false;
388     }
389     std::string realPath;
390     if (!IsFileExist(filePath, realPath)) {
391         NETMGR_EXT_LOG_E("[%{public}s] not exist.", filePath.c_str());
392         return false;
393     }
394     std::fstream file(realPath.c_str(), std::fstream::in);
395     if (!file.is_open()) {
396         NETMGR_EXT_LOG_E("EthernetConfiguration read file failed.err %{public}d %{public}s", errno, strerror(errno));
397         return false;
398     }
399     std::stringstream buffer;
400     buffer << file.rdbuf();
401     fileContent = buffer.str();
402     file.close();
403     return true;
404 }
405 
WriteFile(const std::string & filePath,const std::string & fileContent)406 bool EthernetConfiguration::WriteFile(const std::string &filePath, const std::string &fileContent)
407 {
408     std::fstream file(filePath.c_str(), std::fstream::out | std::fstream::trunc);
409     if (!file.is_open()) {
410         NETMGR_EXT_LOG_E("EthernetConfiguration write file=%{public}s fstream failed. err %{public}d %{public}s",
411                          filePath.c_str(), errno, strerror(errno));
412         return false;
413     }
414     file << fileContent.c_str();
415     file.close();
416     sync();
417     return true;
418 }
419 
ParserFileConfig(const std::string & fileContent,std::string & iface,sptr<InterfaceConfiguration> cfg)420 void EthernetConfiguration::ParserFileConfig(const std::string &fileContent, std::string &iface,
421                                              sptr<InterfaceConfiguration> cfg)
422 {
423     std::string::size_type pos = fileContent.find(KEY_DEVICE) + strlen(KEY_DEVICE);
424     if (pos == std::string::npos || fileContent.find(WRAP, pos) == std::string::npos) {
425         return;
426     }
427     const auto &device = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
428     pos = fileContent.find(KEY_BOOTPROTO) + strlen(KEY_BOOTPROTO);
429     if (pos == std::string::npos || fileContent.find(WRAP, pos) == std::string::npos) {
430         return;
431     }
432     const auto &bootProto = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
433     iface = device;
434     if (bootProto == KEY_STATIC) {
435         cfg->mode_ = STATIC;
436         MakeConfig(cfg, fileContent);
437     } else if (bootProto == KEY_DHCP) {
438         cfg->mode_ = DHCP;
439     }
440 }
441 
MakeConfig(sptr<InterfaceConfiguration> cfg,const std::string & fileContent)442 void EthernetConfiguration::MakeConfig(sptr<InterfaceConfiguration> cfg, const std::string &fileContent)
443 {
444     std::string::size_type pos = fileContent.find(KEY_IPADDR) + strlen(KEY_IPADDR);
445     if (pos == std::string::npos || fileContent.find(WRAP, pos) == std::string::npos) {
446         return;
447     }
448     const auto &ipAddr = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
449     pos = fileContent.find(KEY_NETMASK) + strlen(KEY_NETMASK);
450     if (pos == std::string::npos || fileContent.find(WRAP, pos) == std::string::npos) {
451         return;
452     }
453     const auto &netMask = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
454     pos = fileContent.find(KEY_GATEWAY) + strlen(KEY_GATEWAY);
455     if (pos == std::string::npos || fileContent.find(WRAP, pos) == std::string::npos) {
456         return;
457     }
458     const auto &gatway = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
459     pos = fileContent.find(KEY_ROUTE) + strlen(KEY_ROUTE);
460     if (pos == std::string::npos || fileContent.find(WRAP, pos) == std::string::npos) {
461         return;
462     }
463     const auto &route = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
464     pos = fileContent.find(KEY_ROUTE_NETMASK) + strlen(KEY_ROUTE_NETMASK);
465     if (pos == std::string::npos || fileContent.find(WRAP, pos) == std::string::npos) {
466         return;
467     }
468     const auto &routeNetmask = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
469     pos = fileContent.find(KEY_DNS) + strlen(KEY_DNS);
470     if (pos == std::string::npos || fileContent.find(WRAP, pos) == std::string::npos) {
471         return;
472     }
473     const auto &dnsServers = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
474     cfg->ipStatic_.ipAddr_.address_ = ipAddr;
475     cfg->ipStatic_.ipAddr_.netMask_ = netMask;
476     cfg->ipStatic_.ipAddr_.family_ = CommonUtils::GetAddrFamily(ipAddr);
477     int prefixLen = CommonUtils::GetMaskLength(netMask);
478     if (cfg->ipStatic_.ipAddr_.family_ == AF_INET) {
479         cfg->ipStatic_.ipAddr_.prefixlen_ = prefixLen;
480     }
481     cfg->ipStatic_.netMask_.address_ = netMask;
482     cfg->ipStatic_.gateway_.address_ = gatway;
483     cfg->ipStatic_.gateway_.family_ = CommonUtils::GetAddrFamily(gatway);
484     if (cfg->ipStatic_.gateway_.family_ == AF_INET) {
485         cfg->ipStatic_.gateway_.prefixlen_ = prefixLen;
486     }
487     cfg->ipStatic_.route_.address_ = !route.empty() ? route : DEFAULT_NET_ADDR;
488     int routePrefixLen = 0;
489     if (!routeNetmask.empty()) {
490         routePrefixLen = CommonUtils::GetMaskLength(routeNetmask);
491     }
492     cfg->ipStatic_.route_.family_ = CommonUtils::GetAddrFamily(cfg->ipStatic_.route_.address_);
493     if (cfg->ipStatic_.route_.family_ == AF_INET) {
494         cfg->ipStatic_.route_.prefixlen_ = routePrefixLen;
495     }
496     for (const auto &dns : CommonUtils::Split(dnsServers, DNS_SEPARATOR)) {
497         INetAddr addr;
498         addr.address_ = dns;
499         cfg->ipStatic_.dnsServers_.push_back(addr);
500     }
501 }
502 
GenCfgContent(const std::string & iface,sptr<InterfaceConfiguration> cfg,std::string & fileContent)503 void EthernetConfiguration::GenCfgContent(const std::string &iface, sptr<InterfaceConfiguration> cfg,
504                                           std::string &fileContent)
505 {
506     if (cfg == nullptr) {
507         NETMGR_EXT_LOG_E("cfg is nullptr");
508         return;
509     }
510     std::string().swap(fileContent);
511     fileContent = fileContent + KEY_DEVICE + iface + WRAP;
512     if (cfg->mode_ == STATIC) {
513         fileContent = fileContent + KEY_BOOTPROTO + KEY_STATIC + WRAP;
514         fileContent = fileContent + KEY_IPADDR + cfg->ipStatic_.ipAddr_.address_ + WRAP;
515         if (cfg->ipStatic_.netMask_.address_.empty()) {
516             fileContent = fileContent + KEY_NETMASK + cfg->ipStatic_.ipAddr_.netMask_ + WRAP;
517         } else {
518             fileContent = fileContent + KEY_NETMASK + cfg->ipStatic_.netMask_.address_ + WRAP;
519         }
520         fileContent = fileContent + KEY_GATEWAY + cfg->ipStatic_.gateway_.address_ + WRAP;
521         fileContent = fileContent + KEY_ROUTE + cfg->ipStatic_.route_.address_ + WRAP;
522         fileContent = fileContent + KEY_ROUTE_NETMASK + cfg->ipStatic_.route_.netMask_ + WRAP;
523         fileContent = fileContent + KEY_DNS;
524         for (uint32_t i = 0; i < cfg->ipStatic_.dnsServers_.size(); i++) {
525             fileContent = fileContent + cfg->ipStatic_.dnsServers_[i].address_;
526             if (cfg->ipStatic_.dnsServers_.size() - i > 1) {
527                 fileContent = fileContent + DNS_SEPARATOR;
528             }
529         }
530         fileContent = fileContent + WRAP;
531     } else {
532         fileContent = fileContent + KEY_BOOTPROTO + KEY_DHCP + WRAP;
533     }
534 }
535 } // namespace NetManagerStandard
536 } // namespace OHOS
537