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 URSPCONFIG_H 17 #define URSPCONFIG_H 18 19 #include <cstdint> 20 #include <string> 21 #include <vector> 22 #include <fstream> 23 #include <libxml/parser.h> 24 #include <libxml/tree.h> 25 #include <libxml/xpath.h> 26 #include <arpa/inet.h> 27 #include <sys/socket.h> 28 #include <algorithm> 29 #include "refbase.h" 30 #include "allowednssaiconfig.h" 31 #include "networksliceutil.h" 32 #include "netmgr_ext_log_wrapper.h" 33 34 namespace OHOS { 35 namespace NetManagerStandard { 36 37 struct TrafficDescriptorWhiteList { 38 std::string osAppIds = ""; 39 std::string dnns = ""; 40 std::string fqdns = ""; 41 std::string cct = ""; 42 }; 43 44 class TrafficDescriptor { 45 public: 46 bool isMatchAll; 47 std::vector<OsAppId> osAppIds; 48 std::vector<Ipv4Addr> ipv4Addrs; 49 std::vector<Ipv6Addr> ipv6Addrs; 50 std::vector<int> protocolIds; 51 std::vector<int> singleRemotePorts; 52 std::vector<RemotePortRange> remotePortRanges; 53 std::vector<std::string> dnns; 54 std::vector<std::string> fqdns; 55 std::vector<int> connectionCapabilities; 56 to_json()57 nlohmann::json to_json() const 58 { 59 nlohmann::json tdJson = nlohmann::json::object({ 60 {"isMatchAll", isMatchAll}, 61 {"protocolIds", protocolIds}, 62 {"singleRemotePorts", singleRemotePorts}, 63 {"dnns", dnns}, 64 {"fqdns", fqdns}, 65 {"connectionCapabilities", connectionCapabilities} 66 }); 67 68 for (const auto& osAppId : osAppIds) { 69 tdJson["osAppIds"].push_back(osAppId.to_json()); 70 } 71 for (const auto& ipv4Addr : ipv4Addrs) { 72 tdJson["ipv4Addrs"].push_back(ipv4Addr.to_json()); 73 } 74 for (const auto& ipv6Addr : ipv6Addrs) { 75 tdJson["ipv6Addrs"].push_back(ipv6Addr.to_json()); 76 } 77 for (const auto& remotePortRange : remotePortRanges) { 78 tdJson["remotePortRanges"].push_back(remotePortRange.to_json()); 79 } 80 return tdJson; 81 } from_json(const nlohmann::json & jsonDescriptor)82 void from_json(const nlohmann::json& jsonDescriptor) 83 { 84 if (jsonDescriptor.find("isMatchAll") != jsonDescriptor.end()) { 85 this->isMatchAll = jsonDescriptor.at("isMatchAll").get<bool>(); 86 } 87 if (jsonDescriptor.find("osAppIds") != jsonDescriptor.end()) { 88 NETMGR_EXT_LOG_I("TrafficDescriptor find osAppIds"); 89 const nlohmann::json& osAppIdsJson = jsonDescriptor.at("osAppIds"); 90 this->osAppIds.clear(); 91 for (const auto& osAppIdJson : osAppIdsJson) { 92 OsAppId osAppId; 93 osAppId.from_json(osAppIdJson); 94 this->osAppIds.push_back(osAppId); 95 } 96 } 97 if (jsonDescriptor.find("ipv4Addrs") != jsonDescriptor.end()) { 98 const nlohmann::json& ipv4AddrsJson = jsonDescriptor.at("ipv4Addrs"); 99 this->ipv4Addrs.clear(); 100 for (const auto& ipv4AddrJson : ipv4AddrsJson) { 101 Ipv4Addr ipv4Addr; 102 ipv4Addr.from_json(ipv4AddrJson); 103 this->ipv4Addrs.push_back(ipv4Addr); 104 } 105 } 106 if (jsonDescriptor.find("ipv6Addrs") != jsonDescriptor.end()) { 107 const nlohmann::json& ipv6AddrsJson = jsonDescriptor.at("ipv6Addrs"); 108 this->ipv6Addrs.clear(); 109 for (const auto& ipv6AddrJson : ipv6AddrsJson) { 110 Ipv6Addr ipv6Addr; 111 ipv6Addr.from_json(ipv6AddrJson); 112 this->ipv6Addrs.push_back(ipv6Addr); 113 } 114 } 115 from_json_ex(jsonDescriptor); 116 } 117 from_json_ex(const nlohmann::json & jsonDescriptor)118 void from_json_ex(const nlohmann::json& jsonDescriptor) 119 { 120 if (jsonDescriptor.find("protocolIds") != jsonDescriptor.end()) { 121 const nlohmann::json& protocolIdsJson = jsonDescriptor.at("protocolIds"); 122 this->protocolIds.clear(); 123 for (const auto& protocolIdJson : protocolIdsJson) { 124 this->protocolIds.push_back(protocolIdJson.get<int>()); 125 } 126 } 127 if (jsonDescriptor.find("singleRemotePorts") != jsonDescriptor.end()) { 128 const nlohmann::json& singleRemotePortsJson = jsonDescriptor.at("singleRemotePorts"); 129 this->singleRemotePorts.clear(); 130 for (const auto& singleRemotePortJson : singleRemotePortsJson) { 131 this->singleRemotePorts.push_back(singleRemotePortJson.get<int>()); 132 } 133 } 134 if (jsonDescriptor.find("remotePortRanges") != jsonDescriptor.end()) { 135 const nlohmann::json& remotePortRangesJson = jsonDescriptor.at("remotePortRanges"); 136 this->remotePortRanges.clear(); 137 for (const auto& remotePortRangeJson : remotePortRangesJson) { 138 RemotePortRange remotePortRange; 139 remotePortRange.from_json(remotePortRangeJson); 140 this->remotePortRanges.push_back(remotePortRange); 141 } 142 } 143 if (jsonDescriptor.find("dnns") != jsonDescriptor.end()) { 144 const nlohmann::json& dnnsJson = jsonDescriptor.at("dnns"); 145 this->dnns.clear(); 146 for (const auto& dnnJson : dnnsJson) { 147 this->dnns.push_back(dnnJson.get<std::string>()); 148 } 149 } 150 if (jsonDescriptor.find("fqdns") != jsonDescriptor.end()) { 151 const nlohmann::json& fqdnsJson = jsonDescriptor.at("fqdns"); 152 this->fqdns.clear(); 153 for (const auto& fqdnJson : fqdnsJson) { 154 this->fqdns.push_back(fqdnJson.get<std::string>()); 155 } 156 } 157 if (jsonDescriptor.find("connectionCapabilities") != jsonDescriptor.end()) { 158 const nlohmann::json& connectionCapabilitiesJson = jsonDescriptor.at("connectionCapabilities"); 159 this->connectionCapabilities.clear(); 160 for (const auto& connectionCapabilityJson : connectionCapabilitiesJson) { 161 this->connectionCapabilities.push_back(connectionCapabilityJson.get<int>()); 162 } 163 } 164 } 165 }; 166 167 class RouteSelectionDescriptor { 168 public: 169 int routePrecedence; 170 int pduSessionType; 171 uint8_t sscMode; 172 std::vector<Snssai> snssais; 173 std::vector<std::string> dnns; 174 to_json()175 nlohmann::json to_json() const 176 { 177 nlohmann::json rsdJson = nlohmann::json::object({ 178 {"routePrecedence", routePrecedence}, 179 {"pduSessionType", pduSessionType}, 180 {"sscMode", sscMode}, 181 {"dnns", dnns} 182 }); 183 for (const auto& snssai : snssais) { 184 rsdJson["snssais"].push_back(snssai.to_json()); 185 } 186 return rsdJson; 187 } 188 from_json(const nlohmann::json & jsonDescriptor)189 void from_json(const nlohmann::json& jsonDescriptor) 190 { 191 if (jsonDescriptor.find("routePrecedence") != jsonDescriptor.end()) { 192 this->routePrecedence = jsonDescriptor.at("routePrecedence").get<int>(); 193 } 194 if (jsonDescriptor.find("pduSessionType") != jsonDescriptor.end()) { 195 this->pduSessionType = jsonDescriptor.at("pduSessionType").get<int>(); 196 } 197 if (jsonDescriptor.find("sscMode") != jsonDescriptor.end()) { 198 this->sscMode = jsonDescriptor.at("sscMode").get<uint8_t>(); 199 } 200 if (jsonDescriptor.find("snssais") != jsonDescriptor.end()) { 201 const nlohmann::json& snssaisJson = jsonDescriptor.at("snssais"); 202 this->snssais.clear(); 203 for (const auto& snssaiJson : snssaisJson) { 204 Snssai snssai; 205 snssai.from_json(snssaiJson); 206 this->snssais.push_back(snssai); 207 } 208 } 209 if (jsonDescriptor.find("dnns") != jsonDescriptor.end()) { 210 const nlohmann::json& dnnsJson = jsonDescriptor.at("dnns"); 211 this->dnns.clear(); 212 for (const auto& dnnJson : dnnsJson) { 213 this->dnns.push_back(dnnJson.get<std::string>()); 214 } 215 } 216 } 217 }; 218 219 class UrspRule { 220 public: 221 int32_t urspPrecedence; 222 TrafficDescriptor trafficDescriptor; 223 std::vector<RouteSelectionDescriptor> routeSelectionDescriptors; 224 to_json()225 nlohmann::json to_json() const 226 { 227 nlohmann::json urspruleJson = nlohmann::json::object({ 228 {"urspPrecedence", urspPrecedence}, 229 {"trafficDescriptor", trafficDescriptor.to_json()}, 230 }); 231 for (const auto& routeSelectionDescriptor : routeSelectionDescriptors) { 232 urspruleJson["routeSelectionDescriptors"].push_back(routeSelectionDescriptor.to_json()); 233 } 234 return urspruleJson; 235 } from_json(const nlohmann::json & urspruleJson)236 void from_json(const nlohmann::json& urspruleJson) 237 { 238 if (urspruleJson.find("urspPrecedence") != urspruleJson.end()) { 239 this->urspPrecedence = urspruleJson.at("urspPrecedence").get<int32_t>(); 240 } 241 242 if (urspruleJson.find("trafficDescriptor") != urspruleJson.end()) { 243 const nlohmann::json& trafficDescriptorJson = urspruleJson.at("trafficDescriptor"); 244 TrafficDescriptor td; 245 td.from_json(trafficDescriptorJson); 246 this->trafficDescriptor = td; 247 } 248 249 if (urspruleJson.find("routeSelectionDescriptors") != urspruleJson.end()) { 250 const nlohmann::json& routeSelectionDescriptorsJson = urspruleJson.at("routeSelectionDescriptors"); 251 routeSelectionDescriptors.clear(); 252 for (const auto& descriptorJson : routeSelectionDescriptorsJson) { 253 RouteSelectionDescriptor rsd; 254 rsd.from_json(descriptorJson); 255 this->routeSelectionDescriptors.push_back(rsd); 256 } 257 } 258 } 259 }; 260 261 class UrspConfig { 262 public: 263 static UrspConfig& GetInstance(); 264 ~UrspConfig() = default; 265 bool SliceNetworkSelection(SelectedRouteDescriptor& routeRule, std::string plmn, AppDescriptor appDescriptor); 266 bool isIpThreeTuplesInWhiteList(std::string plmn, AppDescriptor appDescriptor); 267 private: 268 UrspConfig(); 269 }; 270 271 272 } // namespace NetManagerStandard 273 } // namespace OHOS 274 #endif // URSPCONFIG_H 275