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
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> ®exs)
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> ®exs, 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
ParseLineData(std::string & strKey,std::string & strVal)202 void NetworkShareConfiguration::ParseLineData(std::string &strKey, std::string &strVal)
203 {
204 switch (configMap_[strKey]) {
205 case Config_Value::CONFIG_VALUE_SHARE_SUPPORT:
206 if (strVal == VALUE_SUPPORT_TRUE) {
207 supported_ = true;
208 }
209 break;
210 case Config_Value::CONFIG_VALUE_USB_REGEXS:
211 ParseRegexsData(usbRegexs_, strVal);
212 break;
213 case Config_Value::CONFIG_VALUE_WIFI_REGEXS:
214 ParseRegexsData(wifiRegexs_, strVal);
215 break;
216 case Config_Value::CONFIG_VALUE_BLUETOOTH_REGEXS:
217 ParseRegexsData(blueToothRegexs_, strVal);
218 break;
219 case Config_Value::CONFIG_VALUE_BT_PAN_ADDR:
220 btPanIpv4Str_ = strVal;
221 break;
222 case Config_Value::CONFIG_VALUE_WIFI_HOTSPOT_ADDR:
223 wifiIpv4Str_ = strVal;
224 break;
225 case Config_Value::CONFIG_VALUE_USB_RNDIS_ADDR:
226 usbIpv4Str_ = strVal;
227 break;
228 case Config_Value::CONFIG_VALUE_BT_PAN_DHCP_NAME:
229 btPanDhcpServerName_ = strVal;
230 break;
231 case Config_Value::CONFIG_VALUE_WIFI_DHCP_NAME:
232 wifiDhcpServerName_ = strVal;
233 break;
234 case Config_Value::CONFIG_VALUE_USB_DHCP_NAME:
235 usbDhcpServerName_ = strVal;
236 break;
237 case Config_Value::CONFIG_VALUE_USB_IFACE_NAME:
238 usbIfaceName_ = strVal;
239 break;
240 case Config_Value::CONFIG_VALUE_ROUTE_SUFFIX:
241 routeSuffix_ = strVal;
242 break;
243 case Config_Value::CONFIG_VALUE_DHCP_ENDIP:
244 dhcpEndIP_ = strVal;
245 break;
246 case Config_Value::CONFIG_VALUE_DEFAULT_MASK:
247 defaultMask_ = strVal;
248 break;
249 case Config_Value::CONFIG_VALUE_WIFI_SET_DHCP:
250 if (strVal == VALUE_SUPPORT_TRUE) {
251 isWifiHotspotSetDhcp_ = true;
252 }
253 break;
254 default:
255 NETMGR_EXT_LOG_E("strKey:%{public}s is unknown data.", strKey.c_str());
256 break;
257 }
258 }
259
LoadConfigData()260 int32_t NetworkShareConfiguration::LoadConfigData()
261 {
262 isWifiHotspotSetDhcp_ = false;
263 supported_ = false;
264 usbRegexs_.clear();
265 wifiRegexs_.clear();
266 blueToothRegexs_.clear();
267
268 std::vector<std::string> strVec = ReadConfigFile();
269 if (strVec.size() == 0) {
270 return NETWORKSHARE_ERROR_IFACE_CFG_ERROR;
271 }
272
273 for_each(strVec.begin(), strVec.end(), [this](std::string &line) {
274 std::size_t pos = line.find(SPLIT_SYMBOL_1);
275 if (std::string::npos != pos) {
276 std::string strKey = line.substr(0, pos);
277 std::string strValue = line.substr(pos + 1, line.size());
278 NETMGR_EXT_LOG_I("strKey:%{public}s, strValue:%{private}s.", strKey.c_str(), strValue.c_str());
279 this->ParseLineData(strKey, strValue);
280 }
281 });
282 return NETMANAGER_EXT_SUCCESS;
283 }
284 } // namespace NetManagerStandard
285 } // namespace OHOS
286