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 "local_network.h"
21 namespace OHOS {
22 namespace nmd {
23 using namespace NetManagerStandard;
24
LocalNetwork(uint16_t netId)25 LocalNetwork::LocalNetwork(uint16_t netId) : NetsysNetwork(netId) {}
26
AddInterface(std::string & interfaceName)27 int32_t LocalNetwork::AddInterface(std::string &interfaceName)
28 {
29 NETNATIVE_LOGI("Entry LocalNetwork::AddInterface %{public}s", interfaceName.c_str());
30 if (ExistInterface(interfaceName)) {
31 return NETMANAGER_SUCCESS;
32 }
33
34 if (RouteManager::AddInterfaceToLocalNetwork(netId_, interfaceName) != 0) {
35 NETNATIVE_LOGE("Failed to add interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
36 return NETMANAGER_ERROR;
37 }
38
39 interfaces_.insert(interfaceName);
40 return NETMANAGER_SUCCESS;
41 }
42
RemoveInterface(std::string & interfaceName)43 int32_t LocalNetwork::RemoveInterface(std::string &interfaceName)
44 {
45 NETNATIVE_LOGI("Entry LocalNetwork::RemoveInterface %{public}s", interfaceName.c_str());
46 if (!ExistInterface(interfaceName)) {
47 return NETMANAGER_SUCCESS;
48 }
49
50 if (RouteManager::RemoveInterfaceFromLocalNetwork(netId_, interfaceName) != 0) {
51 NETNATIVE_LOGE("Failed to remove interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
52 return NETMANAGER_ERROR;
53 }
54
55 interfaces_.erase(interfaceName);
56 return NETMANAGER_SUCCESS;
57 }
58 } // namespace nmd
59 } // namespace OHOS
60