Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 22 | #include <android-base/strings.h> |
| 23 | #include <sstream> |
| 24 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace net { |
| 27 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 28 | Network::~Network() { |
| 29 | if (!mInterfaces.empty()) { |
| 30 | ALOGE("deleting network with netId %u without clearing its interfaces", mNetId); |
| 31 | } |
| 32 | } |
| 33 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 34 | unsigned Network::getNetId() const { |
| 35 | return mNetId; |
| 36 | } |
| 37 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 38 | bool Network::hasInterface(const std::string& interface) const { |
| 39 | return mInterfaces.find(interface) != mInterfaces.end(); |
| 40 | } |
| 41 | |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 42 | const std::set<std::string>& Network::getInterfaces() const { |
| 43 | return mInterfaces; |
| 44 | } |
| 45 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 46 | int Network::clearInterfaces() { |
| 47 | while (!mInterfaces.empty()) { |
| 48 | // Make a copy of the string, so removeInterface() doesn't lose its parameter when it |
| 49 | // removes the string from the set. |
| 50 | std::string interface = *mInterfaces.begin(); |
| 51 | if (int ret = removeInterface(interface)) { |
| 52 | return ret; |
| 53 | } |
| 54 | } |
| 55 | return 0; |
| 56 | } |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 57 | |
Erik Kline | 2d3a163 | 2016-03-15 16:33:48 +0900 | [diff] [blame] | 58 | std::string Network::toString() const { |
| 59 | const char kSeparator[] = " "; |
| 60 | std::stringstream repr; |
| 61 | |
| 62 | repr << mNetId; |
| 63 | |
| 64 | repr << kSeparator; |
| 65 | switch (getType()) { |
| 66 | case DUMMY: |
| 67 | repr << "DUMMY"; |
| 68 | break; |
| 69 | case LOCAL: |
| 70 | repr << "LOCAL"; |
| 71 | break; |
| 72 | case PHYSICAL: |
| 73 | repr << "PHYSICAL"; |
| 74 | break; |
| 75 | case VIRTUAL: |
| 76 | repr << "VIRTUAL"; |
| 77 | break; |
| 78 | default: |
| 79 | repr << "unknown"; |
| 80 | } |
| 81 | |
| 82 | if (mInterfaces.size() > 0) { |
| 83 | repr << kSeparator << android::base::Join(mInterfaces, ","); |
| 84 | } |
| 85 | |
| 86 | return repr.str(); |
| 87 | } |
| 88 | |
| 89 | |
Sreeram Ramachandran | 89dad01 | 2014-07-02 10:09:49 -0700 | [diff] [blame] | 90 | Network::Network(unsigned netId) : mNetId(netId) { |
| 91 | } |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 92 | |
| 93 | } // namespace net |
| 94 | } // namespace android |