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/IDnsEventListener.h"
25 #include "NetdCommand.h"
26 
27 class NetworkController;
28 
29 class DnsProxyListener : public FrameworkListener {
30 public:
31     explicit DnsProxyListener(const NetworkController* netCtrl);
~DnsProxyListener()32     virtual ~DnsProxyListener() {}
33 
34     // Returns the binder reference to the DNS listener service, attempting to fetch it if we do not
35     // have it already. This method mutates internal state without taking a lock and must only be
36     // called on one thread. This is safe because we only call this in the runCommand methods of our
37     // commands, which are only called by FrameworkListener::onDataAvailable, which is only called
38     // from SocketListener::runListener, which is a single-threaded select loop.
39     android::sp<android::net::metrics::IDnsEventListener> getDnsEventListener();
40 
41 private:
42     const NetworkController *mNetCtrl;
43     android::sp<android::net::metrics::IDnsEventListener> mDnsEventListener;
44 
45     class GetAddrInfoCmd : public NetdCommand {
46     public:
47         GetAddrInfoCmd(DnsProxyListener* dnsProxyListener);
~GetAddrInfoCmd()48         virtual ~GetAddrInfoCmd() {}
49         int runCommand(SocketClient *c, int argc, char** argv);
50     private:
51         DnsProxyListener* mDnsProxyListener;
52     };
53 
54     class GetAddrInfoHandler {
55     public:
56         // Note: All of host, service, and hints may be NULL
57         GetAddrInfoHandler(SocketClient *c,
58                            char* host,
59                            char* service,
60                            struct addrinfo* hints,
61                            const struct android_net_context& netcontext,
62                            const android::sp<android::net::metrics::IDnsEventListener>& listener);
63         ~GetAddrInfoHandler();
64 
65         static void* threadStart(void* handler);
66         void start();
67 
68     private:
69         void run();
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         android::sp<android::net::metrics::IDnsEventListener> mDnsEventListener;
76     };
77 
78     /* ------ gethostbyname ------*/
79     class GetHostByNameCmd : public NetdCommand {
80     public:
81         GetHostByNameCmd(DnsProxyListener* dnsProxyListener);
~GetHostByNameCmd()82         virtual ~GetHostByNameCmd() {}
83         int runCommand(SocketClient *c, int argc, char** argv);
84     private:
85         DnsProxyListener* mDnsProxyListener;
86     };
87 
88     class GetHostByNameHandler {
89     public:
90         GetHostByNameHandler(SocketClient *c,
91                             char *name,
92                             int af,
93                             unsigned netId,
94                             uint32_t mark,
95                             const android::sp<android::net::metrics::IDnsEventListener>& listener);
96         ~GetHostByNameHandler();
97         static void* threadStart(void* handler);
98         void start();
99     private:
100         void run();
101         SocketClient* mClient; //ref counted
102         char* mName; // owned
103         int mAf;
104         unsigned mNetId;
105         uint32_t mMark;
106         android::sp<android::net::metrics::IDnsEventListener> mDnsEventListener;
107     };
108 
109     /* ------ gethostbyaddr ------*/
110     class GetHostByAddrCmd : public NetdCommand {
111     public:
112         GetHostByAddrCmd(const DnsProxyListener* dnsProxyListener);
~GetHostByAddrCmd()113         virtual ~GetHostByAddrCmd() {}
114         int runCommand(SocketClient *c, int argc, char** argv);
115     private:
116         const DnsProxyListener* mDnsProxyListener;
117     };
118 
119     class GetHostByAddrHandler {
120     public:
121         GetHostByAddrHandler(SocketClient *c,
122                             void* address,
123                             int addressLen,
124                             int addressFamily,
125                             unsigned netId,
126                             uint32_t mark);
127         ~GetHostByAddrHandler();
128 
129         static void* threadStart(void* handler);
130         void start();
131 
132     private:
133         void run();
134         SocketClient* mClient;  // ref counted
135         void* mAddress;    // address to lookup; owned
136         int mAddressLen; // length of address to look up
137         int mAddressFamily;  // address family
138         unsigned mNetId;
139         uint32_t mMark;
140     };
141 };
142 
143 #endif
144