Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 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 | * |
Lorenzo Colitti | 48dec38 | 2020-05-29 21:34:47 +0900 | [diff] [blame] | 16 | * tun_interface.h - creates tun or tap interfaces for testing purposes |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #ifndef _SYSTEM_NETD_TESTS_TUN_INTERACE_H |
| 20 | #define _SYSTEM_NETD_TESTS_TUN_INTERACE_H |
| 21 | |
| 22 | namespace android { |
| 23 | namespace net { |
| 24 | |
| 25 | class TunInterface { |
| 26 | public: |
| 27 | TunInterface() = default; |
| 28 | ~TunInterface() { destroy(); } |
| 29 | |
Lorenzo Colitti | 48dec38 | 2020-05-29 21:34:47 +0900 | [diff] [blame] | 30 | // Creates a tun or tap interface. Returns 0 on success or -errno on failure. Must succeed |
| 31 | // before it is legal to call any of the other methods in this class. |
| 32 | int init(const std::string& ifName, bool isTap); |
| 33 | |
| 34 | // Convenience overloads. |
| 35 | int init() { return init("", false); } |
| 36 | int init(const std::string& ifName) { return init(ifName, false); } |
| 37 | int init(bool isTap) { return init("", isTap); } |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 38 | void destroy(); |
| 39 | |
| 40 | const std::string& name() const { return mIfName; } |
Lorenzo Colitti | 54520a0 | 2018-02-09 18:39:16 +0900 | [diff] [blame] | 41 | int ifindex() const { return mIfIndex; } |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 42 | const in6_addr& srcAddr() const { return mSrcAddr; } |
| 43 | const in6_addr& dstAddr() const { return mDstAddr; } |
| 44 | |
Lorenzo Colitti | 8a9f1ad | 2019-02-26 00:30:18 +0900 | [diff] [blame] | 45 | int addAddress(const std::string& addr, int prefixlen); |
Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 46 | int getFdForTesting() const { return mFd; } |
Lorenzo Colitti | 8a9f1ad | 2019-02-26 00:30:18 +0900 | [diff] [blame] | 47 | |
| 48 | private: |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 49 | int mFd = -1; |
| 50 | std::string mIfName; |
Lorenzo Colitti | 54520a0 | 2018-02-09 18:39:16 +0900 | [diff] [blame] | 51 | int mIfIndex; |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 52 | in6_addr mSrcAddr, mDstAddr; |
| 53 | }; |
| 54 | |
| 55 | } // namespace net |
| 56 | } // namespace android |
| 57 | |
| 58 | #endif |