2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "VirtualNetwork.h"
19 #include "RouteController.h"
21 #define LOG_TAG "Netd"
24 VirtualNetwork::VirtualNetwork(unsigned netId, bool hasDns): Network(netId), mHasDns(hasDns) {
27 VirtualNetwork::~VirtualNetwork() {
30 bool VirtualNetwork::getHasDns() const {
34 bool VirtualNetwork::appliesToUser(uid_t uid) const {
35 return mUidRanges.hasUid(uid);
38 int VirtualNetwork::addUsers(const UidRanges& uidRanges) {
39 for (const std::string& interface : mInterfaces) {
40 if (int ret = RouteController::addUsersToVirtualNetwork(mNetId, interface.c_str(),
42 ALOGE("failed to add users on interface %s of netId %u", interface.c_str(), mNetId);
46 mUidRanges.add(uidRanges);
50 int VirtualNetwork::removeUsers(const UidRanges& uidRanges) {
51 for (const std::string& interface : mInterfaces) {
52 if (int ret = RouteController::removeUsersFromVirtualNetwork(mNetId, interface.c_str(),
54 ALOGE("failed to remove users on interface %s of netId %u", interface.c_str(), mNetId);
58 mUidRanges.remove(uidRanges);
62 Network::Type VirtualNetwork::getType() const {
66 int VirtualNetwork::addInterface(const std::string& interface) {
67 if (hasInterface(interface)) {
70 if (int ret = RouteController::addInterfaceToVirtualNetwork(mNetId, interface.c_str(),
72 ALOGE("failed to add interface %s to VPN netId %u", interface.c_str(), mNetId);
75 mInterfaces.insert(interface);
79 int VirtualNetwork::removeInterface(const std::string& interface) {
80 if (!hasInterface(interface)) {
83 if (int ret = RouteController::removeInterfaceFromVirtualNetwork(mNetId, interface.c_str(),
85 ALOGE("failed to remove interface %s from VPN netId %u", interface.c_str(), mNetId);
88 mInterfaces.erase(interface);