1 /*
2 * Copyright (c) 2025-2026 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 "dns_result_callback.h"
17 #include "net_conn_client.h"
18 #include "networksliceutil.h"
19 #include "hwnetworkslicemanager.h"
20
21 namespace OHOS::NetManagerStandard {
22 static constexpr char DOT = '.';
DnsResultCallback()23 DnsResultCallback::DnsResultCallback()
24 {
25 NETMGR_EXT_LOG_I("DnsResultCallback constructor");
26 }
27
~DnsResultCallback()28 DnsResultCallback::~DnsResultCallback()
29 {
30 NETMGR_EXT_LOG_I("DnsResultCallback destroy");
31 }
32
HandleConnectivityChanged(int32_t & wifiNetId,int32_t & cellularNetId)33 void DnsResultCallback::HandleConnectivityChanged(int32_t &wifiNetId, int32_t &cellularNetId)
34 {
35 std::list<sptr<NetHandle>> netList;
36 int32_t ret = NetConnClient::GetInstance().GetAllNets(netList);
37 if (ret != NETMANAGER_SUCCESS) {
38 NETMGR_EXT_LOG_E("HandleConnectivityChanged, GetAllNets failed ret = %d", ret);
39 return;
40 }
41
42 if (netList.empty()) {
43 NETMGR_EXT_LOG_E("HandleConnectivityChanged, no net");
44 return;
45 }
46
47 for (auto netHandlePtr : netList) {
48 if (!netHandlePtr) {
49 NETMGR_EXT_LOG_E("invalid netHandle");
50 continue;
51 }
52 NetAllCapabilities netAllCap;
53 NetConnClient::GetInstance().GetNetCapabilities(*netHandlePtr, netAllCap);
54
55 if (netAllCap.bearerTypes_.count(BEARER_WIFI) > 0) {
56 wifiNetId = netHandlePtr->GetNetId();
57 }
58
59 if (netAllCap.bearerTypes_.count(BEARER_CELLULAR) > 0) {
60 cellularNetId = netHandlePtr->GetNetId();
61 }
62 }
63 }
64
GetDefaultNetId()65 int32_t DnsResultCallback::GetDefaultNetId()
66 {
67 NetHandle defaultNet;
68 NetConnClient::GetInstance().GetDefaultNet(defaultNet);
69 return defaultNet.GetNetId();
70 }
71
OnDnsResultReport(uint32_t size,const std::list<NetsysNative::NetDnsResultReport> netDnsResultReport)72 int32_t DnsResultCallback::OnDnsResultReport(uint32_t size,
73 const std::list<NetsysNative::NetDnsResultReport> netDnsResultReport)
74 {
75 int32_t wifiNetId = 0;
76 int32_t cellularNetId = 0;
77 HandleConnectivityChanged(wifiNetId, cellularNetId);
78 int32_t defaultNetId = GetDefaultNetId();
79 for (auto &it : netDnsResultReport) {
80 int32_t netId = static_cast<int32_t>(it.netid_);
81 NetBearType netType;
82 int targetNetId = netId > 0 ? netId : defaultNetId > 0 ? defaultNetId : 0;
83 if (wifiNetId > 0 && wifiNetId == targetNetId) {
84 netType = BEARER_WIFI;
85 } else if (cellularNetId > 0 && cellularNetId == targetNetId) {
86 netType = BEARER_CELLULAR;
87 } else {
88 NETMGR_EXT_LOG_E("DnsResultCallback unknow dns result %{public}d %{public}d %{public}d %{public}d",
89 netId, defaultNetId, cellularNetId, wifiNetId);
90 continue;
91 }
92 if (it.host_.find(DOT) == std::string::npos) {
93 continue;
94 }
95
96 std::list<AddrInfo> addrInfoList;
97 for (auto &info : it.addrlist_) {
98 AddrInfo addrInfo;
99 addrInfo.type_ = info.type_;
100 addrInfo.addr_ = info.addr_;
101 addrInfoList.push_back(addrInfo);
102 }
103 DelayedSingleton<HwNetworkSliceManager>::GetInstance()->RequestNetworkSliceForFqdn(
104 static_cast<int32_t>(it.uid_), it.host_, addrInfoList);
105 }
106 return NETMANAGER_SUCCESS;
107 }
108
IsValidNetId(int32_t netId,int32_t wifiNetId,int32_t cellularNetId)109 bool DnsResultCallback::IsValidNetId(int32_t netId, int32_t wifiNetId, int32_t cellularNetId)
110 {
111 return (netId == wifiNetId || netId == cellularNetId || netId == 0);
112 }
113 } // namespace OHOS
114