CommandListener.h revision e4d626ea35b7a402388b524e2feafc81e6387697
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 "NatController.h"
28#include "PppController.h"
29#include "SoftapController.h"
30#include "BandwidthController.h"
31#include "IdletimerController.h"
32#include "InterfaceController.h"
33#include "ResolverController.h"
34#include "FirewallController.h"
35#include "ClatdController.h"
36#include "StrictController.h"
37
38
39class LockingFrameworkCommand : public FrameworkCommand {
40public:
41    LockingFrameworkCommand(FrameworkCommand *wrappedCmd) :
42            FrameworkCommand(wrappedCmd->getCommand()),
43            mWrappedCmd(wrappedCmd) {}
44
45    int runCommand(SocketClient *c, int argc, char **argv) {
46        android::RWLock::AutoWLock lock(android::net::gBigNetdLock);
47        return mWrappedCmd->runCommand(c, argc, argv);
48    }
49
50private:
51    FrameworkCommand *mWrappedCmd;
52};
53
54class CommandListener : public FrameworkListener {
55    static TetherController *sTetherCtrl;
56    static NatController *sNatCtrl;
57    static PppController *sPppCtrl;
58    static SoftapController *sSoftapCtrl;
59    static BandwidthController *sBandwidthCtrl;
60    static IdletimerController *sIdletimerCtrl;
61    static InterfaceController *sInterfaceCtrl;
62    static ResolverController *sResolverCtrl;
63    static FirewallController *sFirewallCtrl;
64    static ClatdController *sClatdCtrl;
65    static StrictController *sStrictCtrl;
66
67public:
68    static NetworkController *sNetCtrl;
69
70    CommandListener();
71    virtual ~CommandListener() {}
72
73private:
74
75    void registerLockingCmd(FrameworkCommand *cmd) {
76        registerCmd(new LockingFrameworkCommand(cmd));
77    }
78
79    class SoftapCmd : public NetdCommand {
80    public:
81        SoftapCmd();
82        virtual ~SoftapCmd() {}
83        int runCommand(SocketClient *c, int argc, char ** argv);
84    };
85
86    class InterfaceCmd : public NetdCommand {
87    public:
88        InterfaceCmd();
89        virtual ~InterfaceCmd() {}
90        int runCommand(SocketClient *c, int argc, char ** argv);
91    };
92
93    class IpFwdCmd : public NetdCommand {
94    public:
95        IpFwdCmd();
96        virtual ~IpFwdCmd() {}
97        int runCommand(SocketClient *c, int argc, char ** argv);
98    };
99
100    class TetherCmd : public NetdCommand {
101    public:
102        TetherCmd();
103        virtual ~TetherCmd() {}
104        int runCommand(SocketClient *c, int argc, char ** argv);
105    };
106
107    class NatCmd : public NetdCommand {
108    public:
109        NatCmd();
110        virtual ~NatCmd() {}
111        int runCommand(SocketClient *c, int argc, char ** argv);
112    };
113
114    class ListTtysCmd : public NetdCommand {
115    public:
116        ListTtysCmd();
117        virtual ~ListTtysCmd() {}
118        int runCommand(SocketClient *c, int argc, char ** argv);
119    };
120
121    class PppdCmd : public NetdCommand {
122    public:
123        PppdCmd();
124        virtual ~PppdCmd() {}
125        int runCommand(SocketClient *c, int argc, char ** argv);
126    };
127
128    class BandwidthControlCmd : public NetdCommand {
129    public:
130        BandwidthControlCmd();
131        virtual ~BandwidthControlCmd() {}
132        int runCommand(SocketClient *c, int argc, char ** argv);
133    protected:
134        void sendGenericOkFail(SocketClient *cli, int cond);
135        void sendGenericOpFailed(SocketClient *cli, const char *errMsg);
136        void sendGenericSyntaxError(SocketClient *cli, const char *usageMsg);
137    };
138
139    class IdletimerControlCmd : public NetdCommand {
140    public:
141        IdletimerControlCmd();
142        virtual ~IdletimerControlCmd() {}
143        int runCommand(SocketClient *c, int argc, char ** argv);
144    };
145
146    class ResolverCmd : public NetdCommand {
147    public:
148        ResolverCmd();
149        virtual ~ResolverCmd() {}
150        int runCommand(SocketClient *c, int argc, char ** argv);
151    };
152
153    class FirewallCmd: public NetdCommand {
154    public:
155        FirewallCmd();
156        virtual ~FirewallCmd() {}
157        int runCommand(SocketClient *c, int argc, char ** argv);
158    protected:
159        int sendGenericOkFail(SocketClient *cli, int cond);
160        static FirewallRule parseRule(const char* arg);
161        static FirewallType parseFirewallType(const char* arg);
162        static ChildChain parseChildChain(const char* arg);
163    };
164
165    class ClatdCmd : public NetdCommand {
166    public:
167        ClatdCmd();
168        virtual ~ClatdCmd() {}
169        int runCommand(SocketClient *c, int argc, char ** argv);
170    };
171
172    class StrictCmd : public NetdCommand {
173    public:
174        StrictCmd();
175        virtual ~StrictCmd() {}
176        int runCommand(SocketClient *c, int argc, char ** argv);
177    protected:
178        int sendGenericOkFail(SocketClient *cli, int cond);
179        static StrictPenalty parsePenalty(const char* arg);
180    };
181
182    class NetworkCommand : public NetdCommand {
183    public:
184        NetworkCommand();
185        virtual ~NetworkCommand() {}
186        int runCommand(SocketClient* client, int argc, char** argv);
187    private:
188        int syntaxError(SocketClient* cli, const char* message);
189        int operationError(SocketClient* cli, const char* message, int ret);
190        int success(SocketClient* cli);
191    };
192};
193
194#endif
195