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 NETWORKSHARE_CONFIGURATION_H 17 #define NETWORKSHARE_CONFIGURATION_H 18 19 #include <fstream> 20 #include <map> 21 #include <vector> 22 #include <sys/stat.h> 23 #include <sys/types.h> 24 #include <fcntl.h> 25 #include <unistd.h> 26 #include "networkshare_configuration.h" 27 28 namespace OHOS { 29 namespace NetManagerStandard { 30 class NetworkShareConfiguration { 31 public: 32 NetworkShareConfiguration(); 33 ~NetworkShareConfiguration() = default; 34 35 /** 36 * is surpport share network 37 */ 38 bool IsNetworkSharingSupported() const; 39 40 /** 41 * is usb iface by regex 42 */ 43 bool IsUsbIface(const std::string &iface); 44 45 /** 46 * is wifi iface by regex 47 */ 48 bool IsWifiIface(const std::string &iface); 49 50 /** 51 * is bluetooth iface by regex 52 */ 53 bool IsBluetoothIface(const std::string &iface); 54 55 /** 56 * get usb iface regex 57 */ 58 const std::vector<std::string> &GetUsbIfaceRegexs(); 59 60 /** 61 * get wifi iface regex 62 */ 63 const std::vector<std::string> &GetWifiIfaceRegexs(); 64 65 /** 66 * get bluetooth iface regex 67 */ 68 const std::vector<std::string> &GetBluetoothIfaceRegexs(); 69 70 /** 71 * get wifi hotspot set dhcp flag 72 */ 73 bool GetWifiHotspotSetDhcpFlag() const; 74 75 /** 76 * get btpan default ipv4 address 77 */ 78 std::string &GetBtpanIpv4Addr(); 79 80 /** 81 * get wifi hotspot default ipv4 address 82 */ 83 std::string &GetWifiHotspotIpv4Addr(); 84 85 /** 86 * get usb rndis default ipv4 address 87 */ 88 std::string &GetUsbRndisIpv4Addr(); 89 90 /** 91 * get the default route suffix 92 */ 93 std::string &GetRouteSuffix(); 94 95 /** 96 * get the btpan dhcp server name 97 */ 98 std::string &GetBtpanDhcpServerName(); 99 100 /** 101 * get the wifi hotspot dhcp server name 102 */ 103 std::string &GetWifiHotspotDhcpServerName(); 104 105 /** 106 * get the usb rndis dhcp server name 107 */ 108 std::string &GetUsbRndisDhcpServerName(); 109 110 /** 111 * get the usb rndis iface name 112 */ 113 std::string &GetUsbRndisIfaceName(); 114 115 /** 116 * get default net mask 117 */ 118 std::string &GetDefaultMask(); 119 120 /** 121 * get dhcp endIP 122 */ 123 std::string &GetDhcpEndIP(); 124 125 /** 126 * set tethering sys ctl prop 127 */ 128 void SetConfigureForShare(bool enabled); 129 130 private: 131 int32_t LoadConfigData(); 132 bool MatchesDownstreamRegexs(const std::string &, std::vector<std::string> ®exs); 133 std::vector<std::string> ReadConfigFile(); 134 void ParseLineData(std::string &strKey, std::string &strVal); 135 void ParseRegexsData(std::vector<std::string> ®exs, std::string &strVal); 136 137 private: 138 enum class Config_Value { 139 CONFIG_VALUE_SHARE_SUPPORT, 140 CONFIG_VALUE_USB_REGEXS, 141 CONFIG_VALUE_WIFI_REGEXS, 142 CONFIG_VALUE_BLUETOOTH_REGEXS, 143 CONFIG_VALUE_BT_PAN_ADDR, 144 CONFIG_VALUE_WIFI_HOTSPOT_ADDR, 145 CONFIG_VALUE_USB_RNDIS_ADDR, 146 CONFIG_VALUE_BT_PAN_DHCP_NAME, 147 CONFIG_VALUE_WIFI_DHCP_NAME, 148 CONFIG_VALUE_USB_DHCP_NAME, 149 CONFIG_VALUE_USB_IFACE_NAME, 150 CONFIG_VALUE_ROUTE_SUFFIX, 151 CONFIG_VALUE_DHCP_ENDIP, 152 CONFIG_VALUE_DEFAULT_MASK, 153 CONFIG_VALUE_WIFI_SET_DHCP, 154 }; 155 156 bool isWifiHotspotSetDhcp_ = false; 157 bool supported_ = false; 158 std::vector<std::string> usbRegexs_; 159 std::vector<std::string> wifiRegexs_; 160 std::vector<std::string> blueToothRegexs_; 161 std::string btPanIpv4Str_; 162 std::string wifiIpv4Str_; 163 std::string usbIpv4Str_; 164 std::string routeSuffix_; 165 std::string btPanDhcpServerName_; 166 std::string wifiDhcpServerName_; 167 std::string usbDhcpServerName_; 168 std::string usbIfaceName_; 169 std::string defaultMask_; 170 std::string dhcpEndIP_; 171 std::map<std::string, Config_Value> configMap_; 172 static constexpr const char kTcpBeLiberal_[] = "/proc/sys/net/netfilter/nf_conntrack_tcp_be_liberal"; 173 174 private: 175 void ParseConfigData(Config_Value cfgValue, std::string &strKey, std::string &strVal); 176 }; 177 } // namespace NetManagerStandard 178 } // namespace OHOS 179 #endif // NETWORKSHARE_CONFIGURATION_H 180