• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <string>
20 
21 #include <netd_resolv/resolv.h>  // android_net_context
22 #include <sysutils/FrameworkCommand.h>
23 #include <sysutils/FrameworkListener.h>
24 
25 struct addrinfo;
26 struct hostent;
27 
28 namespace android {
29 namespace net {
30 
31 class NetworkDnsEventReported;
32 
33 class DnsProxyListener : public FrameworkListener {
34   public:
35     DnsProxyListener();
~DnsProxyListener()36     virtual ~DnsProxyListener() {}
37 
38     static constexpr const char* SOCKET_NAME = "dnsproxyd";
39 
40   private:
41     class Handler {
42       public:
Handler(SocketClient * c)43         Handler(SocketClient* c) : mClient(c) { mClient->incRef(); }
~Handler()44         virtual ~Handler() { mClient->decRef(); }
45         void operator=(const Handler&) = delete;
46 
47         // Attept to spawn the worker thread, or return an error to the client.
48         // The Handler instance will self-delete in either case.
49         void spawn();
50 
51         virtual void run() = 0;
52         virtual std::string threadName() = 0;
53 
54         SocketClient* mClient;  // ref-counted
55     };
56 
57     /* ------ getaddrinfo ------*/
58     class GetAddrInfoCmd : public FrameworkCommand {
59       public:
60         GetAddrInfoCmd();
~GetAddrInfoCmd()61         virtual ~GetAddrInfoCmd() {}
62         int runCommand(SocketClient* c, int argc, char** argv) override;
63     };
64 
65     class GetAddrInfoHandler : public Handler {
66       public:
67         // Note: All of host, service, and hints may be NULL
68         GetAddrInfoHandler(SocketClient* c, std::string host, std::string service,
69                            std::unique_ptr<addrinfo> hints, const android_net_context& netcontext);
70         ~GetAddrInfoHandler() override = default;
71 
72         void run() override;
73         std::string threadName() override;
74 
75       private:
76         void doDns64Synthesis(int32_t* rv, addrinfo** res, NetworkDnsEventReported* event);
77 
78         std::string mHost;
79         std::string mService;
80         std::unique_ptr<addrinfo> mHints;
81         android_net_context mNetContext;
82     };
83 
84     /* ------ gethostbyname ------*/
85     class GetHostByNameCmd : public FrameworkCommand {
86       public:
87         GetHostByNameCmd();
~GetHostByNameCmd()88         virtual ~GetHostByNameCmd() {}
89         int runCommand(SocketClient* c, int argc, char** argv) override;
90     };
91 
92     class GetHostByNameHandler : public Handler {
93       public:
94         GetHostByNameHandler(SocketClient* c, std::string name, int af,
95                              const android_net_context& netcontext);
96         ~GetHostByNameHandler() override = default;
97 
98         void run() override;
99         std::string threadName() override;
100 
101       private:
102         void doDns64Synthesis(int32_t* rv, hostent* hbuf, char* buf, size_t buflen, hostent** hpp,
103                               NetworkDnsEventReported* event);
104 
105         std::string mName;
106         int mAf;
107         android_net_context mNetContext;
108     };
109 
110     /* ------ gethostbyaddr ------*/
111     class GetHostByAddrCmd : public FrameworkCommand {
112       public:
113         GetHostByAddrCmd();
~GetHostByAddrCmd()114         virtual ~GetHostByAddrCmd() {}
115         int runCommand(SocketClient* c, int argc, char** argv) override;
116     };
117 
118     class GetHostByAddrHandler : public Handler {
119       public:
120         GetHostByAddrHandler(SocketClient* c, in6_addr address, int addressLen, int addressFamily,
121                              const android_net_context& netcontext);
122         ~GetHostByAddrHandler() override = default;
123 
124         void run() override;
125         std::string threadName() override;
126 
127       private:
128         void doDns64ReverseLookup(hostent* hbuf, char* buf, size_t buflen, hostent** hpp,
129                                   NetworkDnsEventReported* event);
130 
131         in6_addr mAddress;
132         int mAddressLen;        // length of address to look up
133         int mAddressFamily;     // address family
134         android_net_context mNetContext;
135     };
136 
137     /* ------ resnsend ------*/
138     class ResNSendCommand : public FrameworkCommand {
139       public:
140         ResNSendCommand();
~ResNSendCommand()141         virtual ~ResNSendCommand() {}
142         int runCommand(SocketClient* c, int argc, char** argv) override;
143     };
144 
145     class ResNSendHandler : public Handler {
146       public:
147         ResNSendHandler(SocketClient* c, std::string msg, uint32_t flags,
148                         const android_net_context& netcontext);
149         ~ResNSendHandler() override = default;
150 
151         void run() override;
152         std::string threadName() override;
153 
154       private:
155         std::string mMsg;
156         uint32_t mFlags;
157         android_net_context mNetContext;
158     };
159 
160     /* ------ getdnsnetid ------*/
161     class GetDnsNetIdCommand : public FrameworkCommand {
162       public:
163         GetDnsNetIdCommand();
~GetDnsNetIdCommand()164         virtual ~GetDnsNetIdCommand() {}
165         int runCommand(SocketClient* c, int argc, char** argv) override;
166     };
167 };
168 
169 }  // namespace net
170 }  // namespace android
171