blob: f33cd887b704df266126d4ca6e65ee144c328308 [file] [log] [blame]
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "Network.h"
18
19#define LOG_TAG "Netd"
20#include "log/log.h"
21
Erik Kline2d3a1632016-03-15 16:33:48 +090022#include <android-base/strings.h>
23#include <sstream>
24
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070025Network::~Network() {
26 if (!mInterfaces.empty()) {
27 ALOGE("deleting network with netId %u without clearing its interfaces", mNetId);
28 }
29}
30
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070031unsigned Network::getNetId() const {
32 return mNetId;
33}
34
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070035bool Network::hasInterface(const std::string& interface) const {
36 return mInterfaces.find(interface) != mInterfaces.end();
37}
38
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070039const std::set<std::string>& Network::getInterfaces() const {
40 return mInterfaces;
41}
42
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070043int Network::clearInterfaces() {
44 while (!mInterfaces.empty()) {
45 // Make a copy of the string, so removeInterface() doesn't lose its parameter when it
46 // removes the string from the set.
47 std::string interface = *mInterfaces.begin();
48 if (int ret = removeInterface(interface)) {
49 return ret;
50 }
51 }
52 return 0;
53}
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070054
Erik Kline2d3a1632016-03-15 16:33:48 +090055std::string Network::toString() const {
56 const char kSeparator[] = " ";
57 std::stringstream repr;
58
59 repr << mNetId;
60
61 repr << kSeparator;
62 switch (getType()) {
63 case DUMMY:
64 repr << "DUMMY";
65 break;
66 case LOCAL:
67 repr << "LOCAL";
68 break;
69 case PHYSICAL:
70 repr << "PHYSICAL";
71 break;
72 case VIRTUAL:
73 repr << "VIRTUAL";
74 break;
75 default:
76 repr << "unknown";
77 }
78
79 if (mInterfaces.size() > 0) {
80 repr << kSeparator << android::base::Join(mInterfaces, ",");
81 }
82
83 return repr.str();
84}
85
86
Sreeram Ramachandran89dad012014-07-02 10:09:49 -070087Network::Network(unsigned netId) : mNetId(netId) {
88}