1 /* 2 * Copyright (C) 2019 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 #ifndef _NDC_DISPATCHER_H__ 18 #define _NDC_DISPATCHER_H__ 19 20 #include <string> 21 22 #include <android-base/logging.h> 23 #include <android/net/IDnsResolver.h> 24 #include <android/net/INetd.h> 25 #include "binder/IServiceManager.h" 26 27 #include "NetdConstants.h" 28 29 namespace android { 30 namespace net { 31 32 class NdcClient { 33 public: 34 NdcClient() = default; 35 ~NdcClient() = default; 36 sendMsg(int code,const char * msg,bool addErrno)37 int sendMsg(int code, const char* msg, bool addErrno) { 38 if (addErrno) { 39 printf("%d 0 %s (%s)\n", code, msg, strerror(errno)); 40 } else { 41 printf("%d 0 %s\n", code, msg); 42 } 43 return 0; 44 } 45 }; 46 47 class NdcNetdCommand { 48 public: NdcNetdCommand(std::string cmd)49 NdcNetdCommand(std::string cmd) : mCommand(std::move(cmd)) {} ~NdcNetdCommand()50 virtual ~NdcNetdCommand() {} 51 52 virtual int runCommand(NdcClient* c, int argc, char** argv) const = 0; 53 getCommand()54 const std::string& getCommand() const { return mCommand; } 55 56 private: 57 std::string mCommand; 58 }; 59 60 class NdcDispatcher { 61 public: 62 // Matches the restrictions previously imposed by CommandListener.cpp. 63 static const int CMD_ARGS_MAX = 26; 64 // Default log level is set to minimum one. 65 static const android::base::LogSeverity LOGLEVEL = android::base::VERBOSE; 66 67 NdcDispatcher(); 68 ~NdcDispatcher() = default; 69 70 static sp<INetd> mNetd; 71 static sp<IDnsResolver> mDnsResolver; 72 NdcClient mNdc; 73 74 int dispatchCommand(int argc, char** argv); 75 void registerCmd(NdcNetdCommand* cmd); 76 77 private: 78 std::vector<NdcNetdCommand*> mCommands; 79 80 class InterfaceCmd : public NdcNetdCommand { 81 public: 82 InterfaceCmd(); ~InterfaceCmd()83 virtual ~InterfaceCmd() {} 84 int runCommand(NdcClient* cli, int argc, char** argv) const; 85 }; 86 87 class IpFwdCmd : public NdcNetdCommand { 88 public: 89 IpFwdCmd(); ~IpFwdCmd()90 virtual ~IpFwdCmd() {} 91 int runCommand(NdcClient* cli, int argc, char** argv) const; 92 }; 93 94 class TetherCmd : public NdcNetdCommand { 95 public: 96 TetherCmd(); ~TetherCmd()97 virtual ~TetherCmd() {} 98 int runCommand(NdcClient* cli, int argc, char** argv) const; 99 }; 100 101 class NatCmd : public NdcNetdCommand { 102 public: 103 NatCmd(); ~NatCmd()104 virtual ~NatCmd() {} 105 int runCommand(NdcClient* cli, int argc, char** argv) const; 106 }; 107 108 class BandwidthControlCmd : public NdcNetdCommand { 109 public: 110 BandwidthControlCmd(); ~BandwidthControlCmd()111 virtual ~BandwidthControlCmd() {} 112 int runCommand(NdcClient* cli, int argc, char** argv) const; 113 114 protected: 115 void sendGenericOkFail(NdcClient* cli, int cond) const; 116 void sendGenericOpFailed(NdcClient* cli, const char* errMsg) const; 117 void sendGenericSyntaxError(NdcClient* cli, const char* usageMsg) const; 118 }; 119 120 class IdletimerControlCmd : public NdcNetdCommand { 121 public: 122 IdletimerControlCmd(); ~IdletimerControlCmd()123 virtual ~IdletimerControlCmd() {} 124 int runCommand(NdcClient* cli, int argc, char** argv) const; 125 }; 126 127 class FirewallCmd : public NdcNetdCommand { 128 public: 129 FirewallCmd(); ~FirewallCmd()130 virtual ~FirewallCmd() {} 131 int runCommand(NdcClient* cli, int argc, char** argv) const; 132 133 protected: 134 int sendGenericOkFail(NdcClient* cli, int cond) const; 135 static int parseRule(const char* arg); 136 static int parseFirewallType(const char* arg); 137 static int parseChildChain(const char* arg); 138 }; 139 140 class ClatdCmd : public NdcNetdCommand { 141 public: 142 ClatdCmd(); ~ClatdCmd()143 virtual ~ClatdCmd() {} 144 int runCommand(NdcClient* cli, int argc, char** argv) const; 145 }; 146 147 class StrictCmd : public NdcNetdCommand { 148 public: 149 StrictCmd(); ~StrictCmd()150 virtual ~StrictCmd() {} 151 int runCommand(NdcClient* cli, int argc, char** argv) const; 152 153 protected: 154 int sendGenericOkFail(NdcClient* cli, int cond) const; 155 static int parsePenalty(const char* arg); 156 }; 157 158 class NetworkCommand : public NdcNetdCommand { 159 public: 160 NetworkCommand(); ~NetworkCommand()161 virtual ~NetworkCommand() {} 162 int runCommand(NdcClient* cli, int argc, char** argv) const; 163 164 private: 165 int syntaxError(NdcClient* cli, const char* message) const; 166 int operationError(NdcClient* cli, const char* message, int ret) const; 167 int success(NdcClient* cli) const; 168 }; 169 }; 170 171 } // namespace net 172 } // namespace android 173 174 #endif 175