Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | package android.net; |
| 18 | |
Luke Huang | c211b27 | 2019-05-29 01:30:07 +0800 | [diff] [blame] | 19 | import static android.net.NetworkUtils.getDnsNetwork; |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 20 | import static android.net.NetworkUtils.resNetworkCancel; |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 21 | import static android.net.NetworkUtils.resNetworkQuery; |
| 22 | import static android.net.NetworkUtils.resNetworkResult; |
| 23 | import static android.net.NetworkUtils.resNetworkSend; |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 24 | import static android.net.util.DnsUtils.haveIpv4; |
| 25 | import static android.net.util.DnsUtils.haveIpv6; |
| 26 | import static android.net.util.DnsUtils.rfc6724Sort; |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 27 | import static android.os.MessageQueue.OnFileDescriptorEventListener.EVENT_ERROR; |
| 28 | import static android.os.MessageQueue.OnFileDescriptorEventListener.EVENT_INPUT; |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 29 | import static android.system.OsConstants.ENONET; |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 30 | |
Luke Huang | e4c7913 | 2019-03-07 19:01:26 +0800 | [diff] [blame] | 31 | import android.annotation.CallbackExecutor; |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 32 | import android.annotation.IntDef; |
| 33 | import android.annotation.NonNull; |
| 34 | import android.annotation.Nullable; |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 35 | import android.os.CancellationSignal; |
Luke Huang | e4c7913 | 2019-03-07 19:01:26 +0800 | [diff] [blame] | 36 | import android.os.Looper; |
Luke Huang | 5b31126 | 2019-06-11 14:25:45 +0800 | [diff] [blame] | 37 | import android.os.MessageQueue; |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 38 | import android.system.ErrnoException; |
| 39 | import android.util.Log; |
| 40 | |
| 41 | import java.io.FileDescriptor; |
| 42 | import java.lang.annotation.Retention; |
| 43 | import java.lang.annotation.RetentionPolicy; |
| 44 | import java.net.InetAddress; |
| 45 | import java.net.UnknownHostException; |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.List; |
Luke Huang | e4c7913 | 2019-03-07 19:01:26 +0800 | [diff] [blame] | 48 | import java.util.concurrent.Executor; |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Dns resolver class for asynchronous dns querying |
| 52 | * |
Luke Huang | 33bfef3 | 2019-01-23 21:53:13 +0800 | [diff] [blame] | 53 | * Note that if a client sends a query with more than 1 record in the question section but |
| 54 | * the remote dns server does not support this, it may not respond at all, leading to a timeout. |
| 55 | * |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 56 | */ |
| 57 | public final class DnsResolver { |
| 58 | private static final String TAG = "DnsResolver"; |
| 59 | private static final int FD_EVENTS = EVENT_INPUT | EVENT_ERROR; |
| 60 | private static final int MAXPACKET = 8 * 1024; |
Luke Huang | 5bf7aa2 | 2019-03-29 18:01:11 +0800 | [diff] [blame] | 61 | private static final int SLEEP_TIME_MS = 2; |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 62 | |
| 63 | @IntDef(prefix = { "CLASS_" }, value = { |
| 64 | CLASS_IN |
| 65 | }) |
| 66 | @Retention(RetentionPolicy.SOURCE) |
| 67 | @interface QueryClass {} |
| 68 | public static final int CLASS_IN = 1; |
| 69 | |
| 70 | @IntDef(prefix = { "TYPE_" }, value = { |
| 71 | TYPE_A, |
| 72 | TYPE_AAAA |
| 73 | }) |
| 74 | @Retention(RetentionPolicy.SOURCE) |
| 75 | @interface QueryType {} |
| 76 | public static final int TYPE_A = 1; |
| 77 | public static final int TYPE_AAAA = 28; |
| 78 | |
| 79 | @IntDef(prefix = { "FLAG_" }, value = { |
| 80 | FLAG_EMPTY, |
| 81 | FLAG_NO_RETRY, |
| 82 | FLAG_NO_CACHE_STORE, |
| 83 | FLAG_NO_CACHE_LOOKUP |
| 84 | }) |
| 85 | @Retention(RetentionPolicy.SOURCE) |
| 86 | @interface QueryFlag {} |
| 87 | public static final int FLAG_EMPTY = 0; |
| 88 | public static final int FLAG_NO_RETRY = 1 << 0; |
| 89 | public static final int FLAG_NO_CACHE_STORE = 1 << 1; |
| 90 | public static final int FLAG_NO_CACHE_LOOKUP = 1 << 2; |
| 91 | |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 92 | @IntDef(prefix = { "ERROR_" }, value = { |
| 93 | ERROR_PARSE, |
| 94 | ERROR_SYSTEM |
| 95 | }) |
| 96 | @Retention(RetentionPolicy.SOURCE) |
| 97 | @interface DnsError {} |
| 98 | /** |
| 99 | * Indicates that there was an error parsing the response the query. |
| 100 | * The cause of this error is available via getCause() and is a ParseException. |
| 101 | */ |
| 102 | public static final int ERROR_PARSE = 0; |
| 103 | /** |
| 104 | * Indicates that there was an error sending the query. |
| 105 | * The cause of this error is available via getCause() and is an ErrnoException. |
| 106 | */ |
| 107 | public static final int ERROR_SYSTEM = 1; |
| 108 | |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 109 | private static final int NETID_UNSET = 0; |
| 110 | |
| 111 | private static final DnsResolver sInstance = new DnsResolver(); |
| 112 | |
| 113 | /** |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 114 | * Get instance for DnsResolver |
| 115 | */ |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 116 | public static @NonNull DnsResolver getInstance() { |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 117 | return sInstance; |
| 118 | } |
| 119 | |
| 120 | private DnsResolver() {} |
| 121 | |
| 122 | /** |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 123 | * Base interface for answer callbacks |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 124 | * |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 125 | * @param <T> The type of the answer |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 126 | */ |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 127 | public interface Callback<T> { |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 128 | /** |
| 129 | * Success response to |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 130 | * {@link android.net.DnsResolver#query query()} or |
| 131 | * {@link android.net.DnsResolver#rawQuery rawQuery()}. |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 132 | * |
| 133 | * Invoked when the answer to a query was successfully parsed. |
| 134 | * |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 135 | * @param answer <T> answer to the query. |
| 136 | * @param rcode The response code in the DNS response. |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 137 | * |
| 138 | * {@see android.net.DnsResolver#query query()} |
| 139 | */ |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 140 | void onAnswer(@NonNull T answer, int rcode); |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 141 | /** |
| 142 | * Error response to |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 143 | * {@link android.net.DnsResolver#query query()} or |
| 144 | * {@link android.net.DnsResolver#rawQuery rawQuery()}. |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 145 | * |
| 146 | * Invoked when there is no valid answer to |
| 147 | * {@link android.net.DnsResolver#query query()} |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 148 | * {@link android.net.DnsResolver#rawQuery rawQuery()}. |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 149 | * |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 150 | * @param error a {@link DnsException} object with additional |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 151 | * detail regarding the failure |
| 152 | */ |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 153 | void onError(@NonNull DnsException error); |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 157 | * Class to represent DNS error |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 158 | */ |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 159 | public static class DnsException extends Exception { |
| 160 | /** |
| 161 | * DNS error code as one of the ERROR_* constants |
| 162 | */ |
| 163 | @DnsError public final int code; |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 164 | |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 165 | DnsException(@DnsError int code, @Nullable Throwable cause) { |
| 166 | super(cause); |
| 167 | this.code = code; |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
Luke Huang | e4c7913 | 2019-03-07 19:01:26 +0800 | [diff] [blame] | 172 | * Send a raw DNS query. |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 173 | * The answer will be provided asynchronously through the provided {@link Callback}. |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 174 | * |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 175 | * @param network {@link Network} specifying which network to query on. |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 176 | * {@code null} for query on default network. |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 177 | * @param query blob message to query |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 178 | * @param flags flags as a combination of the FLAGS_* constants |
Luke Huang | e4c7913 | 2019-03-07 19:01:26 +0800 | [diff] [blame] | 179 | * @param executor The {@link Executor} that the callback should be executed on. |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 180 | * @param cancellationSignal used by the caller to signal if the query should be |
| 181 | * cancelled. May be {@code null}. |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 182 | * @param callback a {@link Callback} which will be called to notify the caller |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 183 | * of the result of dns query. |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 184 | */ |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 185 | public void rawQuery(@Nullable Network network, @NonNull byte[] query, @QueryFlag int flags, |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 186 | @NonNull @CallbackExecutor Executor executor, |
| 187 | @Nullable CancellationSignal cancellationSignal, |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 188 | @NonNull Callback<? super byte[]> callback) { |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 189 | if (cancellationSignal != null && cancellationSignal.isCanceled()) { |
| 190 | return; |
| 191 | } |
Luke Huang | 5386f49 | 2019-03-26 15:50:10 +0800 | [diff] [blame] | 192 | final Object lock = new Object(); |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 193 | final FileDescriptor queryfd; |
| 194 | try { |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 195 | queryfd = resNetworkSend((network != null) |
| 196 | ? network.getNetIdForResolv() : NETID_UNSET, query, query.length, flags); |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 197 | } catch (ErrnoException e) { |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 198 | executor.execute(() -> callback.onError(new DnsException(ERROR_SYSTEM, e))); |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | |
Luke Huang | 5bf7aa2 | 2019-03-29 18:01:11 +0800 | [diff] [blame] | 202 | synchronized (lock) { |
| 203 | registerFDListener(executor, queryfd, callback, cancellationSignal, lock); |
| 204 | if (cancellationSignal == null) return; |
| 205 | addCancellationSignal(cancellationSignal, queryfd, lock); |
| 206 | } |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Luke Huang | e4c7913 | 2019-03-07 19:01:26 +0800 | [diff] [blame] | 210 | * Send a DNS query with the specified name, class and query type. |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 211 | * The answer will be provided asynchronously through the provided {@link Callback}. |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 212 | * |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 213 | * @param network {@link Network} specifying which network to query on. |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 214 | * {@code null} for query on default network. |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 215 | * @param domain domain name to query |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 216 | * @param nsClass dns class as one of the CLASS_* constants |
| 217 | * @param nsType dns resource record (RR) type as one of the TYPE_* constants |
| 218 | * @param flags flags as a combination of the FLAGS_* constants |
Luke Huang | e4c7913 | 2019-03-07 19:01:26 +0800 | [diff] [blame] | 219 | * @param executor The {@link Executor} that the callback should be executed on. |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 220 | * @param cancellationSignal used by the caller to signal if the query should be |
| 221 | * cancelled. May be {@code null}. |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 222 | * @param callback a {@link Callback} which will be called to notify the caller |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 223 | * of the result of dns query. |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 224 | */ |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 225 | public void rawQuery(@Nullable Network network, @NonNull String domain, |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 226 | @QueryClass int nsClass, @QueryType int nsType, @QueryFlag int flags, |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 227 | @NonNull @CallbackExecutor Executor executor, |
| 228 | @Nullable CancellationSignal cancellationSignal, |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 229 | @NonNull Callback<? super byte[]> callback) { |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 230 | if (cancellationSignal != null && cancellationSignal.isCanceled()) { |
| 231 | return; |
| 232 | } |
Luke Huang | 5386f49 | 2019-03-26 15:50:10 +0800 | [diff] [blame] | 233 | final Object lock = new Object(); |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 234 | final FileDescriptor queryfd; |
| 235 | try { |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 236 | queryfd = resNetworkQuery((network != null) |
| 237 | ? network.getNetIdForResolv() : NETID_UNSET, domain, nsClass, nsType, flags); |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 238 | } catch (ErrnoException e) { |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 239 | executor.execute(() -> callback.onError(new DnsException(ERROR_SYSTEM, e))); |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 240 | return; |
| 241 | } |
Luke Huang | 5bf7aa2 | 2019-03-29 18:01:11 +0800 | [diff] [blame] | 242 | synchronized (lock) { |
| 243 | registerFDListener(executor, queryfd, callback, cancellationSignal, lock); |
| 244 | if (cancellationSignal == null) return; |
| 245 | addCancellationSignal(cancellationSignal, queryfd, lock); |
| 246 | } |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 247 | } |
| 248 | |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 249 | private class InetAddressAnswerAccumulator implements Callback<byte[]> { |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 250 | private final List<InetAddress> mAllAnswers; |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 251 | private final Network mNetwork; |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 252 | private int mRcode; |
| 253 | private DnsException mDnsException; |
| 254 | private final Callback<? super List<InetAddress>> mUserCallback; |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 255 | private final int mTargetAnswerCount; |
| 256 | private int mReceivedAnswerCount = 0; |
| 257 | |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 258 | InetAddressAnswerAccumulator(@NonNull Network network, int size, |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 259 | @NonNull Callback<? super List<InetAddress>> callback) { |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 260 | mNetwork = network; |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 261 | mTargetAnswerCount = size; |
| 262 | mAllAnswers = new ArrayList<>(); |
| 263 | mUserCallback = callback; |
| 264 | } |
| 265 | |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 266 | private boolean maybeReportError() { |
| 267 | if (mRcode != 0) { |
| 268 | mUserCallback.onAnswer(mAllAnswers, mRcode); |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 269 | return true; |
| 270 | } |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 271 | if (mDnsException != null) { |
| 272 | mUserCallback.onError(mDnsException); |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | private void maybeReportAnswer() { |
| 279 | if (++mReceivedAnswerCount != mTargetAnswerCount) return; |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 280 | if (mAllAnswers.isEmpty() && maybeReportError()) return; |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 281 | mUserCallback.onAnswer(rfc6724Sort(mNetwork, mAllAnswers), mRcode); |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | @Override |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 285 | public void onAnswer(@NonNull byte[] answer, int rcode) { |
| 286 | // If at least one query succeeded, return an rcode of 0. |
| 287 | // Otherwise, arbitrarily return the first rcode received. |
| 288 | if (mReceivedAnswerCount == 0 || rcode == 0) { |
| 289 | mRcode = rcode; |
| 290 | } |
| 291 | try { |
| 292 | mAllAnswers.addAll(new DnsAddressAnswer(answer).getAddresses()); |
| 293 | } catch (ParseException e) { |
| 294 | mDnsException = new DnsException(ERROR_PARSE, e); |
| 295 | } |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 296 | maybeReportAnswer(); |
| 297 | } |
| 298 | |
| 299 | @Override |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 300 | public void onError(@NonNull DnsException error) { |
| 301 | mDnsException = error; |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 302 | maybeReportAnswer(); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 307 | * Send a DNS query with the specified name on a network with both IPv4 and IPv6, |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 308 | * get back a set of InetAddresses with rfc6724 sorting style asynchronously. |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 309 | * |
| 310 | * This method will examine the connection ability on given network, and query IPv4 |
| 311 | * and IPv6 if connection is available. |
| 312 | * |
| 313 | * If at least one query succeeded with valid answer, rcode will be 0 |
| 314 | * |
| 315 | * The answer will be provided asynchronously through the provided {@link Callback}. |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 316 | * |
| 317 | * @param network {@link Network} specifying which network to query on. |
| 318 | * {@code null} for query on default network. |
| 319 | * @param domain domain name to query |
| 320 | * @param flags flags as a combination of the FLAGS_* constants |
| 321 | * @param executor The {@link Executor} that the callback should be executed on. |
| 322 | * @param cancellationSignal used by the caller to signal if the query should be |
| 323 | * cancelled. May be {@code null}. |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 324 | * @param callback a {@link Callback} which will be called to notify the |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 325 | * caller of the result of dns query. |
| 326 | */ |
| 327 | public void query(@Nullable Network network, @NonNull String domain, @QueryFlag int flags, |
| 328 | @NonNull @CallbackExecutor Executor executor, |
| 329 | @Nullable CancellationSignal cancellationSignal, |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 330 | @NonNull Callback<? super List<InetAddress>> callback) { |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 331 | if (cancellationSignal != null && cancellationSignal.isCanceled()) { |
| 332 | return; |
| 333 | } |
| 334 | final Object lock = new Object(); |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 335 | final Network queryNetwork; |
| 336 | try { |
Luke Huang | c211b27 | 2019-05-29 01:30:07 +0800 | [diff] [blame] | 337 | queryNetwork = (network != null) ? network : getDnsNetwork(); |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 338 | } catch (ErrnoException e) { |
| 339 | executor.execute(() -> callback.onError(new DnsException(ERROR_SYSTEM, e))); |
| 340 | return; |
| 341 | } |
| 342 | final boolean queryIpv6 = haveIpv6(queryNetwork); |
| 343 | final boolean queryIpv4 = haveIpv4(queryNetwork); |
| 344 | |
| 345 | // This can only happen if queryIpv4 and queryIpv6 are both false. |
| 346 | // This almost certainly means that queryNetwork does not exist or no longer exists. |
| 347 | if (!queryIpv6 && !queryIpv4) { |
| 348 | executor.execute(() -> callback.onError( |
| 349 | new DnsException(ERROR_SYSTEM, new ErrnoException("resNetworkQuery", ENONET)))); |
| 350 | return; |
| 351 | } |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 352 | |
| 353 | final FileDescriptor v4fd; |
| 354 | final FileDescriptor v6fd; |
| 355 | |
| 356 | int queryCount = 0; |
| 357 | |
| 358 | if (queryIpv6) { |
| 359 | try { |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 360 | v6fd = resNetworkQuery(queryNetwork.getNetIdForResolv(), domain, CLASS_IN, |
| 361 | TYPE_AAAA, flags); |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 362 | } catch (ErrnoException e) { |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 363 | executor.execute(() -> callback.onError(new DnsException(ERROR_SYSTEM, e))); |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | queryCount++; |
| 367 | } else v6fd = null; |
| 368 | |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 369 | // Avoiding gateways drop packets if queries are sent too close together |
| 370 | try { |
Luke Huang | 5bf7aa2 | 2019-03-29 18:01:11 +0800 | [diff] [blame] | 371 | Thread.sleep(SLEEP_TIME_MS); |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 372 | } catch (InterruptedException ex) { |
| 373 | Thread.currentThread().interrupt(); |
| 374 | } |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 375 | |
| 376 | if (queryIpv4) { |
| 377 | try { |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 378 | v4fd = resNetworkQuery(queryNetwork.getNetIdForResolv(), domain, CLASS_IN, TYPE_A, |
| 379 | flags); |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 380 | } catch (ErrnoException e) { |
| 381 | if (queryIpv6) resNetworkCancel(v6fd); // Closes fd, marks it invalid. |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 382 | executor.execute(() -> callback.onError(new DnsException(ERROR_SYSTEM, e))); |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | queryCount++; |
| 386 | } else v4fd = null; |
| 387 | |
| 388 | final InetAddressAnswerAccumulator accumulator = |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 389 | new InetAddressAnswerAccumulator(queryNetwork, queryCount, callback); |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 390 | |
Luke Huang | 5bf7aa2 | 2019-03-29 18:01:11 +0800 | [diff] [blame] | 391 | synchronized (lock) { |
| 392 | if (queryIpv6) { |
| 393 | registerFDListener(executor, v6fd, accumulator, cancellationSignal, lock); |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 394 | } |
Luke Huang | 5bf7aa2 | 2019-03-29 18:01:11 +0800 | [diff] [blame] | 395 | if (queryIpv4) { |
| 396 | registerFDListener(executor, v4fd, accumulator, cancellationSignal, lock); |
| 397 | } |
| 398 | if (cancellationSignal == null) return; |
| 399 | cancellationSignal.setOnCancelListener(() -> { |
| 400 | synchronized (lock) { |
| 401 | if (queryIpv4) cancelQuery(v4fd); |
| 402 | if (queryIpv6) cancelQuery(v6fd); |
| 403 | } |
| 404 | }); |
| 405 | } |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 406 | } |
| 407 | |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 408 | /** |
| 409 | * Send a DNS query with the specified name and query type, get back a set of |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 410 | * InetAddresses with rfc6724 sorting style asynchronously. |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 411 | * |
| 412 | * The answer will be provided asynchronously through the provided {@link Callback}. |
| 413 | * |
| 414 | * @param network {@link Network} specifying which network to query on. |
| 415 | * {@code null} for query on default network. |
| 416 | * @param domain domain name to query |
| 417 | * @param nsType dns resource record (RR) type as one of the TYPE_* constants |
| 418 | * @param flags flags as a combination of the FLAGS_* constants |
| 419 | * @param executor The {@link Executor} that the callback should be executed on. |
| 420 | * @param cancellationSignal used by the caller to signal if the query should be |
| 421 | * cancelled. May be {@code null}. |
| 422 | * @param callback a {@link Callback} which will be called to notify the caller |
| 423 | * of the result of dns query. |
| 424 | */ |
| 425 | public void query(@Nullable Network network, @NonNull String domain, |
| 426 | @QueryType int nsType, @QueryFlag int flags, |
| 427 | @NonNull @CallbackExecutor Executor executor, |
| 428 | @Nullable CancellationSignal cancellationSignal, |
| 429 | @NonNull Callback<? super List<InetAddress>> callback) { |
| 430 | if (cancellationSignal != null && cancellationSignal.isCanceled()) { |
| 431 | return; |
| 432 | } |
| 433 | final Object lock = new Object(); |
| 434 | final FileDescriptor queryfd; |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 435 | final Network queryNetwork; |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 436 | try { |
Luke Huang | c211b27 | 2019-05-29 01:30:07 +0800 | [diff] [blame] | 437 | queryNetwork = (network != null) ? network : getDnsNetwork(); |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 438 | queryfd = resNetworkQuery(queryNetwork.getNetIdForResolv(), domain, CLASS_IN, nsType, |
| 439 | flags); |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 440 | } catch (ErrnoException e) { |
| 441 | executor.execute(() -> callback.onError(new DnsException(ERROR_SYSTEM, e))); |
| 442 | return; |
| 443 | } |
| 444 | final InetAddressAnswerAccumulator accumulator = |
Luke Huang | 247d83a | 2019-04-17 22:27:56 +0800 | [diff] [blame] | 445 | new InetAddressAnswerAccumulator(queryNetwork, 1, callback); |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 446 | synchronized (lock) { |
| 447 | registerFDListener(executor, queryfd, accumulator, cancellationSignal, lock); |
| 448 | if (cancellationSignal == null) return; |
| 449 | addCancellationSignal(cancellationSignal, queryfd, lock); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Class to retrieve DNS response |
| 455 | * |
| 456 | * @hide |
| 457 | */ |
| 458 | public static final class DnsResponse { |
| 459 | public final @NonNull byte[] answerbuf; |
| 460 | public final int rcode; |
| 461 | public DnsResponse(@NonNull byte[] answerbuf, int rcode) { |
| 462 | this.answerbuf = answerbuf; |
| 463 | this.rcode = rcode; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | private void registerFDListener(@NonNull Executor executor, |
| 468 | @NonNull FileDescriptor queryfd, @NonNull Callback<? super byte[]> answerCallback, |
Luke Huang | 5386f49 | 2019-03-26 15:50:10 +0800 | [diff] [blame] | 469 | @Nullable CancellationSignal cancellationSignal, @NonNull Object lock) { |
Luke Huang | 5b31126 | 2019-06-11 14:25:45 +0800 | [diff] [blame] | 470 | final MessageQueue mainThreadMessageQueue = Looper.getMainLooper().getQueue(); |
| 471 | mainThreadMessageQueue.addOnFileDescriptorEventListener( |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 472 | queryfd, |
| 473 | FD_EVENTS, |
| 474 | (fd, events) -> { |
Luke Huang | 5b31126 | 2019-06-11 14:25:45 +0800 | [diff] [blame] | 475 | // b/134310704 |
| 476 | // Unregister fd event listener before resNetworkResult is called to prevent |
| 477 | // race condition caused by fd reused. |
| 478 | // For example when querying v4 and v6, it's possible that the first query ends |
| 479 | // and the fd is closed before the second request starts, which might return |
| 480 | // the same fd for the second request. By that time, the looper must have |
| 481 | // unregistered the fd, otherwise another event listener can't be registered. |
| 482 | mainThreadMessageQueue.removeOnFileDescriptorEventListener(fd); |
| 483 | |
Luke Huang | e4c7913 | 2019-03-07 19:01:26 +0800 | [diff] [blame] | 484 | executor.execute(() -> { |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 485 | DnsResponse resp = null; |
| 486 | ErrnoException exception = null; |
Luke Huang | 5386f49 | 2019-03-26 15:50:10 +0800 | [diff] [blame] | 487 | synchronized (lock) { |
| 488 | if (cancellationSignal != null && cancellationSignal.isCanceled()) { |
| 489 | return; |
| 490 | } |
Luke Huang | 5386f49 | 2019-03-26 15:50:10 +0800 | [diff] [blame] | 491 | try { |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 492 | resp = resNetworkResult(fd); // Closes fd, marks it invalid. |
Luke Huang | 5386f49 | 2019-03-26 15:50:10 +0800 | [diff] [blame] | 493 | } catch (ErrnoException e) { |
| 494 | Log.e(TAG, "resNetworkResult:" + e.toString()); |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 495 | exception = e; |
Luke Huang | 5386f49 | 2019-03-26 15:50:10 +0800 | [diff] [blame] | 496 | } |
Luke Huang | e4c7913 | 2019-03-07 19:01:26 +0800 | [diff] [blame] | 497 | } |
Luke Huang | 4219ab6 | 2019-04-08 15:16:04 +0800 | [diff] [blame] | 498 | if (exception != null) { |
| 499 | answerCallback.onError(new DnsException(ERROR_SYSTEM, exception)); |
| 500 | return; |
| 501 | } |
| 502 | answerCallback.onAnswer(resp.answerbuf, resp.rcode); |
Luke Huang | e4c7913 | 2019-03-07 19:01:26 +0800 | [diff] [blame] | 503 | }); |
Luke Huang | 5b31126 | 2019-06-11 14:25:45 +0800 | [diff] [blame] | 504 | |
| 505 | // The file descriptor has already been unregistered, so it does not really |
| 506 | // matter what is returned here. In spirit 0 (meaning "unregister this FD") |
| 507 | // is still the closest to what the looper needs to do. When returning 0, |
| 508 | // Looper knows to ignore the fd if it has already been unregistered. |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 509 | return 0; |
| 510 | }); |
| 511 | } |
| 512 | |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 513 | private void cancelQuery(@NonNull FileDescriptor queryfd) { |
| 514 | if (!queryfd.valid()) return; |
| 515 | Looper.getMainLooper().getQueue().removeOnFileDescriptorEventListener(queryfd); |
| 516 | resNetworkCancel(queryfd); // Closes fd, marks it invalid. |
| 517 | } |
| 518 | |
Luke Huang | 5bf7aa2 | 2019-03-29 18:01:11 +0800 | [diff] [blame] | 519 | private void addCancellationSignal(@NonNull CancellationSignal cancellationSignal, |
Luke Huang | 5386f49 | 2019-03-26 15:50:10 +0800 | [diff] [blame] | 520 | @NonNull FileDescriptor queryfd, @NonNull Object lock) { |
Luke Huang | 5386f49 | 2019-03-26 15:50:10 +0800 | [diff] [blame] | 521 | cancellationSignal.setOnCancelListener(() -> { |
| 522 | synchronized (lock) { |
Luke Huang | e9b2bba | 2019-03-28 13:56:31 +0800 | [diff] [blame] | 523 | cancelQuery(queryfd); |
Luke Huang | 5386f49 | 2019-03-26 15:50:10 +0800 | [diff] [blame] | 524 | } |
| 525 | }); |
Luke Huang | c09f2d6 | 2019-03-08 14:48:59 +0800 | [diff] [blame] | 526 | } |
| 527 | |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 528 | private static class DnsAddressAnswer extends DnsPacket { |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 529 | private static final String TAG = "DnsResolver.DnsAddressAnswer"; |
| 530 | private static final boolean DBG = false; |
| 531 | |
| 532 | private final int mQueryType; |
| 533 | |
| 534 | DnsAddressAnswer(@NonNull byte[] data) throws ParseException { |
| 535 | super(data); |
| 536 | if ((mHeader.flags & (1 << 15)) == 0) { |
| 537 | throw new ParseException("Not an answer packet"); |
| 538 | } |
Luke Huang | 33bfef3 | 2019-01-23 21:53:13 +0800 | [diff] [blame] | 539 | if (mHeader.getRecordCount(QDSECTION) == 0) { |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 540 | throw new ParseException("No question found"); |
| 541 | } |
Luke Huang | 33bfef3 | 2019-01-23 21:53:13 +0800 | [diff] [blame] | 542 | // Expect only one question in question section. |
| 543 | mQueryType = mRecords[QDSECTION].get(0).nsType; |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | public @NonNull List<InetAddress> getAddresses() { |
| 547 | final List<InetAddress> results = new ArrayList<InetAddress>(); |
Luke Huang | 304491d | 2019-03-04 17:08:03 +0800 | [diff] [blame] | 548 | if (mHeader.getRecordCount(ANSECTION) == 0) return results; |
| 549 | |
Luke Huang | 33bfef3 | 2019-01-23 21:53:13 +0800 | [diff] [blame] | 550 | for (final DnsRecord ansSec : mRecords[ANSECTION]) { |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 551 | // Only support A and AAAA, also ignore answers if query type != answer type. |
| 552 | int nsType = ansSec.nsType; |
| 553 | if (nsType != mQueryType || (nsType != TYPE_A && nsType != TYPE_AAAA)) { |
| 554 | continue; |
| 555 | } |
| 556 | try { |
| 557 | results.add(InetAddress.getByAddress(ansSec.getRR())); |
| 558 | } catch (UnknownHostException e) { |
| 559 | if (DBG) { |
| 560 | Log.w(TAG, "rr to address fail"); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | return results; |
| 565 | } |
| 566 | } |
| 567 | |
Luke Huang | 00b15f3 | 2019-01-04 19:56:29 +0800 | [diff] [blame] | 568 | } |