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 SLICEROUTEINFO_H 17 #define SLICEROUTEINFO_H 18 19 #include <cstdint> 20 #include <string> 21 #include <vector> 22 #include <iostream> 23 24 namespace OHOS { 25 namespace NetManagerStandard { 26 27 class SliceRouteInfo { 28 public: 29 static const std::string TAG; 30 static const std::string SEPARATOR_FOR_NORMAL_DATA; 31 static const uint8_t MATCH_ALL; 32 isBindCompleted(int uid,const FqdnIps fqdnIps,TrafficDescriptorsInfo::RouteBindType routeBindType)33 bool isBindCompleted(int uid, const FqdnIps fqdnIps, TrafficDescriptorsInfo::RouteBindType routeBindType) const 34 { 35 if (routeBindType == TrafficDescriptorsInfo::INVALID_TDS) { 36 return true; 37 } 38 switch (routeBindType) { 39 case TrafficDescriptorsInfo::UID_TDS: 40 return isUidBindCompleted(uid); 41 case TrafficDescriptorsInfo::IP_TDS: 42 return isNoNewFqdnIp(fqdnIps); 43 case TrafficDescriptorsInfo::UID_IP_TDS: 44 return isUidBindCompleted(uid) && isNoNewFqdnIp(fqdnIps); 45 default: 46 NETMGR_EXT_LOG_I("Invalid TrafficDescriptors RouteBindType."); 47 } 48 return true; 49 } 50 clearUids()51 void clearUids() 52 { 53 mUids.clear(); 54 } 55 addUid(int uid)56 void addUid(int uid) 57 { 58 mUids.push_back(uid); 59 } 60 removeUid(int uid)61 void removeUid(int uid) 62 { 63 auto it = std::find(mUids.begin(), mUids.end(), uid); 64 if (it != mUids.end()) { 65 mUids.erase(it); 66 } 67 } 68 getUidsStr()69 std::string getUidsStr() 70 { 71 std::stringstream ss; 72 for (const int& uid : mUids) { 73 ss << uid << SEPARATOR_FOR_NORMAL_DATA; 74 } 75 return ss.str(); 76 } 77 addUsedUid(int uid)78 void addUsedUid(int uid) 79 { 80 mUsedUids.push_back(uid); 81 } 82 clearUsedUids()83 void clearUsedUids() 84 { 85 mUsedUids.clear(); 86 } 87 removeUsedUid(int uid)88 void removeUsedUid(int uid) 89 { 90 auto it = std::find(mUsedUids.begin(), mUsedUids.end(), uid); 91 if (it != mUsedUids.end()) { 92 mUsedUids.erase(it); 93 } 94 } 95 clearWaittingFqdnIps()96 void clearWaittingFqdnIps() 97 { 98 mWaittingFqdnIps.clear(); 99 } 100 getFqdnIps()101 std::shared_ptr<FqdnIps> getFqdnIps() const 102 { 103 return std::make_shared<FqdnIps>(mFqdnIps); 104 } 105 setFqdnIps(FqdnIps fqdnIps)106 void setFqdnIps(FqdnIps fqdnIps) 107 { 108 mFqdnIps = fqdnIps; 109 } 110 getWaittingFqdnIps()111 const std::vector<FqdnIps> getWaittingFqdnIps() const 112 { 113 return mWaittingFqdnIps; 114 } 115 setWaittingFqdnIps(const std::vector<FqdnIps> & waittingFqdnIps)116 void setWaittingFqdnIps(const std::vector<FqdnIps>& waittingFqdnIps) 117 { 118 if (!waittingFqdnIps.empty()) { 119 mWaittingFqdnIps.clear(); 120 for (const auto& fqdnIp : waittingFqdnIps) { 121 mWaittingFqdnIps.push_back(fqdnIp); 122 } 123 } 124 } 125 getUsedUids()126 std::vector<int> getUsedUids() const 127 { 128 return mUsedUids; 129 } 130 getUids()131 std::vector<int> getUids() const 132 { 133 return mUids; 134 } 135 setUids(const std::set<int> & uids)136 void setUids(const std::set<int>& uids) 137 { 138 if (!uids.empty()) { 139 mUids.clear(); 140 for (const auto& uid : uids) { 141 mUids.push_back(uid); 142 } 143 } 144 } 145 addSignedUid(int uid)146 void addSignedUid(int uid) 147 { 148 mSignedUids.push_back(uid); 149 } 150 removeSignedUid(int uid)151 void removeSignedUid(int uid) 152 { 153 auto it = std::find(mSignedUids.begin(), mSignedUids.end(), uid); 154 if (it != mSignedUids.end()) { 155 mSignedUids.erase(it); 156 } 157 } 158 getSignedUids()159 std::vector<int> getSignedUids() const 160 { 161 return mSignedUids; 162 } 163 isNoNewFqdnIp(const FqdnIps & fqdnIps)164 bool isNoNewFqdnIp(const FqdnIps& fqdnIps) const 165 { 166 return mFqdnIps.isFqdnIpsEmpty() || mFqdnIps.getNewFqdnIps(fqdnIps).isFqdnIpsEmpty(); 167 } 168 isUidBindCompleted(int uid)169 bool isUidBindCompleted(int uid) const 170 { 171 return (std::find(mUids.begin(), mUids.end(), uid) == mUids.end()) || 172 (std::find(mSignedUids.begin(), mSignedUids.end(), uid) == mSignedUids.end()); 173 } 174 175 private: 176 std::vector<int> mUids; 177 std::vector<int> mUsedUids; 178 std::vector<int> mSignedUids; 179 std::vector<FqdnIps> mWaittingFqdnIps; 180 FqdnIps mFqdnIps; 181 }; 182 183 const inline std::string SliceRouteInfo::TAG = "SliceRouteInfo"; 184 const inline std::string SliceRouteInfo::SEPARATOR_FOR_NORMAL_DATA = ","; 185 const inline uint8_t SliceRouteInfo::MATCH_ALL = 1; 186 187 } // namespace NetManagerStandard 188 } // namespace OHOS 189 190 #endif // SLICEROUTEINFO_H 191