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