/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.net; import android.content.Context; import android.net.ConnectivityManager; import android.net.LinkProperties; import android.net.NetworkUtils; import android.os.SystemClock; import android.provider.Settings; import android.util.Slog; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketTimeoutException; import java.util.Collection; import java.util.Random; /** * Performs a simple DNS "ping" by sending a "server status" query packet to the * DNS server. As long as the server replies, we consider it a success. *
* We do not use a simple hostname lookup because that could be cached and the * API may not differentiate between a time out and a failure lookup (which we * really care about). *
* TODO : More general API. Socket does not bind to specified connection type
* TODO : Choice of DNS query location - current looks up www.android.com
*
* @hide
*/
public final class DnsPinger {
private static final boolean V = true;
/** Number of bytes for the query */
private static final int DNS_QUERY_BASE_SIZE = 32;
/** The DNS port */
private static final int DNS_PORT = 53;
/** Used to generate IDs */
private static Random sRandom = new Random();
private ConnectivityManager mConnectivityManager = null;
private Context mContext;
private int mConnectionType;
private InetAddress mDefaultDns;
private String TAG;
/**
* @param connectionType The connection type from {@link ConnectivityManager}
*/
public DnsPinger(String TAG, Context context, int connectionType) {
mContext = context;
mConnectionType = connectionType;
if (!ConnectivityManager.isNetworkTypeValid(connectionType)) {
Slog.e(TAG, "Invalid connectionType in constructor: " + connectionType);
}
this.TAG = TAG;
mDefaultDns = getDefaultDns();
}
/**
* @return The first DNS in the link properties of the specified connection
* type or the default system DNS if the link properties has null
* dns set. Should not be null.
*/
public InetAddress getDns() {
LinkProperties curLinkProps = getCurrentLinkProperties();
if (curLinkProps == null) {
Slog.e(TAG, "getCurLinkProperties:: LP for type" + mConnectionType + " is null!");
return mDefaultDns;
}
Collection