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