• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 const bool REGISTER_LOCAL_RESULT_NETSHARE =
29     SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetworkShareService>::GetInstance().get());
30 
NetworkShareService()31 NetworkShareService::NetworkShareService() : SystemAbility(COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID, true) {}
32 
~NetworkShareService()33 NetworkShareService::~NetworkShareService(){};
34 
OnStart()35 void NetworkShareService::OnStart()
36 {
37     if (state_ == STATE_RUNNING) {
38         NETMGR_EXT_LOG_D("OnStart Service state is already running");
39         return;
40     }
41     if (!Init()) {
42         NETMGR_EXT_LOG_E("OnStart init failed");
43         EventInfo eventInfo;
44         eventInfo.operatorType = static_cast<int32_t>(NetworkShareEventOperator::OPERATION_START_SA);
45         eventInfo.errorType = static_cast<int32_t>(NetworkShareEventErrorType::ERROR_START_SA);
46         eventInfo.errorMsg = "Start Network Share Service failed";
47         NetEventReport::SendSetupFaultEvent(eventInfo);
48         return;
49     }
50     state_ = STATE_RUNNING;
51     NETMGR_EXT_LOG_I("OnStart successful");
52 }
53 
OnStop()54 void NetworkShareService::OnStop()
55 {
56     NetworkShareTracker::GetInstance().Uninit();
57     state_ = STATE_STOPPED;
58     registerToService_ = false;
59     NETMGR_EXT_LOG_I("OnStop successful");
60 }
61 
Dump(int32_t fd,const std::vector<std::u16string> & args)62 int32_t NetworkShareService::Dump(int32_t fd, const std::vector<std::u16string> &args)
63 {
64     NETMGR_EXT_LOG_I("Start Dump, fd: %{public}d", fd);
65     std::string result;
66     GetDumpMessage(result);
67     NETMGR_EXT_LOG_I("Dump content: %{public}s", result.c_str());
68     int32_t ret = dprintf(fd, "%s\n", result.c_str());
69     return ret < 0 ? NETWORKSHARE_ERROR_INTERNAL_ERROR : NETMANAGER_EXT_SUCCESS;
70 }
71 
Init()72 bool NetworkShareService::Init()
73 {
74     if (!REGISTER_LOCAL_RESULT_NETSHARE) {
75         NETMGR_EXT_LOG_E("Register to local sa manager failed");
76         return false;
77     }
78     if (!registerToService_) {
79         if (!Publish(DelayedSingleton<NetworkShareService>::GetInstance().get())) {
80             NETMGR_EXT_LOG_E("Register to sa manager failed");
81             return false;
82         }
83         registerToService_ = true;
84     }
85     return NetworkShareTracker::GetInstance().Init();
86 }
87 
GetDumpMessage(std::string & message)88 void NetworkShareService::GetDumpMessage(std::string &message)
89 {
90     message.append("Net Sharing Info:\n");
91     int32_t supported = NETWORKSHARE_IS_UNSUPPORTED;
92     NetworkShareTracker::GetInstance().IsNetworkSharingSupported(supported);
93     std::string surpportContent = supported == NETWORKSHARE_IS_SUPPORTED ? "surpported" : "not surpported";
94     message.append("\tIs Sharing Supported: " + surpportContent + "\n");
95     int32_t sharingStatus = NETWORKSHARE_IS_UNSHARING;
96     NetworkShareTracker::GetInstance().IsSharing(sharingStatus);
97     std::string sharingState = sharingStatus ? "is sharing" : "not sharing";
98     message.append("\tSharing State: " + sharingState + "\n");
99     if (sharingStatus) {
100         std::string sharingType;
101         GetSharingType(SharingIfaceType::SHARING_WIFI, "wifi;", sharingType);
102         GetSharingType(SharingIfaceType::SHARING_USB, "usb;", sharingType);
103         GetSharingType(SharingIfaceType::SHARING_BLUETOOTH, "bluetooth;", sharingType);
104         message.append("\tSharing Types: " + sharingType + "\n");
105     }
106 
107     std::string wifiShareRegexs;
108     GetShareRegexsContent(SharingIfaceType::SHARING_WIFI, wifiShareRegexs);
109     message.append("\tUsb Regexs: " + wifiShareRegexs + "\n");
110     std::string usbShareRegexs;
111     GetShareRegexsContent(SharingIfaceType::SHARING_USB, usbShareRegexs);
112     message.append("\tWifi Regexs: " + usbShareRegexs + "\n");
113     std::string btpanShareRegexs;
114     GetShareRegexsContent(SharingIfaceType::SHARING_BLUETOOTH, btpanShareRegexs);
115     message.append("\tBluetooth Regexs: " + btpanShareRegexs + "\n");
116 }
117 
GetSharingType(const SharingIfaceType & type,const std::string & typeContent,std::string & sharingType)118 void NetworkShareService::GetSharingType(const SharingIfaceType &type, const std::string &typeContent,
119                                          std::string &sharingType)
120 {
121     SharingIfaceState state;
122     NetworkShareTracker::GetInstance().GetSharingState(type, state);
123     if (state == SharingIfaceState::SHARING_NIC_SERVING) {
124         sharingType += typeContent;
125     }
126 }
127 
GetShareRegexsContent(const SharingIfaceType & type,std::string & shareRegexsContent)128 void NetworkShareService::GetShareRegexsContent(const SharingIfaceType &type, std::string &shareRegexsContent)
129 {
130     std::vector<std::string> regexs;
131     NetworkShareTracker::GetInstance().GetSharableRegexs(type, regexs);
132     for_each(regexs.begin(), regexs.end(),
133              [&shareRegexsContent](const std::string &regex) { shareRegexsContent += regex + ";"; });
134 }
135 
IsNetworkSharingSupported(int32_t & supported)136 int32_t NetworkShareService::IsNetworkSharingSupported(int32_t &supported)
137 {
138     if (!NetManagerPermission::IsSystemCaller()) {
139         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
140     }
141     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
142         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
143     }
144     return NetworkShareTracker::GetInstance().IsNetworkSharingSupported(supported);
145 }
146 
IsSharing(int32_t & sharingStatus)147 int32_t NetworkShareService::IsSharing(int32_t &sharingStatus)
148 {
149     if (!NetManagerPermission::IsSystemCaller()) {
150         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
151     }
152     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
153         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
154     }
155     return NetworkShareTracker::GetInstance().IsSharing(sharingStatus);
156 }
157 
StartNetworkSharing(const SharingIfaceType & type)158 int32_t NetworkShareService::StartNetworkSharing(const SharingIfaceType &type)
159 {
160     if (!NetManagerPermission::IsSystemCaller()) {
161         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
162     }
163     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
164         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
165     }
166     return NetworkShareTracker::GetInstance().StartNetworkSharing(type);
167 }
168 
StopNetworkSharing(const SharingIfaceType & type)169 int32_t NetworkShareService::StopNetworkSharing(const SharingIfaceType &type)
170 {
171     if (!NetManagerPermission::IsSystemCaller()) {
172         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
173     }
174     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
175         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
176     }
177     return NetworkShareTracker::GetInstance().StopNetworkSharing(type);
178 }
179 
RegisterSharingEvent(sptr<ISharingEventCallback> callback)180 int32_t NetworkShareService::RegisterSharingEvent(sptr<ISharingEventCallback> callback)
181 {
182     if (!NetManagerPermission::IsSystemCaller()) {
183         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
184     }
185     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
186         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
187     }
188     return NetworkShareTracker::GetInstance().RegisterSharingEvent(callback);
189 }
190 
UnregisterSharingEvent(sptr<ISharingEventCallback> callback)191 int32_t NetworkShareService::UnregisterSharingEvent(sptr<ISharingEventCallback> callback)
192 {
193     if (!NetManagerPermission::IsSystemCaller()) {
194         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
195     }
196     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
197         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
198     }
199     return NetworkShareTracker::GetInstance().UnregisterSharingEvent(callback);
200 }
201 
GetSharableRegexs(SharingIfaceType type,std::vector<std::string> & ifaceRegexs)202 int32_t NetworkShareService::GetSharableRegexs(SharingIfaceType type, std::vector<std::string> &ifaceRegexs)
203 {
204     if (!NetManagerPermission::IsSystemCaller()) {
205         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
206     }
207     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
208         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
209     }
210     return NetworkShareTracker::GetInstance().GetSharableRegexs(type, ifaceRegexs);
211 }
212 
GetSharingState(SharingIfaceType type,SharingIfaceState & state)213 int32_t NetworkShareService::GetSharingState(SharingIfaceType type, SharingIfaceState &state)
214 {
215     if (!NetManagerPermission::IsSystemCaller()) {
216         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
217     }
218     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
219         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
220     }
221     return NetworkShareTracker::GetInstance().GetSharingState(type, state);
222 }
223 
GetNetSharingIfaces(const SharingIfaceState & state,std::vector<std::string> & ifaces)224 int32_t NetworkShareService::GetNetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces)
225 {
226     if (!NetManagerPermission::IsSystemCaller()) {
227         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
228     }
229     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
230         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
231     }
232     return NetworkShareTracker::GetInstance().GetNetSharingIfaces(state, ifaces);
233 }
234 
GetStatsRxBytes(int32_t & bytes)235 int32_t NetworkShareService::GetStatsRxBytes(int32_t &bytes)
236 {
237     if (!NetManagerPermission::IsSystemCaller()) {
238         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
239     }
240     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
241         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
242     }
243     return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_RX, bytes);
244 }
245 
GetStatsTxBytes(int32_t & bytes)246 int32_t NetworkShareService::GetStatsTxBytes(int32_t &bytes)
247 {
248     if (!NetManagerPermission::IsSystemCaller()) {
249         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
250     }
251     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
252         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
253     }
254     return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_TX, bytes);
255 }
256 
GetStatsTotalBytes(int32_t & bytes)257 int32_t NetworkShareService::GetStatsTotalBytes(int32_t &bytes)
258 {
259     if (!NetManagerPermission::IsSystemCaller()) {
260         return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
261     }
262     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
263         return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
264     }
265     return NetworkShareTracker::GetInstance().GetSharedSubSMTraffic(TrafficType::TRAFFIC_ALL, bytes);
266 }
267 } // namespace NetManagerStandard
268 } // namespace OHOS
269