1 /*
2 * Copyright (c) 2021 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 <csignal>
17 #include <cstdio>
18 #include <thread>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include "netnative_log_wrapper.h"
22 #include "system_ability_definition.h"
23 #include "netsys_native_service.h"
24
25 namespace OHOS {
26 namespace NetsysNative {
27 using namespace std;
28
29 REGISTER_SYSTEM_ABILITY_BY_ID(NetsysNativeService, COMM_NETSYS_NATIVE_SYS_ABILITY_ID, true);
30
NetsysNativeService()31 NetsysNativeService::NetsysNativeService()
32 : SystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID, true), netsysService_(nullptr),
33 manager_(nullptr), notifyCallback_(nullptr)
34 {
35 }
36
OnStart()37 void NetsysNativeService::OnStart()
38 {
39 NETNATIVE_LOGI("NetsysNativeService::OnStart Begin");
40 std::lock_guard<std::mutex> guard(instanceLock_);
41 if (state_ == ServiceRunningState::STATE_RUNNING) {
42 return;
43 }
44
45 if (!Init()) {
46 NETNATIVE_LOGE("NetsysNativeService init failed!");
47 return;
48 }
49 bool res = SystemAbility::Publish(this);
50 if (!res) {
51 NETNATIVE_LOGE("publishing NetsysNativeService to sa manager failed!");
52 return;
53 }
54 NETNATIVE_LOGI("Publish NetsysNativeService SUCCESS");
55 state_ = ServiceRunningState::STATE_RUNNING;
56 struct tm *timeNow;
57 time_t second = time(0);
58 if (second < 0) {
59 return;
60 }
61 timeNow = localtime(&second);
62 if (timeNow != nullptr) {
63 NETNATIVE_LOGI(
64 "NetsysNativeService start time:%{public}d-%{public}d-%{public}d %{public}d:%{public}d:%{public}d",
65 timeNow->tm_year + startTime_, timeNow->tm_mon + extraMonth_, timeNow->tm_mday, timeNow->tm_hour,
66 timeNow->tm_min, timeNow->tm_sec);
67 }
68 }
69
OnStop()70 void NetsysNativeService::OnStop()
71 {
72 std::lock_guard<std::mutex> guard(instanceLock_);
73 struct tm *timeNow;
74 time_t second = time(0);
75 if (second < 0) {
76 return;
77 }
78 timeNow = localtime(&second);
79 if (timeNow != nullptr) {
80 NETNATIVE_LOGI(
81 "NetsysNativeService dump time:%{public}d-%{public}d-%{public}d %{public}d:%{public}d:%{public}d",
82 timeNow->tm_year + startTime_, timeNow->tm_mon + extraMonth_, timeNow->tm_mday, timeNow->tm_hour,
83 timeNow->tm_min, timeNow->tm_sec);
84 }
85 state_ = ServiceRunningState::STATE_STOPPED;
86 }
87
ExitHandler(int32_t signum)88 void ExitHandler(int32_t signum)
89 {
90 exit(1);
91 }
92
Init()93 bool NetsysNativeService::Init()
94 {
95 (void)signal(SIGTERM, ExitHandler);
96 (void)signal(SIGABRT, ExitHandler);
97
98 netsysService_ = std::make_unique<nmd::NetManagerNative>();
99 if (netsysService_ == nullptr) {
100 NETNATIVE_LOGE("netsysService_ is nullptr!");
101 return false;
102 }
103 netsysService_->Init();
104
105 int32_t pid = getpid();
106 manager_ = std::make_unique<OHOS::nmd::NetlinkManager>(pid);
107 if (manager_ == nullptr) {
108 NETNATIVE_LOGE("manager_ is nullptr!");
109 return false;
110 }
111 dhcpController_ = std::make_unique<OHOS::nmd::DhcpController>();
112
113 return true;
114 }
115
SetResolverConfigParcel(const DnsresolverParamsParcel & resolvParams)116 int32_t NetsysNativeService::SetResolverConfigParcel(const DnsresolverParamsParcel& resolvParams)
117 {
118 NETNATIVE_LOGI("SetResolverConfig retryCount = %{public}d", resolvParams.retryCount_);
119
120 return 0;
121 }
122
SetResolverConfig(const DnsresolverParams & resolvParams)123 int32_t NetsysNativeService::SetResolverConfig(const DnsresolverParams &resolvParams)
124 {
125 NETNATIVE_LOGI("SetResolverConfig retryCount = %{public}d", resolvParams.retryCount);
126 return 0;
127 }
128
GetResolverConfig(const uint16_t netid,std::vector<std::string> & servers,std::vector<std::string> & domains,nmd::DnsResParams & param)129 int32_t NetsysNativeService::GetResolverConfig(const uint16_t netid, std::vector<std::string> &servers,
130 std::vector<std::string> &domains, nmd::DnsResParams ¶m)
131 {
132 NETNATIVE_LOGI("GetResolverConfig netid = %{public}d", netid);
133 NETNATIVE_LOGE("NETSYSSERVICE: %{public}d, %{public}d", param.baseTimeoutMsec, param.retryCount);
134 return 0;
135 }
136
CreateNetworkCache(const uint16_t netid)137 int32_t NetsysNativeService::CreateNetworkCache(const uint16_t netid)
138 {
139 NETNATIVE_LOGI("CreateNetworkCache Begin");
140 return 0;
141 }
142
FlushNetworkCache(const uint16_t netid)143 int32_t NetsysNativeService::FlushNetworkCache(const uint16_t netid)
144 {
145 NETNATIVE_LOGI("FlushNetworkCache Begin");
146 return 0;
147 }
148
DestroyNetworkCache(const uint16_t netid)149 int32_t NetsysNativeService::DestroyNetworkCache(const uint16_t netid)
150 {
151 NETNATIVE_LOGI("DestroyNetworkCache");
152 return 0;
153 }
154
Getaddrinfo(const char * node,const char * service,const struct addrinfo * hints,struct addrinfo ** result,const uint16_t netid)155 int32_t NetsysNativeService::Getaddrinfo(const char* node, const char* service, const struct addrinfo* hints,
156 struct addrinfo** result, const uint16_t netid)
157 {
158 NETNATIVE_LOGI("Getaddrinfo");
159 return 0;
160 }
161
InterfaceSetMtu(const std::string & interfaceName,int32_t mtu)162 int32_t NetsysNativeService::InterfaceSetMtu(const std::string &interfaceName, int32_t mtu)
163 {
164 NETNATIVE_LOGI("InterfaceSetMtu Begin");
165 return netsysService_->InterfaceSetMtu(interfaceName, mtu);
166 }
167
InterfaceGetMtu(const std::string & interfaceName)168 int32_t NetsysNativeService::InterfaceGetMtu(const std::string &interfaceName)
169 {
170 NETNATIVE_LOGI("InterfaceSetMtu Begin");
171 return netsysService_->InterfaceGetMtu(interfaceName);
172 }
173
RegisterNotifyCallback(sptr<INotifyCallback> & callback)174 int32_t NetsysNativeService::RegisterNotifyCallback(sptr<INotifyCallback> &callback)
175 {
176 NETNATIVE_LOGI("RegisterNotifyCallback");
177 notifyCallback_ = callback;
178 dhcpController_->RegisterNotifyCallback(callback);
179 return 0;
180 }
181
NetworkAddRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)182 int32_t NetsysNativeService::NetworkAddRoute(int32_t netId, const std::string &interfaceName,
183 const std::string &destination, const std::string &nextHop)
184 {
185 NETNATIVE_LOGI("NetsysNativeService::NetworkAddRoute unpacket %{public}d %{public}s", netId, interfaceName.c_str());
186
187 int32_t result = this->netsysService_->NetworkAddRoute(netId, interfaceName, destination, nextHop);
188 NETNATIVE_LOGI("NetworkAddRoute %{public}d", result);
189 return result;
190 }
191
NetworkRemoveRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)192 int32_t NetsysNativeService::NetworkRemoveRoute(int32_t netId, const std::string &interfaceName,
193 const std::string &destination, const std::string &nextHop)
194 {
195 int32_t result = this->netsysService_->NetworkRemoveRoute(netId, interfaceName, destination, nextHop);
196 NETNATIVE_LOGI("NetworkRemoveRoute %{public}d", result);
197 return result;
198 }
199
NetworkAddRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)200 int32_t NetsysNativeService::NetworkAddRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
201 {
202 int32_t result = this->netsysService_->NetworkAddRouteParcel(netId, routeInfo);
203 NETNATIVE_LOGI("NetworkAddRouteParcel %{public}d", result);
204 return result;
205 }
206
NetworkRemoveRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)207 int32_t NetsysNativeService::NetworkRemoveRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
208 {
209 int32_t result = this->netsysService_->NetworkRemoveRouteParcel(netId, routeInfo);
210 NETNATIVE_LOGI("NetworkRemoveRouteParcel %{public}d", result);
211 return result;
212 }
213
NetworkSetDefault(int32_t netId)214 int32_t NetsysNativeService::NetworkSetDefault(int32_t netId)
215 {
216 NETNATIVE_LOG_D("NetworkSetDefault in.");
217 int32_t result = this->netsysService_->NetworkSetDefault(netId);
218 NETNATIVE_LOG_D("NetworkSetDefault out.");
219 return result;
220 }
221
NetworkGetDefault()222 int32_t NetsysNativeService::NetworkGetDefault()
223 {
224 int32_t result = this->netsysService_->NetworkGetDefault();
225 NETNATIVE_LOGI("NetworkGetDefault");
226 return result;
227 }
228
NetworkClearDefault()229 int32_t NetsysNativeService::NetworkClearDefault()
230 {
231 int32_t result = this->netsysService_->NetworkClearDefault();
232 NETNATIVE_LOGI("NetworkClearDefault");
233 return result;
234 }
235
GetProcSysNet(int32_t ipversion,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)236 int32_t NetsysNativeService::GetProcSysNet(int32_t ipversion, int32_t which, const std::string &ifname,
237 const std::string ¶meter, std::string &value)
238 {
239 int32_t result = this->netsysService_->GetProcSysNet(ipversion, which, ifname, parameter, &value);
240 NETNATIVE_LOGI("GetProcSysNet");
241 return result;
242 }
243
SetProcSysNet(int32_t ipversion,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)244 int32_t NetsysNativeService::SetProcSysNet(int32_t ipversion, int32_t which, const std::string &ifname,
245 const std::string ¶meter, std::string &value)
246 {
247 int32_t result = this->netsysService_->SetProcSysNet(ipversion, which, ifname, parameter, value);
248 NETNATIVE_LOGI("SetProcSysNet");
249 return result;
250 }
251
NetworkCreatePhysical(int32_t netId,int32_t permission)252 int32_t NetsysNativeService::NetworkCreatePhysical(int32_t netId, int32_t permission)
253 {
254 int32_t result = this->netsysService_->NetworkCreatePhysical(netId, permission);
255 NETNATIVE_LOGI("NetworkCreatePhysical out.");
256 return result;
257 }
258
InterfaceAddAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)259 int32_t NetsysNativeService::InterfaceAddAddress(const std::string &interfaceName, const std::string &addrString,
260 int32_t prefixLength)
261 {
262 int32_t result = this->netsysService_->InterfaceAddAddress(interfaceName, addrString, prefixLength);
263 NETNATIVE_LOGI("InterfaceAddAddress");
264 return result;
265 }
266
InterfaceDelAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)267 int32_t NetsysNativeService::InterfaceDelAddress(const std::string &interfaceName, const std::string &addrString,
268 int32_t prefixLength)
269 {
270 int32_t result = this->netsysService_->InterfaceDelAddress(interfaceName, addrString, prefixLength);
271 NETNATIVE_LOGI("InterfaceDelAddress");
272 return result;
273 }
274
NetworkAddInterface(int32_t netId,const std::string & iface)275 int32_t NetsysNativeService::NetworkAddInterface(int32_t netId, const std::string &iface)
276 {
277 NETNATIVE_LOGI("NetworkAddInterface");
278 int32_t result = this->netsysService_->NetworkAddInterface(netId, iface);
279 return result;
280 }
281
NetworkRemoveInterface(int32_t netId,const std::string & iface)282 int32_t NetsysNativeService::NetworkRemoveInterface(int32_t netId, const std::string &iface)
283 {
284 int32_t result = this->netsysService_->NetworkRemoveInterface(netId, iface);
285 NETNATIVE_LOGI("NetworkRemoveInterface");
286 return result;
287 }
288
NetworkDestroy(int32_t netId)289 int32_t NetsysNativeService::NetworkDestroy(int32_t netId)
290 {
291 int32_t result = this->netsysService_->NetworkDestroy(netId);
292 NETNATIVE_LOGI("NetworkDestroy");
293 return result;
294 }
295
GetFwmarkForNetwork(int32_t netId,MarkMaskParcel & markMaskParcel)296 int32_t NetsysNativeService::GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel)
297 {
298 markMaskParcel = this->netsysService_->GetFwmarkForNetwork(netId);
299 NETNATIVE_LOGI("GetFwmarkForNetwork");
300 return ERR_NONE;
301 }
302
InterfaceSetConfig(const InterfaceConfigurationParcel & cfg)303 int32_t NetsysNativeService::InterfaceSetConfig(const InterfaceConfigurationParcel &cfg)
304 {
305 NETNATIVE_LOGI("InterfaceSetConfig");
306 this->netsysService_->InterfaceSetConfig(cfg);
307 return ERR_NONE;
308 }
309
InterfaceGetConfig(InterfaceConfigurationParcel & cfg)310 int32_t NetsysNativeService::InterfaceGetConfig(InterfaceConfigurationParcel &cfg)
311 {
312 NETNATIVE_LOGI("InterfaceGetConfig");
313 std::string ifName = cfg.ifName;
314 cfg = this->netsysService_->InterfaceGetConfig(ifName);
315 return ERR_NONE;
316 }
317
InterfaceGetList(std::vector<std::string> & ifaces)318 int32_t NetsysNativeService::InterfaceGetList(std::vector<std::string> &ifaces)
319 {
320 NETNATIVE_LOGI("InterfaceGetList");
321 ifaces = this->netsysService_->InterfaceGetList();
322 return ERR_NONE;
323 }
324
StartDhcpClient(const std::string & iface,bool bIpv6)325 int32_t NetsysNativeService::StartDhcpClient(const std::string &iface, bool bIpv6)
326 {
327 NETNATIVE_LOGI("StartDhcpClient");
328 this->dhcpController_->StartDhcpClient(iface, bIpv6);
329 return ERR_NONE;
330 }
331
StopDhcpClient(const std::string & iface,bool bIpv6)332 int32_t NetsysNativeService::StopDhcpClient(const std::string &iface, bool bIpv6)
333 {
334 NETNATIVE_LOGI("StopDhcpClient");
335 this->dhcpController_->StopDhcpClient(iface, bIpv6);
336 return ERR_NONE;
337 }
338
StartDhcpService(const std::string & iface,const std::string & ipv4addr)339 int32_t NetsysNativeService::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
340 {
341 NETNATIVE_LOGI("StartDhcpService");
342 this->dhcpController_->StartDhcpService(iface, ipv4addr);
343 return ERR_NONE;
344 }
345
StopDhcpService(const std::string & iface)346 int32_t NetsysNativeService::StopDhcpService(const std::string &iface)
347 {
348 NETNATIVE_LOGI("StopDhcpService");
349 this->dhcpController_->StopDhcpService(iface);
350 return ERR_NONE;
351 }
352 } // namespace NetsysNative
353 } // namespace OHOS
354