1 /*
2 * Copyright (c) 2021-2024 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 <net/if.h>
17
18 #include "interface_manager.h"
19 #include "net_manager_constants.h"
20 #include "net_manager_native.h"
21 #include "netmanager_base_common_utils.h"
22 #include "netnative_log_wrapper.h"
23 #include "network_permission.h"
24 #include "route_manager.h"
25 #include "traffic_manager.h"
26 #include "vpn_manager.h"
27 #include "vnic_manager.h"
28 #include "distributed_manager.h"
29
30 using namespace OHOS::NetManagerStandard::CommonUtils;
31
32 namespace OHOS {
33 namespace nmd {
34 namespace {
35 constexpr const char *TUN_CARD_NAME = "vpn-tun";
36 constexpr const char *TCP_RMEM_PROC_FILE = "/proc/sys/net/ipv4/tcp_rmem";
37 constexpr const char *TCP_WMEM_PROC_FILE = "/proc/sys/net/ipv4/tcp_wmem";
38 constexpr uint32_t TCP_BUFFER_SIZES_TYPE = 2;
39 constexpr uint32_t MAX_TCP_BUFFER_SIZES_COUNT = 6;
40 } // namespace
41
NetManagerNative()42 NetManagerNative::NetManagerNative()
43 : bandwidthManager_(std::make_shared<BandwidthManager>()),
44 connManager_(std::make_shared<ConnManager>()),
45 firewallManager_(std::make_shared<FirewallManager>()),
46 #ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
47 wearableDistributedNet_(std::make_shared<WearableDistributedNet>()),
48 #endif
49 routeManager_(std::make_shared<RouteManager>()),
50 interfaceManager_(std::make_shared<InterfaceManager>()),
51 sharingManager_(std::make_shared<SharingManager>()),
52 dnsManager_(std::make_shared<DnsManager>())
53 {
54 }
55
GetOriginInterfaceIndex()56 void NetManagerNative::GetOriginInterfaceIndex()
57 {
58 std::vector<std::string> ifNameList = InterfaceManager::GetInterfaceNames();
59 interfaceIdex_.clear();
60 for (auto iter = ifNameList.begin(); iter != ifNameList.end(); ++iter) {
61 uint32_t infIndex = if_nametoindex((*iter).c_str());
62 interfaceIdex_.push_back(infIndex);
63 }
64 }
65
UpdateInterfaceIndex(uint32_t infIndex)66 void NetManagerNative::UpdateInterfaceIndex(uint32_t infIndex)
67 {
68 interfaceIdex_.push_back(infIndex);
69 }
70
GetCurrentInterfaceIndex()71 std::vector<uint32_t> NetManagerNative::GetCurrentInterfaceIndex()
72 {
73 return interfaceIdex_;
74 }
75
Init()76 void NetManagerNative::Init()
77 {
78 GetOriginInterfaceIndex();
79 }
80
NetworkReinitRoute()81 int32_t NetManagerNative::NetworkReinitRoute()
82 {
83 return connManager_->ReinitRoute();
84 }
85
SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker)86 int32_t NetManagerNative::SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker)
87 {
88 return connManager_->SetInternetPermission(uid, allow, isBroker);
89 }
90
NetworkCreatePhysical(int32_t netId, int32_t permission)91 int32_t NetManagerNative::NetworkCreatePhysical(int32_t netId, int32_t permission)
92 {
93 return connManager_->CreatePhysicalNetwork(static_cast<uint16_t>(netId),
94 static_cast<NetworkPermission>(permission));
95 }
96
NetworkCreateVirtual(int32_t netId, bool hasDns)97 int32_t NetManagerNative::NetworkCreateVirtual(int32_t netId, bool hasDns)
98 {
99 return connManager_->CreateVirtualNetwork(netId, hasDns);
100 }
101
NetworkDestroy(int32_t netId)102 int32_t NetManagerNative::NetworkDestroy(int32_t netId)
103 {
104 auto ret = connManager_->DestroyNetwork(netId);
105 dnsManager_->DestroyNetworkCache(netId);
106 return ret;
107 }
108
CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix, const std::set<int32_t> &uids)109 int32_t NetManagerNative::CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix,
110 const std::set<int32_t> &uids)
111 {
112 return VnicManager::GetInstance().CreateVnic(mtu, tunAddr, prefix, uids);
113 }
114
DestroyVnic()115 int32_t NetManagerNative::DestroyVnic()
116 {
117 return VnicManager::GetInstance().DestroyVnic();
118 }
119
EnableDistributedClientNet(const std::string &virnicAddr, const std::string &iif)120 int32_t NetManagerNative::EnableDistributedClientNet(const std::string &virnicAddr, const std::string &iif)
121 {
122 return routeManager_->EnableDistributedClientNet(virnicAddr, iif);
123 }
124
EnableDistributedServerNet(const std::string &iif, const std::string &devIface, const std::string &dstAddr)125 int32_t NetManagerNative::EnableDistributedServerNet(const std::string &iif, const std::string &devIface,
126 const std::string &dstAddr)
127 {
128 return routeManager_->EnableDistributedServerNet(iif, devIface, dstAddr);
129 }
130
DisableDistributedNet(bool isServer)131 int32_t NetManagerNative::DisableDistributedNet(bool isServer)
132 {
133 return routeManager_->DisableDistributedNet(isServer);
134 }
135
NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges)136 int32_t NetManagerNative::NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges)
137 {
138 dnsManager_->AddUidRange(netId, uidRanges);
139 return connManager_->AddUidsToNetwork(netId, uidRanges);
140 }
141
NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges)142 int32_t NetManagerNative::NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges)
143 {
144 dnsManager_->DelUidRange(netId, uidRanges);
145 return connManager_->RemoveUidsFromNetwork(netId, uidRanges);
146 }
147
NetworkAddInterface(int32_t netId, std::string interfaceName, NetBearType netBearerType)148 int32_t NetManagerNative::NetworkAddInterface(int32_t netId, std::string interfaceName, NetBearType netBearerType)
149 {
150 return connManager_->AddInterfaceToNetwork(netId, interfaceName, netBearerType);
151 }
152
NetworkRemoveInterface(int32_t netId, std::string interfaceName)153 int32_t NetManagerNative::NetworkRemoveInterface(int32_t netId, std::string interfaceName)
154 {
155 return connManager_->RemoveInterfaceFromNetwork(netId, interfaceName);
156 }
157
AddInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength)158 int32_t NetManagerNative::AddInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength)
159 {
160 if (strncmp(ifName.c_str(), TUN_CARD_NAME, strlen(TUN_CARD_NAME)) != 0) {
161 return interfaceManager_->AddAddress(ifName.c_str(), addrString.c_str(), prefixLength);
162 }
163 return VpnManager::GetInstance().SetVpnAddress(ifName, addrString, prefixLength);
164 }
165
DelInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength)166 int32_t NetManagerNative::DelInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength)
167 {
168 return interfaceManager_->DelAddress(ifName.c_str(), addrString.c_str(), prefixLength);
169 }
170
DelInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength, const std::string &netCapabilities)171 int32_t NetManagerNative::DelInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength,
172 const std::string &netCapabilities)
173 {
174 return interfaceManager_->DelAddress(ifName.c_str(), addrString.c_str(), prefixLength, netCapabilities);
175 }
176
NetworkAddRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop)177 int32_t NetManagerNative::NetworkAddRoute(int32_t netId, std::string interfaceName, std::string destination,
178 std::string nextHop)
179 {
180 bool routeRepeat = false;
181 auto ret = connManager_->AddRoute(netId, interfaceName, destination, nextHop, routeRepeat);
182 if (!ret || routeRepeat) {
183 dnsManager_->EnableIpv6(netId, destination, nextHop);
184 }
185 return ret;
186 }
187
NetworkRemoveRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop)188 int32_t NetManagerNative::NetworkRemoveRoute(int32_t netId, std::string interfaceName, std::string destination,
189 std::string nextHop)
190 {
191 return connManager_->RemoveRoute(netId, interfaceName, destination, nextHop);
192 }
193
NetworkGetDefault()194 int32_t NetManagerNative::NetworkGetDefault()
195 {
196 return connManager_->GetDefaultNetwork();
197 }
198
NetworkSetDefault(int32_t netId)199 int32_t NetManagerNative::NetworkSetDefault(int32_t netId)
200 {
201 dnsManager_->SetDefaultNetwork(netId);
202 return connManager_->SetDefaultNetwork(netId);
203 }
204
NetworkClearDefault()205 int32_t NetManagerNative::NetworkClearDefault()
206 {
207 return connManager_->ClearDefaultNetwork();
208 }
209
NetworkSetPermissionForNetwork(int32_t netId, NetworkPermission permission)210 int32_t NetManagerNative::NetworkSetPermissionForNetwork(int32_t netId, NetworkPermission permission)
211 {
212 return connManager_->SetPermissionForNetwork(netId, permission);
213 }
214
InterfaceGetList()215 std::vector<std::string> NetManagerNative::InterfaceGetList()
216 {
217 return InterfaceManager::GetInterfaceNames();
218 }
219
GetInterfaceConfig(std::string interfaceName)220 nmd::InterfaceConfigurationParcel NetManagerNative::GetInterfaceConfig(std::string interfaceName)
221 {
222 return InterfaceManager::GetIfaceConfig(interfaceName.c_str());
223 }
224
SetInterfaceConfig(nmd::InterfaceConfigurationParcel parcel)225 void NetManagerNative::SetInterfaceConfig(nmd::InterfaceConfigurationParcel parcel)
226 {
227 InterfaceManager::SetIfaceConfig(parcel);
228 }
229
ClearInterfaceAddrs(const std::string ifName)230 void NetManagerNative::ClearInterfaceAddrs(const std::string ifName) {}
231
GetInterfaceMtu(std::string ifName)232 int32_t NetManagerNative::GetInterfaceMtu(std::string ifName)
233 {
234 return InterfaceManager::GetMtu(ifName.c_str());
235 }
236
SetInterfaceMtu(std::string ifName, int32_t mtuValue)237 int32_t NetManagerNative::SetInterfaceMtu(std::string ifName, int32_t mtuValue)
238 {
239 if (strncmp(ifName.c_str(), TUN_CARD_NAME, strlen(TUN_CARD_NAME)) != 0) {
240 return InterfaceManager::SetMtu(ifName.c_str(), std::to_string(mtuValue).c_str());
241 }
242 return VpnManager::GetInstance().SetVpnMtu(ifName, mtuValue);
243 }
244
SetTcpBufferSizes(const std::string &tcpBufferSizes)245 int32_t NetManagerNative::SetTcpBufferSizes(const std::string &tcpBufferSizes)
246 {
247 NETNATIVE_LOGI("tcpBufferSizes:%{public}s", tcpBufferSizes.c_str());
248 const std::vector<std::string> vTcpBufferSizes = Split(tcpBufferSizes, ",");
249 if (vTcpBufferSizes.size() != MAX_TCP_BUFFER_SIZES_COUNT) {
250 NETNATIVE_LOGE("NetManagerNative::SetTcpBufferSizes size is not equals MAX_TCP_BUFFER_SIZES_COUNT");
251 return -1;
252 }
253 std::string tcp_rwmem[TCP_BUFFER_SIZES_TYPE];
254 for (size_t i = 0; i < TCP_BUFFER_SIZES_TYPE; i++) {
255 for (size_t j = 0; j < MAX_TCP_BUFFER_SIZES_COUNT / TCP_BUFFER_SIZES_TYPE; j++) {
256 tcp_rwmem[i] += Strip(vTcpBufferSizes[i * (MAX_TCP_BUFFER_SIZES_COUNT / TCP_BUFFER_SIZES_TYPE) + j]);
257 tcp_rwmem[i] += ' ';
258 }
259 }
260 if (!WriteFile(TCP_RMEM_PROC_FILE, tcp_rwmem[0]) || !WriteFile(TCP_WMEM_PROC_FILE, tcp_rwmem[1])) {
261 NETNATIVE_LOGE("NetManagerNative::SetTcpBufferSizes sysctlbyname fail %{public}d", errno);
262 return -1;
263 }
264 return 0;
265 }
266
InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)267 int32_t NetManagerNative::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
268 {
269 return InterfaceManager::SetIpAddress(ifaceName.c_str(), ipAddress.c_str());
270 }
271
InterfaceSetIffUp(std::string ifaceName)272 int32_t NetManagerNative::InterfaceSetIffUp(std::string ifaceName)
273 {
274 return InterfaceManager::SetIffUp(ifaceName.c_str());
275 }
276
GetFwmarkForNetwork(int32_t netId)277 nmd::MarkMaskParcel NetManagerNative::GetFwmarkForNetwork(int32_t netId)
278 {
279 nmd::MarkMaskParcel mark;
280 mark.mark = connManager_->GetFwmarkForNetwork(netId);
281 mark.mask = 0XFFFF;
282 return mark;
283 }
284
NetworkAddRouteParcel(int32_t netId, RouteInfoParcel parcel)285 int32_t NetManagerNative::NetworkAddRouteParcel(int32_t netId, RouteInfoParcel parcel)
286 {
287 bool routeRepeat = false;
288 return connManager_->AddRoute(netId, parcel.ifName, parcel.destination, parcel.nextHop, routeRepeat);
289 }
290
NetworkRemoveRouteParcel(int32_t netId, RouteInfoParcel parcel)291 int32_t NetManagerNative::NetworkRemoveRouteParcel(int32_t netId, RouteInfoParcel parcel)
292 {
293 return connManager_->RemoveRoute(netId, parcel.ifName, parcel.destination, parcel.nextHop);
294 }
295
SetProcSysNet(int32_t family, int32_t which, const std::string ifname, const std::string parameter, const std::string value)296 int32_t NetManagerNative::SetProcSysNet(int32_t family, int32_t which, const std::string ifname,
297 const std::string parameter, const std::string value)
298 {
299 return 0;
300 }
301
GetProcSysNet(int32_t family, int32_t which, const std::string ifname, const std::string parameter, std::string *value)302 int32_t NetManagerNative::GetProcSysNet(int32_t family, int32_t which, const std::string ifname,
303 const std::string parameter, std::string *value)
304 {
305 return 0;
306 }
307
GetCellularRxBytes()308 int64_t NetManagerNative::GetCellularRxBytes()
309 {
310 return 0;
311 }
312
GetCellularTxBytes()313 int64_t NetManagerNative::GetCellularTxBytes()
314 {
315 return 0;
316 }
317
GetAllRxBytes()318 int64_t NetManagerNative::GetAllRxBytes()
319 {
320 return nmd::TrafficManager::GetAllRxTraffic();
321 }
322
GetAllTxBytes()323 int64_t NetManagerNative::GetAllTxBytes()
324 {
325 return nmd::TrafficManager::GetAllTxTraffic();
326 }
327
GetUidTxBytes(int32_t uid)328 int64_t NetManagerNative::GetUidTxBytes(int32_t uid)
329 {
330 return 0;
331 }
332
GetUidRxBytes(int32_t uid)333 int64_t NetManagerNative::GetUidRxBytes(int32_t uid)
334 {
335 return 0;
336 }
337
GetIfaceRxBytes(std::string interfaceName)338 int64_t NetManagerNative::GetIfaceRxBytes(std::string interfaceName)
339 {
340 nmd::TrafficStatsParcel interfaceTraffic = nmd::TrafficManager::GetInterfaceTraffic(interfaceName);
341 return interfaceTraffic.rxBytes;
342 }
343
GetIfaceTxBytes(std::string interfaceName)344 int64_t NetManagerNative::GetIfaceTxBytes(std::string interfaceName)
345 {
346 nmd::TrafficStatsParcel interfaceTraffic = nmd::TrafficManager::GetInterfaceTraffic(interfaceName);
347 return interfaceTraffic.txBytes;
348 }
349
IpEnableForwarding(const std::string &requester)350 int32_t NetManagerNative::IpEnableForwarding(const std::string &requester)
351 {
352 return sharingManager_->IpEnableForwarding(requester);
353 }
354
SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on)355 int32_t NetManagerNative::SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on)
356 {
357 return sharingManager_->SetIpv6PrivacyExtensions(interfaceName, on);
358 }
359
SetEnableIpv6(const std::string &interfaceName, const uint32_t on)360 int32_t NetManagerNative::SetEnableIpv6(const std::string &interfaceName, const uint32_t on)
361 {
362 return sharingManager_->SetEnableIpv6(interfaceName, on);
363 }
364
IpDisableForwarding(const std::string &requester)365 int32_t NetManagerNative::IpDisableForwarding(const std::string &requester)
366 {
367 return sharingManager_->IpDisableForwarding(requester);
368 }
369
EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)370 int32_t NetManagerNative::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
371 {
372 return sharingManager_->EnableNat(downstreamIface, upstreamIface);
373 }
374
DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)375 int32_t NetManagerNative::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
376 {
377 return sharingManager_->DisableNat(downstreamIface, upstreamIface);
378 }
379
IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)380 int32_t NetManagerNative::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
381 {
382 return sharingManager_->IpfwdAddInterfaceForward(fromIface, toIface);
383 }
384
IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)385 int32_t NetManagerNative::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
386 {
387 return sharingManager_->IpfwdRemoveInterfaceForward(fromIface, toIface);
388 }
389
DnsSetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount, const std::vector<std::string> &servers, const std::vector<std::string> &domains)390 int32_t NetManagerNative::DnsSetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
391 const std::vector<std::string> &servers,
392 const std::vector<std::string> &domains)
393 {
394 return dnsManager_->SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
395 }
396
DnsGetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains, uint16_t &baseTimeoutMsec, uint8_t &retryCount)397 int32_t NetManagerNative::DnsGetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
398 std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
399 uint8_t &retryCount)
400 {
401 return dnsManager_->GetResolverConfig(netId, servers, domains, baseTimeoutMsec, retryCount);
402 }
403
DnsCreateNetworkCache(uint16_t netId)404 int32_t NetManagerNative::DnsCreateNetworkCache(uint16_t netId)
405 {
406 return dnsManager_->CreateNetworkCache(netId);
407 }
408
DnsDestroyNetworkCache(uint16_t netId)409 int32_t NetManagerNative::DnsDestroyNetworkCache(uint16_t netId)
410 {
411 return dnsManager_->DestroyNetworkCache(netId);
412 }
413
BandwidthEnableDataSaver(bool enable)414 int32_t NetManagerNative::BandwidthEnableDataSaver(bool enable)
415 {
416 return bandwidthManager_->EnableDataSaver(enable);
417 }
418
BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)419 int32_t NetManagerNative::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
420 {
421 return bandwidthManager_->SetIfaceQuota(ifName, bytes);
422 }
423
BandwidthRemoveIfaceQuota(const std::string &ifName)424 int32_t NetManagerNative::BandwidthRemoveIfaceQuota(const std::string &ifName)
425 {
426 return bandwidthManager_->RemoveIfaceQuota(ifName);
427 }
428
BandwidthAddDeniedList(uint32_t uid)429 int32_t NetManagerNative::BandwidthAddDeniedList(uint32_t uid)
430 {
431 return bandwidthManager_->AddDeniedList(uid);
432 }
433
BandwidthRemoveDeniedList(uint32_t uid)434 int32_t NetManagerNative::BandwidthRemoveDeniedList(uint32_t uid)
435 {
436 return bandwidthManager_->RemoveDeniedList(uid);
437 }
438
BandwidthAddAllowedList(uint32_t uid)439 int32_t NetManagerNative::BandwidthAddAllowedList(uint32_t uid)
440 {
441 return bandwidthManager_->AddAllowedList(uid);
442 }
443
BandwidthRemoveAllowedList(uint32_t uid)444 int32_t NetManagerNative::BandwidthRemoveAllowedList(uint32_t uid)
445 {
446 return bandwidthManager_->RemoveAllowedList(uid);
447 }
448
FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)449 int32_t NetManagerNative::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
450 {
451 auto chainType = static_cast<NetManagerStandard::ChainType>(chain);
452 return firewallManager_->SetUidsAllowedListChain(chainType, uids);
453 }
454
FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)455 int32_t NetManagerNative::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
456 {
457 auto chainType = static_cast<NetManagerStandard::ChainType>(chain);
458 return firewallManager_->SetUidsDeniedListChain(chainType, uids);
459 }
460
FirewallEnableChain(uint32_t chain, bool enable)461 int32_t NetManagerNative::FirewallEnableChain(uint32_t chain, bool enable)
462 {
463 auto chainType = static_cast<NetManagerStandard::ChainType>(chain);
464 return firewallManager_->EnableChain(chainType, enable);
465 }
466
FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule)467 int32_t NetManagerNative::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule)
468 {
469 auto chainType = static_cast<NetManagerStandard::ChainType>(chain);
470 auto rule = static_cast<NetManagerStandard::FirewallRule>(firewallRule);
471 for (auto &uid : uids) {
472 auto ret = firewallManager_->SetUidRule(chainType, uid, rule);
473 if (ret != NetManagerStandard::NETMANAGER_SUCCESS) {
474 NETNATIVE_LOGE("SetUidRule fail, uid is %{public}u, ret is %{public}d", uid, ret);
475 }
476 }
477 return NetManagerStandard::NETMANAGER_SUCCESS;
478 }
479
480 #ifdef FEATURE_NET_FIREWALL_ENABLE
SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault)481 int32_t NetManagerNative::SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault)
482 {
483 NETNATIVE_LOG_D("NetManagerNative, SetFirewallDefaultAction");
484 return dnsManager_->SetFirewallDefaultAction(inDefault, outDefault);
485 }
486
SetFirewallCurrentUserId(int32_t userId)487 int32_t NetManagerNative::SetFirewallCurrentUserId(int32_t userId)
488 {
489 NETNATIVE_LOG_D("NetManagerNative, SetFirewallCurrentUserId");
490 return dnsManager_->SetFirewallCurrentUserId(userId);
491 }
492
SetFirewallRules(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList, bool isFinish)493 int32_t NetManagerNative::SetFirewallRules(NetFirewallRuleType type,
494 const std::vector<sptr<NetFirewallBaseRule>> &ruleList, bool isFinish)
495 {
496 return dnsManager_->SetFirewallRules(type, ruleList, isFinish);
497 }
498
ClearFirewallRules(NetFirewallRuleType type)499 int32_t NetManagerNative::ClearFirewallRules(NetFirewallRuleType type)
500 {
501 NETNATIVE_LOG_D("NetManagerNative, ClearFirewallRules");
502 return dnsManager_->ClearFirewallRules(type);
503 }
504
RegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback)505 int32_t NetManagerNative::RegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback)
506 {
507 NETNATIVE_LOG_D("NetManagerNative, RegisterNetFirewallCallback");
508 return dnsManager_->RegisterNetFirewallCallback(callback);
509 }
510
UnRegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback)511 int32_t NetManagerNative::UnRegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback)
512 {
513 NETNATIVE_LOG_D("NetManagerNative, UnRegisterNetFirewallCallback");
514 return dnsManager_->UnRegisterNetFirewallCallback(callback);
515 }
516 #endif
517
518 #ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
EnableWearableDistributedNetForward(const int32_t tcpPortId, const int32_t udpPortId)519 int32_t NetManagerNative::EnableWearableDistributedNetForward(const int32_t tcpPortId, const int32_t udpPortId)
520 {
521 NETNATIVE_LOG_D("Enabling wearable distributed net forward for TCP port and UDP port");
522 return wearableDistributedNet_->EnableWearableDistributedNetForward(tcpPortId, udpPortId);
523 }
524
DisableWearableDistributedNetForward()525 int32_t NetManagerNative::DisableWearableDistributedNetForward()
526 {
527 NETNATIVE_LOG_D("NetManagerNative disable wearable distributed net forward");
528 return wearableDistributedNet_->DisableWearableDistributedNetForward();
529 }
530 #endif
531
ShareDnsSet(uint16_t netId)532 void NetManagerNative::ShareDnsSet(uint16_t netId)
533 {
534 dnsManager_->ShareDnsSet(netId);
535 }
536
StartDnsProxyListen()537 void NetManagerNative::StartDnsProxyListen()
538 {
539 dnsManager_->StartDnsProxyListen();
540 }
541
StopDnsProxyListen()542 void NetManagerNative::StopDnsProxyListen()
543 {
544 dnsManager_->StopDnsProxyListen();
545 }
546
DnsGetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints, uint16_t netId, std::vector<AddrInfo> &res)547 int32_t NetManagerNative::DnsGetAddrInfo(const std::string &hostName, const std::string &serverName,
548 const AddrInfo &hints, uint16_t netId, std::vector<AddrInfo> &res)
549 {
550 return dnsManager_->GetAddrInfo(hostName, serverName, hints, netId, res);
551 }
552
GetDumpInfo(std::string &infos)553 void NetManagerNative::GetDumpInfo(std::string &infos)
554 {
555 connManager_->GetDumpInfos(infos);
556 dnsManager_->GetDumpInfo(infos);
557 }
558
AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)559 int32_t NetManagerNative::AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
560 const std::string &ifName)
561 {
562 if (interfaceManager_ == nullptr) {
563 NETNATIVE_LOGE("interfaceManager_ is nullptr");
564 return NETMANAGER_ERR_LOCAL_PTR_NULL;
565 }
566
567 return interfaceManager_->AddStaticArp(ipAddr, macAddr, ifName);
568 }
569
DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)570 int32_t NetManagerNative::DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
571 const std::string &ifName)
572 {
573 if (interfaceManager_ == nullptr) {
574 NETNATIVE_LOGE("interfaceManager_ is nullptr");
575 return NETMANAGER_ERR_LOCAL_PTR_NULL;
576 }
577
578 return interfaceManager_->DelStaticArp(ipAddr, macAddr, ifName);
579 }
580
RegisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback, uint32_t timeStep)581 int32_t NetManagerNative::RegisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback, uint32_t timeStep)
582 {
583 return dnsManager_->RegisterDnsResultCallback(callback, timeStep);
584 }
585
UnregisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback)586 int32_t NetManagerNative::UnregisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback)
587 {
588 return dnsManager_->UnregisterDnsResultCallback(callback);
589 }
590
RegisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback)591 int32_t NetManagerNative::RegisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback)
592 {
593 return dnsManager_->RegisterDnsHealthCallback(callback);
594 }
595
UnregisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback)596 int32_t NetManagerNative::UnregisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback)
597 {
598 return dnsManager_->UnregisterDnsHealthCallback(callback);
599 }
600
SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag, bool isBroker)601 int32_t NetManagerNative::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag,
602 bool isBroker)
603 {
604 return connManager_->SetNetworkAccessPolicy(uid, policy, reconfirmFlag, isBroker);
605 }
606
DeleteNetworkAccessPolicy(uint32_t uid)607 int32_t NetManagerNative::DeleteNetworkAccessPolicy(uint32_t uid)
608 {
609 return connManager_->DeleteNetworkAccessPolicy(uid);
610 }
611
NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)612 int32_t NetManagerNative::NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)
613 {
614 return connManager_->NotifyNetBearerTypeChange(bearerTypes);
615 }
616
ClearFirewallAllRules()617 int32_t NetManagerNative::ClearFirewallAllRules()
618 {
619 return firewallManager_->ClearAllRules();
620 }
621
CloseSocketsUid(const std::string &ipAddr, uint32_t uid)622 int32_t NetManagerNative::CloseSocketsUid(const std::string &ipAddr, uint32_t uid)
623 {
624 return connManager_->CloseSocketsUid(ipAddr, uid);
625 }
626 } // namespace nmd
627 } // namespace OHOS
628