• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ROUTE_CONTROLLER_H__
17 #define INCLUDE_ROUTE_CONTROLLER_H__
18 
19 #include <map>
20 #include <netinet/in.h>
21 #include "nmd_network.h"
22 
23 namespace OHOS {
24 namespace nmd {
25 typedef struct InetAddr {
26     int family;
27     int bitlen;
28     int prefixlen;
29     uint8_t data[sizeof(struct in6_addr)];
30 } InetAddr;
31 
32 static const int LOCAL_NETWORK_NETID = 99;
33 
34 class RouteController {
35 public:
36     RouteController();
37     ~RouteController();
38 
39     static int AddInterfaceToDefaultNetwork(const char *interface, NetworkPermission permission);
40     static int RemoveInterfaceFromDefaultNetwork(const char *interface, NetworkPermission permission);
41 
42     static int AddRoute(int netId, std::string interfaceName, std::string destination, std::string nextHop);
43     static int RemoveRoute(int netId, std::string interfaceName, std::string destination, std::string nextHop);
44 
45     static int ReadAddr(const char *addr, InetAddr *res);
46     static int ReadAddrGw(const char *addr, InetAddr *res);
47 
48 private:
49     static std::map<std::string, uint32_t> interfaceToTable;
50     static uint32_t GetRouteTableForInterface(const char *interfaceName);
51     static int ModifyRule(uint32_t type, uint32_t table, uint8_t action, uint32_t priority);
52 };
53 } // namespace nmd
54 } // namespace OHOS
55 #endif // !INCLUDE_ROUTE_CONTROLLER_H__
56