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 GetAddrInfoCmd : public FrameworkCommand {
42       public:
43         GetAddrInfoCmd();
~GetAddrInfoCmd()44         virtual ~GetAddrInfoCmd() {}
45         int runCommand(SocketClient* c, int argc, char** argv) override;
46     };
47 
48     /* ------ getaddrinfo ------*/
49     class GetAddrInfoHandler {
50       public:
51         // Note: All of host, service, and hints may be NULL
52         GetAddrInfoHandler(SocketClient* c, char* host, char* service, addrinfo* hints,
53                            const android_net_context& netcontext);
54         ~GetAddrInfoHandler();
55 
56         void run();
57         std::string threadName();
58 
59       private:
60         void doDns64Synthesis(int32_t* rv, addrinfo** res, NetworkDnsEventReported* event);
61 
62         SocketClient* mClient;  // ref counted
63         char* mHost;            // owned. TODO: convert to std::string.
64         char* mService;         // owned. TODO: convert to std::string.
65         addrinfo* mHints;       // owned
66         android_net_context mNetContext;
67     };
68 
69     /* ------ gethostbyname ------*/
70     class GetHostByNameCmd : public FrameworkCommand {
71       public:
72         GetHostByNameCmd();
~GetHostByNameCmd()73         virtual ~GetHostByNameCmd() {}
74         int runCommand(SocketClient* c, int argc, char** argv) override;
75     };
76 
77     class GetHostByNameHandler {
78       public:
79         GetHostByNameHandler(SocketClient* c, char* name, int af,
80                              const android_net_context& netcontext);
81         ~GetHostByNameHandler();
82 
83         void run();
84         std::string threadName();
85 
86       private:
87         void doDns64Synthesis(int32_t* rv, hostent* hbuf, char* buf, size_t buflen, hostent** hpp,
88                               NetworkDnsEventReported* event);
89 
90         SocketClient* mClient;  // ref counted
91         char* mName;            // owned. TODO: convert to std::string.
92         int mAf;
93         android_net_context mNetContext;
94     };
95 
96     /* ------ gethostbyaddr ------*/
97     class GetHostByAddrCmd : public FrameworkCommand {
98       public:
99         GetHostByAddrCmd();
~GetHostByAddrCmd()100         virtual ~GetHostByAddrCmd() {}
101         int runCommand(SocketClient* c, int argc, char** argv) override;
102     };
103 
104     class GetHostByAddrHandler {
105       public:
106         GetHostByAddrHandler(SocketClient* c, void* address, int addressLen, int addressFamily,
107                              const android_net_context& netcontext);
108         ~GetHostByAddrHandler();
109 
110         void run();
111         std::string threadName();
112 
113       private:
114         void doDns64ReverseLookup(hostent* hbuf, char* buf, size_t buflen, hostent** hpp,
115                                   NetworkDnsEventReported* event);
116 
117         SocketClient* mClient;  // ref counted
118         void* mAddress;         // address to lookup; owned
119         int mAddressLen;        // length of address to look up
120         int mAddressFamily;     // address family
121         android_net_context mNetContext;
122     };
123 
124     /* ------ resnsend ------*/
125     class ResNSendCommand : public FrameworkCommand {
126       public:
127         ResNSendCommand();
~ResNSendCommand()128         virtual ~ResNSendCommand() {}
129         int runCommand(SocketClient* c, int argc, char** argv) override;
130     };
131 
132     class ResNSendHandler {
133       public:
134         ResNSendHandler(SocketClient* c, std::string msg, uint32_t flags,
135                         const android_net_context& netcontext);
136         ~ResNSendHandler();
137 
138         void run();
139         std::string threadName();
140 
141       private:
142         SocketClient* mClient;  // ref counted
143         std::string mMsg;
144         uint32_t mFlags;
145         android_net_context mNetContext;
146     };
147 
148     /* ------ getdnsnetid ------*/
149     class GetDnsNetIdCommand : public FrameworkCommand {
150       public:
151         GetDnsNetIdCommand();
~GetDnsNetIdCommand()152         virtual ~GetDnsNetIdCommand() {}
153         int runCommand(SocketClient* c, int argc, char** argv) override;
154     };
155 };
156 
157 }  // namespace net
158 }  // namespace android
159