1 /* 2 * Copyright (C) 2021 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_NET_MANAGER_NATIVE_H__ 17 #define INCLUDE_NET_MANAGER_NATIVE_H__ 18 19 #include <interface_controller.h> 20 #include <memory> 21 #include <network_controller.h> 22 #include <route_controller.h> 23 #include <string> 24 #include <vector> 25 26 namespace OHOS { 27 namespace nmd { 28 typedef struct RouteInfoParcel { 29 std::string destination; 30 std::string ifName; 31 std::string nextHop; 32 int mtu; 33 } RouteInfoParcel; 34 35 typedef struct MarkMaskParcel { 36 int mark; 37 int mask; 38 } MarkMaskParcel; 39 40 class NetManagerNative { 41 public: 42 NetManagerNative(); 43 ~NetManagerNative(); 44 45 static void GetOriginInterfaceIndex(); 46 static std::vector<unsigned int> GetCurrentInterfaceIndex(); 47 static void UpdateInterfaceIndex(unsigned int infIndex); 48 49 void Init(); 50 51 int NetworkCreatePhysical(int netId, int permission); 52 int NetworkDestroy(int netId); 53 int NetworkAddInterface(int netId, std::string iface); 54 int NetworkRemoveInterface(int netId, std::string iface); 55 56 MarkMaskParcel GetFwmarkForNetwork(int netId); 57 int NetworkAddRoute(int netId, std::string ifName, std::string destination, std::string nextHop); 58 int NetworkRemoveRoute(int netId, std::string ifName, std::string destination, std::string nextHop); 59 int NetworkGetDefault(); 60 int NetworkSetDefault(int netId); 61 int NetworkClearDefault(); 62 int NetworkSetPermissionForNetwork(int netId, NetworkPermission permission); 63 std::vector<std::string> InterfaceGetList(); 64 65 int SetProcSysNet(int32_t ipversion, int32_t which, const std::string ifname, const std::string parameter, 66 const std::string value); 67 int GetProcSysNet(int32_t ipversion, int32_t which, const std::string ifname, const std::string parameter, 68 std::string *value); 69 70 nmd::InterfaceConfigurationParcel InterfaceGetConfig(std::string ifName); 71 void InterfaceSetConfig(InterfaceConfigurationParcel cfg); 72 void InterfaceClearAddrs(const std::string ifName); 73 int InterfaceGetMtu(std::string ifName); 74 int InterfaceSetMtu(std::string ifName, int mtuValue); 75 int InterfaceAddAddress(std::string ifName, std::string addrString, int prefixLength); 76 int InterfaceDelAddress(std::string ifName, std::string addrString, int prefixLength); 77 78 int NetworkAddRouteParcel(int netId, RouteInfoParcel routeInfo); 79 int NetworkRemoveRouteParcel(int netId, RouteInfoParcel routeInfo); 80 81 long GetCellularRxBytes(); 82 long GetCellularTxBytes(); 83 long GetAllRxBytes(); 84 long GetAllTxBytes(); 85 long GetUidTxBytes(int uid); 86 long GetUidRxBytes(int uid); 87 long GetIfaceRxBytes(std::string interfaceName); 88 long GetIfaceTxBytes(std::string interfaceName); 89 long GetTetherRxBytes(); 90 long GetTetherTxBytes(); 91 92 private: 93 std::shared_ptr<NetworkController> networkController; 94 std::shared_ptr<RouteController> routeController; 95 std::shared_ptr<InterfaceController> interfaceController; 96 static std::vector<unsigned int> interfaceIdex; 97 }; 98 } // namespace nmd 99 } // namespace OHOS 100 #endif // !INCLUDE_NET_MANAGER_NATIVE_H__ 101