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 #ifndef FQDNIPS_H 17 #define FQDNIPS_H 18 19 #include <cstdint> 20 #include <string> 21 #include <vector> 22 #include <set> 23 #include <iostream> 24 #include "inet_addr.h" 25 #include "networksliceutil.h" 26 27 namespace OHOS { 28 namespace NetManagerStandard { 29 30 class FqdnIps { 31 public: 32 static const std::string TAG; 33 static const std::string CHAR_ENCODING; 34 35 static const std::string IPV4_MASK; 36 static const std::string IPV6_PREFIX; 37 mergeFqdnIps(const FqdnIps & newFqdnIps)38 void mergeFqdnIps(const FqdnIps& newFqdnIps) 39 { 40 if (newFqdnIps.mIpv4Addr.size() > 0) { 41 for (INetAddr ip : newFqdnIps.mIpv4Addr) { 42 mIpv4Addr.insert(ip); 43 } 44 } 45 if (newFqdnIps.mIpv6Addr.size() > 0) { 46 for (INetAddr ip : newFqdnIps.mIpv6Addr) { 47 mIpv6Addr.insert(ip); 48 } 49 } 50 } 51 hasNewFqdnIps(const FqdnIps & fqdnIps)52 bool hasNewFqdnIps(const FqdnIps& fqdnIps) const 53 { 54 bool containsAllIpv4 = true; 55 bool containsAllIpv6 = true; 56 57 for (const auto& ipv4 : fqdnIps.mIpv4Addr) { 58 bool found = false; 59 for (const auto& myIpv4 : mIpv4Addr) { 60 if (ipv4 == myIpv4) { 61 found = true; 62 break; 63 } 64 } 65 if (!found) { 66 containsAllIpv4 = false; 67 break; 68 } 69 } 70 71 for (const auto& ipv6 : fqdnIps.mIpv6Addr) { 72 bool found = false; 73 for (const auto& myIpv6 : mIpv6Addr) { 74 if (ipv6 == myIpv6) { 75 found = true; 76 break; 77 } 78 } 79 if (!found) { 80 containsAllIpv6 = false; 81 break; 82 } 83 } 84 return !containsAllIpv4 || !containsAllIpv6; 85 } 86 getNewFqdnIps(const FqdnIps & fqdnIps)87 FqdnIps getNewFqdnIps(const FqdnIps& fqdnIps) const 88 { 89 if (fqdnIps.isFqdnIpsEmpty()) { 90 return FqdnIps(); 91 } 92 FqdnIps newFqdnIps; 93 for (const INetAddr& ip : fqdnIps.mIpv4Addr) { 94 if (std::find(mIpv4Addr.begin(), mIpv4Addr.end(), ip) == mIpv4Addr.end()) { 95 newFqdnIps.mIpv4Addr.insert(ip); 96 } 97 } 98 for (const INetAddr& ip : fqdnIps.mIpv6Addr) { 99 if (std::find(mIpv6Addr.begin(), mIpv6Addr.end(), ip) == mIpv6Addr.end()) { 100 newFqdnIps.mIpv6Addr.insert(ip); 101 } 102 } 103 return newFqdnIps; 104 } 105 isFqdnIpsEmpty()106 bool isFqdnIpsEmpty() const 107 { 108 return mIpv4Addr.empty() && mIpv6Addr.empty(); 109 } 110 setIpv4AddrAndMask()111 std::vector<uint8_t> setIpv4AddrAndMask() 112 { 113 std::string ipv4AddrAndMask; 114 for (const INetAddr& ip : mIpv4Addr) { 115 ipv4AddrAndMask += ip.address_; 116 ipv4AddrAndMask += IPV4_MASK; 117 } 118 mIpv4AddrAndMask = ConvertstringTouInt8Vector(ipv4AddrAndMask); 119 return mIpv4AddrAndMask; 120 } 121 setIpv6AddrAndPrefix()122 std::vector<uint8_t> setIpv6AddrAndPrefix() 123 { 124 std::string ipv6AddrAndPrefix; 125 for (const INetAddr& ip : mIpv6Addr) { 126 ipv6AddrAndPrefix += ip.address_; 127 ipv6AddrAndPrefix += IPV6_PREFIX; 128 } 129 mIpv6AddrAndPrefix = ConvertstringTouInt8Vector(ipv6AddrAndPrefix); 130 return mIpv6AddrAndPrefix; 131 } 132 getIpv4Addr()133 std::set<INetAddr> getIpv4Addr() const 134 { 135 return mIpv4Addr; 136 } 137 getIpv6Addr()138 std::set<INetAddr> getIpv6Addr() const 139 { 140 return mIpv6Addr; 141 } 142 getIpv4Num()143 size_t getIpv4Num() const 144 { 145 return mIpv4Addr.size(); 146 } 147 getIpv6Num()148 size_t getIpv6Num() const 149 { 150 return mIpv6Addr.size(); 151 } 152 getIpv4AddrAndMask()153 std::vector<uint8_t> getIpv4AddrAndMask() const 154 { 155 return mIpv4AddrAndMask; 156 } 157 getIpv6AddrAndPrefix()158 std::vector<uint8_t> getIpv6AddrAndPrefix() const 159 { 160 return mIpv6AddrAndPrefix; 161 } 162 setIpv4Addr(std::set<INetAddr> ipv4Addr)163 void setIpv4Addr(std::set<INetAddr> ipv4Addr) 164 { 165 mIpv4Addr = ipv4Addr; 166 } 167 setIpv6Addr(std::set<INetAddr> ipv6Addr)168 void setIpv6Addr(std::set<INetAddr> ipv6Addr) 169 { 170 mIpv6Addr = ipv6Addr; 171 } 172 173 bool operator==(const FqdnIps& other) const 174 { 175 return mIpv4Addr == other.mIpv4Addr && mIpv6Addr == other.mIpv6Addr; 176 } 177 178 bool operator<(const FqdnIps& other) const 179 { 180 if (mIpv4Addr.size() < other.mIpv4Addr.size()) return true; 181 if (mIpv4Addr.size() > other.mIpv4Addr.size()) return false; 182 auto it1 = mIpv4Addr.begin(); 183 auto it2 = other.mIpv4Addr.begin(); 184 while (it1 != mIpv4Addr.end() && it2 != other.mIpv4Addr.end()) { 185 if (*it1 < *it2) return true; 186 if (!(*it1 == *it2)) return false; 187 ++it1; 188 ++it2; 189 } 190 191 if (mIpv6Addr.size() < other.mIpv6Addr.size()) return true; 192 if (mIpv6Addr.size() > other.mIpv6Addr.size()) return false; 193 it1 = mIpv6Addr.begin(); 194 it2 = other.mIpv6Addr.begin(); 195 while (it1 != mIpv6Addr.end() && it2 != other.mIpv6Addr.end()) { 196 if (*it1 < *it2) return true; 197 if (!(*it1 == *it2)) return false; 198 ++it1; 199 ++it2; 200 } 201 return false; 202 } 203 204 private: 205 std::set<INetAddr> mIpv4Addr; 206 std::set<INetAddr> mIpv6Addr; 207 208 std::vector<uint8_t> mIpv4AddrAndMask; 209 std::vector<uint8_t> mIpv6AddrAndPrefix; 210 }; 211 212 const inline std::string FqdnIps::TAG = "FqdnIps"; 213 const inline std::string FqdnIps::CHAR_ENCODING = "ISO_8859_1"; 214 const inline std::string FqdnIps::IPV4_MASK = "0.0.0.0"; 215 const inline std::string FqdnIps::IPV6_PREFIX = "::1"; 216 217 } // namespace NetManagerStandard 218 } // namespace OHOS 219 220 #endif // FQDNIPS_H 221