1 /* 2 * Copyright (C) 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 #pragma once 17 18 #include <string> 19 #include <vector> 20 21 #include "interface.h" 22 #include "router.h" 23 24 struct pollfd; 25 class Message; 26 class Packet; 27 class Socket; 28 29 class Proxy { 30 public: 31 template<typename Iter> 32 Proxy(std::string outerInterfaceName, 33 Iter innerInterfacesBegin, Iter innerInterfacesEnd) 34 : mOuterIf(outerInterfaceName), 35 mLogDebug(false) { 36 37 for (Iter i = innerInterfacesBegin; i != innerInterfacesEnd; ++i) { 38 mInnerIfs.emplace_back(*i); 39 } 40 } 41 42 int run(); 43 44 private: 45 enum ForwardOpt { 46 kForwardOnly = 0, 47 kRewriteTargetLink = (1 << 0), 48 kRewriteSourceLink = (1 << 1), 49 kSpoofSource = (1 << 2), 50 kAddRoute = (1 << 3), 51 kSetDefaultGateway = (1 << 4) 52 }; 53 54 bool receiveIfPossible(const pollfd&, Socket& socket, Message* message); 55 56 void handleOuterMessage(Message& message); 57 void handleInnerMessage(const Interface& inner, Message& message); 58 void forward(const Interface& from, Interface& to, 59 Packet& packet, uint32_t options); 60 61 std::vector<Interface> mInnerIfs; 62 Interface mOuterIf; 63 64 Router mRouter; 65 bool mLogDebug; 66 }; 67 68