• 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 #include "nmd_network.h"
17 #include "route_controller.h"
18 #include "netnative_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace nmd {
NmdNetwork(uint16_t netId,NetworkPermission permission)22 NmdNetwork::NmdNetwork(uint16_t netId, NetworkPermission permission) : netId(netId), permission(permission) {}
23 
~NmdNetwork()24 NmdNetwork::~NmdNetwork() {}
25 
AddDefault()26 void NmdNetwork::AddDefault()
27 {
28     std::set<std::string>::iterator it;
29     for (it = this->interfaces.begin(); it != this->interfaces.end(); ++it) {
30         RouteController::AddInterfaceToDefaultNetwork(it->c_str(), this->permission);
31     }
32     this->isDefault = true;
33 }
34 
RemoveDefault()35 void NmdNetwork::RemoveDefault()
36 {
37     std::set<std::string>::iterator it;
38     for (it = this->interfaces.begin(); it != this->interfaces.end(); ++it) {
39         RouteController::RemoveInterfaceFromDefaultNetwork(it->c_str(), this->permission);
40     }
41     this->isDefault = false;
42 }
43 
AddInterface(std::string & interfaceName)44 int NmdNetwork::AddInterface(std::string &interfaceName)
45 {
46     NETNATIVE_LOGI("Entry NmdNetwork::AddInterface");
47     if (ExistInterface(interfaceName)) {
48         return 1;
49     }
50 
51     if (this->isDefault) {
52         RouteController::AddInterfaceToDefaultNetwork(interfaceName.c_str(), this->permission);
53     }
54 
55     this->interfaces.insert(interfaceName);
56     return 1;
57 }
58 
RemoveInterface(std::string & interfaceName)59 int NmdNetwork::RemoveInterface(std::string &interfaceName)
60 {
61     if (!ExistInterface(interfaceName)) {
62         return 1;
63     }
64 
65     if (this->isDefault) {
66         RouteController::RemoveInterfaceFromDefaultNetwork(interfaceName.c_str(), this->permission);
67     }
68 
69     this->interfaces.erase(interfaceName);
70     return 1;
71 }
72 
ClearInterfaces()73 int NmdNetwork::ClearInterfaces()
74 {
75     this->interfaces.clear();
76     return 1;
77 }
78 
ExistInterface(std::string & interfaceName)79 bool NmdNetwork::ExistInterface(std::string &interfaceName)
80 {
81     return this->interfaces.find(interfaceName) != this->interfaces.end();
82 }
83 } // namespace nmd
84 } // namespace OHOS