3

I encountered a wired question recently. I have an android device which connected the wifi, I'm sure the wifi is fine. but the android device can't access the internet. Then I found something wrong with the DNS resolution, here is what I did.

1. I written an app which just invoke InetAddress.getAllByName("www.google.com") method in MainActivity.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Log.d("DNSTEST", "here >>> 0");
                InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
                Log.d("DNSTEST", "here >>> 1");
                if (addresses != null) {
                    Log.d("DNSTEST", "here >>> 2");
                    for (InetAddress address : addresses) {
                        Log.d("DNSTEST", "here >>> 3");
                        Log.d("DNSTEST", "address: " + address.getHostAddress());
                    }
                }
                Log.d("DNSTEST", "here >>> 4");
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

Here is the logcat output:

2019-05-27 11:14:36.710 5204-5221/com.upward.dns D/DNSTEST: here >>> 0

As you can see, It's blocked when execute InetAddress.getAllByName("www.google.com").


2. Then I have written an executable program using C++ try to see whether is blocked when executing on android directly.

/*
 * dns.cpp
 *
 *  Created on: May 27, 2019
 *  Author: Will Tang
 */
#include<cstdio>
#include<netdb.h>
#include<arpa/inet.h>

int main()
{
    printf("here >>> 0\n");
    struct hostent *hptr = gethostbyname("www.google.com");

    printf("here >>> 1\n");
    char** pptr = hptr->h_addr_list;

    printf("here >>> 2\n");
    char str[32] = {0};

    printf("here >>> 3\n");
    for (; *pptr != nullptr; pptr++) {
        printf("here >>> 4\n");
        printf("address: %s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
    }

    printf("done\n");
}

Make the source code to an executable program 'DNS', then push it into an android sd card directory, then execute it. enter image description here

It's OK!


3. Then I written a shared library with the same C++ code, and Use JNI to let java invoke C++ code.

#include "com_upward_dns_DNSTest.h"
#include <android/log.h>
#include <thread>
#include <netdb.h>
#include <arpa/inet.h>

#define TAG "DNSTEST"
#define printf(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)


JNIEXPORT void JNICALL Java_com_upward_dns_DNSTest_test(JNIEnv *env, jobject obj, jint count)
{
    printf("here >>> 0");
    struct hostent *hptr = gethostbyname("www.google.com");

    printf("here >>> 1");
    char** pptr = hptr->h_addr_list;

    printf("here >>> 2");
    char str[32] = {0};

    printf("here >>> 3");
    for (; *pptr != nullptr; pptr++) {
        printf("here >>> 4");
        printf("address: %s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
    }

    printf("done");
}

package com.upward.dns;

public class DNSTest {

    static {
        System.loadLibrary("dns");
    }

    public native void test();

}
2019-05-27 11:41:18.694 5860-5860/com.upward.dns D/DNSTEST_JNI: here >>> 0

Bad news, It's blocked. Can anyone give me some suggestions? Thanks!

Minhaj Patel
  • 579
  • 1
  • 6
  • 21
Will Tang
  • 975
  • 1
  • 7
  • 17

0 Answers0