Network.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "Network.h"
  17. #define LOG_TAG "Netd"
  18. #include "log/log.h"
  19. #include <android-base/strings.h>
  20. #include <sstream>
  21. namespace android {
  22. namespace net {
  23. Network::~Network() {
  24. if (!mInterfaces.empty()) {
  25. ALOGE("deleting network with netId %u without clearing its interfaces", mNetId);
  26. }
  27. }
  28. unsigned Network::getNetId() const {
  29. return mNetId;
  30. }
  31. bool Network::hasInterface(const std::string& interface) const {
  32. return mInterfaces.find(interface) != mInterfaces.end();
  33. }
  34. const std::set<std::string>& Network::getInterfaces() const {
  35. return mInterfaces;
  36. }
  37. int Network::clearInterfaces() {
  38. while (!mInterfaces.empty()) {
  39. // Make a copy of the string, so removeInterface() doesn't lose its parameter when it
  40. // removes the string from the set.
  41. std::string interface = *mInterfaces.begin();
  42. if (int ret = removeInterface(interface)) {
  43. return ret;
  44. }
  45. }
  46. return 0;
  47. }
  48. std::string Network::toString() const {
  49. const char kSeparator[] = " ";
  50. std::stringstream repr;
  51. repr << mNetId;
  52. repr << kSeparator;
  53. switch (getType()) {
  54. case DUMMY:
  55. repr << "DUMMY";
  56. break;
  57. case LOCAL:
  58. repr << "LOCAL";
  59. break;
  60. case PHYSICAL:
  61. repr << "PHYSICAL";
  62. break;
  63. case VIRTUAL:
  64. repr << "VIRTUAL";
  65. break;
  66. default:
  67. repr << "unknown";
  68. }
  69. if (mInterfaces.size() > 0) {
  70. repr << kSeparator << android::base::Join(mInterfaces, ",");
  71. }
  72. return repr.str();
  73. }
  74. Network::Network(unsigned netId) : mNetId(netId) {
  75. }
  76. } // namespace net
  77. } // namespace android