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 TRAFFICDESCRITORSINFO_H 17 #define TRAFFICDESCRITORSINFO_H 18 19 #include <string> 20 #include <vector> 21 #include <cstdint> 22 #include "inet_addr.h" 23 #include "hwosappId.h" 24 #include "hwnetworkslicemanager.h" 25 26 namespace OHOS { 27 namespace NetManagerStandard { 28 29 class TrafficDescriptorsInfo { 30 public: 31 enum RouteBindType { 32 UID_TDS, 33 UID_IP_TDS, 34 IP_TDS, 35 INVALID_TDS 36 }; 37 class Builder { 38 public: Builder()39 Builder() 40 : mUrspPrecedence(0), mAppIds(""), mIpv4Num(0), mIpv4AddrAndMask({}), mIpv6Num(0), mIpv6AddrAndPrefix({}), 41 mProtocolIds(""), mRemotePorts(""), mRouteBitmap(0), mIsIpTriad(false), mIsMatchFqdn(false), 42 mIsMatchDnn(false), mHasAvailableUrsp(false), mIsMatchCct(false), 43 mRouteBindType(RouteBindType::INVALID_TDS), mUid(0), mDnn(""), mFqdn(""), mIp(), mProtocolId(""), 44 mRemotePort(""), mFqdnIps(), mIsNeedToCreateRequest(false), mIsRequestAgain(false), mCct(0) 45 {} setUrspPrecedence(uint8_t precedence)46 Builder& setUrspPrecedence(uint8_t precedence) 47 { 48 mUrspPrecedence = precedence; 49 return *this; 50 } setAppIds(const std::string & ids)51 Builder& setAppIds(const std::string& ids) 52 { 53 mAppIds = ids; 54 return *this; 55 } setIpv4Num(uint8_t num)56 Builder& setIpv4Num(uint8_t num) 57 { 58 mIpv4Num = num; 59 return *this; 60 } setIpv4AddrAndMask(const std::vector<uint8_t> & addrAndMask)61 Builder& setIpv4AddrAndMask(const std::vector<uint8_t>& addrAndMask) 62 { 63 mIpv4AddrAndMask = addrAndMask; 64 return *this; 65 } setIpv6Num(uint8_t num)66 Builder& setIpv6Num(uint8_t num) 67 { 68 mIpv6Num = num; 69 return *this; 70 } setIpv6AddrAndPrefix(const std::vector<uint8_t> & addrAndPrefix)71 Builder& setIpv6AddrAndPrefix(const std::vector<uint8_t>& addrAndPrefix) 72 { 73 mIpv6AddrAndPrefix = addrAndPrefix; 74 return *this; 75 } setProtocolIds(const std::string & ids)76 Builder& setProtocolIds(const std::string& ids) 77 { 78 mProtocolIds = ids; 79 return *this; 80 } setRemotePorts(const std::string & ports)81 Builder& setRemotePorts(const std::string& ports) 82 { 83 mRemotePorts = ports; 84 return *this; 85 } setRouteBitmap(uint8_t bitmap)86 Builder& setRouteBitmap(uint8_t bitmap) 87 { 88 mRouteBitmap = bitmap; 89 return *this; 90 } setUid(int uid)91 Builder& setUid(int uid) 92 { 93 mUid = uid; 94 return *this; 95 } setDnn(const std::string & dnn)96 Builder& setDnn(const std::string& dnn) 97 { 98 mDnn = dnn; 99 return *this; 100 } setCct(int cct)101 Builder& setCct(int cct) 102 { 103 mCct = cct; 104 return *this; 105 } setFqdn(const std::string & fqdn)106 Builder& setFqdn(const std::string& fqdn) 107 { 108 mFqdn = fqdn; 109 return *this; 110 } setIp(const INetAddr & ip)111 Builder& setIp(const INetAddr& ip) 112 { 113 mIp = ip; 114 return *this; 115 } setProtocolId(const std::string & id)116 Builder& setProtocolId(const std::string& id) 117 { 118 mProtocolId = id; 119 return *this; 120 } setRemotePort(const std::string & port)121 Builder& setRemotePort(const std::string& port) 122 { 123 mRemotePort = port; 124 return *this; 125 } setFqdnIps(const FqdnIps & fqdnIps)126 Builder& setFqdnIps(const FqdnIps& fqdnIps) 127 { 128 mFqdnIps = fqdnIps; 129 return *this; 130 } setNeedToCreateRequest(bool needToCreateRequest)131 Builder& setNeedToCreateRequest(bool needToCreateRequest) 132 { 133 mIsNeedToCreateRequest = needToCreateRequest; 134 return *this; 135 } build()136 TrafficDescriptorsInfo build() 137 { 138 TrafficDescriptorsInfo trafficDescriptors; 139 trafficDescriptors.mUrspPrecedence = mUrspPrecedence; 140 trafficDescriptors.mAppIds = trafficDescriptors.getUnsignedAppIds(mAppIds); 141 trafficDescriptors.mIpv4Num = mIpv4Num; 142 trafficDescriptors.mIpv4AddrAndMask = mIpv4AddrAndMask; 143 trafficDescriptors.mIpv6Num = mIpv6Num; 144 trafficDescriptors.mIpv6AddrAndPrefix = mIpv6AddrAndPrefix; 145 trafficDescriptors.mProtocolIds = mProtocolIds; 146 trafficDescriptors.mRemotePorts = mRemotePorts; 147 trafficDescriptors.mRouteBitmap = mRouteBitmap; 148 trafficDescriptors.mUid = mUid; 149 trafficDescriptors.mDnn = mDnn; 150 trafficDescriptors.mFqdn = mFqdn; 151 trafficDescriptors.mIp = mIp; 152 trafficDescriptors.mProtocolId = mProtocolId; 153 trafficDescriptors.mRemotePort = mRemotePort; 154 trafficDescriptors.mFqdnIps = mFqdnIps; 155 trafficDescriptors.mCct = mCct; 156 trafficDescriptors.mIsNeedToCreateRequest = mIsNeedToCreateRequest; 157 trafficDescriptors.mIsIpTriad = (mIpv4Num != 0) || (mIpv6Num != 0); 158 trafficDescriptors.mIsMatchFqdn = (mRouteBitmap & MATCH_FQDN) != 0; 159 trafficDescriptors.mIsMatchDnn = (mRouteBitmap & MATCH_DNN) != 0; 160 trafficDescriptors.mHasAvailableUrsp = (mRouteBitmap & MATCH_AVAILABLE) != 0; 161 trafficDescriptors.mIsMatchCct = (mRouteBitmap & MATCH_CCT) != 0; 162 bool hasIps = mIsIpTriad || mIsMatchFqdn; 163 bool hasAppids = !mAppIds.empty() || mIsMatchDnn; 164 mRouteBindType = TrafficDescriptorsInfo::RouteBindType::INVALID_TDS; 165 if (hasAppids && hasIps) { 166 mRouteBindType = TrafficDescriptorsInfo::RouteBindType::UID_IP_TDS; 167 } else if (hasAppids) { 168 mRouteBindType = TrafficDescriptorsInfo::RouteBindType::UID_TDS; 169 } else if (hasIps) { 170 mRouteBindType = TrafficDescriptorsInfo::RouteBindType::IP_TDS; 171 } 172 trafficDescriptors.mRouteBindType = mRouteBindType; 173 return trafficDescriptors; 174 } 175 private: 176 uint8_t mUrspPrecedence; 177 std::string mAppIds; 178 uint8_t mIpv4Num; 179 std::vector<uint8_t> mIpv4AddrAndMask; 180 uint8_t mIpv6Num; 181 std::vector<uint8_t> mIpv6AddrAndPrefix; 182 std::string mProtocolIds; 183 std::string mRemotePorts; 184 uint8_t mRouteBitmap; 185 bool mIsIpTriad; 186 bool mIsMatchFqdn; 187 bool mIsMatchDnn; 188 bool mHasAvailableUrsp; 189 bool mIsMatchCct; 190 TrafficDescriptorsInfo::RouteBindType mRouteBindType; 191 int mUid; 192 std::string mDnn; 193 std::string mFqdn; 194 INetAddr mIp; 195 std::string mProtocolId; 196 std::string mRemotePort; 197 FqdnIps mFqdnIps; 198 bool mIsNeedToCreateRequest; 199 bool mIsRequestAgain; 200 int mCct; 201 }; 202 static const int CCT_TYPE_INVALID; 203 static const int CCT_TYPE_IMS; 204 static const int CCT_TYPE_MMS; 205 static const int CCT_TYPE_SUPL; 206 static const std::string TDS_ROUTE_BITMAP; 207 static const std::string TDS_URSP_PRECEDENCE; 208 static const std::string TDS_APPIDS; 209 static const std::string TDS_IPV4_NUM; 210 static const std::string TDS_IPV4_ADDRANDMASK; 211 static const std::string TDS_IPV6_NUM; 212 static const std::string TDS_IPV6_ADDRANDPREFIX; 213 static const std::string TDS_PROTOCOLIDS; 214 static const std::string TDS_REMOTEPORTS; 215 static const std::string SEPARATOR; 216 static const int ROUTE_BITMAP_BIT4; 217 static const uint8_t MATCH_DNN; 218 static const uint8_t MATCH_FQDN; 219 static const uint8_t MATCH_AVAILABLE; 220 static const uint8_t MATCH_CCT; TrafficDescriptorsInfo()221 TrafficDescriptorsInfo() 222 : mUrspPrecedence(0), mAppIds(""), mIpv4Num(0), mIpv4AddrAndMask({}), mIpv6Num(0), mIpv6AddrAndPrefix({}), 223 mProtocolIds(""), mRemotePorts(""), mRouteBitmap(0), mIsIpTriad(false), mIsMatchFqdn(false), mIsMatchDnn(false), 224 mHasAvailableUrsp(false), mIsMatchCct(false), mRouteBindType(RouteBindType::INVALID_TDS), mUid(0), mDnn(""), 225 mFqdn(""), mIp(), mProtocolId(""), mRemotePort(""), mFqdnIps(), mIsNeedToCreateRequest(false), 226 mIsRequestAgain(false), mCct(0) {} 227 makeTrafficDescriptorsInfo(std::map<std::string,std::string> & data)228 static TrafficDescriptorsInfo makeTrafficDescriptorsInfo(std::map<std::string, std::string>& data) 229 { 230 std::string appIds = ""; 231 if (data.find(TDS_APPIDS) != data.end()) { 232 appIds = data[TDS_APPIDS]; 233 } 234 uint8_t urspPrecedence = 0; 235 if (data.find(TDS_URSP_PRECEDENCE) != data.end()) { 236 urspPrecedence = static_cast<uint8_t>(std::stoi(data[TDS_URSP_PRECEDENCE])); 237 } 238 uint8_t ipv4Num = 0; 239 if (data.find(TDS_IPV4_NUM) != data.end()) { 240 ipv4Num = static_cast<uint8_t>(std::stoi(data[TDS_IPV4_NUM])); 241 } 242 uint8_t ipv6Num = 0; 243 if (data.find(TDS_IPV6_NUM) != data.end()) { 244 ipv6Num = static_cast<uint8_t>(std::stoi(data[TDS_IPV6_NUM])); 245 } 246 std::string ipv4AddrAndMaskStr = ""; 247 if (data.find(TDS_IPV4_ADDRANDMASK) != data.end()) { 248 ipv4AddrAndMaskStr = data[TDS_IPV4_ADDRANDMASK]; 249 } 250 std::string ipv6AddrAndPrefixStr = ""; 251 if (data.find(TDS_IPV6_ADDRANDPREFIX) != data.end()) { 252 ipv6AddrAndPrefixStr = data[TDS_IPV6_ADDRANDPREFIX]; 253 } 254 std::string protocolIds = ""; 255 if (data.find(TDS_PROTOCOLIDS) != data.end()) { 256 protocolIds = data[TDS_PROTOCOLIDS]; 257 } 258 std::string remotePorts = ""; 259 if (data.find(TDS_REMOTEPORTS) != data.end()) { 260 remotePorts = data[TDS_REMOTEPORTS]; 261 } 262 uint8_t routeBitmap = 0; 263 if (data.find(TDS_ROUTE_BITMAP) != data.end()) { 264 routeBitmap = static_cast<uint8_t>(std::stoi(data[TDS_ROUTE_BITMAP])); 265 } 266 TrafficDescriptorsInfo::Builder builder; 267 builder.setAppIds(appIds); 268 builder.setUrspPrecedence(urspPrecedence); 269 builder.setIpv4Num(ipv4Num); 270 builder.setIpv4AddrAndMask(ConvertstringTouInt8Vector(ipv4AddrAndMaskStr)); 271 builder.setIpv6Num(ipv6Num); 272 builder.setIpv6AddrAndPrefix(ConvertstringTouInt8Vector(ipv6AddrAndPrefixStr)); 273 builder.setProtocolIds(protocolIds); 274 builder.setRemotePorts(remotePorts); 275 builder.setRouteBitmap(routeBitmap); 276 return builder.build(); 277 } 278 getUnsignedAppIds(const std::string & appIds)279 std::string getUnsignedAppIds(const std::string& appIds) 280 { 281 if (appIds.empty()) { 282 return ""; 283 } 284 std::vector<std::string> osAppIds; 285 osAppIds = Split(appIds, SEPARATOR); 286 for (int i = 0; i < (int)osAppIds.size(); ++i) { 287 HwOsAppId osAppId = HwOsAppId::Create(osAppIds[i]); 288 if (osAppId.getOsId().empty() && osAppId.getAppId().empty()) { 289 continue; 290 } 291 } 292 std::string result; 293 for (const auto& id : osAppIds) { 294 if (!id.empty()) { 295 result += id + SEPARATOR; 296 } 297 } 298 return result; 299 } isUidRouteBindType()300 bool isUidRouteBindType() const 301 { 302 return mRouteBindType == RouteBindType::UID_IP_TDS || mRouteBindType == RouteBindType::UID_TDS; 303 } isMatchDnn()304 bool isMatchDnn() const 305 { 306 return mIsMatchDnn; 307 } isMatchNetworkCap()308 bool isMatchNetworkCap() const 309 { 310 return mIsMatchDnn || mIsMatchCct; 311 } isMatchFqdn()312 bool isMatchFqdn() const 313 { 314 return mIsMatchFqdn; 315 } hasAvailableUrsp()316 bool hasAvailableUrsp() const 317 { 318 return mHasAvailableUrsp; 319 } isAtiveTriggeringApp(const std::string & packageName)320 bool isAtiveTriggeringApp(const std::string& packageName) const 321 { 322 return (std::find(mAtiveTriggeringApps.begin(), mAtiveTriggeringApps.end(), packageName) 323 != mAtiveTriggeringApps.end()); 324 } setRequestAgain(bool requestAgain)325 void setRequestAgain(bool requestAgain) 326 { 327 mIsRequestAgain = requestAgain; 328 } isRequestAgain()329 bool isRequestAgain() 330 { 331 return mIsRequestAgain; 332 } getRouteBindType()333 RouteBindType getRouteBindType() const 334 { 335 return mRouteBindType; 336 } getUrspPrecedence()337 uint8_t getUrspPrecedence() const 338 { 339 return mUrspPrecedence; 340 } getAppIds()341 const std::string& getAppIds() const 342 { 343 return mAppIds; 344 } getIpv4Num()345 uint8_t getIpv4Num() const 346 { 347 return mIpv4Num; 348 } getIpv4AddrAndMask()349 const std::vector<uint8_t>& getIpv4AddrAndMask() const 350 { 351 return mIpv4AddrAndMask; 352 } getIpv6Num()353 uint8_t getIpv6Num() const 354 { 355 return mIpv6Num; 356 } getIpv6AddrAndPrefix()357 const std::vector<uint8_t>& getIpv6AddrAndPrefix() const 358 { 359 return mIpv6AddrAndPrefix; 360 } getProtocolIds()361 const std::string& getProtocolIds() const 362 { 363 return mProtocolIds; 364 } getRemotePorts()365 const std::string& getRemotePorts() const 366 { 367 return mRemotePorts; 368 } getRouteBitmap()369 uint8_t getRouteBitmap() const 370 { 371 return mRouteBitmap; 372 } getFqdnIps()373 const FqdnIps& getFqdnIps() const 374 { 375 return mFqdnIps; 376 } getUid()377 int getUid() const 378 { 379 return mUid; 380 } getDnn()381 const std::string& getDnn() const 382 { 383 return mDnn; 384 } getCct()385 int getCct() const 386 { 387 return mCct; 388 } getFqdn()389 const std::string& getFqdn() const 390 { 391 return mFqdn; 392 } getIp()393 const INetAddr& getIp() const 394 { 395 return mIp; 396 } getProtocolId()397 const std::string& getProtocolId() const 398 { 399 return mProtocolId; 400 } getRemotePort()401 const std::string& getRemotePort() const 402 { 403 return mRemotePort; 404 } isNeedToCreateRequest()405 bool isNeedToCreateRequest() const 406 { 407 return mIsNeedToCreateRequest; 408 } isIpTriad()409 bool isIpTriad() const 410 { 411 return mIsIpTriad; 412 } 413 bool operator==(const TrafficDescriptorsInfo& other) const 414 { 415 return mRouteBitmap == other.mRouteBitmap && mUrspPrecedence == other.mUrspPrecedence 416 && mAppIds == other.mAppIds && mIpv4Num == other.mIpv4Num && mIpv6Num == other.mIpv6Num 417 && mIsIpTriad == other.mIsIpTriad && mIsMatchDnn == other.mIsMatchDnn && mIsMatchFqdn == other.mIsMatchFqdn 418 && mHasAvailableUrsp == other.mHasAvailableUrsp && mIsMatchCct == other.mIsMatchCct && mUid == other.mUid 419 && mIsNeedToCreateRequest == other.mIsNeedToCreateRequest && mIsRequestAgain == other.mIsRequestAgain 420 && mCct == other.mCct && mIpv4AddrAndMask == other.mIpv4AddrAndMask 421 && mIpv6AddrAndPrefix == other.mIpv6AddrAndPrefix && mProtocolIds == other.mProtocolIds 422 && mRemotePorts == other.mRemotePorts && mAtiveTriggeringApps == other.mAtiveTriggeringApps 423 && mRouteBindType == other.mRouteBindType && mDnn == other.mDnn && mFqdn == other.mFqdn 424 && mIp == other.getIp() && mProtocolId == other.mProtocolId 425 && mRemotePort == other.mRemotePort && mFqdnIps == other.mFqdnIps; 426 } 427 bool operator<(const TrafficDescriptorsInfo& other) const 428 { 429 if (std::tie(mRouteBitmap, mUrspPrecedence, mAppIds, mIpv4Num, mIpv6Num, mIsIpTriad, mIsMatchDnn, mIsMatchFqdn, 430 mHasAvailableUrsp, mIsMatchCct, mUid, mIsNeedToCreateRequest, mIsRequestAgain, mCct, mIpv4AddrAndMask, 431 mIpv6AddrAndPrefix, mProtocolIds, mRemotePorts, mAtiveTriggeringApps, mRouteBindType, mDnn, mFqdn, mIp, 432 mProtocolId, mRemotePort, mFqdnIps) < std::tie(other.mRouteBitmap, other.mUrspPrecedence, other.mAppIds, 433 other.mIpv4Num, other.mIpv6Num, other.mIsIpTriad, other.mIsMatchDnn, other.mIsMatchFqdn, 434 other.mHasAvailableUrsp, other.mIsMatchCct, other.mUid, other.mIsNeedToCreateRequest, other.mIsRequestAgain, 435 other.mCct, other.mIpv4AddrAndMask, other.mIpv6AddrAndPrefix, other.mProtocolIds, other.mRemotePorts, 436 other.mAtiveTriggeringApps, other.mRouteBindType, other.mDnn, other.mFqdn, other.getIp(), other.mProtocolId, 437 other.mRemotePort, other.mFqdnIps)) { 438 return true; 439 } 440 return false; 441 } 442 private: 443 uint8_t mUrspPrecedence; 444 std::string mAppIds; 445 uint8_t mIpv4Num; 446 std::vector<uint8_t> mIpv4AddrAndMask; 447 uint8_t mIpv6Num; 448 std::vector<uint8_t> mIpv6AddrAndPrefix; 449 std::string mProtocolIds; 450 std::string mRemotePorts; 451 uint8_t mRouteBitmap; 452 bool mIsIpTriad; 453 bool mIsMatchFqdn; 454 bool mIsMatchDnn; 455 bool mHasAvailableUrsp; 456 bool mIsMatchCct; 457 RouteBindType mRouteBindType; 458 int mUid; 459 std::string mDnn; 460 std::string mFqdn; 461 INetAddr mIp; 462 std::string mProtocolId; 463 std::string mRemotePort; 464 FqdnIps mFqdnIps; 465 bool mIsNeedToCreateRequest; 466 bool mIsRequestAgain; 467 int mCct; 468 std::vector<std::string> mAtiveTriggeringApps; 469 }; 470 471 const inline int TrafficDescriptorsInfo::CCT_TYPE_INVALID = -1; 472 const inline int TrafficDescriptorsInfo::CCT_TYPE_IMS = 1; 473 const inline int TrafficDescriptorsInfo::CCT_TYPE_MMS = 2; 474 const inline int TrafficDescriptorsInfo::CCT_TYPE_SUPL = 4; 475 const inline std::string TrafficDescriptorsInfo::TDS_ROUTE_BITMAP = "routeBitmap"; 476 const inline std::string TrafficDescriptorsInfo::TDS_URSP_PRECEDENCE = "urspPrecedence"; 477 const inline std::string TrafficDescriptorsInfo::TDS_APPIDS = "appIds"; 478 const inline std::string TrafficDescriptorsInfo::TDS_IPV4_NUM = "ipv4Num"; 479 const inline std::string TrafficDescriptorsInfo::TDS_IPV4_ADDRANDMASK = "ipv4AddrAndMask"; 480 const inline std::string TrafficDescriptorsInfo::TDS_IPV6_NUM = "ipv6Num"; 481 const inline std::string TrafficDescriptorsInfo::TDS_IPV6_ADDRANDPREFIX = "ipv6AddrAndPrefix"; 482 const inline std::string TrafficDescriptorsInfo::TDS_PROTOCOLIDS = "protocolIds"; 483 const inline std::string TrafficDescriptorsInfo::TDS_REMOTEPORTS = "remotePorts"; 484 const inline std::string TrafficDescriptorsInfo::SEPARATOR = ","; 485 const inline int TrafficDescriptorsInfo::ROUTE_BITMAP_BIT4 = 4; 486 const inline uint8_t TrafficDescriptorsInfo::MATCH_DNN = (uint8_t) (1 << 1); 487 const inline uint8_t TrafficDescriptorsInfo::MATCH_FQDN = (uint8_t) (1 << 2); 488 const inline uint8_t TrafficDescriptorsInfo::MATCH_AVAILABLE = (uint8_t) (1 << 3); 489 const inline uint8_t TrafficDescriptorsInfo::MATCH_CCT = (uint8_t) (1 << TrafficDescriptorsInfo::ROUTE_BITMAP_BIT4); 490 } // namespace NetManagerStandard 491 } // namespace OHOS 492 493 #endif // ROUTESELECTIONDESCRIPTORINFO_H 494