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_service.h"
17
18 #include "net_event_report.h"
19 #include "net_manager_center.h"
20 #include "net_manager_constants.h"
21 #include "netmanager_base_permission.h"
22 #include "netmgr_ext_log_wrapper.h"
23 #include "networkshare_constants.h"
24 #include "xcollie/xcollie.h"
25 #include "xcollie/xcollie_define.h"
26 #include "system_ability_definition.h"
27 #include "netsys_controller.h"
28 #include "edm_parameter_utils.h"
29
30 namespace OHOS {
31 namespace NetManagerStandard {
32 const std::string NETWORK_TIMER = "NetworkShare::RegisterSharingEvent";
33 const bool REGISTER_LOCAL_RESULT_NETSHARE =
34 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetworkShareService>::GetInstance().get());
35 constexpr int32_t XCOLLIE_TIMEOUT_DURATION = 30;
36 constexpr const char *NETWORK_SHARE_POLICY_PARAM = "persist.edm.tethering_disallowed";
37
NetworkShareService()38 NetworkShareService::NetworkShareService() : SystemAbility(COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID, true) {}
39
~NetworkShareService()40 NetworkShareService::~NetworkShareService(){};
41
OnStart()42 void NetworkShareService::OnStart()
43 {
44 if (state_ == STATE_RUNNING) {
45 NETMGR_EXT_LOG_D("OnStart Service state is already running");
46 return;
47 }
48 if (!Init()) {
49 NETMGR_EXT_LOG_E("OnStart init failed");
50 EventInfo eventInfo;
51 eventInfo.operatorType = static_cast<int32_t>(NetworkShareEventOperator::OPERATION_START_SA);
52 eventInfo.errorType = static_cast<int32_t>(NetworkShareEventErrorType::ERROR_START_SA);
53 eventInfo.errorMsg = "Start Network Share Service failed";
54 NetEventReport::SendSetupFaultEvent(eventInfo);
55 return;
56 }
57 state_ = STATE_RUNNING;
58 NETMGR_EXT_LOG_I("OnStart successful");
59 }
60
OnStop()61 void NetworkShareService::OnStop()
62 {
63 NetworkShareTracker::GetInstance().Uninit();
64 state_ = STATE_STOPPED;
65 registerToService_ = false;
66 NETMGR_EXT_LOG_I("OnStop successful");
67 }
68
Dump(int32_t fd, const std::vector<std::u16string> &args)69 int32_t NetworkShareService::Dump(int32_t fd, const std::vector<std::u16string> &args)
70 {
71 NETMGR_EXT_LOG_I("Start Dump, fd: %{public}d", fd);
72 std::string result;
73 GetDumpMessage(result);
74 NETMGR_EXT_LOG_I("Dump content: %{public}s", result.c_str());
75 int32_t ret = dprintf(fd, "%s\n", result.c_str());
76 return ret < 0 ? NETWORKSHARE_ERROR_INTERNAL_ERROR : NETMANAGER_EXT_SUCCESS;
77 }
78
Init()79 bool NetworkShareService::Init()
80 {
81 if (!REGISTER_LOCAL_RESULT_NETSHARE) {
82 NETMGR_EXT_LOG_E("Register to local sa manager failed");
83 return false;
84 }
85 if (!registerToService_) {
86 if (!Publish(DelayedSingleton<NetworkShareService>::GetInstance().get())) {
87 NETMGR_EXT_LOG_E("Register to sa manager failed");
88 return false;
89 }
90 registerToService_ = true;
91 }
92
93 AddSystemAbilityListener(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
94 AddSystemAbilityListener(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
95
96 return true;
97 }
98
GetDumpMessage(std::string &message)99 void NetworkShareService::GetDumpMessage(std::string &message)
100 {
101 message.append("Net Sharing Info:\n");
102 int32_t supported = NETWORKSHARE_IS_UNSUPPORTED;
103 NetworkShareTracker::GetInstance().IsNetworkSharingSupported(supported);
104 std::string surpportContent = supported == NETWORKSHARE_IS_SUPPORTED ? "surpported" : "not surpported";
105 message.append("\tIs Sharing Supported: " + surpportContent + "\n");
106 int32_t sharingStatus = NETWORKSHARE_IS_UNSHARING;
107 NetworkShareTracker::GetInstance().IsSharing(sharingStatus);
108 std::string sharingState = sharingStatus ? "is sharing" : "not sharing";
109 message.append("\tSharing State: " + sharingState + "\n");
110 if (sharingStatus) {
111 std::string sharingType;
112 GetSharingType(SharingIfaceType::SHARING_WIFI, "wifi;", sharingType);
113 GetSharingType(SharingIfaceType::SHARING_USB, "usb;", sharingType);
114 GetSharingType(SharingIfaceType::SHARING_BLUETOOTH, "bluetooth;", sharingType);
115 message.append("\tSharing Types: " + sharingType + "\n");
116 }
117
118 std::string wifiShareRegexs;
119 GetShareRegexsContent(SharingIfaceType::SHARING_WIFI, wifiShareRegexs);
120 message.append("\tUsb Regexs: " + wifiShareRegexs + "\n");
121 std::string usbShareRegexs;
122 GetShareRegexsContent(SharingIfaceType::SHARING_USB, usbShareRegexs);
123 message.append("\tWifi Regexs: " + usbShareRegexs + "\n");
124 std::string btpanShareRegexs;
125 GetShareRegexsContent(SharingIfaceType::SHARING_BLUETOOTH, btpanShareRegexs);
126 message.append("\tBluetooth Regexs: " + btpanShareRegexs + "\n");
127 }
128
GetSharingType(const SharingIfaceType &type, const std::string &typeContent, std::string &sharingType)129 void NetworkShareService::GetSharingType(const SharingIfaceType &type, const std::string &typeContent,
130 std::string &sharingType)
131 {
132 SharingIfaceState state;
133 NetworkShareTracker::GetInstance().GetSharingState(type, state);
134 if (state == SharingIfaceState::SHARING_NIC_SERVING) {
135 sharingType += typeContent;
136 }
137 }
138
GetShareRegexsContent(const SharingIfaceType &type, std::string &shareRegexsContent)139 void NetworkShareService::GetShareRegexsContent(const SharingIfaceType &type, std::string &shareRegexsContent)
140 {
141 std::vector<std::string> regexs;
142 NetworkShareTracker::GetInstance().GetSharableRegexs(type, regexs);
143 for_each(regexs.begin(), regexs.end(),
144 [&shareRegexsContent](const std::string ®ex) { shareRegexsContent += regex + ";"; });
145 }
146
IsNetworkSharingSupported(int32_t &supported)147 int32_t NetworkShareService::IsNetworkSharingSupported(int32_t &supported)
148 {
149 NETMGR_EXT_LOG_I("NetworkSharing IsNetworkSharingSupported");
150 if (!NetManagerPermission::IsSystemCaller()) {
151 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
152 }
153 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
154 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
155 }
156 return NetworkShareTracker::GetInstance().IsNetworkSharingSupported(supported);
157 }
158
IsSharing(int32_t &sharingStatus)159 int32_t NetworkShareService::IsSharing(int32_t &sharingStatus)
160 {
161 NETMGR_EXT_LOG_I("NetworkSharing IsSharing");
162 if (!NetManagerPermission::IsSystemCaller()) {
163 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
164 }
165 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
166 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
167 }
168 return NetworkShareTracker::GetInstance().IsSharing(sharingStatus);
169 }
170
StartNetworkSharing(const SharingIfaceType &type)171 int32_t NetworkShareService::StartNetworkSharing(const SharingIfaceType &type)
172 {
173 if (EdmParameterUtils::GetInstance().CheckBoolEdmParameter(NETWORK_SHARE_POLICY_PARAM, "false")) {
174 NETMGR_EXT_LOG_E("NetworkSharing start sharing, check EDM param true");
175 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
176 }
177 NETMGR_EXT_LOG_I("NetworkSharing start sharing,type is %{public}d", type);
178 if (!NetManagerPermission::IsSystemCaller()) {
179 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
180 }
181 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
182 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
183 }
184 int32_t ret = NetworkShareTracker::GetInstance().StartNetworkSharing(type);
185 if (ret == NETMANAGER_EXT_SUCCESS) {
186 EdmParameterUtils::GetInstance().RegisterEdmParameterChangeEvent(NETWORK_SHARE_POLICY_PARAM,
187 DisAllowNetworkShareEventCallback, this);
188 ret = NetsysController::GetInstance().UpdateNetworkSharingType(static_cast<uint32_t>(type), true);
189 }
190 return ret;
191 }
192
StopNetworkSharing(const SharingIfaceType &type)193 int32_t NetworkShareService::StopNetworkSharing(const SharingIfaceType &type)
194 {
195 NETMGR_EXT_LOG_I("NetworkSharing stop sharing,type is %{public}d", type);
196 if (!NetManagerPermission::IsSystemCaller()) {
197 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
198 }
199 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
200 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
201 }
202 int32_t ret = NetworkShareTracker::GetInstance().StopNetworkSharing(type);
203 if (ret == NETMANAGER_EXT_SUCCESS) {
204 EdmParameterUtils::GetInstance().UnRegisterEdmParameterChangeEvent(NETWORK_SHARE_POLICY_PARAM);
205 ret = NetsysController::GetInstance().UpdateNetworkSharingType(static_cast<uint32_t>(type), false);
206 }
207
208 return ret;
209 }
210
RegisterSharingEvent(sptr<ISharingEventCallback> callback)211 int32_t NetworkShareService::RegisterSharingEvent(sptr<ISharingEventCallback> callback)
212 {
213 NETMGR_EXT_LOG_I("NetworkSharing Register Sharing Event.");
214 int id = HiviewDFX::XCollie::GetInstance().SetTimer(NETWORK_TIMER, XCOLLIE_TIMEOUT_DURATION, nullptr, nullptr,
215 HiviewDFX::XCOLLIE_FLAG_LOG);
216 if (!NetManagerPermission::IsSystemCaller()) {
217 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
218 }
219 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
220 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
221 }
222 auto ret = NetworkShareTracker::GetInstance().RegisterSharingEvent(callback);
223 HiviewDFX::XCollie::GetInstance().CancelTimer(id);
224 return ret;
225 }
226
UnregisterSharingEvent(sptr<ISharingEventCallback> callback)227 int32_t NetworkShareService::UnregisterSharingEvent(sptr<ISharingEventCallback> callback)
228 {
229 NETMGR_EXT_LOG_I("NetworkSharing UnRegister Sharing Event.");
230 if (!NetManagerPermission::IsSystemCaller()) {
231 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
232 }
233 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
234 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
235 }
236 return NetworkShareTracker::GetInstance().UnregisterSharingEvent(callback);
237 }
238
GetSharableRegexs(SharingIfaceType type, std::vector<std::string> &ifaceRegexs)239 int32_t NetworkShareService::GetSharableRegexs(SharingIfaceType type, std::vector<std::string> &ifaceRegexs)
240 {
241 if (!NetManagerPermission::IsSystemCaller()) {
242 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
243 }
244 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
245 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
246 }
247 return NetworkShareTracker::GetInstance().GetSharableRegexs(type, ifaceRegexs);
248 }
249
GetSharingState(SharingIfaceType type, SharingIfaceState &state)250 int32_t NetworkShareService::GetSharingState(SharingIfaceType type, SharingIfaceState &state)
251 {
252 if (!NetManagerPermission::IsSystemCaller()) {
253 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
254 }
255 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
256 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
257 }
258 return NetworkShareTracker::GetInstance().GetSharingState(type, state);
259 }
260
GetNetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces)261 int32_t NetworkShareService::GetNetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces)
262 {
263 if (!NetManagerPermission::IsSystemCaller()) {
264 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
265 }
266 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
267 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
268 }
269 return NetworkShareTracker::GetInstance().GetNetSharingIfaces(state, ifaces);
270 }
271
GetStatsRxBytes(int32_t &bytes)272 int32_t NetworkShareService::GetStatsRxBytes(int32_t &bytes)
273 {
274 if (!NetManagerPermission::IsSystemCaller()) {
275 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
276 }
277 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
278 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
279 }
280 return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_RX, bytes);
281 }
282
GetStatsTxBytes(int32_t &bytes)283 int32_t NetworkShareService::GetStatsTxBytes(int32_t &bytes)
284 {
285 if (!NetManagerPermission::IsSystemCaller()) {
286 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
287 }
288 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
289 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
290 }
291 return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_TX, bytes);
292 }
293
GetStatsTotalBytes(int32_t &bytes)294 int32_t NetworkShareService::GetStatsTotalBytes(int32_t &bytes)
295 {
296 if (!NetManagerPermission::IsSystemCaller()) {
297 return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
298 }
299 if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
300 return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
301 }
302 return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_ALL, bytes);
303 }
304
OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)305 void NetworkShareService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
306 {
307 NETMGR_EXT_LOG_D("OnAddSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
308 if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
309 if (hasSARemoved_) {
310 OnNetSysRestart();
311 hasSARemoved_ = false;
312 }
313 }
314 if (systemAbilityId == COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
315 NetworkShareTracker::GetInstance().Init();
316 }
317 }
318
OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)319 void NetworkShareService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
320 {
321 NETMGR_EXT_LOG_D("OnRemoveSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
322 if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
323 hasSARemoved_ = true;
324 }
325 }
326
OnNetSysRestart()327 void NetworkShareService::OnNetSysRestart()
328 {
329 NETMGR_EXT_LOG_I("OnNetSysRestart");
330 NetworkShareTracker::GetInstance().RestartResume();
331 }
332
DisAllowNetworkShareEventCallback(const char *key, const char *value, void *context)333 void NetworkShareService::DisAllowNetworkShareEventCallback(const char *key, const char *value, void *context)
334 {
335 if (strcmp(value, "true") == 0) {
336 NETMGR_EXT_LOG_I("DisAllowNetworkShareEventCallback calledstop all network sharing with %{public}s", value);
337
338 if (!context) {
339 NETMGR_EXT_LOG_I("DisAllowNetworkShareEventCallback context is NULL");
340 return;
341 }
342
343 NetworkShareService* servicePtr = static_cast<NetworkShareService*>(context);
344 std::string sharingType;
345 servicePtr->GetSharingType(SharingIfaceType::SHARING_WIFI, "wifi;", sharingType);
346 servicePtr->GetSharingType(SharingIfaceType::SHARING_USB, "usb;", sharingType);
347 servicePtr->GetSharingType(SharingIfaceType::SHARING_BLUETOOTH, "bluetooth;", sharingType);
348 if (sharingType.find("wifi") != std::string::npos) {
349 servicePtr->StopNetworkSharing(SharingIfaceType::SHARING_WIFI);
350 }
351 if (sharingType.find("usb") != std::string::npos) {
352 servicePtr->StopNetworkSharing(SharingIfaceType::SHARING_USB);
353 }
354 if (sharingType.find("bluetooth") != std::string::npos) {
355 servicePtr->StopNetworkSharing(SharingIfaceType::SHARING_BLUETOOTH);
356 }
357 }
358 }
359 } // namespace NetManagerStandard
360 } // namespace OHOS
361