1 /*
2  * Copyright (c) 2022-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 #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::lock_guard<std::mutex> lock(physicalNetMutex_);
34     std::set<std::string>::iterator it;
35     for (it = interfaces_.begin(); it != interfaces_.end(); ++it) {
36         RouteManager::AddInterfaceToDefaultNetwork(*it, permission_);
37     }
38     isDefault_ = true;
39 }
40 
RemoveDefault()41 void PhysicalNetwork::RemoveDefault()
42 {
43     std::lock_guard<std::mutex> lock(physicalNetMutex_);
44     std::set<std::string>::iterator it;
45     for (it = interfaces_.begin(); it != interfaces_.end(); ++it) {
46         RouteManager::RemoveInterfaceFromDefaultNetwork(*it, permission_);
47     }
48     isDefault_ = false;
49 }
50 
AddInterface(std::string & interfaceName)51 int32_t PhysicalNetwork::AddInterface(std::string &interfaceName)
52 {
53     NETNATIVE_LOGI("AddInterface %{public}s", interfaceName.c_str());
54     if (ExistInterface(interfaceName)) {
55         return NETMANAGER_SUCCESS;
56     }
57 
58     if (RouteManager::AddInterfaceToPhysicalNetwork(netId_, interfaceName, permission_) != 0) {
59         NETNATIVE_LOGE("Failed to add interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
60         return NETMANAGER_ERROR;
61     }
62 
63     if (isDefault_) {
64         RouteManager::AddInterfaceToDefaultNetwork(interfaceName, permission_);
65     }
66     std::lock_guard<std::mutex> lock(physicalNetMutex_);
67     interfaces_.insert(interfaceName);
68 
69     return NETMANAGER_SUCCESS;
70 }
71 
RemoveInterface(std::string & interfaceName)72 int32_t PhysicalNetwork::RemoveInterface(std::string &interfaceName)
73 {
74     NETNATIVE_LOGI("RemoveInterface %{public}s", interfaceName.c_str());
75     if (!ExistInterface(interfaceName)) {
76         return NETMANAGER_SUCCESS;
77     }
78 
79     if (isDefault_) {
80         RouteManager::RemoveInterfaceFromDefaultNetwork(interfaceName, permission_);
81     }
82 
83     if (RouteManager::RemoveInterfaceFromPhysicalNetwork(netId_, interfaceName, permission_) != 0) {
84         NETNATIVE_LOGE("Failed to remove interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
85         return NETMANAGER_ERROR;
86     }
87     std::lock_guard<std::mutex> lock(physicalNetMutex_);
88     interfaces_.erase(interfaceName);
89     return NETMANAGER_SUCCESS;
90 }
91 } // namespace nmd
92 } // namespace OHOS
93