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 
16 #ifndef ETHERNET_CONFIGURATION_H
17 #define ETHERNET_CONFIGURATION_H
18 
19 #include <map>
20 #include <mutex>
21 #include <numeric>
22 #include <set>
23 #include <string>
24 #include <vector>
25 
26 #include "cJSON.h"
27 #include "ethernet_dhcp_callback.h"
28 #include "http_proxy.h"
29 #include "interface_configuration.h"
30 #include "net_all_capabilities.h"
31 #include "net_link_info.h"
32 
33 namespace OHOS {
34 namespace NetManagerStandard {
35 class EthernetConfiguration {
36 public:
37     EthernetConfiguration();
38     ~EthernetConfiguration() = default;
39 
40     bool ReadSystemConfiguration(std::map<std::string, std::set<NetCap>> &devCaps,
41                                  std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs);
42     bool ReadUserConfiguration(std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs);
43     bool WriteUserConfiguration(const std::string &iface, sptr<InterfaceConfiguration> &cfg);
44     bool ClearAllUserConfiguration();
45     bool ConvertToConfiguration(const EthernetDhcpCallback::DhcpResult &dhcpResult, sptr<StaticConfiguration> &config);
46     sptr<InterfaceConfiguration> MakeInterfaceConfiguration(const sptr<InterfaceConfiguration> &devCfg,
47                                                             const sptr<NetLinkInfo> &devLinkInfo);
48 
49 private:
50     void ParseDevice(const std::string &fileContent, std::string &iface);
51     void ParseBootProto(const std::string &fileContent, sptr<InterfaceConfiguration> cfg);
52     void ParseStaticConfig(const std::string &fileContent, sptr<InterfaceConfiguration> cfg);
53 
54     bool ReadEthernetInterfaces(std::map<std::string, std::set<NetCap>> &devCaps,
55                                 std::map<std::string, sptr<InterfaceConfiguration>> &devCfgs,
56                                 const cJSON* const json);
57     std::string ReadJsonFile(const std::string &filePath);
58     sptr<InterfaceConfiguration> ConvertJsonToConfiguration(const cJSON* const jsonData, bool isLan);
59     bool IsDirExist(const std::string &dirPath);
60     bool CreateDir(const std::string &dirPath);
61     bool DelDir(const std::string &dirPath);
62     bool IsFileExist(const std::string &filePath, std::string &realPath);
63     bool ReadFile(const std::string &filePath, std::string &fileContent);
64     bool WriteFile(const std::string &filePath, const std::string &fileContent);
65     void ParserFileConfig(const std::string &fileContent, std::string &iface, sptr<InterfaceConfiguration> cfg);
66     void ParserFileHttpProxy(const std::string &fileContent, const sptr<InterfaceConfiguration> &cfg);
67     void ParserIfaceIpAndRoute(sptr<InterfaceConfiguration> &cfg, const std::string &rootNetMask);
68     void GenCfgContent(const std::string &iface, sptr<InterfaceConfiguration> cfg, std::string &fileContent);
69     void GenHttpProxyContent(const sptr<InterfaceConfiguration> &cfg, std::string &fileContent);
70     std::string AccumulateNetAddress(const std::vector<INetAddr> &netAddrList);
71     bool IsValidDhcpResult(const EthernetDhcpCallback::DhcpResult &dhcpResult, sptr<StaticConfiguration> &config);
72     std::string GetIfaceMode(IPSetMode mode);
73     std::vector<INetAddr> GetGatewayFromMap(const std::unordered_map<std::string, INetAddr> &temp);
74     std::vector<INetAddr> GetGatewayFromRouteList(std::list<Route> &routeList);
75 
76 private:
77     std::mutex mutex_;
78 };
79 } // namespace NetManagerStandard
80 } // namespace OHOS
81 #endif // ETHERNET_CONFIGURATION_H
82