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