Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 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 | |
Bernie Innocenti | e71a28a | 2019-05-29 00:42:35 +0900 | [diff] [blame] | 17 | #pragma once |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 18 | |
Luke Huang | 36796f3 | 2019-03-13 02:54:45 +0800 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 21 | #include <netd_resolv/resolv.h> // android_net_context |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 22 | #include <sysutils/FrameworkCommand.h> |
| 23 | #include <sysutils/FrameworkListener.h> |
| 24 | |
Bernie Innocenti | e71a28a | 2019-05-29 00:42:35 +0900 | [diff] [blame] | 25 | struct addrinfo; |
| 26 | struct hostent; |
| 27 | |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace net { |
| 30 | |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 31 | class NetworkDnsEventReported; |
| 32 | |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 33 | class DnsProxyListener : public FrameworkListener { |
| 34 | public: |
| 35 | DnsProxyListener(); |
| 36 | virtual ~DnsProxyListener() {} |
| 37 | |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 38 | static constexpr const char* SOCKET_NAME = "dnsproxyd"; |
| 39 | |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 40 | private: |
Bernie Innocenti | e2af226 | 2020-10-28 16:53:19 +0900 | [diff] [blame] | 41 | 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 Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 58 | class GetAddrInfoCmd : public FrameworkCommand { |
| 59 | public: |
| 60 | GetAddrInfoCmd(); |
| 61 | virtual ~GetAddrInfoCmd() {} |
| 62 | int runCommand(SocketClient* c, int argc, char** argv) override; |
| 63 | }; |
| 64 | |
Bernie Innocenti | e2af226 | 2020-10-28 16:53:19 +0900 | [diff] [blame] | 65 | class GetAddrInfoHandler : public Handler { |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 66 | public: |
| 67 | // Note: All of host, service, and hints may be NULL |
Bernie Innocenti | 7cbd5f7 | 2020-11-27 00:12:10 +0900 | [diff] [blame] | 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; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 71 | |
Bernie Innocenti | e2af226 | 2020-10-28 16:53:19 +0900 | [diff] [blame] | 72 | void run() override; |
| 73 | std::string threadName() override; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 74 | |
| 75 | private: |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 76 | void doDns64Synthesis(int32_t* rv, addrinfo** res, NetworkDnsEventReported* event); |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 77 | |
Bernie Innocenti | 7cbd5f7 | 2020-11-27 00:12:10 +0900 | [diff] [blame] | 78 | std::string mHost; |
| 79 | std::string mService; |
| 80 | std::unique_ptr<addrinfo> mHints; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 81 | 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 Innocenti | e2af226 | 2020-10-28 16:53:19 +0900 | [diff] [blame] | 92 | class GetHostByNameHandler : public Handler { |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 93 | public: |
Bernie Innocenti | 7cbd5f7 | 2020-11-27 00:12:10 +0900 | [diff] [blame] | 94 | GetHostByNameHandler(SocketClient* c, std::string name, int af, |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 95 | const android_net_context& netcontext); |
Bernie Innocenti | 7cbd5f7 | 2020-11-27 00:12:10 +0900 | [diff] [blame] | 96 | ~GetHostByNameHandler() override = default; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 97 | |
Bernie Innocenti | e2af226 | 2020-10-28 16:53:19 +0900 | [diff] [blame] | 98 | void run() override; |
| 99 | std::string threadName() override; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 100 | |
| 101 | private: |
Bernie Innocenti | 36db773 | 2019-10-11 18:03:02 +0900 | [diff] [blame] | 102 | void doDns64Synthesis(int32_t* rv, hostent* hbuf, char* buf, size_t buflen, hostent** hpp, |
| 103 | NetworkDnsEventReported* event); |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 104 | |
Bernie Innocenti | 7cbd5f7 | 2020-11-27 00:12:10 +0900 | [diff] [blame] | 105 | std::string mName; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 106 | 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 Innocenti | e2af226 | 2020-10-28 16:53:19 +0900 | [diff] [blame] | 118 | class GetHostByAddrHandler : public Handler { |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 119 | public: |
Bernie Innocenti | 7cbd5f7 | 2020-11-27 00:12:10 +0900 | [diff] [blame] | 120 | GetHostByAddrHandler(SocketClient* c, in6_addr address, int addressLen, int addressFamily, |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 121 | const android_net_context& netcontext); |
Bernie Innocenti | 7cbd5f7 | 2020-11-27 00:12:10 +0900 | [diff] [blame] | 122 | ~GetHostByAddrHandler() override = default; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 123 | |
Bernie Innocenti | e2af226 | 2020-10-28 16:53:19 +0900 | [diff] [blame] | 124 | void run() override; |
| 125 | std::string threadName() override; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 126 | |
| 127 | private: |
Bernie Innocenti | 324f9f8 | 2019-10-11 18:50:20 +0900 | [diff] [blame] | 128 | void doDns64ReverseLookup(hostent* hbuf, char* buf, size_t buflen, hostent** hpp, |
| 129 | NetworkDnsEventReported* event); |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 130 | |
Bernie Innocenti | 7cbd5f7 | 2020-11-27 00:12:10 +0900 | [diff] [blame] | 131 | in6_addr mAddress; |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 132 | int mAddressLen; // length of address to look up |
| 133 | int mAddressFamily; // address family |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 134 | android_net_context mNetContext; |
| 135 | }; |
| 136 | |
| 137 | /* ------ resnsend ------*/ |
| 138 | class ResNSendCommand : public FrameworkCommand { |
| 139 | public: |
| 140 | ResNSendCommand(); |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 141 | virtual ~ResNSendCommand() {} |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 142 | int runCommand(SocketClient* c, int argc, char** argv) override; |
| 143 | }; |
| 144 | |
Bernie Innocenti | e2af226 | 2020-10-28 16:53:19 +0900 | [diff] [blame] | 145 | class ResNSendHandler : public Handler { |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 146 | public: |
| 147 | ResNSendHandler(SocketClient* c, std::string msg, uint32_t flags, |
| 148 | const android_net_context& netcontext); |
Bernie Innocenti | e2af226 | 2020-10-28 16:53:19 +0900 | [diff] [blame] | 149 | ~ResNSendHandler() override = default; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 150 | |
Bernie Innocenti | e2af226 | 2020-10-28 16:53:19 +0900 | [diff] [blame] | 151 | void run() override; |
| 152 | std::string threadName() override; |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 153 | |
| 154 | private: |
Mike Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 155 | std::string mMsg; |
| 156 | uint32_t mFlags; |
| 157 | android_net_context mNetContext; |
| 158 | }; |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 159 | |
| 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 Yu | 19108d5 | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | } // namespace net |
| 170 | } // namespace android |