• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 <securec.h>
16 #include "dhcp_ipv6_dns_repository.h"
17 #include "dhcp_logger.h"
18 namespace OHOS {
19 namespace DHCP {
20 DEFINE_DHCPLOG_DHCP_LABEL("DnsServerRepository");
21 const int FIRST_DNS_SERVER = 0;
22 const int SECOND_DNS_SERVER = 1;
DnsServerRepository(int minLifeTime)23 DnsServerRepository::DnsServerRepository(int minLifeTime)
24 {
25     currentServers_ = std::unordered_set<std::string>();
26     allServers_ = std::vector<DnsServerEntry>();
27     minLifetime_ = static_cast<uint32_t>(minLifeTime);
28 }
29 
~DnsServerRepository()30 DnsServerRepository::~DnsServerRepository()
31 {
32 }
33 
Clear()34 bool DnsServerRepository::Clear()
35 {
36     std::lock_guard<std::mutex> lock(mutex);
37     currentServers_.clear();
38     allServers_.clear();
39     return true;
40 }
41 
AddServers(uint32_t lifetime,const std::vector<std::string> & addresses)42 bool DnsServerRepository::AddServers(uint32_t lifetime, const std::vector<std::string>& addresses)
43 {
44     std::lock_guard<std::mutex> lock(mutex);
45     // ingnore lifetime < minLifetime RDNSS, if is 0, need to update exsiting
46     if (lifetime != 0 && lifetime < minLifetime_) {
47         DHCP_LOGE("invalid lifetime, do not use these dns %{public}u", lifetime);
48         return false;
49     }
50 
51     uint64_t now = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
52         std::chrono::system_clock::now().time_since_epoch()
53     ).count());
54 
55     uint64_t expiry = now + static_cast<uint64_t>(lifetime) * 1000; // to ms
56 
57     for (const std::string& addressString : addresses) {
58         //if exsiting, update expiry time
59         if (!UpdateExistingEntry(addressString, expiry)) {
60             if (expiry > now) { // if not exsiting, add
61                 DnsServerEntry entry = {addressString, expiry};
62                 allServers_.push_back(entry);
63             }
64         }
65     }
66 
67     std::sort(allServers_.begin(), allServers_.end(),
68         [](const DnsServerEntry& a, const DnsServerEntry& b) {
69             return a.expiry > b.expiry;
70         });
71 
72     return UpdateCurrentServers();
73 }
74 
SetCurrentServers(DhcpIpv6Info & ipv6Info)75 bool DnsServerRepository::SetCurrentServers(DhcpIpv6Info &ipv6Info)
76 {
77     std::lock_guard<std::mutex> lock(mutex);
78     ipv6Info.vectorDnsAddr.clear();
79     int ret  = memset_s(ipv6Info.dnsAddr, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
80     if (ret != EOK) {
81         DHCP_LOGE("SetCurrentServers() memset_s failed!");
82     }
83     ret = memset_s(ipv6Info.dnsAddr2, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
84     if (ret != EOK) {
85         DHCP_LOGE("SetCurrentServers() memset_s failed!");
86     }
87     int index = 0;
88     for (auto dnsServer: currentServers_) {
89         DHCP_LOGI("SetCurrentServers() dnsServer: %{private}s", dnsServer.c_str());
90         ipv6Info.vectorDnsAddr.push_back(dnsServer);
91         if (index == FIRST_DNS_SERVER) {
92             ret = strcpy_s(ipv6Info.dnsAddr, DHCP_INET6_ADDRSTRLEN, dnsServer.c_str());
93             if (ret != EOK) {
94                 DHCP_LOGE("SetCurrentServers() strcpy_s failed!");
95             }
96         }
97         if (index == SECOND_DNS_SERVER) {
98             ret = strcpy_s(ipv6Info.dnsAddr2, DHCP_INET6_ADDRSTRLEN, dnsServer.c_str());
99             if (ret != EOK) {
100                 DHCP_LOGE("SetCurrentServers() strcpy_s failed!");
101             }
102         }
103         index++;
104     }
105     return true;
106 }
107 
UpdateExistingEntry(const std::string & address,uint64_t expiry)108 bool DnsServerRepository::UpdateExistingEntry(const std::string& address, uint64_t expiry)
109 {
110     for (auto& it : allServers_) {
111         if (it.address == address) {
112             it.expiry = expiry;
113             return true;
114         }
115     }
116     return false;
117 }
118 
UpdateCurrentServers()119 bool DnsServerRepository::UpdateCurrentServers()
120 {
121     uint64_t now = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
122         std::chrono::system_clock::now().time_since_epoch()
123     ).count());
124 
125     bool changed = false;
126     if (allServers_.empty()) {
127         return changed;
128     }
129     for (int i = static_cast<int>(allServers_.size()) - 1; i >= 0; --i) {
130         DHCP_LOGI("DhcpIpv6 UpdateCurrentServers() %{public}d", i);
131         if (i >= NUM_SERVERS || allServers_[i].expiry <= now) {
132             DHCP_LOGI("DhcpIpv6 UpdateCurrentServers() remove server %{private}s", allServers_[i].address.c_str());
133             // remove expired or too many servers
134             const std::string address = allServers_[i].address;  // Copy, not reference
135             allServers_.erase(allServers_.begin() + i);
136             if (currentServers_.erase(address)) {
137                 changed = true;
138             }
139         } else {
140             break;
141         }
142     }
143 
144     for (const DnsServerEntry& entry : allServers_) {
145         if (currentServers_.size() < NUM_CURRENT_SERVERS) {
146             DHCP_LOGI("DhcpIpv6 UpdateCurrentServers() add server %{private}s", entry.address.c_str());
147             if (currentServers_.insert(entry.address).second) {
148                 changed = true;
149             }
150         } else {
151             break;
152         }
153     }
154 
155     return changed;
156 }
157 }
158 }