1/*
2 * Copyright (C) 2008 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 _COMMANDLISTENER_H__
18#define _COMMANDLISTENER_H__
19
20#include <sysutils/FrameworkListener.h>
21#include "utils/RWLock.h"
22
23#include "NetdCommand.h"
24#include "NetdConstants.h"
25#include "NetworkController.h"
26#include "TetherController.h"
27#include "PppController.h"
28#include "BandwidthController.h"
29#include "IdletimerController.h"
30#include "InterfaceController.h"
31#include "ResolverController.h"
32#include "FirewallController.h"
33#include "ClatdController.h"
34#include "StrictController.h"
35
36namespace android {
37namespace net {
38
39class CommandListener : public FrameworkListener {
40public:
41    CommandListener();
42    virtual ~CommandListener() {}
43
44    static constexpr const char* SOCKET_NAME = "netd";
45
46private:
47    void registerLockingCmd(FrameworkCommand *cmd, android::RWLock& lock);
48    void registerLockingCmd(FrameworkCommand *cmd) {
49        registerLockingCmd(cmd, android::net::gBigNetdLock);
50    }
51
52    class InterfaceCmd : public NetdCommand {
53    public:
54        InterfaceCmd();
55        virtual ~InterfaceCmd() {}
56        int runCommand(SocketClient *c, int argc, char ** argv);
57    };
58
59    class IpFwdCmd : public NetdCommand {
60    public:
61        IpFwdCmd();
62        virtual ~IpFwdCmd() {}
63        int runCommand(SocketClient *c, int argc, char ** argv);
64    };
65
66    class TetherCmd : public NetdCommand {
67    public:
68        TetherCmd();
69        virtual ~TetherCmd() {}
70        int runCommand(SocketClient *c, int argc, char ** argv);
71    };
72
73    class NatCmd : public NetdCommand {
74    public:
75        NatCmd();
76        virtual ~NatCmd() {}
77        int runCommand(SocketClient *c, int argc, char ** argv);
78    };
79
80    class ListTtysCmd : public NetdCommand {
81    public:
82        ListTtysCmd();
83        virtual ~ListTtysCmd() {}
84        int runCommand(SocketClient *c, int argc, char ** argv);
85    };
86
87    class PppdCmd : public NetdCommand {
88    public:
89        PppdCmd();
90        virtual ~PppdCmd() {}
91        int runCommand(SocketClient *c, int argc, char ** argv);
92    };
93
94    class BandwidthControlCmd : public NetdCommand {
95    public:
96        BandwidthControlCmd();
97        virtual ~BandwidthControlCmd() {}
98        int runCommand(SocketClient *c, int argc, char ** argv);
99    protected:
100        void sendGenericOkFail(SocketClient *cli, int cond);
101        void sendGenericOpFailed(SocketClient *cli, const char *errMsg);
102        void sendGenericSyntaxError(SocketClient *cli, const char *usageMsg);
103    };
104
105    class IdletimerControlCmd : public NetdCommand {
106    public:
107        IdletimerControlCmd();
108        virtual ~IdletimerControlCmd() {}
109        int runCommand(SocketClient *c, int argc, char ** argv);
110    };
111
112    class ResolverCmd : public NetdCommand {
113    public:
114        ResolverCmd();
115        virtual ~ResolverCmd() {}
116        int runCommand(SocketClient *c, int argc, char ** argv);
117
118    private:
119        bool parseAndExecuteSetNetDns(int netId, int argc, const char** argv);
120    };
121
122    class FirewallCmd: public NetdCommand {
123    public:
124        FirewallCmd();
125        virtual ~FirewallCmd() {}
126        int runCommand(SocketClient *c, int argc, char ** argv);
127    protected:
128        int sendGenericOkFail(SocketClient *cli, int cond);
129        static FirewallRule parseRule(const char* arg);
130        static FirewallType parseFirewallType(const char* arg);
131        static ChildChain parseChildChain(const char* arg);
132    };
133
134    class ClatdCmd : public NetdCommand {
135    public:
136        ClatdCmd();
137        virtual ~ClatdCmd() {}
138        int runCommand(SocketClient *c, int argc, char ** argv);
139    };
140
141    class StrictCmd : public NetdCommand {
142    public:
143        StrictCmd();
144        virtual ~StrictCmd() {}
145        int runCommand(SocketClient *c, int argc, char ** argv);
146    protected:
147        int sendGenericOkFail(SocketClient *cli, int cond);
148        static StrictPenalty parsePenalty(const char* arg);
149    };
150
151    class NetworkCommand : public NetdCommand {
152    public:
153        NetworkCommand();
154        virtual ~NetworkCommand() {}
155        int runCommand(SocketClient* client, int argc, char** argv);
156    private:
157        int syntaxError(SocketClient* cli, const char* message);
158        int operationError(SocketClient* cli, const char* message, int ret);
159        int success(SocketClient* cli);
160    };
161};
162
163}  // namespace net
164}  // namespace android
165
166#endif
167