1 /* 2 * Copyright (c) 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 ROUTEUTILS_H 17 #define ROUTEUTILS_H 18 19 #include <list> 20 #include <string> 21 22 #include "net_link_info.h" 23 #include "route.h" 24 25 namespace OHOS { 26 namespace NetManagerStandard { 27 enum routeOperateType { ROUTE_ADD, ROUTE_REMOVE }; 28 29 enum routeLocalNetId { LOCAL_NET_ID = 99 }; 30 31 static constexpr const char *NEXTHOP_UNREACHABLE = "unreachable"; 32 static constexpr const char *NEXTHOP_THROW = "throw"; 33 34 class RouteUtils { 35 public: 36 RouteUtils() = default; 37 ~RouteUtils() = default; 38 39 /** 40 * Add route info to table by default netId 41 * 42 * @param iface interface name 43 * @param routes the list of route 44 * 45 * @return 0, add route success, otherwise it will fail 46 */ 47 static int32_t AddRoutesToLocal(const std::string &iface, const std::list<Route> &routes); 48 49 /** 50 * Remove route info from table by default netId 51 * 52 * @param routes the list of route 53 * 54 * @return 0, remove route success, otherwise it will fail 55 */ 56 static int32_t RemoveRoutesFromLocal(const std::list<Route> &routes); 57 58 /** 59 * Add route info to table by given netId 60 * 61 * @param netId 62 * @param route route info will add to route table 63 * 64 * @return 0, add route success, otherwise it will fail 65 */ 66 static int32_t AddRoute(int32_t netId, const Route &route); 67 68 /** 69 * Remove route info from table by given netId 70 * 71 * @param netId 72 * @param route route info will remove from route table 73 * 74 * @return 0, remove route success, otherwise it will fail 75 */ 76 static int32_t RemoveRoute(int32_t netId, const Route &route); 77 78 /** 79 * Update route from oldnl to newnl 80 * 81 * @param netId 82 * @param newnl route info will add or update to route table 83 * @param oldnl route info will remove from route table 84 * 85 * @return 1, route changed between oldnl and newnl 86 */ 87 static int32_t UpdateRoutes(int32_t netId, const NetLinkInfo &newnl, const NetLinkInfo &oldnl); 88 89 private: 90 static int32_t ModifyRoute(routeOperateType op, int32_t netId, const Route &route); 91 static void ToPrefixString(const std::string &src, int32_t prefixLen, std::string &dest); 92 static std::string MaskAddress(const std::string &addr, int32_t prefixLen); 93 }; 94 } // namespace NetManagerStandard 95 } // namespace OHOS 96 #endif // ROUTEUTILS_H 97