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     static constexpr const char* SOCKET_NAME = "dnsproxyd";
39 
40 private:
41     const NetworkController *mNetCtrl;
42     EventReporter *mEventReporter;
43     static void addIpAddrWithinLimit(std::vector<android::String16>& ip_addrs, const sockaddr* addr,
44             socklen_t addrlen);
45 
46     class GetAddrInfoCmd : public NetdCommand {
47     public:
48         explicit GetAddrInfoCmd(DnsProxyListener* dnsProxyListener);
~GetAddrInfoCmd()49         virtual ~GetAddrInfoCmd() {}
50         int runCommand(SocketClient *c, int argc, char** argv);
51     private:
52         DnsProxyListener* mDnsProxyListener;
53     };
54 
55     class GetAddrInfoHandler {
56     public:
57         // Note: All of host, service, and hints may be NULL
58         GetAddrInfoHandler(SocketClient *c,
59                            char* host,
60                            char* service,
61                            struct addrinfo* hints,
62                            const struct android_net_context& netcontext,
63                            const int reportingLevel,
64                            const android::sp<android::net::metrics::INetdEventListener>& listener);
65         ~GetAddrInfoHandler();
66 
67         void run();
68 
69     private:
70         SocketClient* mClient;  // ref counted
71         char* mHost;    // owned
72         char* mService; // owned
73         struct addrinfo* mHints;  // owned
74         struct android_net_context mNetContext;
75         const int mReportingLevel;
76         android::sp<android::net::metrics::INetdEventListener> mNetdEventListener;
77     };
78 
79     /* ------ gethostbyname ------*/
80     class GetHostByNameCmd : public NetdCommand {
81     public:
82         explicit GetHostByNameCmd(DnsProxyListener* dnsProxyListener);
~GetHostByNameCmd()83         virtual ~GetHostByNameCmd() {}
84         int runCommand(SocketClient *c, int argc, char** argv);
85     private:
86         DnsProxyListener* mDnsProxyListener;
87     };
88 
89     class GetHostByNameHandler {
90     public:
91         GetHostByNameHandler(SocketClient *c,
92                             char *name,
93                             int af,
94                             const android_net_context& netcontext,
95                             int reportingLevel,
96                             const android::sp<android::net::metrics::INetdEventListener>& listener);
97         ~GetHostByNameHandler();
98 
99         void run();
100 
101     private:
102         SocketClient* mClient; //ref counted
103         char* mName; // owned
104         int mAf;
105         android_net_context mNetContext;
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                             const android_net_context& netcontext);
127         ~GetHostByAddrHandler();
128 
129         void run();
130 
131     private:
132         SocketClient* mClient;  // ref counted
133         void* mAddress;    // address to lookup; owned
134         int mAddressLen; // length of address to look up
135         int mAddressFamily;  // address family
136         android_net_context mNetContext;
137     };
138 };
139 
140 }  // namespace net
141 }  // namespace android
142 
143 #endif
144