• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "net_manager_constants.h"
17 #include "netnative_log_wrapper.h"
18 #include "route_manager.h"
19 
20 #include "physical_network.h"
21 
22 namespace OHOS {
23 namespace nmd {
24 using namespace NetManagerStandard;
25 
PhysicalNetwork(uint16_t netId,NetworkPermission permission)26 PhysicalNetwork::PhysicalNetwork(uint16_t netId, NetworkPermission permission)
27     : NetsysNetwork(netId), permission_(permission)
28 {
29 }
30 
AddDefault()31 void PhysicalNetwork::AddDefault()
32 {
33     std::set<std::string>::iterator it;
34     for (it = interfaces_.begin(); it != interfaces_.end(); ++it) {
35         RouteManager::AddInterfaceToDefaultNetwork(*it, permission_);
36     }
37     isDefault_ = true;
38 }
39 
RemoveDefault()40 void PhysicalNetwork::RemoveDefault()
41 {
42     std::set<std::string>::iterator it;
43     for (it = interfaces_.begin(); it != interfaces_.end(); ++it) {
44         RouteManager::RemoveInterfaceFromDefaultNetwork(*it, permission_);
45     }
46     isDefault_ = false;
47 }
48 
AddInterface(std::string & interfaceName)49 int32_t PhysicalNetwork::AddInterface(std::string &interfaceName)
50 {
51     NETNATIVE_LOGI("Entry PhysicalNetwork::AddInterface %{public}s", interfaceName.c_str());
52     if (ExistInterface(interfaceName)) {
53         return NETMANAGER_SUCCESS;
54     }
55 
56     if (RouteManager::AddInterfaceToPhysicalNetwork(netId_, interfaceName, permission_) != 0) {
57         NETNATIVE_LOGE("Failed to add interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
58         return NETMANAGER_ERROR;
59     }
60 
61     if (isDefault_) {
62         RouteManager::AddInterfaceToDefaultNetwork(interfaceName, permission_);
63     }
64 
65     interfaces_.insert(interfaceName);
66 
67     return NETMANAGER_SUCCESS;
68 }
69 
RemoveInterface(std::string & interfaceName)70 int32_t PhysicalNetwork::RemoveInterface(std::string &interfaceName)
71 {
72     NETNATIVE_LOGI("Entry PhysicalNetwork::RemoveInterface %{public}s", interfaceName.c_str());
73     if (!ExistInterface(interfaceName)) {
74         return NETMANAGER_SUCCESS;
75     }
76 
77     if (isDefault_) {
78         RouteManager::RemoveInterfaceFromDefaultNetwork(interfaceName, permission_);
79     }
80 
81     if (RouteManager::RemoveInterfaceFromPhysicalNetwork(netId_, interfaceName, permission_) != 0) {
82         NETNATIVE_LOGE("Failed to remove interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
83         return NETMANAGER_ERROR;
84     }
85 
86     interfaces_.erase(interfaceName);
87     return NETMANAGER_SUCCESS;
88 }
89 } // namespace nmd
90 } // namespace OHOS
91