Merge "Fix issue for DnsResolver#query"

This commit is contained in:
Luke Huang
2019-05-30 11:22:50 +00:00
committed by Gerrit Code Review
3 changed files with 17 additions and 12 deletions

View File

@@ -16,7 +16,7 @@
package android.net; package android.net;
import static android.net.NetworkUtils.getDnsNetId; import static android.net.NetworkUtils.getDnsNetwork;
import static android.net.NetworkUtils.resNetworkCancel; import static android.net.NetworkUtils.resNetworkCancel;
import static android.net.NetworkUtils.resNetworkQuery; import static android.net.NetworkUtils.resNetworkQuery;
import static android.net.NetworkUtils.resNetworkResult; import static android.net.NetworkUtils.resNetworkResult;
@@ -333,7 +333,7 @@ public final class DnsResolver {
final Object lock = new Object(); final Object lock = new Object();
final Network queryNetwork; final Network queryNetwork;
try { try {
queryNetwork = (network != null) ? network : new Network(getDnsNetId()); queryNetwork = (network != null) ? network : getDnsNetwork();
} catch (ErrnoException e) { } catch (ErrnoException e) {
executor.execute(() -> callback.onError(new DnsException(ERROR_SYSTEM, e))); executor.execute(() -> callback.onError(new DnsException(ERROR_SYSTEM, e)));
return; return;
@@ -433,7 +433,7 @@ public final class DnsResolver {
final FileDescriptor queryfd; final FileDescriptor queryfd;
final Network queryNetwork; final Network queryNetwork;
try { try {
queryNetwork = (network != null) ? network : new Network(getDnsNetId()); queryNetwork = (network != null) ? network : getDnsNetwork();
queryfd = resNetworkQuery(queryNetwork.getNetIdForResolv(), domain, CLASS_IN, nsType, queryfd = resNetworkQuery(queryNetwork.getNetIdForResolv(), domain, CLASS_IN, nsType,
flags); flags);
} catch (ErrnoException e) { } catch (ErrnoException e) {

View File

@@ -153,10 +153,9 @@ public class NetworkUtils {
/** /**
* DNS resolver series jni method. * DNS resolver series jni method.
* Attempts to get netid of network which resolver will * Attempts to get network which resolver will use if no network is explicitly selected.
* use if no network is explicitly selected.
*/ */
public static native int getDnsNetId() throws ErrnoException; public static native Network getDnsNetwork() throws ErrnoException;
/** /**
* Get the tcp repair window associated with the {@code fd}. * Get the tcp repair window associated with the {@code fd}.

View File

@@ -304,13 +304,19 @@ static void android_net_utils_resNetworkCancel(JNIEnv *env, jobject thiz, jobjec
jniSetFileDescriptorOfFD(env, javaFd, -1); jniSetFileDescriptorOfFD(env, javaFd, -1);
} }
static jint android_net_utils_getDnsNetId(JNIEnv *env, jobject thiz) { static jobject android_net_utils_getDnsNetwork(JNIEnv *env, jobject thiz) {
int dnsNetId = getNetworkForDns(); unsigned dnsNetId = 0;
if (dnsNetId < 0) { if (int res = getNetworkForDns(&dnsNetId) < 0) {
throwErrnoException(env, "getDnsNetId", -dnsNetId); throwErrnoException(env, "getDnsNetId", -res);
return nullptr;
} }
bool privateDnsBypass = dnsNetId & NETID_USE_LOCAL_NAMESERVERS;
return dnsNetId; static jclass class_Network = MakeGlobalRefOrDie(
env, FindClassOrDie(env, "android/net/Network"));
static jmethodID ctor = env->GetMethodID(class_Network, "<init>", "(IZ)V");
return env->NewObject(
class_Network, ctor, dnsNetId & ~NETID_USE_LOCAL_NAMESERVERS, privateDnsBypass);
} }
static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, jobject javaFd) { static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, jobject javaFd) {
@@ -369,7 +375,7 @@ static const JNINativeMethod gNetworkUtilMethods[] = {
{ "resNetworkQuery", "(ILjava/lang/String;III)Ljava/io/FileDescriptor;", (void*) android_net_utils_resNetworkQuery }, { "resNetworkQuery", "(ILjava/lang/String;III)Ljava/io/FileDescriptor;", (void*) android_net_utils_resNetworkQuery },
{ "resNetworkResult", "(Ljava/io/FileDescriptor;)Landroid/net/DnsResolver$DnsResponse;", (void*) android_net_utils_resNetworkResult }, { "resNetworkResult", "(Ljava/io/FileDescriptor;)Landroid/net/DnsResolver$DnsResponse;", (void*) android_net_utils_resNetworkResult },
{ "resNetworkCancel", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_resNetworkCancel }, { "resNetworkCancel", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_resNetworkCancel },
{ "getDnsNetId", "()I", (void*) android_net_utils_getDnsNetId }, { "getDnsNetwork", "()Landroid/net/Network;", (void*) android_net_utils_getDnsNetwork },
}; };
int register_android_net_NetworkUtils(JNIEnv* env) int register_android_net_NetworkUtils(JNIEnv* env)