1 /*
2  * Copyright (C) 2010 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 _DNSPROXYLISTENER_H__
18 #define _DNSPROXYLISTENER_H__
19 
20 #include <resolv_netid.h>  // struct android_net_context
21 #include <binder/IServiceManager.h>
22 #include <sysutils/FrameworkListener.h>
23 
24 #include "android/net/metrics/INetdEventListener.h"
25 #include "EventReporter.h"
26 #include "NetdCommand.h"
27 
28 namespace android {
29 namespace net {
30 
31 class NetworkController;
32 
33 class DnsProxyListener : public FrameworkListener {
34 public:
35     explicit DnsProxyListener(const NetworkController* netCtrl, EventReporter* eventReporter);
~DnsProxyListener()36     virtual ~DnsProxyListener() {}
37 
38 private:
39     const NetworkController *mNetCtrl;
40     EventReporter *mEventReporter;
41     static void addIpAddrWithinLimit(std::vector<android::String16>& ip_addrs, const sockaddr* addr,
42             socklen_t addrlen);
43 
44     class GetAddrInfoCmd : public NetdCommand {
45     public:
46         explicit GetAddrInfoCmd(DnsProxyListener* dnsProxyListener);
~GetAddrInfoCmd()47         virtual ~GetAddrInfoCmd() {}
48         int runCommand(SocketClient *c, int argc, char** argv);
49     private:
50         DnsProxyListener* mDnsProxyListener;
51     };
52 
53     class GetAddrInfoHandler {
54     public:
55         // Note: All of host, service, and hints may be NULL
56         GetAddrInfoHandler(SocketClient *c,
57                            char* host,
58                            char* service,
59                            struct addrinfo* hints,
60                            const struct android_net_context& netcontext,
61                            const int reportingLevel,
62                            const android::sp<android::net::metrics::INetdEventListener>& listener);
63         ~GetAddrInfoHandler();
64 
65         void run();
66 
67     private:
68         SocketClient* mClient;  // ref counted
69         char* mHost;    // owned
70         char* mService; // owned
71         struct addrinfo* mHints;  // owned
72         struct android_net_context mNetContext;
73         const int mReportingLevel;
74         android::sp<android::net::metrics::INetdEventListener> mNetdEventListener;
75     };
76 
77     /* ------ gethostbyname ------*/
78     class GetHostByNameCmd : public NetdCommand {
79     public:
80         explicit GetHostByNameCmd(DnsProxyListener* dnsProxyListener);
~GetHostByNameCmd()81         virtual ~GetHostByNameCmd() {}
82         int runCommand(SocketClient *c, int argc, char** argv);
83     private:
84         DnsProxyListener* mDnsProxyListener;
85     };
86 
87     class GetHostByNameHandler {
88     public:
89         GetHostByNameHandler(SocketClient *c,
90                             char *name,
91                             int af,
92                             unsigned netId,
93                             uint32_t mark,
94                             int reportingLevel,
95                             const android::sp<android::net::metrics::INetdEventListener>& listener);
96         ~GetHostByNameHandler();
97 
98         void run();
99 
100     private:
101         SocketClient* mClient; //ref counted
102         char* mName; // owned
103         int mAf;
104         unsigned mNetId;
105         uint32_t mMark;
106         const int mReportingLevel;
107         android::sp<android::net::metrics::INetdEventListener> mNetdEventListener;
108     };
109 
110     /* ------ gethostbyaddr ------*/
111     class GetHostByAddrCmd : public NetdCommand {
112     public:
113         explicit GetHostByAddrCmd(const DnsProxyListener* dnsProxyListener);
~GetHostByAddrCmd()114         virtual ~GetHostByAddrCmd() {}
115         int runCommand(SocketClient *c, int argc, char** argv);
116     private:
117         const DnsProxyListener* mDnsProxyListener;
118     };
119 
120     class GetHostByAddrHandler {
121     public:
122         GetHostByAddrHandler(SocketClient *c,
123                             void* address,
124                             int addressLen,
125                             int addressFamily,
126                             unsigned netId,
127                             uint32_t mark);
128         ~GetHostByAddrHandler();
129 
130         void run();
131 
132     private:
133         SocketClient* mClient;  // ref counted
134         void* mAddress;    // address to lookup; owned
135         int mAddressLen; // length of address to look up
136         int mAddressFamily;  // address family
137         unsigned mNetId;
138         uint32_t mMark;
139     };
140 };
141 
142 }  // namespace net
143 }  // namespace android
144 
145 #endif
146