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 #include "networkshare_configuration.h"
17 
18 #include <regex>
19 
20 #include "netmgr_ext_log_wrapper.h"
21 #include "net_manager_constants.h"
22 #include "networkshare_constants.h"
23 
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 constexpr const char *CONFIG_KEY_SHARE_SUPPORT = "share_support";
28 constexpr const char *CONFIG_KEY_USB_REGEXS = "usb_regexs";
29 constexpr const char *CONFIG_KEY_WIFI_REGEXS = "wifi_regexs";
30 constexpr const char *CONFIG_KEY_BLUETOOTH_REGEXS = "bluetooth_regexs";
31 constexpr const char *CONFIG_KEY_BT_PAN_ADDR = "bt_pan_ipv4_addr";
32 constexpr const char *CONFIG_KEY_WIFI_HOTSPOT_ADDR = "wifi_hotspot_ipv4_addr";
33 constexpr const char *CONFIG_KEY_USB_RNDIS_ADDR = "usb_rndis_ipv4_addr";
34 constexpr const char *CONFIG_KEY_BT_PAN_DHCP_NAME = "bt_pan_dhcp_server_name";
35 constexpr const char *CONFIG_KEY_WIFI_DHCP_NAME = "wifi_hotspot_dhcp_server_name";
36 constexpr const char *CONFIG_KEY_USB_DHCP_NAME = "usb_rndis_dhcp_server_name";
37 constexpr const char *CONFIG_KEY_USB_IFACE_NAME = "usb_iface_name";
38 constexpr const char *CONFIG_KEY_ROUTE_SUFFIX = "route_suffix";
39 constexpr const char *CONFIG_KEY_DHCP_ENDIP = "dhcp_endip";
40 constexpr const char *CONFIG_KEY_DEFAULT_MASK = "default_mask";
41 constexpr const char *CONFIG_KEY_WIFI_SET_DHCP = "wifi_hotspot_set_dhcp";
42 constexpr const char *SPLIT_SYMBOL_1 = ":";
43 constexpr const char *SPLIT_SYMBOL_2 = ",";
44 constexpr const char *VALUE_SUPPORT_TRUE = "true";
45 } // namespace
46 
NetworkShareConfiguration()47 NetworkShareConfiguration::NetworkShareConfiguration()
48 {
49     configMap_[CONFIG_KEY_SHARE_SUPPORT] = Config_Value::CONFIG_VALUE_SHARE_SUPPORT;
50     configMap_[CONFIG_KEY_USB_REGEXS] = Config_Value::CONFIG_VALUE_USB_REGEXS;
51     configMap_[CONFIG_KEY_WIFI_REGEXS] = Config_Value::CONFIG_VALUE_WIFI_REGEXS;
52     configMap_[CONFIG_KEY_BLUETOOTH_REGEXS] = Config_Value::CONFIG_VALUE_BLUETOOTH_REGEXS;
53     configMap_[CONFIG_KEY_BT_PAN_ADDR] = Config_Value::CONFIG_VALUE_BT_PAN_ADDR;
54     configMap_[CONFIG_KEY_WIFI_HOTSPOT_ADDR] = Config_Value::CONFIG_VALUE_WIFI_HOTSPOT_ADDR;
55     configMap_[CONFIG_KEY_USB_RNDIS_ADDR] = Config_Value::CONFIG_VALUE_USB_RNDIS_ADDR;
56     configMap_[CONFIG_KEY_BT_PAN_DHCP_NAME] = Config_Value::CONFIG_VALUE_BT_PAN_DHCP_NAME;
57     configMap_[CONFIG_KEY_WIFI_DHCP_NAME] = Config_Value::CONFIG_VALUE_WIFI_DHCP_NAME;
58     configMap_[CONFIG_KEY_USB_DHCP_NAME] = Config_Value::CONFIG_VALUE_USB_DHCP_NAME;
59     configMap_[CONFIG_KEY_USB_IFACE_NAME] = Config_Value::CONFIG_VALUE_USB_IFACE_NAME;
60     configMap_[CONFIG_KEY_ROUTE_SUFFIX] = Config_Value::CONFIG_VALUE_ROUTE_SUFFIX;
61     configMap_[CONFIG_KEY_DHCP_ENDIP] = Config_Value::CONFIG_VALUE_DHCP_ENDIP;
62     configMap_[CONFIG_KEY_DEFAULT_MASK] = Config_Value::CONFIG_VALUE_DEFAULT_MASK;
63     configMap_[CONFIG_KEY_WIFI_SET_DHCP] = Config_Value::CONFIG_VALUE_WIFI_SET_DHCP;
64     LoadConfigData();
65 }
66 
IsNetworkSharingSupported() const67 bool NetworkShareConfiguration::IsNetworkSharingSupported() const
68 {
69     return supported_;
70 }
71 
IsUsbIface(const std::string & iface)72 bool NetworkShareConfiguration::IsUsbIface(const std::string &iface)
73 {
74     return MatchesDownstreamRegexs(iface, usbRegexs_);
75 }
76 
IsWifiIface(const std::string & iface)77 bool NetworkShareConfiguration::IsWifiIface(const std::string &iface)
78 {
79     return MatchesDownstreamRegexs(iface, wifiRegexs_);
80 }
81 
IsBluetoothIface(const std::string & iface)82 bool NetworkShareConfiguration::IsBluetoothIface(const std::string &iface)
83 {
84     return MatchesDownstreamRegexs(iface, blueToothRegexs_);
85 }
86 
GetUsbIfaceRegexs()87 const std::vector<std::string> &NetworkShareConfiguration::GetUsbIfaceRegexs()
88 {
89     return usbRegexs_;
90 }
91 
GetWifiIfaceRegexs()92 const std::vector<std::string> &NetworkShareConfiguration::GetWifiIfaceRegexs()
93 {
94     return wifiRegexs_;
95 }
96 
GetBluetoothIfaceRegexs()97 const std::vector<std::string> &NetworkShareConfiguration::GetBluetoothIfaceRegexs()
98 {
99     return blueToothRegexs_;
100 }
101 
GetBtpanIpv4Addr()102 std::string &NetworkShareConfiguration::GetBtpanIpv4Addr()
103 {
104     return btPanIpv4Str_;
105 }
106 
GetWifiHotspotIpv4Addr()107 std::string &NetworkShareConfiguration::GetWifiHotspotIpv4Addr()
108 {
109     return wifiIpv4Str_;
110 }
111 
GetUsbRndisIpv4Addr()112 std::string &NetworkShareConfiguration::GetUsbRndisIpv4Addr()
113 {
114     return usbIpv4Str_;
115 }
116 
GetRouteSuffix()117 std::string &NetworkShareConfiguration::GetRouteSuffix()
118 {
119     return routeSuffix_;
120 }
121 
GetBtpanDhcpServerName()122 std::string &NetworkShareConfiguration::GetBtpanDhcpServerName()
123 {
124     return btPanDhcpServerName_;
125 }
126 
GetWifiHotspotDhcpServerName()127 std::string &NetworkShareConfiguration::GetWifiHotspotDhcpServerName()
128 {
129     return wifiDhcpServerName_;
130 }
131 
GetUsbRndisDhcpServerName()132 std::string &NetworkShareConfiguration::GetUsbRndisDhcpServerName()
133 {
134     return usbDhcpServerName_;
135 }
136 
GetUsbRndisIfaceName()137 std::string &NetworkShareConfiguration::GetUsbRndisIfaceName()
138 {
139     return usbIfaceName_;
140 }
141 
GetDefaultMask()142 std::string &NetworkShareConfiguration::GetDefaultMask()
143 {
144     return defaultMask_;
145 }
146 
GetDhcpEndIP()147 std::string &NetworkShareConfiguration::GetDhcpEndIP()
148 {
149     return dhcpEndIP_;
150 }
151 
GetWifiHotspotSetDhcpFlag() const152 bool NetworkShareConfiguration::GetWifiHotspotSetDhcpFlag() const
153 {
154     return isWifiHotspotSetDhcp_;
155 }
156 
MatchesDownstreamRegexs(const std::string & iface,std::vector<std::string> & regexs)157 bool NetworkShareConfiguration::MatchesDownstreamRegexs(const std::string &iface, std::vector<std::string> &regexs)
158 {
159     for (std::vector<std::string>::iterator iter = regexs.begin(); iter != regexs.end(); iter++) {
160         std::regex str_regex(*iter);
161         if (std::regex_match(iface.c_str(), str_regex)) {
162             return true;
163         }
164     }
165     return false;
166 }
167 
ReadConfigFile()168 std::vector<std::string> NetworkShareConfiguration::ReadConfigFile()
169 {
170     NETMGR_EXT_LOG_I("filePath[%{private}s]", NETWORK_SHARING_CONFIG_PATH);
171     std::vector<std::string> strAll;
172     std::ifstream infile;
173     infile.open(NETWORK_SHARING_CONFIG_PATH);
174     if (!infile.is_open()) {
175         NETMGR_EXT_LOG_E("filePath[%{private}s] open failed.", NETWORK_SHARING_CONFIG_PATH);
176         return strAll;
177     }
178     std::string strLine;
179     while (getline(infile, strLine)) {
180         strAll.push_back(strLine);
181     }
182     infile.close();
183     return strAll;
184 }
185 
ParseRegexsData(std::vector<std::string> & regexs,std::string & strVal)186 void NetworkShareConfiguration::ParseRegexsData(std::vector<std::string> &regexs, std::string &strVal)
187 {
188     std::size_t pos = strVal.find(SPLIT_SYMBOL_2);
189     while (std::string::npos != pos) {
190         std::string single = strVal.substr(0, pos);
191         strVal = strVal.substr(pos + 1, strVal.size());
192         pos = strVal.find(SPLIT_SYMBOL_2);
193         if (!single.empty()) {
194             regexs.push_back(single);
195         }
196     }
197     if (!strVal.empty()) {
198         regexs.push_back(strVal);
199     }
200 }
201 
ParseConfigData(Config_Value cfgValue,std::string & strKey,std::string & strVal)202 void NetworkShareConfiguration::ParseConfigData(Config_Value cfgValue, std::string &strKey, std::string &strVal)
203 {
204     if (cfgValue == Config_Value::CONFIG_VALUE_BT_PAN_ADDR) {
205         btPanIpv4Str_ = strVal;
206     } else if (cfgValue == Config_Value::CONFIG_VALUE_WIFI_HOTSPOT_ADDR) {
207         wifiIpv4Str_ = strVal;
208     } else if (cfgValue == Config_Value::CONFIG_VALUE_USB_RNDIS_ADDR) {
209         usbIpv4Str_ = strVal;
210     } else if (cfgValue == Config_Value::CONFIG_VALUE_BT_PAN_DHCP_NAME) {
211         btPanDhcpServerName_ = strVal;
212     } else if (cfgValue == Config_Value::CONFIG_VALUE_WIFI_DHCP_NAME) {
213         wifiDhcpServerName_ = strVal;
214     } else if (cfgValue == Config_Value::CONFIG_VALUE_USB_DHCP_NAME) {
215         usbDhcpServerName_ = strVal;
216     } else if (cfgValue == Config_Value::CONFIG_VALUE_USB_IFACE_NAME) {
217         usbIfaceName_ = strVal;
218     } else if (cfgValue == Config_Value::CONFIG_VALUE_ROUTE_SUFFIX) {
219         routeSuffix_ = strVal;
220     } else if (cfgValue == Config_Value::CONFIG_VALUE_DHCP_ENDIP) {
221         dhcpEndIP_ = strVal;
222     } else if (cfgValue == Config_Value::CONFIG_VALUE_DEFAULT_MASK) {
223         defaultMask_ = strVal;
224     } else {
225         NETMGR_EXT_LOG_E("strKey:%{public}s is unknown data.", strKey.c_str());
226     }
227 }
228 
ParseLineData(std::string & strKey,std::string & strVal)229 void NetworkShareConfiguration::ParseLineData(std::string &strKey, std::string &strVal)
230 {
231     switch (configMap_[strKey]) {
232         case Config_Value::CONFIG_VALUE_SHARE_SUPPORT:
233             if (strVal == VALUE_SUPPORT_TRUE) {
234                 supported_ = true;
235             }
236             break;
237         case Config_Value::CONFIG_VALUE_USB_REGEXS:
238             ParseRegexsData(usbRegexs_, strVal);
239             break;
240         case Config_Value::CONFIG_VALUE_WIFI_REGEXS:
241             ParseRegexsData(wifiRegexs_, strVal);
242             break;
243         case Config_Value::CONFIG_VALUE_BLUETOOTH_REGEXS:
244             ParseRegexsData(blueToothRegexs_, strVal);
245             break;
246         case Config_Value::CONFIG_VALUE_WIFI_SET_DHCP:
247             if (strVal == VALUE_SUPPORT_TRUE) {
248                 isWifiHotspotSetDhcp_ = true;
249             }
250             break;
251         default:
252             ParseConfigData(configMap_[strKey], strKey, strVal);
253             break;
254     }
255 }
256 
LoadConfigData()257 int32_t NetworkShareConfiguration::LoadConfigData()
258 {
259     isWifiHotspotSetDhcp_ = false;
260     supported_ = false;
261     usbRegexs_.clear();
262     wifiRegexs_.clear();
263     blueToothRegexs_.clear();
264 
265     std::vector<std::string> strVec = ReadConfigFile();
266     if (strVec.size() == 0) {
267         return NETWORKSHARE_ERROR_IFACE_CFG_ERROR;
268     }
269 
270     for_each(strVec.begin(), strVec.end(), [this](std::string &line) {
271         std::size_t pos = line.find(SPLIT_SYMBOL_1);
272         if (std::string::npos != pos) {
273             std::string strKey = line.substr(0, pos);
274             std::string strValue = line.substr(pos + 1, line.size());
275             NETMGR_EXT_LOG_I("strKey:%{public}s, strValue:%{private}s.", strKey.c_str(), strValue.c_str());
276             this->ParseLineData(strKey, strValue);
277         }
278     });
279     return NETMANAGER_EXT_SUCCESS;
280 }
281 } // namespace NetManagerStandard
282 } // namespace OHOS
283