1 /* 2 * Copyright (c) 2021-2022 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 INCLUDE_CONN_MANAGER_H 17 #define INCLUDE_CONN_MANAGER_H 18 19 #include <map> 20 #include <set> 21 #include <vector> 22 23 #include "netsys_network.h" 24 #include "network_permission.h" 25 #include "route_manager.h" 26 27 namespace OHOS { 28 namespace nmd { 29 class ConnManager { 30 public: 31 enum RouteAction { 32 ROUTE_ADD, 33 ROUTE_REMOVE, 34 ROUTE_UPDATE, 35 }; 36 37 ConnManager(); 38 ~ConnManager(); 39 40 /** 41 * Creates a physical network 42 * 43 * @param netId The network Id to create 44 * @param permission The permission necessary to use the network. Must be one of 45 * PERMISSION_NONE/PERMISSION_NETWORK/PERMISSION_SYSTEM 46 * 47 * @return Returns 0, successfully create the physical network, otherwise it will fail 48 */ 49 int32_t CreatePhysicalNetwork(uint16_t netId, NetworkPermission permission); 50 51 /** 52 * Destroy a network. Any interfaces added to the network are removed, and the network ceases 53 * to be the default network 54 * 55 * @param netId The network to destroy 56 * 57 * @return Returns 0, successfully destroy the network, otherwise it will fail 58 */ 59 int32_t DestroyNetwork(int32_t netId); 60 61 /** 62 * Set network as default network 63 * 64 * @param netId The network to set as the default 65 * 66 * @return Returns 0, successfully Set default network, otherwise it will fail 67 */ 68 int32_t SetDefaultNetwork(int32_t netId); 69 70 /** 71 * Clear default network 72 * 73 * @return Returns 0, successfully clear default network, otherwise it will fail 74 */ 75 int32_t ClearDefaultNetwork(); 76 77 /** 78 * Get default network 79 * 80 * @return NetId of default network 81 */ 82 int32_t GetDefaultNetwork() const; 83 84 /** 85 * Add an interface to a network. The interface must not be assigned to any network, including 86 * the specified network 87 * 88 * @param netId The network to add the interface 89 * @param interafceName The name of the interface to add 90 * 91 * @return Returns 0, successfully add an interface to a network, otherwise it will fail 92 */ 93 int32_t AddInterfaceToNetwork(int32_t netId, std::string &interafceName); 94 95 /** 96 * Remove an interface to a network. The interface must be assigned to the specified network 97 * 98 * @param netId The network to add the interface 99 * @param interafceName The name of the interface to remove 100 * 101 * @return Returns 0, successfully remove an interface to a network, otherwise it will fail 102 */ 103 int32_t RemoveInterfaceFromNetwork(int32_t netId, std::string &interafceName); 104 105 /** 106 * Reinit route when netmanager restart 107 * 108 * @param 109 * 110 * @return Returns 0, reinit route successfully, otherwise it will fail 111 */ 112 int32_t ReinitRoute(); 113 114 /** 115 * Add a route for specific network 116 * 117 * @param netId The network to add the route 118 * @param interfaceName The name of interface of the route 119 * This interface should be assigned to the netID 120 * @param destination The destination of the route 121 * @param nextHop The route's next hop address 122 * 123 * @return Returns 0, successfully add a route for specific network, otherwise it will fail 124 */ 125 int32_t AddRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop); 126 127 /** 128 * Remove a route for specific network 129 * 130 * @param netId The network to remove the route 131 * @param interfaceName The name of interface of the route 132 * This interface should be assigned to the netID 133 * @param destination The destination of the route 134 * @param nextHop The route's next hop address 135 * 136 * @return Returns 0, successfully remove a route for specific network, otherwise it will fail 137 */ 138 int32_t RemoveRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop); 139 140 /** 141 * Update a route for specific network 142 * 143 * @param netId The network to update the route 144 * @param interfaceName The name of interface of the route 145 * This interface should be assigned to the netID 146 * @param destination The destination of the route 147 * @param nextHop The route's next hop address 148 * 149 * @return Returns 0, successfully update a route for specific network, otherwise it will fail 150 */ 151 int32_t UpdateRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop); 152 153 /** 154 * Get the mark for the given network id 155 * 156 * @param netId The network to get the mark 157 * 158 * @return A Mark of the given network id. 159 */ 160 int32_t GetFwmarkForNetwork(int32_t netId); 161 162 /** 163 * Set the permission required to access a specific network 164 * 165 * @param netId The network to set 166 * @param permission Network permission to use 167 * 168 * @return Returns 0, successfully set the permission for specific network, otherwise it will fail 169 */ 170 int32_t SetPermissionForNetwork(int32_t netId, NetworkPermission permission); 171 172 /** 173 * Get the Dump Infos object 174 * 175 * @param infos The output message 176 */ 177 void GetDumpInfos(std::string &infos); 178 179 private: 180 int32_t defaultNetId_; 181 bool needReinitRouteFlag_; 182 std::map<int32_t, std::string> physicalInterfaceName_; 183 std::map<int32_t, std::shared_ptr<NetsysNetwork>> networks_; 184 std::tuple<bool, std::shared_ptr<NetsysNetwork>> FindNetworkById(int32_t netId); 185 int32_t GetNetworkForInterface(std::string &interfaceName); 186 RouteManager::TableType GetTableType(int32_t netId); 187 }; 188 } // namespace nmd 189 } // namespace OHOS 190 #endif // INCLUDE_CONN_MANAGER_H 191