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 #include "netsys_controller.h"
16
17 #include "netsys_controller_service_impl.h"
18 #include "net_conn_types.h"
19 #include "net_mgr_log_wrapper.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
NetsysController()23 NetsysController::NetsysController()
24 {}
25
~NetsysController()26 NetsysController::~NetsysController() {}
27
Init()28 void NetsysController::Init()
29 {
30 NETMGR_LOG_I("netsys Init");
31 if (initFlag_) {
32 NETMGR_LOG_I("netsys initialization is complete");
33 return;
34 }
35 initFlag_ = true;
36 netsysService_ = std::make_unique<NetsysControllerServiceImpl>().release();
37 netsysService_->Init();
38 }
39
GetInstance()40 NetsysController &NetsysController::GetInstance()
41 {
42 static NetsysController g_singleInstance_;
43 static std::mutex g_mutex_;
44 if (!g_singleInstance_.initFlag_) {
45 std::unique_lock<std::mutex> lock(g_mutex_);
46 if (!g_singleInstance_.initFlag_) {
47 g_singleInstance_.Init();
48 }
49 }
50 return g_singleInstance_;
51 }
52
NetworkCreatePhysical(int32_t netId,int32_t permission)53 int32_t NetsysController::NetworkCreatePhysical(int32_t netId, int32_t permission)
54 {
55 NETMGR_LOG_I("Create Physical network: netId[%{public}d], permission[%{public}d]", netId, permission);
56 if (netsysService_ == nullptr) {
57 NETMGR_LOG_E("netsysService_ is null");
58 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
59 }
60 return netsysService_->NetworkCreatePhysical(netId, permission);
61 }
62
NetworkDestroy(int32_t netId)63 int32_t NetsysController::NetworkDestroy(int32_t netId)
64 {
65 NETMGR_LOG_I("Destroy network: netId[%{public}d]", netId);
66 if (netsysService_ == nullptr) {
67 NETMGR_LOG_E("netsysService_ is null");
68 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
69 }
70 return netsysService_->NetworkDestroy(netId);
71 }
72
NetworkAddInterface(int32_t netId,const std::string & iface)73 int32_t NetsysController::NetworkAddInterface(int32_t netId, const std::string &iface)
74 {
75 NETMGR_LOG_I("Add network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
76 if (netsysService_ == nullptr) {
77 NETMGR_LOG_E("netsysService_ is null");
78 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
79 }
80 return netsysService_->NetworkAddInterface(netId, iface);
81 }
82
NetworkRemoveInterface(int32_t netId,const std::string & iface)83 int32_t NetsysController::NetworkRemoveInterface(int32_t netId, const std::string &iface)
84 {
85 NETMGR_LOG_I("Remove network interface: netId[%{public}d], iface[%{public}s]", netId, iface.c_str());
86 if (netsysService_ == nullptr) {
87 NETMGR_LOG_E("netsysService_ is null");
88 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
89 }
90 return netsysService_->NetworkRemoveInterface(netId, iface);
91 }
92
NetworkAddRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)93 int32_t NetsysController::NetworkAddRoute(int32_t netId, const std::string &ifName,
94 const std::string &destination, const std::string &nextHop)
95 {
96 NETMGR_LOG_I("Add Route: netId[%{public}d], ifName[%{public}s]", netId, ifName.c_str());
97 if (netsysService_ == nullptr) {
98 NETMGR_LOG_E("netsysService_ is null");
99 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
100 }
101 return netsysService_->NetworkAddRoute(netId, ifName, destination, nextHop);
102 }
103
NetworkRemoveRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)104 int32_t NetsysController::NetworkRemoveRoute(int32_t netId, const std::string &ifName,
105 const std::string &destination, const std::string &nextHop)
106 {
107 NETMGR_LOG_I("Remove Route: netId[%{public}d], ifName[%{public}s]", netId, ifName.c_str());
108 if (netsysService_ == nullptr) {
109 NETMGR_LOG_E("netsysService_ is null");
110 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
111 }
112 return netsysService_->NetworkRemoveRoute(netId, ifName, destination, nextHop);
113 }
114
SetInterfaceDown(const std::string & iface)115 int32_t NetsysController::SetInterfaceDown(const std::string &iface)
116 {
117 NETMGR_LOG_I("Set interface down: iface[%{public}s]", iface.c_str());
118 if (netsysService_ == nullptr) {
119 NETMGR_LOG_E("netsysService_ is null");
120 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
121 }
122 return netsysService_->SetInterfaceDown(iface);
123 }
124
SetInterfaceUp(const std::string & iface)125 int32_t NetsysController::SetInterfaceUp(const std::string &iface)
126 {
127 NETMGR_LOG_I("Set interface up: iface[%{public}s]", iface.c_str());
128 if (netsysService_ == nullptr) {
129 NETMGR_LOG_E("netsysService_ is null");
130 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
131 }
132 return netsysService_->SetInterfaceUp(iface);
133 }
134
InterfaceClearAddrs(const std::string & ifName)135 void NetsysController::InterfaceClearAddrs(const std::string &ifName)
136 {
137 NETMGR_LOG_I("Clear addrs: ifName[%{public}s]", ifName.c_str());
138 if (netsysService_ == nullptr) {
139 NETMGR_LOG_E("netsysService_ is null");
140 return;
141 }
142 return netsysService_->InterfaceClearAddrs(ifName);
143 }
144
InterfaceGetMtu(const std::string & ifName)145 int32_t NetsysController::InterfaceGetMtu(const std::string &ifName)
146 {
147 NETMGR_LOG_I("Get mtu: ifName[%{public}s]", ifName.c_str());
148 if (netsysService_ == nullptr) {
149 NETMGR_LOG_E("netsysService_ is null");
150 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
151 }
152 return netsysService_->InterfaceGetMtu(ifName);
153 }
154
InterfaceSetMtu(const std::string & ifName,int32_t mtu)155 int32_t NetsysController::InterfaceSetMtu(const std::string &ifName, int32_t mtu)
156 {
157 NETMGR_LOG_I("Set mtu: ifName[%{public}s], mtu[%{public}d]", ifName.c_str(), mtu);
158 if (netsysService_ == nullptr) {
159 NETMGR_LOG_E("netsysService_ is null");
160 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
161 }
162 return netsysService_->InterfaceSetMtu(ifName, mtu);
163 }
164
InterfaceAddAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)165 int32_t NetsysController::InterfaceAddAddress(const std::string &ifName,
166 const std::string &ipAddr, int32_t prefixLength)
167 {
168 NETMGR_LOG_I("Add address: ifName[%{public}s], prefixLength[%{public}d]", ifName.c_str(), prefixLength);
169 if (netsysService_ == nullptr) {
170 NETMGR_LOG_E("netsysService_ is null");
171 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
172 }
173 return netsysService_->InterfaceAddAddress(ifName, ipAddr, prefixLength);
174 }
175
InterfaceDelAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)176 int32_t NetsysController::InterfaceDelAddress(const std::string &ifName,
177 const std::string &ipAddr, int32_t prefixLength)
178 {
179 NETMGR_LOG_I("Delete address: ifName[%{public}s], prefixLength[%{public}d]", ifName.c_str(), prefixLength);
180 if (netsysService_ == nullptr) {
181 NETMGR_LOG_E("netsysService_ is null");
182 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
183 }
184 return netsysService_->InterfaceDelAddress(ifName, ipAddr, prefixLength);
185 }
186
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)187 int32_t NetsysController::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
188 const std::vector<std::string> &servers, const std::vector<std::string> &domains)
189 {
190 NETMGR_LOG_I("Set resolver config: netId[%{public}d]", netId);
191 if (netsysService_ == nullptr) {
192 NETMGR_LOG_E("netsysService_ is null");
193 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
194 }
195 return netsysService_->SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
196 }
197
GetResolverInfo(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)198 int32_t NetsysController::GetResolverInfo(uint16_t netId, std::vector<std::string> &servers,
199 std::vector<std::string> &domains, uint16_t &baseTimeoutMsec, uint8_t &retryCount)
200 {
201 NETMGR_LOG_I("Get resolver config: netId[%{public}d]", netId);
202 if (netsysService_ == nullptr) {
203 NETMGR_LOG_E("netsysService_ is null");
204 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
205 }
206 return netsysService_->GetResolverInfo(netId, servers, domains, baseTimeoutMsec, retryCount);
207 }
208
CreateNetworkCache(uint16_t netId)209 int32_t NetsysController::CreateNetworkCache(uint16_t netId)
210 {
211 NETMGR_LOG_I("create dns cache: netId[%{public}d]", netId);
212 if (netsysService_ == nullptr) {
213 NETMGR_LOG_E("netsysService_ is null");
214 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
215 }
216 return netsysService_->CreateNetworkCache(netId);
217 }
218
DestroyNetworkCache(uint16_t netId)219 int32_t NetsysController::DestroyNetworkCache(uint16_t netId)
220 {
221 NETMGR_LOG_I("Destroy dns cache: netId[%{public}d]", netId);
222 if (netsysService_ == nullptr) {
223 NETMGR_LOG_E("netsysService_ is null");
224 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
225 }
226 return netsysService_->DestroyNetworkCache(netId);
227 }
228
FlushNetworkCache(uint16_t netId)229 int32_t NetsysController::FlushNetworkCache(uint16_t netId)
230 {
231 NETMGR_LOG_I("Destroy Flush dns cache: netId[%{public}d]", netId);
232 if (netsysService_ == nullptr) {
233 NETMGR_LOG_E("netsysService_ is null");
234 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
235 }
236 return netsysService_->FlushNetworkCache(netId);
237 }
238
GetAddrInfo(const std::string & hostName,const std::string & serverName,const struct addrinfo & hints,std::unique_ptr<addrinfo> & res,uint16_t netId)239 int32_t NetsysController::GetAddrInfo(const std::string &hostName, const std::string &serverName,
240 const struct addrinfo &hints, std::unique_ptr<addrinfo> &res, uint16_t netId)
241 {
242 NETMGR_LOG_I("NetsysController GetAddrInfo");
243 if (netsysService_ == nullptr) {
244 NETMGR_LOG_E("netsysService_ is null");
245 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
246 }
247 return netsysService_->GetAddrInfo(hostName, serverName, hints, res, netId);
248 }
249
GetCellularRxBytes()250 int64_t NetsysController::GetCellularRxBytes()
251 {
252 NETMGR_LOG_I("NetsysController GetCellularRxBytes");
253 if (netsysService_ == nullptr) {
254 NETMGR_LOG_E("netsysService_ is null");
255 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
256 }
257 return netsysService_->GetCellularRxBytes();
258 }
259
GetCellularTxBytes()260 int64_t NetsysController::GetCellularTxBytes()
261 {
262 NETMGR_LOG_I("NetsysController GetCellularTxBytes");
263 if (netsysService_ == nullptr) {
264 NETMGR_LOG_E("netsysService_ is null");
265 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
266 }
267 return netsysService_->GetCellularTxBytes();
268 }
269
GetAllRxBytes()270 int64_t NetsysController::GetAllRxBytes()
271 {
272 NETMGR_LOG_I("NetsysController GetAllRxBytes");
273 if (netsysService_ == nullptr) {
274 NETMGR_LOG_E("netsysService_ is null");
275 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
276 }
277 return netsysService_->GetAllRxBytes();
278 }
279
GetAllTxBytes()280 int64_t NetsysController::GetAllTxBytes()
281 {
282 NETMGR_LOG_I("NetsysController GetAllTxBytes");
283 if (netsysService_ == nullptr) {
284 NETMGR_LOG_E("netsysService_ is null");
285 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
286 }
287 return netsysService_->GetAllTxBytes();
288 }
289
GetUidRxBytes(uint32_t uid)290 int64_t NetsysController::GetUidRxBytes(uint32_t uid)
291 {
292 NETMGR_LOG_I("NetsysController GetUidRxBytes");
293 if (netsysService_ == nullptr) {
294 NETMGR_LOG_E("netsysService_ is null");
295 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
296 }
297 return netsysService_->GetUidRxBytes(uid);
298 }
299
GetUidTxBytes(uint32_t uid)300 int64_t NetsysController::GetUidTxBytes(uint32_t uid)
301 {
302 NETMGR_LOG_I("NetsysController GetUidTxBytes");
303 if (netsysService_ == nullptr) {
304 NETMGR_LOG_E("netsysService_ is null");
305 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
306 }
307 return netsysService_->GetUidTxBytes(uid);
308 }
309
GetUidOnIfaceRxBytes(uint32_t uid,const std::string & interfaceName)310 int64_t NetsysController::GetUidOnIfaceRxBytes(uint32_t uid, const std::string &interfaceName)
311 {
312 NETMGR_LOG_I("NetsysController GetUidOnIfaceRxBytes");
313 if (netsysService_ == nullptr) {
314 NETMGR_LOG_E("netsysService_ is null");
315 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
316 }
317 return netsysService_->GetUidOnIfaceRxBytes(uid, interfaceName);
318 }
319
GetUidOnIfaceTxBytes(uint32_t uid,const std::string & interfaceName)320 int64_t NetsysController::GetUidOnIfaceTxBytes(uint32_t uid, const std::string &interfaceName)
321 {
322 NETMGR_LOG_I("NetsysController GetUidOnIfaceTxBytes");
323 if (netsysService_ == nullptr) {
324 NETMGR_LOG_E("netsysService_ is null");
325 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
326 }
327 return netsysService_->GetUidOnIfaceTxBytes(uid, interfaceName);
328 }
329
GetIfaceRxBytes(const std::string & interfaceName)330 int64_t NetsysController::GetIfaceRxBytes(const std::string &interfaceName)
331 {
332 NETMGR_LOG_I("NetsysController GetIfaceRxBytes");
333 if (netsysService_ == nullptr) {
334 NETMGR_LOG_E("netsysService_ is null");
335 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
336 }
337 return netsysService_->GetIfaceRxBytes(interfaceName);
338 }
339
GetIfaceTxBytes(const std::string & interfaceName)340 int64_t NetsysController::GetIfaceTxBytes(const std::string &interfaceName)
341 {
342 NETMGR_LOG_I("NetsysController GetIfaceTxBytes");
343 if (netsysService_ == nullptr) {
344 NETMGR_LOG_E("netsysService_ is null");
345 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
346 }
347 return netsysService_->GetIfaceTxBytes(interfaceName);
348 }
349
InterfaceGetList()350 std::vector<std::string> NetsysController::InterfaceGetList()
351 {
352 NETMGR_LOG_I("NetsysController InterfaceGetList");
353 std::vector<std::string> ret;
354 if (netsysService_ == nullptr) {
355 NETMGR_LOG_E("netsysService_ is null");
356 return ret;
357 }
358 return netsysService_->InterfaceGetList();
359 }
360
UidGetList()361 std::vector<std::string> NetsysController::UidGetList()
362 {
363 NETMGR_LOG_I("NetsysController UidGetList");
364 std::vector<std::string> ret;
365 if (netsysService_ == nullptr) {
366 NETMGR_LOG_E("netsysService_ is null");
367 return ret;
368 }
369 return netsysService_->UidGetList();
370 }
371
GetIfaceRxPackets(const std::string & interfaceName)372 int64_t NetsysController::GetIfaceRxPackets(const std::string &interfaceName)
373 {
374 NETMGR_LOG_D("NetsysController GetIfaceRxPackets");
375 if (netsysService_ == nullptr) {
376 NETMGR_LOG_E("netsysService_ is null");
377 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
378 }
379 return netsysService_->GetIfaceRxPackets(interfaceName);
380 }
381
GetIfaceTxPackets(const std::string & interfaceName)382 int64_t NetsysController::GetIfaceTxPackets(const std::string &interfaceName)
383 {
384 NETMGR_LOG_D("NetsysController GetIfaceTxPackets");
385 if (netsysService_ == nullptr) {
386 NETMGR_LOG_E("netsysService_ is null");
387 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
388 }
389 return netsysService_->GetIfaceTxPackets(interfaceName);
390 }
391
SetDefaultNetWork(int32_t netId)392 int32_t NetsysController::SetDefaultNetWork(int32_t netId)
393 {
394 NETMGR_LOG_D("Set DefaultNetWork: netId[%{public}d]", netId);
395 return netsysService_->SetDefaultNetWork(netId);
396 }
397
ClearDefaultNetWorkNetId()398 int32_t NetsysController::ClearDefaultNetWorkNetId()
399 {
400 NETMGR_LOG_D("ClearDefaultNetWorkNetId");
401 return netsysService_->ClearDefaultNetWorkNetId();
402 }
403
BindSocket(int32_t socket_fd,uint32_t netId)404 int32_t NetsysController::BindSocket(int32_t socket_fd, uint32_t netId)
405 {
406 NETMGR_LOG_D("NetsysController::BindSocket: netId = [%{public}u]", netId);
407 return netsysService_->BindSocket(socket_fd, netId);
408 }
409
IpEnableForwarding(const std::string & requester)410 int32_t NetsysController::IpEnableForwarding(const std::string& requester)
411 {
412 NETMGR_LOG_D("IpEnableForwarding: requester[%{public}s]", requester.c_str());
413 if (netsysService_ == nullptr) {
414 NETMGR_LOG_E("netsysService_ is null");
415 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
416 }
417 return netsysService_->IpEnableForwarding(requester);
418 }
419
IpDisableForwarding(const std::string & requester)420 int32_t NetsysController::IpDisableForwarding(const std::string& requester)
421 {
422 NETMGR_LOG_D("IpDisableForwarding: requester[%{public}s]", requester.c_str());
423 if (netsysService_ == nullptr) {
424 NETMGR_LOG_E("netsysService_ is null");
425 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
426 }
427 return netsysService_->IpDisableForwarding(requester);
428 }
429
TetherAddForward(const std::string & downstreamIface,const std::string & upstreamIface)430 int32_t NetsysController::TetherAddForward(const std::string& downstreamIface, const std::string& upstreamIface)
431 {
432 NETMGR_LOG_D("TetherAddForward: downstreamIface[%{public}s], upstreamIface[%{public}s]",
433 downstreamIface.c_str(), upstreamIface.c_str());
434 if (netsysService_ == nullptr) {
435 NETMGR_LOG_E("netsysService_ is null");
436 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
437 }
438 return netsysService_->TetherAddForward(downstreamIface, upstreamIface);
439 }
440
TetherRemoveForward(const std::string & downstreamIface,const std::string & upstreamIface)441 int32_t NetsysController::TetherRemoveForward(const std::string& downstreamIface, const std::string& upstreamIface)
442 {
443 NETMGR_LOG_D("TetherRemoveForward: downstreamIface[%{public}s], upstreamIface[%{public}s]",
444 downstreamIface.c_str(), upstreamIface.c_str());
445 if (netsysService_ == nullptr) {
446 NETMGR_LOG_E("netsysService_ is null");
447 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
448 }
449 return netsysService_->TetherRemoveForward(downstreamIface, upstreamIface);
450 }
451
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)452 int32_t NetsysController::IpfwdAddInterfaceForward(const std::string& fromIface, const std::string& toIface)
453 {
454 NETMGR_LOG_D("IpfwdAddInterfaceForward: fromIface[%{public}s], toIface[%{public}s]",
455 fromIface.c_str(), toIface.c_str());
456 if (netsysService_ == nullptr) {
457 NETMGR_LOG_E("netsysService_ is null");
458 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
459 }
460 return netsysService_->IpfwdAddInterfaceForward(fromIface, toIface);
461 }
462
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)463 int32_t NetsysController::IpfwdRemoveInterfaceForward(const std::string& fromIface, const std::string& toIface)
464 {
465 NETMGR_LOG_D("IpfwdRemoveInterfaceForward: fromIface[%{public}s], toIface[%{public}s]",
466 fromIface.c_str(), toIface.c_str());
467 if (netsysService_ == nullptr) {
468 NETMGR_LOG_E("netsysService_ is null");
469 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
470 }
471 return netsysService_->IpfwdRemoveInterfaceForward(fromIface, toIface);
472 }
473
TetherDnsSet(uint32_t netId,const std::vector<std::string> & dnsAddrs)474 int32_t NetsysController::TetherDnsSet(uint32_t netId, const std::vector<std::string>& dnsAddrs)
475 {
476 NETMGR_LOG_D("TetherDnsSet: netId[%{public}d]", netId);
477 for (auto iter = dnsAddrs.begin(); iter != dnsAddrs.end(); ++iter) {
478 NETMGR_LOG_D("TetherDnsSet: dnsAddrs[%{public}s]", iter->c_str());
479 }
480 if (netsysService_ == nullptr) {
481 NETMGR_LOG_E("netsysService_ is null");
482 return ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
483 }
484 return netsysService_->TetherDnsSet(netId, dnsAddrs);
485 }
486
RegisterNetsysNotifyCallback(const NetsysNotifyCallback & callback)487 int32_t NetsysController::RegisterNetsysNotifyCallback(const NetsysNotifyCallback &callback)
488 {
489 return netsysService_->RegisterNetsysNotifyCallback(callback);
490 }
491
BindNetworkServiceVpn(int32_t socketFd)492 int32_t NetsysController::BindNetworkServiceVpn(int32_t socketFd)
493 {
494 NETMGR_LOG_D("NetsysController::BindNetworkServiceVpn: socketFd[%{public}d]", socketFd);
495 if (socketFd <= 0) {
496 NETMGR_LOG_E("socketFd is null");
497 return ERR_VPN;
498 }
499 return netsysService_->BindNetworkServiceVpn(socketFd);
500 }
501
EnableVirtualNetIfaceCard(int32_t socketFd,struct ifreq & ifRequest,int32_t & ifaceFd)502 int32_t NetsysController::EnableVirtualNetIfaceCard(int32_t socketFd, struct ifreq &ifRequest, int32_t &ifaceFd)
503 {
504 NETMGR_LOG_D("NetsysController::EnableVirtualNetIfaceCard: socketFd[%{public}d]", socketFd);
505 if (socketFd <= 0) {
506 NETMGR_LOG_E("socketFd is null");
507 return ERR_VPN;
508 }
509 return netsysService_->EnableVirtualNetIfaceCard(socketFd, ifRequest, ifaceFd);
510 }
511
SetIpAddress(int32_t socketFd,const std::string & ipAddress,int32_t prefixLen,struct ifreq & ifRequest)512 int32_t NetsysController::SetIpAddress(int32_t socketFd, const std::string &ipAddress, int32_t prefixLen,
513 struct ifreq &ifRequest)
514 {
515 NETMGR_LOG_D("NetsysController::set addr");
516 if ((socketFd <= 0) || (ipAddress.length() == 0) || (ipAddress.length() > MAX_IPV4_ADDRESS_LEN) ||
517 (prefixLen <= 0) || (prefixLen > MAX_IPV4_ADDRESS_LEN)) {
518 NETMGR_LOG_E("The paramemters of SetIpAddress is failed, socketFd[%{public}d], "
519 "prefixLen[%{public}d].", socketFd, prefixLen);
520 return ERR_VPN;
521 }
522 return netsysService_->SetIpAddress(socketFd, ipAddress, prefixLen, ifRequest);
523 }
524
SetBlocking(int32_t ifaceFd,bool isBlock)525 int32_t NetsysController::SetBlocking(int32_t ifaceFd, bool isBlock)
526 {
527 NETMGR_LOG_D("NetsysController::SetBlocking: ifaceFd[%{public}d], isBlock[%{public}d]", ifaceFd, isBlock);
528 return netsysService_->SetBlocking(ifaceFd, isBlock);
529 }
530
StartDhcpClient(const std::string & iface,bool bIpv6)531 int32_t NetsysController::StartDhcpClient(const std::string &iface, bool bIpv6)
532 {
533 NETMGR_LOG_D("NetsysController::StartDhcpClient: iface[%{public}s], bIpv6[%{public}d]", iface.c_str(), bIpv6);
534 return netsysService_->StartDhcpClient(iface, bIpv6);
535 }
536
StopDhcpClient(const std::string & iface,bool bIpv6)537 int32_t NetsysController::StopDhcpClient(const std::string &iface, bool bIpv6)
538 {
539 NETMGR_LOG_D("NetsysController::SetBlocking: iface[%{public}s], bIpv6[%{public}d]", iface.c_str(), bIpv6);
540 return netsysService_->StopDhcpClient(iface, bIpv6);
541 }
542
RegisterCallback(sptr<NetsysControllerCallback> callback)543 int32_t NetsysController::RegisterCallback(sptr<NetsysControllerCallback> callback)
544 {
545 NETMGR_LOG_D("NetsysController::RegisterCallback");
546 return netsysService_->RegisterCallback(callback);
547 }
548
StartDhcpService(const std::string & iface,const std::string & ipv4addr)549 int32_t NetsysController::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
550 {
551 NETMGR_LOG_D("NetsysController::StartDhcpService: iface[%{public}s], ipv4addr[%{public}s]",
552 iface.c_str(), ipv4addr.c_str());
553 return netsysService_->StartDhcpService(iface, ipv4addr);
554 }
555
StopDhcpService(const std::string & iface)556 int32_t NetsysController::StopDhcpService(const std::string &iface)
557 {
558 NETMGR_LOG_D("NetsysController::StopDhcpService: ifaceFd[%{public}s]", iface.c_str());
559 return netsysService_->StopDhcpService(iface);
560 }
561 } // namespace NetManagerStandard
562 } // namespace OHOS