tun_interface.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright 2017 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. * tun_interface.h - creates tun interfaces for testing purposes
  17. */
  18. #ifndef _SYSTEM_NETD_TESTS_TUN_INTERACE_H
  19. #define _SYSTEM_NETD_TESTS_TUN_INTERACE_H
  20. namespace android {
  21. namespace net {
  22. class TunInterface {
  23. public:
  24. TunInterface() = default;
  25. ~TunInterface() { destroy(); }
  26. // Creates a tun interface. Returns 0 on success or -errno on failure. Must succeed before it is
  27. // legal to call any of the other methods in this class.
  28. int init(const std::string& ifName = "");
  29. void destroy();
  30. const std::string& name() const { return mIfName; }
  31. int ifindex() const { return mIfIndex; }
  32. const in6_addr& srcAddr() const { return mSrcAddr; }
  33. const in6_addr& dstAddr() const { return mDstAddr; }
  34. int addAddress(const std::string& addr, int prefixlen);
  35. int getFdForTesting() const { return mFd; }
  36. private:
  37. int mFd = -1;
  38. std::string mIfName;
  39. int mIfIndex;
  40. in6_addr mSrcAddr, mDstAddr;
  41. };
  42. } // namespace net
  43. } // namespace android
  44. #endif