• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 <memory>
21 #include <mutex>
22 #include <set>
23 #include <sys/types.h>
24 #include <vector>
25 
26 #include "netsys_network.h"
27 #include "network_permission.h"
28 #include "route_manager.h"
29 #include "safe_map.h"
30 namespace OHOS {
31 namespace nmd {
32 class ConnManager {
33 public:
34     enum RouteAction {
35         ROUTE_ADD,
36         ROUTE_REMOVE,
37         ROUTE_UPDATE,
38     };
39 
40     ConnManager();
41     ~ConnManager();
42 
43     /**
44      * Disallow or allow a app to create AF_INET or AF_INET6 socket
45      *
46      * @param uid App's uid which need to be disallowed ot allowed to create AF_INET or AF_INET6 socket
47      * @param allow 0 means disallow, 1 means allow
48      * @return return 0 if OK, return error number if not OK
49      */
50     int32_t SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker);
51 
52     /**
53      * Creates a physical network
54      *
55      * @param netId The network Id to create
56      * @param permission The permission necessary to use the network. Must be one of
57      *        PERMISSION_NONE/PERMISSION_NETWORK/PERMISSION_SYSTEM
58      *
59      * @return Returns 0, successfully create the physical network, otherwise it will fail
60      */
61     int32_t CreatePhysicalNetwork(uint16_t netId, NetworkPermission permission);
62 
63     /**
64      * Creates a virtual network
65      *
66      * @param netId The network Id to create
67      * @param hasDns true if this network set dns
68      * @param secure true if set bypass=false
69      *
70      * @return Returns 0, successfully create the physical network, otherwise it will fail
71      */
72     int32_t CreateVirtualNetwork(uint16_t netId, bool hasDns);
73 
74     /**
75      * Destroy a network. Any interfaces added to the network are removed, and the network ceases
76      *        to be the default network
77      *
78      * @param netId The network to destroy
79      *
80      * @return Returns 0, successfully destroy the network, otherwise it will fail
81      */
82     int32_t DestroyNetwork(int32_t netId);
83 
84     /**
85      * Set network as default network
86      *
87      * @param netId The network to set as the default
88      *
89      * @return Returns 0, successfully Set default network, otherwise it will fail
90      */
91     int32_t SetDefaultNetwork(int32_t netId);
92 
93     /**
94      * Clear default network
95      *
96      * @return Returns 0, successfully clear default network, otherwise it will fail
97      */
98     int32_t ClearDefaultNetwork();
99 
100     /**
101      * Get default network
102      *
103      * @return NetId of default network
104      */
105     int32_t GetDefaultNetwork() const;
106 
107     /**
108      * Add an interface to a network. The interface must not be assigned to any network, including
109      *        the specified network
110      *
111      * @param netId The network to add the interface
112      * @param interafceName The name of the interface to add
113      *
114      * @return Returns 0, successfully add an interface to a network, otherwise it will fail
115      */
116     int32_t AddInterfaceToNetwork(int32_t netId, std::string &interafceName);
117 
118     /**
119      * Remove an interface to a network. The interface must be assigned to the specified network
120      *
121      * @param netId The network to add the interface
122      * @param interafceName The name of the interface to remove
123      *
124      * @return Returns 0, successfully remove an interface to a network, otherwise it will fail
125      */
126     int32_t RemoveInterfaceFromNetwork(int32_t netId, std::string &interafceName);
127 
128     /**
129      * Reinit route when netmanager restart
130      *
131      * @param
132      *
133      * @return Returns 0, reinit route successfully, otherwise it will fail
134      */
135     int32_t ReinitRoute();
136 
137     /**
138      * Add a route for specific network
139      *
140      * @param netId The network to add the route
141      * @param interfaceName The name of interface of the route
142      *                      This interface should be assigned to the netID
143      * @param destination The destination of the route
144      * @param nextHop The route's next hop address
145      *
146      * @return Returns 0, successfully add a route for specific network, otherwise it will fail
147      */
148     int32_t AddRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop);
149 
150     /**
151      * Remove a route for specific network
152      *
153      * @param netId The network to remove the route
154      * @param interfaceName The name of interface of the route
155      *                      This interface should be assigned to the netID
156      * @param destination The destination of the route
157      * @param nextHop The route's next hop address
158      *
159      * @return Returns 0, successfully remove a route for specific network, otherwise it will fail
160      */
161     int32_t RemoveRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop);
162 
163     /**
164      * Update a route for specific network
165      *
166      * @param netId The network to update the route
167      * @param interfaceName The name of interface of the route
168      *                      This interface should be assigned to the netID
169      * @param destination The destination of the route
170      * @param nextHop The route's next hop address
171      *
172      * @return Returns 0, successfully update a route for specific network, otherwise it will fail
173      */
174     int32_t UpdateRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop);
175 
176     /**
177      * Get the mark for the given network id
178      *
179      * @param netId The network to get the mark
180      *
181      * @return A Mark of the given network id.
182      */
183     int32_t GetFwmarkForNetwork(int32_t netId);
184 
185     /**
186      * Set the permission required to access a specific network
187      *
188      * @param netId The network to set
189      * @param permission Network permission to use
190      *
191      * @return Returns 0, successfully set the permission for specific network, otherwise it will fail
192      */
193     int32_t SetPermissionForNetwork(int32_t netId, NetworkPermission permission);
194 
195     /**
196      * Find virtual network from netId
197      *
198      * @param netId The network id
199      * @return Returns nullptr, the netId is not virtual network
200      */
201     std::shared_ptr<NetsysNetwork> FindVirtualNetwork(int32_t netId);
202 
203     /**
204      * Add uids to virtual network
205      *
206      * @param netId The virtual network id
207      * @param uidRanges App uids to set
208      *
209      * @return Returns 0, successfully set the uids for specific network, otherwise it will fail
210      */
211     int32_t AddUidsToNetwork(int32_t netId, const std::vector<NetManagerStandard::UidRange> &uidRanges);
212 
213     /**
214      * Remove uids from virtual network
215      *
216      * @param netId The virtual network id
217      * @param uidRanges App uids to set
218      *
219      * @return Returns 0, successfully remove the uids for specific network, otherwise it will fail
220      */
221     int32_t RemoveUidsFromNetwork(int32_t netId, const std::vector<NetManagerStandard::UidRange> &uidRanges);
222 
223     /**
224      * Get the Dump Infos object
225      *
226      * @param infos The output message
227      */
228     void GetDumpInfos(std::string &infos);
229 
230 private:
231     int32_t defaultNetId_;
232     bool needReinitRouteFlag_;
233     std::map<int32_t, std::string> physicalInterfaceName_;
234     SafeMap<int32_t, std::shared_ptr<NetsysNetwork>> networks_;
235     std::mutex interfaceNameMutex_;
236     std::tuple<bool, std::shared_ptr<NetsysNetwork>> FindNetworkById(int32_t netId);
237     int32_t GetNetworkForInterface(std::string &interfaceName);
238     RouteManager::TableType GetTableType(int32_t netId);
239 };
240 } // namespace nmd
241 } // namespace OHOS
242 #endif // INCLUDE_CONN_MANAGER_H
243