1 /*
2 * Copyright (C) 2022-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 "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 <regex>
25 #include <sstream>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
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 *KEY_PROXY_HOST = "PROXY_HOST=";
63 constexpr const char *KEY_PROXY_PORT = "PROXY_PORT=";
64 constexpr const char *KEY_PROXY_EXCLUSIONS = "PROXY_EXCLUSIONS=";
65 constexpr const char *WRAP = "\n";
66 constexpr const char *DEFAULT_IPV4_ADDR = "0.0.0.0";
67 constexpr const char *DEFAULT_IPV6_ADDR = "::";
68 constexpr const char *DEFAULT_IPV6_MAX_ADDRESS = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff";
69 constexpr const char *EMPTY_NET_ADDR = "*";
70 constexpr const char *ADDR_SEPARATOR = ",";
71 constexpr const char *EXCLUSIONS_DELIMITER = ",";
72 } // namespace
73
EthernetConfiguration()74 EthernetConfiguration::EthernetConfiguration()
75 {
76 CreateDir(USER_CONFIG_DIR);
77 }
78
ReadSystemConfiguration(std::map<std::string,std::set<NetCap>> & devCaps,std::map<std::string,sptr<InterfaceConfiguration>> & devCfgs)79 bool EthernetConfiguration::ReadSystemConfiguration(std::map<std::string, std::set<NetCap>> &devCaps,
80 std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs)
81 {
82 const auto &jsonStr = ReadJsonFile(NETWORK_CONFIG_PATH);
83 if (jsonStr.length() == 0) {
84 NETMGR_EXT_LOG_E("ReadConfigData config file is return empty!");
85 return false;
86 }
87 const auto &jsonCfg = nlohmann::json::parse(jsonStr);
88 if (jsonCfg.find(CONFIG_KEY_ETH_COMPONENT_FLAG) == jsonCfg.end()) {
89 NETMGR_EXT_LOG_E("ReadConfigData not find network_ethernet_component!");
90 return false;
91 }
92 const auto &arrIface = jsonCfg.at(CONFIG_KEY_ETH_COMPONENT_FLAG);
93 NETMGR_EXT_LOG_D("read ConfigData ethValue:%{public}s", arrIface.dump().c_str());
94 for (const auto &item : arrIface) {
95 const auto &iface = item[CONFIG_KEY_ETH_IFACE].get<std::string>();
96 const auto &caps = item.at(CONFIG_KEY_ETH_CAPS).get<std::set<NetCap>>();
97 if (!caps.empty()) {
98 devCaps[iface] = caps;
99 }
100 const auto &fit = devCfgs.find(iface);
101 if (fit != devCfgs.end()) {
102 NETMGR_EXT_LOG_E("The iface=%{public}s device have set!", fit->first.c_str());
103 continue;
104 }
105 sptr<InterfaceConfiguration> config = ConvertJsonToConfiguration(item);
106 if (config == nullptr) {
107 NETMGR_EXT_LOG_E("config is nullptr");
108 return false;
109 }
110 std::regex re(IFACE_MATCH);
111 if (!item[CONFIG_KEY_ETH_IP].empty() && std::regex_search(iface, re)) {
112 devCfgs[iface] = config;
113 }
114 }
115 return true;
116 }
117
ConvertJsonToConfiguration(const nlohmann::json & jsonData)118 sptr<InterfaceConfiguration> EthernetConfiguration::ConvertJsonToConfiguration(const nlohmann::json &jsonData)
119 {
120 sptr<InterfaceConfiguration> config = new (std::nothrow) InterfaceConfiguration();
121 if (config == nullptr) {
122 NETMGR_EXT_LOG_E("config is nullptr");
123 return nullptr;
124 }
125
126 config->mode_ = STATIC;
127 StaticConfiguration::ExtractNetAddrBySeparator(jsonData[CONFIG_KEY_ETH_IP], config->ipStatic_.ipAddrList_);
128 StaticConfiguration::ExtractNetAddrBySeparator(jsonData[CONFIG_KEY_ETH_ROUTE], config->ipStatic_.routeList_);
129 StaticConfiguration::ExtractNetAddrBySeparator(jsonData[CONFIG_KEY_ETH_GATEWAY], config->ipStatic_.gatewayList_);
130 StaticConfiguration::ExtractNetAddrBySeparator(jsonData[CONFIG_KEY_ETH_NETMASK], config->ipStatic_.netMaskList_);
131 StaticConfiguration::ExtractNetAddrBySeparator(jsonData[CONFIG_KEY_ETH_DNS], config->ipStatic_.dnsServers_);
132
133 ParserIfaceIpAndRoute(config, jsonData[CONFIG_KEY_ETH_ROUTE_MASK]);
134 return config;
135 }
136
ReadUserConfiguration(std::map<std::string,sptr<InterfaceConfiguration>> & devCfgs)137 bool EthernetConfiguration::ReadUserConfiguration(std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs)
138 {
139 DIR *dir = nullptr;
140 dirent *ptr = nullptr;
141 if ((dir = opendir(USER_CONFIG_DIR)) == nullptr) {
142 NETMGR_EXT_LOG_E("Read user configuration open dir error dir=[%{public}s]", USER_CONFIG_DIR);
143 return false;
144 }
145 std::string iface;
146 while ((ptr = readdir(dir)) != nullptr) {
147 if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
148 continue;
149 }
150 if (ptr->d_type == DT_REG) {
151 std::string filePath = std::string(USER_CONFIG_DIR) + FILE_OBLIQUE_LINE + ptr->d_name;
152 std::string fileContent;
153 if (!ReadFile(filePath, fileContent)) {
154 continue;
155 }
156 std::string().swap(iface);
157 sptr<InterfaceConfiguration> cfg = new (std::nothrow) InterfaceConfiguration();
158 if (cfg == nullptr) {
159 NETMGR_EXT_LOG_E("cfg new failed for devname[%{public}s]", iface.c_str());
160 continue;
161 }
162 ParserFileConfig(fileContent, iface, cfg);
163 std::regex re(IFACE_MATCH);
164 if (!iface.empty() && std::regex_search(iface, re)) {
165 NETMGR_EXT_LOG_D("ReadFileList devname[%{public}s]", iface.c_str());
166 devCfgs[iface] = cfg;
167 }
168 }
169 }
170 closedir(dir);
171 return true;
172 }
173
WriteUserConfiguration(const std::string & iface,sptr<InterfaceConfiguration> & cfg)174 bool EthernetConfiguration::WriteUserConfiguration(const std::string &iface, sptr<InterfaceConfiguration> &cfg)
175 {
176 if (cfg == nullptr) {
177 NETMGR_EXT_LOG_E("cfg is nullptr");
178 return false;
179 }
180 if (!CreateDir(USER_CONFIG_DIR)) {
181 NETMGR_EXT_LOG_E("create dir failed");
182 return false;
183 }
184
185 if (cfg->mode_ == STATIC) {
186 ParserIfaceIpAndRoute(cfg, std::string());
187 }
188
189 std::string fileContent;
190 GenCfgContent(iface, cfg, fileContent);
191
192 std::string filePath = std::string(USER_CONFIG_DIR) + FILE_OBLIQUE_LINE + iface;
193 return WriteFile(filePath, fileContent);
194 }
195
ClearAllUserConfiguration()196 bool EthernetConfiguration::ClearAllUserConfiguration()
197 {
198 return DelDir(USER_CONFIG_DIR);
199 }
200
ConvertToConfiguration(const EthernetDhcpCallback::DhcpResult & dhcpResult,sptr<StaticConfiguration> & config)201 bool EthernetConfiguration::ConvertToConfiguration(const EthernetDhcpCallback::DhcpResult &dhcpResult,
202 sptr<StaticConfiguration> &config)
203 {
204 if (config == nullptr) {
205 NETMGR_EXT_LOG_E("Error ConvertToIpConfiguration config is null");
206 return false;
207 }
208 if (!IsValidDhcpResult(dhcpResult, config)) {
209 return false;
210 }
211
212 INetAddr ipAddr;
213 ipAddr.address_ = dhcpResult.ipAddr;
214 ipAddr.family_ = static_cast<uint8_t>(CommonUtils::GetAddrFamily(dhcpResult.ipAddr));
215 ipAddr.prefixlen_ = (ipAddr.family_ == AF_INET6)
216 ? static_cast<uint8_t>(CommonUtils::Ipv6PrefixLen(dhcpResult.subNet))
217 : static_cast<uint8_t>(CommonUtils::Ipv4PrefixLen(dhcpResult.subNet));
218 config->ipAddrList_.push_back(ipAddr);
219
220 INetAddr netMask;
221 netMask.address_ = dhcpResult.subNet;
222 config->netMaskList_.push_back(netMask);
223
224 INetAddr gateway;
225 gateway.address_ = dhcpResult.gateWay;
226 gateway.family_ = static_cast<uint8_t>(CommonUtils::GetAddrFamily(dhcpResult.gateWay));
227 config->gatewayList_.push_back(gateway);
228
229 INetAddr route;
230 if (dhcpResult.gateWay != dhcpResult.route1 && dhcpResult.route1 != EMPTY_NET_ADDR) {
231 route.address_ = dhcpResult.route1;
232 route.prefixlen_ = ipAddr.prefixlen_;
233 } else if (dhcpResult.gateWay != dhcpResult.route2 && dhcpResult.route2 != EMPTY_NET_ADDR) {
234 route.address_ = dhcpResult.route2;
235 route.prefixlen_ = ipAddr.prefixlen_;
236 } else {
237 route.address_ = (ipAddr.family_ == AF_INET6) ? DEFAULT_IPV6_ADDR : DEFAULT_IPV4_ADDR;
238 route.prefixlen_ = 0;
239 }
240 route.family_ = static_cast<uint8_t>(CommonUtils::GetAddrFamily(route.address_));
241 config->routeList_.push_back(route);
242
243 INetAddr dnsNet1;
244 dnsNet1.address_ = dhcpResult.dns1;
245 INetAddr dnsNet2;
246 dnsNet2.address_ = dhcpResult.dns2;
247 config->dnsServers_.push_back(dnsNet1);
248 config->dnsServers_.push_back(dnsNet2);
249 return true;
250 }
251
MakeInterfaceConfiguration(const sptr<InterfaceConfiguration> & devCfg,const sptr<NetLinkInfo> & devLinkInfo)252 sptr<InterfaceConfiguration> EthernetConfiguration::MakeInterfaceConfiguration(
253 const sptr<InterfaceConfiguration> &devCfg, const sptr<NetLinkInfo> &devLinkInfo)
254 {
255 if (devCfg == nullptr || devLinkInfo == nullptr) {
256 NETMGR_EXT_LOG_E("param is nullptr");
257 return nullptr;
258 }
259 sptr<InterfaceConfiguration> cfg = new (std::nothrow) InterfaceConfiguration();
260 if (cfg == nullptr) {
261 NETMGR_EXT_LOG_E("cfg new failed");
262 return nullptr;
263 }
264 cfg->mode_ = devCfg->mode_;
265 for (const auto &ipAddr : devLinkInfo->netAddrList_) {
266 cfg->ipStatic_.ipAddrList_.push_back(ipAddr);
267 auto family = CommonUtils::GetAddrFamily(ipAddr.address_);
268 INetAddr netMask;
269 netMask.address_ =
270 ipAddr.netMask_.empty()
271 ? (((family == AF_INET6) ? CommonUtils::GetIpv6Prefix(DEFAULT_IPV6_MAX_ADDRESS, ipAddr.prefixlen_)
272 : CommonUtils::GetMaskByLength(ipAddr.prefixlen_)))
273 : ipAddr.netMask_;
274 cfg->ipStatic_.netMaskList_.push_back(netMask);
275 }
276 for (const auto &route : devLinkInfo->routeList_) {
277 cfg->ipStatic_.routeList_.push_back(route.destination_);
278 cfg->ipStatic_.gatewayList_.push_back(route.gateway_);
279 }
280
281 cfg->ipStatic_.domain_ = devLinkInfo->domain_;
282 for (const auto &addr : devLinkInfo->dnsList_) {
283 cfg->ipStatic_.dnsServers_.push_back(addr);
284 }
285 return cfg;
286 }
287
ReadJsonFile(const std::string & filePath)288 std::string EthernetConfiguration::ReadJsonFile(const std::string &filePath)
289 {
290 std::ifstream infile;
291 std::string strLine;
292 std::string strAll;
293 infile.open(filePath);
294 if (!infile.is_open()) {
295 NETMGR_EXT_LOG_E("ReadJsonFile filePath failed");
296 return strAll;
297 }
298 while (getline(infile, strLine)) {
299 strAll.append(strLine);
300 }
301 infile.close();
302 return strAll;
303 }
304
IsDirExist(const std::string & dirPath)305 bool EthernetConfiguration::IsDirExist(const std::string &dirPath)
306 {
307 struct stat status;
308 if (dirPath.empty()) {
309 return false;
310 }
311 return (stat(dirPath.c_str(), &status) == 0);
312 }
313
CreateDir(const std::string & dirPath)314 bool EthernetConfiguration::CreateDir(const std::string &dirPath)
315 {
316 if (IsDirExist(dirPath)) {
317 return true;
318 }
319 if (mkdir(dirPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == MKDIR_ERR) {
320 NETMGR_EXT_LOG_E("mkdir failed %{public}d: %{public}s", errno, strerror(errno));
321 return false;
322 }
323 return true;
324 }
325
DelDir(const std::string & dirPath)326 bool EthernetConfiguration::DelDir(const std::string &dirPath)
327 {
328 DIR *dir = nullptr;
329 dirent *entry = nullptr;
330 struct stat statbuf;
331 if ((dir = opendir(dirPath.c_str())) == nullptr) {
332 NETMGR_EXT_LOG_E("EthernetConfiguration DelDir open user dir failed!");
333 return false;
334 }
335 while ((entry = readdir(dir)) != nullptr) {
336 std::string filePath = dirPath + FILE_OBLIQUE_LINE + entry->d_name;
337 lstat(filePath.c_str(), &statbuf);
338 if (S_ISREG(statbuf.st_mode)) {
339 remove(filePath.c_str());
340 }
341 }
342 closedir(dir);
343 sync();
344 return rmdir(dirPath.c_str()) >= 0;
345 }
346
IsFileExist(const std::string & filePath,std::string & realPath)347 bool EthernetConfiguration::IsFileExist(const std::string &filePath, std::string &realPath)
348 {
349 char tmpPath[PATH_MAX] = {0};
350 if (!realpath(filePath.c_str(), tmpPath)) {
351 NETMGR_EXT_LOG_E("file name is error");
352 return false;
353 }
354 if (strncmp(tmpPath, USER_CONFIG_DIR, USER_PATH_LEN) != 0) {
355 NETMGR_EXT_LOG_E("file path is error");
356 return false;
357 }
358 realPath = tmpPath;
359 return true;
360 }
361
ReadFile(const std::string & filePath,std::string & fileContent)362 bool EthernetConfiguration::ReadFile(const std::string &filePath, std::string &fileContent)
363 {
364 std::unique_lock<std::mutex> lock(mutex_);
365 if (filePath.empty()) {
366 NETMGR_EXT_LOG_E("filePath empty.");
367 return false;
368 }
369 std::string realPath;
370 if (!IsFileExist(filePath, realPath)) {
371 NETMGR_EXT_LOG_E("[%{public}s] not exist.", filePath.c_str());
372 return false;
373 }
374 std::fstream file(realPath.c_str(), std::fstream::in);
375 if (!file.is_open()) {
376 NETMGR_EXT_LOG_E("EthernetConfiguration read file failed.err %{public}d %{public}s", errno, strerror(errno));
377 return false;
378 }
379 std::stringstream buffer;
380 buffer << file.rdbuf();
381 fileContent = buffer.str();
382 file.close();
383 return true;
384 }
385
WriteFile(const std::string & filePath,const std::string & fileContent)386 bool EthernetConfiguration::WriteFile(const std::string &filePath, const std::string &fileContent)
387 {
388 std::fstream file(filePath.c_str(), std::fstream::out | std::fstream::trunc);
389 if (!file.is_open()) {
390 NETMGR_EXT_LOG_E("EthernetConfiguration write file=%{public}s fstream failed. err %{public}d %{public}s",
391 filePath.c_str(), errno, strerror(errno));
392 return false;
393 }
394 file << fileContent.c_str();
395 file.close();
396 sync();
397 return true;
398 }
399
ParserFileConfig(const std::string & fileContent,std::string & iface,sptr<InterfaceConfiguration> cfg)400 void EthernetConfiguration::ParserFileConfig(const std::string &fileContent, std::string &iface,
401 sptr<InterfaceConfiguration> cfg)
402 {
403 ParseDevice(fileContent, iface);
404 ParseBootProto(fileContent, cfg);
405 ParseStaticConfig(fileContent, cfg);
406 ParserFileHttpProxy(fileContent, cfg);
407 }
408
ParseDevice(const std::string & fileContent,std::string & iface)409 void EthernetConfiguration::ParseDevice(const std::string &fileContent, std::string &iface)
410 {
411 std::string::size_type pos = fileContent.find(KEY_DEVICE);
412 if (pos == std::string::npos) {
413 return;
414 }
415 pos += strlen(KEY_DEVICE);
416 const auto &device = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
417 iface = device;
418 }
419
ParseBootProto(const std::string & fileContent,sptr<InterfaceConfiguration> cfg)420 void EthernetConfiguration::ParseBootProto(const std::string &fileContent, sptr<InterfaceConfiguration> cfg)
421 {
422 std::string::size_type pos = fileContent.find(KEY_BOOTPROTO);
423 if (pos == std::string::npos) {
424 return;
425 }
426 pos += strlen(KEY_BOOTPROTO);
427 const auto &bootProto = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
428 cfg->mode_ = (bootProto == KEY_STATIC) ? STATIC : DHCP;
429 }
430
ParseStaticConfig(const std::string & fileContent,sptr<InterfaceConfiguration> cfg)431 void EthernetConfiguration::ParseStaticConfig(const std::string &fileContent, sptr<InterfaceConfiguration> cfg)
432 {
433 if (cfg->mode_ != STATIC) {
434 return;
435 }
436 std::string ipAddresses, netMasks, gateways, routes, routeMasks, dnsServers;
437 auto pos = fileContent.find(KEY_IPADDR);
438 if (pos != std::string::npos) {
439 pos += strlen(KEY_IPADDR);
440 ipAddresses = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
441 }
442
443 pos = fileContent.find(KEY_NETMASK);
444 if (pos != std::string::npos) {
445 pos += strlen(KEY_NETMASK);
446 netMasks = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
447 }
448
449 pos = fileContent.find(KEY_GATEWAY);
450 if (pos != std::string::npos) {
451 pos += strlen(KEY_GATEWAY);
452 gateways = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
453 }
454
455 pos = fileContent.find(KEY_ROUTE);
456 if (pos != std::string::npos) {
457 pos += strlen(KEY_ROUTE);
458 routes = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
459 }
460
461 pos = fileContent.find(KEY_ROUTE_NETMASK);
462 if (pos != std::string::npos) {
463 pos += strlen(KEY_ROUTE_NETMASK);
464 routeMasks = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
465 }
466
467 pos = fileContent.find(KEY_DNS);
468 if (pos != std::string::npos) {
469 pos += strlen(KEY_DNS);
470 dnsServers = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
471 }
472
473 StaticConfiguration::ExtractNetAddrBySeparator(ipAddresses, cfg->ipStatic_.ipAddrList_);
474 StaticConfiguration::ExtractNetAddrBySeparator(routes, cfg->ipStatic_.routeList_);
475 StaticConfiguration::ExtractNetAddrBySeparator(gateways, cfg->ipStatic_.gatewayList_);
476 StaticConfiguration::ExtractNetAddrBySeparator(netMasks, cfg->ipStatic_.netMaskList_);
477 StaticConfiguration::ExtractNetAddrBySeparator(dnsServers, cfg->ipStatic_.dnsServers_);
478 ParserIfaceIpAndRoute(cfg, routeMasks);
479 }
480
ParserFileHttpProxy(const std::string & fileContent,const sptr<InterfaceConfiguration> & cfg)481 void EthernetConfiguration::ParserFileHttpProxy(const std::string &fileContent, const sptr<InterfaceConfiguration> &cfg)
482 {
483 std::string::size_type pos = fileContent.find(KEY_PROXY_HOST);
484 if (pos != std::string::npos) {
485 pos += strlen(KEY_PROXY_HOST);
486 cfg->httpProxy_.SetHost(fileContent.substr(pos, fileContent.find(WRAP, pos) - pos));
487 }
488
489 pos = fileContent.find(KEY_PROXY_PORT);
490 if (pos != std::string::npos) {
491 pos += strlen(KEY_PROXY_PORT);
492 uint32_t port = CommonUtils::StrToUint(fileContent.substr(pos, fileContent.find(WRAP, pos) - pos));
493 cfg->httpProxy_.SetPort(static_cast<uint16_t>(port));
494 }
495
496 pos = fileContent.find(KEY_PROXY_EXCLUSIONS);
497 if (pos != std::string::npos) {
498 pos += strlen(KEY_PROXY_EXCLUSIONS);
499 auto exclusions = fileContent.substr(pos, fileContent.find(WRAP, pos) - pos);
500 std::list<std::string> exclusionList;
501 for (const auto &exclusion : CommonUtils::Split(exclusions, EXCLUSIONS_DELIMITER)) {
502 exclusionList.push_back(exclusion);
503 }
504 cfg->httpProxy_.SetExclusionList(exclusionList);
505 }
506 }
507
ParserIfaceIpAndRoute(sptr<InterfaceConfiguration> & cfg,const std::string & rootNetMask)508 void EthernetConfiguration::ParserIfaceIpAndRoute(sptr<InterfaceConfiguration> &cfg, const std::string &rootNetMask)
509 {
510 if (cfg == nullptr) {
511 NETMGR_EXT_LOG_E("cfg is nullptr");
512 return;
513 }
514
515 std::for_each(cfg->ipStatic_.netMaskList_.begin(), cfg->ipStatic_.netMaskList_.end(), [&cfg](const auto &netMask) {
516 auto maskFamily = CommonUtils::GetAddrFamily(netMask.address_);
517 for (auto &ipAddr : cfg->ipStatic_.ipAddrList_) {
518 if (maskFamily != CommonUtils::GetAddrFamily(ipAddr.address_)) {
519 continue;
520 }
521 ipAddr.netMask_ = netMask.address_;
522 ipAddr.prefixlen_ = (maskFamily == AF_INET6) ? CommonUtils::Ipv6PrefixLen(netMask.address_)
523 : CommonUtils::Ipv4PrefixLen(netMask.address_);
524 break;
525 }
526 });
527
528 for (const auto &routeMask : CommonUtils::Split(rootNetMask, ADDR_SEPARATOR)) {
529 auto maskFamily = CommonUtils::GetAddrFamily(routeMask);
530 for (auto &route : cfg->ipStatic_.routeList_) {
531 if (maskFamily != CommonUtils::GetAddrFamily(route.address_)) {
532 continue;
533 }
534 route.prefixlen_ = (maskFamily == AF_INET6) ? CommonUtils::Ipv6PrefixLen(routeMask)
535 : CommonUtils::Ipv4PrefixLen(routeMask);
536 break;
537 }
538 }
539 }
540
GenCfgContent(const std::string & iface,sptr<InterfaceConfiguration> cfg,std::string & fileContent)541 void EthernetConfiguration::GenCfgContent(const std::string &iface, sptr<InterfaceConfiguration> cfg,
542 std::string &fileContent)
543 {
544 if (cfg == nullptr) {
545 NETMGR_EXT_LOG_E("cfg is nullptr");
546 return;
547 }
548 std::string().swap(fileContent);
549 fileContent = fileContent + KEY_DEVICE + iface + WRAP;
550 std::string mode = (cfg->mode_ == STATIC) ? KEY_STATIC : KEY_DHCP;
551 fileContent = fileContent + KEY_BOOTPROTO + mode + WRAP;
552 if (cfg->mode_ == STATIC) {
553 std::string ipAddresses = AccumulateNetAddress(cfg->ipStatic_.ipAddrList_);
554 std::string netMasks = AccumulateNetAddress(cfg->ipStatic_.netMaskList_);
555 std::string gateways = AccumulateNetAddress(cfg->ipStatic_.gatewayList_);
556 std::string routes = AccumulateNetAddress(cfg->ipStatic_.routeList_);
557 std::string routeMasks =
558 std::accumulate(cfg->ipStatic_.routeList_.begin(), cfg->ipStatic_.routeList_.end(), std::string(),
559 [](const std::string &routeMask, const INetAddr &iter) {
560 auto family = CommonUtils::GetAddrFamily(iter.address_);
561 std::string mask = (family == AF_INET6) ? DEFAULT_IPV6_ADDR : DEFAULT_IPV4_ADDR;
562 return routeMask.empty() ? routeMask + mask : (routeMask + ADDR_SEPARATOR + mask);
563 });
564 std::string dnsServers = AccumulateNetAddress(cfg->ipStatic_.dnsServers_);
565
566 fileContent = fileContent + KEY_IPADDR + ipAddresses + WRAP;
567 fileContent = fileContent + KEY_NETMASK + netMasks + WRAP;
568 fileContent = fileContent + KEY_GATEWAY + gateways + WRAP;
569 fileContent = fileContent + KEY_ROUTE + routes + WRAP;
570 fileContent = fileContent + KEY_ROUTE_NETMASK + routeMasks + WRAP;
571 fileContent = fileContent + KEY_DNS + dnsServers + WRAP;
572 }
573 GenHttpProxyContent(cfg, fileContent);
574 }
575
GenHttpProxyContent(const sptr<InterfaceConfiguration> & cfg,std::string & fileContent)576 void EthernetConfiguration::GenHttpProxyContent(const sptr<InterfaceConfiguration> &cfg, std::string &fileContent)
577 {
578 const auto &exclusionList = cfg->httpProxy_.GetExclusionList();
579 std::string exclusions =
580 std::accumulate(exclusionList.begin(), exclusionList.end(), std::string(),
581 [](const std::string &exclusion, const std::string &next) {
582 return exclusion.empty() ? exclusion + next : (exclusion + EXCLUSIONS_DELIMITER + next);
583 });
584
585 fileContent = fileContent + KEY_PROXY_HOST + cfg->httpProxy_.GetHost() + WRAP;
586 fileContent = fileContent + KEY_PROXY_PORT + std::to_string(cfg->httpProxy_.GetPort()) + WRAP;
587 fileContent = fileContent + KEY_PROXY_EXCLUSIONS + exclusions + WRAP;
588 }
589
AccumulateNetAddress(const std::vector<INetAddr> & netAddrList)590 std::string EthernetConfiguration::AccumulateNetAddress(const std::vector<INetAddr> &netAddrList)
591 {
592 return std::accumulate(netAddrList.begin(), netAddrList.end(), std::string(),
593 [](const std::string &addr, const INetAddr &iter) {
594 return addr.empty() ? (addr + iter.address_) : (addr + ADDR_SEPARATOR + iter.address_);
595 });
596 }
597
IsValidDhcpResult(const EthernetDhcpCallback::DhcpResult & dhcpResult,sptr<StaticConfiguration> & config)598 bool EthernetConfiguration::IsValidDhcpResult(const EthernetDhcpCallback::DhcpResult &dhcpResult,
599 sptr<StaticConfiguration> &config)
600 {
601 if (config == nullptr) {
602 NETMGR_EXT_LOG_E("config is nullptr");
603 return false;
604 }
605 if (dhcpResult.ipAddr.empty()) {
606 NETMGR_EXT_LOG_E("DhcpResult ip addr is empty");
607 return false;
608 }
609
610 bool isSameIp = false;
611 bool isSameGateway = false;
612 for (const auto &ipAddr : config->ipAddrList_) {
613 if (dhcpResult.ipAddr == ipAddr.address_) {
614 NETMGR_EXT_LOG_E("Same ip addr:%{public}s", CommonUtils::ToAnonymousIp(dhcpResult.ipAddr).c_str());
615 isSameIp = true;
616 break;
617 }
618 }
619
620 for (const auto &gateway : config->gatewayList_) {
621 if (dhcpResult.gateWay == gateway.address_) {
622 NETMGR_EXT_LOG_E("Same gateway:%{public}s", CommonUtils::ToAnonymousIp(dhcpResult.gateWay).c_str());
623 isSameGateway = true;
624 break;
625 }
626 }
627 return !(isSameIp && isSameGateway);
628 }
629 } // namespace NetManagerStandard
630 } // namespace OHOS