blob: 87f58c8b04ecfd903fd1cbc0bc234205bf9b662a [file] [log] [blame]
Mike Yu19108d52018-11-15 21:58:19 +08001/*
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
Bernie Innocentie71a28a2019-05-29 00:42:35 +090017#pragma once
Mike Yu19108d52018-11-15 21:58:19 +080018
Luke Huang36796f32019-03-13 02:54:45 +080019#include <string>
20
Bernie Innocentiec4219b2019-01-30 11:16:36 +090021#include <netd_resolv/resolv.h> // android_net_context
Mike Yu19108d52018-11-15 21:58:19 +080022#include <sysutils/FrameworkCommand.h>
23#include <sysutils/FrameworkListener.h>
24
Bernie Innocentie71a28a2019-05-29 00:42:35 +090025struct addrinfo;
26struct hostent;
27
Mike Yu19108d52018-11-15 21:58:19 +080028namespace android {
29namespace net {
30
lifr94981782019-05-17 21:15:19 +080031class NetworkDnsEventReported;
32
Mike Yu19108d52018-11-15 21:58:19 +080033class DnsProxyListener : public FrameworkListener {
34 public:
35 DnsProxyListener();
36 virtual ~DnsProxyListener() {}
37
Mike Yu19108d52018-11-15 21:58:19 +080038 static constexpr const char* SOCKET_NAME = "dnsproxyd";
39
Mike Yu19108d52018-11-15 21:58:19 +080040 private:
Bernie Innocentie2af2262020-10-28 16:53:19 +090041 class Handler {
42 public:
43 Handler(SocketClient* c) : mClient(c) { mClient->incRef(); }
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 ------*/
Mike Yu19108d52018-11-15 21:58:19 +080058 class GetAddrInfoCmd : public FrameworkCommand {
59 public:
60 GetAddrInfoCmd();
61 virtual ~GetAddrInfoCmd() {}
62 int runCommand(SocketClient* c, int argc, char** argv) override;
63 };
64
Bernie Innocentie2af2262020-10-28 16:53:19 +090065 class GetAddrInfoHandler : public Handler {
Mike Yu19108d52018-11-15 21:58:19 +080066 public:
67 // Note: All of host, service, and hints may be NULL
Bernie Innocenti7cbd5f72020-11-27 00:12:10 +090068 GetAddrInfoHandler(SocketClient* c, std::string host, std::string service,
69 std::unique_ptr<addrinfo> hints, const android_net_context& netcontext);
70 ~GetAddrInfoHandler() override = default;
Mike Yu19108d52018-11-15 21:58:19 +080071
Bernie Innocentie2af2262020-10-28 16:53:19 +090072 void run() override;
73 std::string threadName() override;
Mike Yu19108d52018-11-15 21:58:19 +080074
75 private:
lifr94981782019-05-17 21:15:19 +080076 void doDns64Synthesis(int32_t* rv, addrinfo** res, NetworkDnsEventReported* event);
Mike Yu19108d52018-11-15 21:58:19 +080077
Bernie Innocenti7cbd5f72020-11-27 00:12:10 +090078 std::string mHost;
79 std::string mService;
80 std::unique_ptr<addrinfo> mHints;
Mike Yu19108d52018-11-15 21:58:19 +080081 android_net_context mNetContext;
82 };
83
84 /* ------ gethostbyname ------*/
85 class GetHostByNameCmd : public FrameworkCommand {
86 public:
87 GetHostByNameCmd();
88 virtual ~GetHostByNameCmd() {}
89 int runCommand(SocketClient* c, int argc, char** argv) override;
90 };
91
Bernie Innocentie2af2262020-10-28 16:53:19 +090092 class GetHostByNameHandler : public Handler {
Mike Yu19108d52018-11-15 21:58:19 +080093 public:
Bernie Innocenti7cbd5f72020-11-27 00:12:10 +090094 GetHostByNameHandler(SocketClient* c, std::string name, int af,
Mike Yu19108d52018-11-15 21:58:19 +080095 const android_net_context& netcontext);
Bernie Innocenti7cbd5f72020-11-27 00:12:10 +090096 ~GetHostByNameHandler() override = default;
Mike Yu19108d52018-11-15 21:58:19 +080097
Bernie Innocentie2af2262020-10-28 16:53:19 +090098 void run() override;
99 std::string threadName() override;
Mike Yu19108d52018-11-15 21:58:19 +0800100
101 private:
Bernie Innocenti36db7732019-10-11 18:03:02 +0900102 void doDns64Synthesis(int32_t* rv, hostent* hbuf, char* buf, size_t buflen, hostent** hpp,
103 NetworkDnsEventReported* event);
Mike Yu19108d52018-11-15 21:58:19 +0800104
Bernie Innocenti7cbd5f72020-11-27 00:12:10 +0900105 std::string mName;
Mike Yu19108d52018-11-15 21:58:19 +0800106 int mAf;
107 android_net_context mNetContext;
108 };
109
110 /* ------ gethostbyaddr ------*/
111 class GetHostByAddrCmd : public FrameworkCommand {
112 public:
113 GetHostByAddrCmd();
114 virtual ~GetHostByAddrCmd() {}
115 int runCommand(SocketClient* c, int argc, char** argv) override;
116 };
117
Bernie Innocentie2af2262020-10-28 16:53:19 +0900118 class GetHostByAddrHandler : public Handler {
Mike Yu19108d52018-11-15 21:58:19 +0800119 public:
Bernie Innocenti7cbd5f72020-11-27 00:12:10 +0900120 GetHostByAddrHandler(SocketClient* c, in6_addr address, int addressLen, int addressFamily,
Mike Yu19108d52018-11-15 21:58:19 +0800121 const android_net_context& netcontext);
Bernie Innocenti7cbd5f72020-11-27 00:12:10 +0900122 ~GetHostByAddrHandler() override = default;
Mike Yu19108d52018-11-15 21:58:19 +0800123
Bernie Innocentie2af2262020-10-28 16:53:19 +0900124 void run() override;
125 std::string threadName() override;
Mike Yu19108d52018-11-15 21:58:19 +0800126
127 private:
Bernie Innocenti324f9f82019-10-11 18:50:20 +0900128 void doDns64ReverseLookup(hostent* hbuf, char* buf, size_t buflen, hostent** hpp,
129 NetworkDnsEventReported* event);
Mike Yu19108d52018-11-15 21:58:19 +0800130
Bernie Innocenti7cbd5f72020-11-27 00:12:10 +0900131 in6_addr mAddress;
Bernie Innocentiec4219b2019-01-30 11:16:36 +0900132 int mAddressLen; // length of address to look up
133 int mAddressFamily; // address family
Mike Yu19108d52018-11-15 21:58:19 +0800134 android_net_context mNetContext;
135 };
136
137 /* ------ resnsend ------*/
138 class ResNSendCommand : public FrameworkCommand {
139 public:
140 ResNSendCommand();
Luke Huang9807e6b2019-05-20 16:17:12 +0800141 virtual ~ResNSendCommand() {}
Mike Yu19108d52018-11-15 21:58:19 +0800142 int runCommand(SocketClient* c, int argc, char** argv) override;
143 };
144
Bernie Innocentie2af2262020-10-28 16:53:19 +0900145 class ResNSendHandler : public Handler {
Mike Yu19108d52018-11-15 21:58:19 +0800146 public:
147 ResNSendHandler(SocketClient* c, std::string msg, uint32_t flags,
148 const android_net_context& netcontext);
Bernie Innocentie2af2262020-10-28 16:53:19 +0900149 ~ResNSendHandler() override = default;
Mike Yu19108d52018-11-15 21:58:19 +0800150
Bernie Innocentie2af2262020-10-28 16:53:19 +0900151 void run() override;
152 std::string threadName() override;
Mike Yu19108d52018-11-15 21:58:19 +0800153
154 private:
Mike Yu19108d52018-11-15 21:58:19 +0800155 std::string mMsg;
156 uint32_t mFlags;
157 android_net_context mNetContext;
158 };
Luke Huang9807e6b2019-05-20 16:17:12 +0800159
160 /* ------ getdnsnetid ------*/
161 class GetDnsNetIdCommand : public FrameworkCommand {
162 public:
163 GetDnsNetIdCommand();
164 virtual ~GetDnsNetIdCommand() {}
165 int runCommand(SocketClient* c, int argc, char** argv) override;
166 };
Mike Yu19108d52018-11-15 21:58:19 +0800167};
168
169} // namespace net
170} // namespace android