• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "net_access_policy_config.h"
17 
18 #include <fstream>
19 #include <sstream>
20 #include <sys/stat.h>
21 #include <vector>
22 
23 #include "net_mgr_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 NetAccessPolicyConfigUtils NetAccessPolicyConfigUtils::instance_;
28 namespace {
29 const char *PATH = "/system/variant/phone/base/etc/netmanager/net_access_policy_config.json";
30 const char *ARRAY_NAME = "configs";
31 const char *ITEM_BUNDLE_NAME = "bundleName";
32 const char *ITEM_DISABLE_WLAN_SWITCH = "disableWlanSwitch";
33 const char *ITEM_DISABLE_CELLULAR_SWITCH = "disableCellularSwitch";
34 
CheckFilePath(const std::string & fileName,std::string & realPath)35 bool CheckFilePath(const std::string &fileName, std::string &realPath)
36 {
37     char tmpPath[PATH_MAX] = {0};
38     if (!realpath(fileName.c_str(), tmpPath)) {
39         NETMGR_LOG_E("file name is illegal");
40         return false;
41     }
42     if (strcmp(tmpPath, PATH) != 0) {
43         NETMGR_LOG_E("file path is illegal");
44         return false;
45     }
46     realPath = tmpPath;
47     return true;
48 }
49 } // namespace
GetInstance()50 NetAccessPolicyConfigUtils &NetAccessPolicyConfigUtils::GetInstance()
51 {
52     return instance_;
53 }
GetNetAccessPolicyConfig()54 std::vector<NetAccessPolicyConfig> NetAccessPolicyConfigUtils::GetNetAccessPolicyConfig()
55 {
56     if (!isInit_) {
57         Init();
58     }
59     return netAccessPolicyConfigs_;
60 }
61 
Init()62 void NetAccessPolicyConfigUtils::Init()
63 {
64     std::lock_guard<ffrt::mutex> lock(lock_);
65     if (isInit_) {
66         return;
67     }
68     netAccessPolicyConfigs_.clear();
69     ParseNetAccessPolicyConfigs();
70     isInit_ = true;
71 }
ParseNetAccessPolicyConfigs()72 void NetAccessPolicyConfigUtils::ParseNetAccessPolicyConfigs()
73 {
74     std::string content;
75     if (!ReadFile(content, PATH)) {
76         NETMGR_LOG_E("read json file failed.");
77         return;
78     }
79     if (content.empty()) {
80         NETMGR_LOG_E("read content is empty.");
81         return;
82     }
83     cJSON *root = cJSON_Parse(content.c_str());
84     if (root == nullptr) {
85         NETMGR_LOG_E("json root is nullptr.");
86         return;
87     }
88     cJSON *configs = cJSON_GetObjectItem(root, ARRAY_NAME);
89     if (configs == nullptr || !cJSON_IsArray(configs) || cJSON_GetArraySize(configs) == 0) {
90         cJSON_Delete(root);
91         configs = nullptr;
92         root = nullptr;
93         return;
94     }
95 
96     cJSON *item = nullptr;
97     for (int i = 0; i < cJSON_GetArraySize(configs); i++) {
98         item = cJSON_GetArrayItem(configs, i);
99         if (item == nullptr) {
100             NETMGR_LOG_E("config item is nullptr.");
101             continue;
102         }
103         NetAccessPolicyConfig tmp;
104         tmp.bundleName = ParseString(cJSON_GetObjectItem(item, ITEM_BUNDLE_NAME));
105         tmp.disableWlanSwitch = ParseBoolean(cJSON_GetObjectItem(item, ITEM_DISABLE_WLAN_SWITCH));
106         tmp.disableCellularSwitch = ParseBoolean(cJSON_GetObjectItem(item, ITEM_DISABLE_CELLULAR_SWITCH));
107         netAccessPolicyConfigs_.push_back(tmp);
108     }
109 
110     cJSON_Delete(root);
111     configs = nullptr;
112     root = nullptr;
113 }
114 
ReadFile(std::string & content,const std::string & fileName)115 bool NetAccessPolicyConfigUtils::ReadFile(std::string &content, const std::string &fileName)
116 {
117     struct stat st;
118     if (stat(fileName.c_str(), &st) != 0) {
119         NETMGR_LOG_E("stat file fail");
120         return false;
121     }
122 
123     std::string realPath;
124     if (!CheckFilePath(fileName, realPath)) {
125         NETMGR_LOG_E("file does not exist");
126         return false;
127     }
128     std::fstream file(realPath.c_str(), std::fstream::in);
129     if (!file.is_open()) {
130         NETMGR_LOG_E("file open fail");
131         return false;
132     }
133 
134     std::stringstream buffer;
135     buffer << file.rdbuf();
136     content = buffer.str();
137     file.close();
138     return true;
139 }
140 
ParseString(cJSON * value)141 std::string NetAccessPolicyConfigUtils::ParseString(cJSON *value)
142 {
143     if (cJSON_IsString(value) && value->valuestring != nullptr) {
144         return value->valuestring;
145     }
146     return "";
147 }
148 
ParseBoolean(cJSON * value)149 bool NetAccessPolicyConfigUtils::ParseBoolean(cJSON *value)
150 {
151     if (cJSON_IsBool(value)) {
152         return cJSON_IsTrue(value);
153     }
154     return false;
155 }
156 
ParseInt32(cJSON * value)157 int32_t NetAccessPolicyConfigUtils::ParseInt32(cJSON *value)
158 {
159     if (cJSON_IsNumber(value)) {
160         return value->valueint;
161     }
162     return false;
163 }
164 } // namespace NetManagerStandard
165 } // namespace OHOS