Skip to content

Commit a407aef

Browse files
isheriffAndroid (Google) Code Review
authored andcommitted
Merge "Added support for multiple DNS servers."
2 parents 8184393 + 79e43f6 commit a407aef

File tree

2 files changed

+68
-56
lines changed

2 files changed

+68
-56
lines changed

core/java/android/net/DnsPinger.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public final class DnsPinger extends Handler {
6767
private final Context mContext;
6868
private final int mConnectionType;
6969
private final Handler mTarget;
70-
private final InetAddress mDefaultDns;
70+
private final ArrayList<InetAddress> mDefaultDns;
7171
private String TAG;
7272

7373
private static final int BASE = Protocol.BASE_DNS_PINGER;
@@ -113,7 +113,8 @@ public DnsPinger(Context context, String TAG, Looper looper,
113113
throw new IllegalArgumentException("Invalid connectionType in constructor: "
114114
+ connectionType);
115115
}
116-
mDefaultDns = getDefaultDns();
116+
mDefaultDns = new ArrayList<InetAddress>();
117+
mDefaultDns.add(getDefaultDns());
117118
mEventCounter = 0;
118119
}
119120

@@ -213,17 +214,16 @@ public void handleMessage(Message msg) {
213214
for (ActivePing activePing : mActivePings)
214215
activePing.socket.close();
215216
mActivePings.clear();
216-
removeMessages(ACTION_PING_DNS);
217217
break;
218218
}
219219
}
220220

221221
/**
222-
* @return The first DNS in the link properties of the specified connection
223-
* type or the default system DNS if the link properties has null
224-
* dns set. Should not be null.
222+
* Returns a list of DNS addresses, coming from either the link properties of the
223+
* specified connection or the default system DNS if the link properties has no dnses.
224+
* @return a non-empty non-null list
225225
*/
226-
public InetAddress getDns() {
226+
public List<InetAddress> getDnsList() {
227227
LinkProperties curLinkProps = getCurrentLinkProperties();
228228
if (curLinkProps == null) {
229229
Slog.e(TAG, "getCurLinkProperties:: LP for type" + mConnectionType + " is null!");
@@ -236,7 +236,7 @@ public InetAddress getDns() {
236236
return mDefaultDns;
237237
}
238238

239-
return dnses.iterator().next();
239+
return new ArrayList<InetAddress>(dnses);
240240
}
241241

242242
/**

wifi/java/android/net/wifi/WifiWatchdogStateMachine.java

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
import java.net.HttpURLConnection;
4747
import java.net.InetAddress;
4848
import java.net.URL;
49+
import java.util.HashMap;
4950
import java.util.HashSet;
5051
import java.util.List;
51-
import java.util.Set;
5252

5353
/**
5454
* {@link WifiWatchdogStateMachine} monitors the initial connection to a Wi-Fi
@@ -79,7 +79,7 @@ public class WifiWatchdogStateMachine extends StateMachine {
7979
private static final int LOW_SIGNAL_CUTOFF = 1;
8080

8181
private static final long DEFAULT_DNS_CHECK_SHORT_INTERVAL_MS = 2 * 60 * 1000;
82-
private static final long DEFAULT_DNS_CHECK_LONG_INTERVAL_MS = 10 * 60 * 1000;
82+
private static final long DEFAULT_DNS_CHECK_LONG_INTERVAL_MS = 30 * 60 * 1000;
8383
private static final long DEFAULT_WALLED_GARDEN_INTERVAL_MS = 30 * 60 * 1000;
8484

8585
private static final int DEFAULT_MAX_SSID_BLACKLISTS = 7;
@@ -661,26 +661,34 @@ public boolean processMessage(Message msg) {
661661
}
662662

663663
class DnsCheckingState extends State {
664-
int dnsCheckSuccesses = 0;
665-
int dnsCheckTries = 0;
666-
String dnsCheckLogStr = "";
667-
Set<Integer> ids = new HashSet<Integer>();
664+
List<InetAddress> mDnsList;
665+
int[] dnsCheckSuccesses;
666+
String dnsCheckLogStr;
667+
String[] dnsResponseStrs;
668+
/** Keeps track of active dns pings. Map is from pingID to index in mDnsList */
669+
HashMap<Integer, Integer> idDnsMap = new HashMap<Integer, Integer>();
668670

669671
@Override
670672
public void enter() {
671-
dnsCheckSuccesses = 0;
672-
dnsCheckTries = 0;
673-
ids.clear();
674-
InetAddress dns = mDnsPinger.getDns();
673+
mDnsList = mDnsPinger.getDnsList();
674+
int numDnses = mDnsList.size();
675+
dnsCheckSuccesses = new int[numDnses];
676+
dnsResponseStrs = new String[numDnses];
677+
for (int i = 0; i < numDnses; i++)
678+
dnsResponseStrs[i] = "";
679+
675680
if (DBG) {
676-
Slog.d(WWSM_TAG, "Starting DNS pings at " + SystemClock.elapsedRealtime());
677681
dnsCheckLogStr = String.format("Pinging %s on ssid [%s]: ",
678-
mDnsPinger.getDns(), mConnectionInfo.getSSID());
682+
mDnsList, mConnectionInfo.getSSID());
683+
Slog.d(WWSM_TAG, dnsCheckLogStr);
679684
}
680685

686+
idDnsMap.clear();
681687
for (int i=0; i < mNumDnsPings; i++) {
682-
ids.add(mDnsPinger.pingDnsAsync(dns, mDnsPingTimeoutMs,
683-
DNS_INTRATEST_PING_INTERVAL * i));
688+
for (int j = 0; j < numDnses; j++) {
689+
idDnsMap.put(mDnsPinger.pingDnsAsync(mDnsList.get(j), mDnsPingTimeoutMs,
690+
DNS_INTRATEST_PING_INTERVAL * i), j);
691+
}
684692
}
685693
}
686694

@@ -693,27 +701,24 @@ public boolean processMessage(Message msg) {
693701
int pingID = msg.arg1;
694702
int pingResponseTime = msg.arg2;
695703

696-
if (!ids.contains(pingID)) {
704+
Integer dnsServerId = idDnsMap.get(pingID);
705+
if (dnsServerId == null) {
697706
Slog.w(WWSM_TAG, "Received a Dns response with unknown ID!");
698707
return HANDLED;
699708
}
700-
ids.remove(pingID);
701-
dnsCheckTries++;
709+
710+
idDnsMap.remove(pingID);
702711
if (pingResponseTime >= 0)
703-
dnsCheckSuccesses++;
712+
dnsCheckSuccesses[dnsServerId]++;
704713

705714
if (DBG) {
706715
if (pingResponseTime >= 0) {
707-
dnsCheckLogStr += "|" + pingResponseTime;
716+
dnsResponseStrs[dnsServerId] += "|" + pingResponseTime;
708717
} else {
709-
dnsCheckLogStr += "|x";
718+
dnsResponseStrs[dnsServerId] += "|x";
710719
}
711720
}
712721

713-
if (VDBG) {
714-
Slog.v(WWSM_TAG, dnsCheckLogStr);
715-
}
716-
717722
/**
718723
* After a full ping count, if we have more responses than this
719724
* cutoff, the outcome is success; else it is 'failure'.
@@ -723,10 +728,10 @@ public boolean processMessage(Message msg) {
723728
* Our final success count will be at least this big, so we're
724729
* guaranteed to succeed.
725730
*/
726-
if (dnsCheckSuccesses >= mMinDnsResponses) {
731+
if (dnsCheckSuccesses[dnsServerId] >= mMinDnsResponses) {
727732
// DNS CHECKS OK, NOW WALLED GARDEN
728733
if (DBG) {
729-
Slog.d(WWSM_TAG, dnsCheckLogStr + "| SUCCESS");
734+
Slog.d(WWSM_TAG, makeLogString() + " SUCCESS");
730735
}
731736

732737
if (!shouldCheckWalledGarden()) {
@@ -748,14 +753,9 @@ public boolean processMessage(Message msg) {
748753
return HANDLED;
749754
}
750755

751-
/**
752-
* Our final count will be at most the current count plus the
753-
* remaining pings - we're guaranteed to fail.
754-
*/
755-
int remainingChecks = mNumDnsPings - dnsCheckTries;
756-
if (remainingChecks + dnsCheckSuccesses < mMinDnsResponses) {
756+
if (idDnsMap.isEmpty()) {
757757
if (DBG) {
758-
Slog.d(WWSM_TAG, dnsCheckLogStr + "| FAILURE");
758+
Slog.d(WWSM_TAG, makeLogString() + " FAILURE");
759759
}
760760
transitionTo(mDnsCheckFailureState);
761761
return HANDLED;
@@ -764,12 +764,18 @@ public boolean processMessage(Message msg) {
764764
return HANDLED;
765765
}
766766

767+
private String makeLogString() {
768+
String logStr = dnsCheckLogStr;
769+
for (String respStr : dnsResponseStrs)
770+
logStr += " [" + respStr + "]";
771+
return logStr;
772+
}
773+
767774
@Override
768775
public void exit() {
769776
mDnsPinger.cancelPings();
770777
}
771778

772-
773779
private boolean shouldCheckWalledGarden() {
774780
if (!mWalledGardenTestEnabled) {
775781
if (VDBG)
@@ -809,15 +815,16 @@ class OnlineWatchState extends State {
809815
int checkGuard = 0;
810816
Long lastCheckTime = null;
811817

812-
int curPingID = 0;
818+
/** Keeps track of dns pings. Map is from pingID to InetAddress used for ping */
819+
HashMap<Integer, InetAddress> pingInfoMap = new HashMap<Integer, InetAddress>();
813820

814821
@Override
815822
public void enter() {
816823
lastCheckTime = SystemClock.elapsedRealtime();
817824
signalUnstable = false;
818825
checkGuard++;
819826
unstableSignalChecks = false;
820-
curPingID = 0;
827+
pingInfoMap.clear();
821828
triggerSingleDnsCheck();
822829
}
823830

@@ -853,32 +860,37 @@ public boolean processMessage(Message msg) {
853860
return HANDLED;
854861
}
855862
lastCheckTime = SystemClock.elapsedRealtime();
856-
curPingID = mDnsPinger.pingDnsAsync(mDnsPinger.getDns(),
857-
mDnsPingTimeoutMs, 0);
863+
pingInfoMap.clear();
864+
for (InetAddress curDns: mDnsPinger.getDnsList()) {
865+
pingInfoMap.put(mDnsPinger.pingDnsAsync(curDns, mDnsPingTimeoutMs, 0),
866+
curDns);
867+
}
858868
return HANDLED;
859869
case DnsPinger.DNS_PING_RESULT:
860-
if ((short) msg.arg1 != curPingID) {
861-
if (VDBG) {
862-
Slog.v(WWSM_TAG, "Received non-matching DnsPing w/ id: " +
863-
msg.arg1);
864-
}
870+
InetAddress curDnsServer = pingInfoMap.get(msg.arg1);
871+
if (curDnsServer == null) {
865872
return HANDLED;
866873
}
874+
pingInfoMap.remove(msg.arg1);
867875
int responseTime = msg.arg2;
868876
if (responseTime >= 0) {
869877
if (VDBG) {
870-
Slog.v(WWSM_TAG, "Ran a single DNS ping. Response time: "
871-
+ responseTime);
878+
Slog.v(WWSM_TAG, "Single DNS ping OK. Response time: "
879+
+ responseTime + " from DNS " + curDnsServer);
872880
}
881+
pingInfoMap.clear();
873882

874883
checkGuard++;
875884
unstableSignalChecks = false;
876885
triggerSingleDnsCheck();
877886
} else {
878-
if (DBG) {
879-
Slog.d(WWSM_TAG, "Single dns ping failure. Starting full checks.");
887+
if (pingInfoMap.isEmpty()) {
888+
if (DBG) {
889+
Slog.d(WWSM_TAG, "Single dns ping failure. All dns servers failed, "
890+
+ "starting full checks.");
891+
}
892+
transitionTo(mDnsCheckingState);
880893
}
881-
transitionTo(mDnsCheckingState);
882894
}
883895
return HANDLED;
884896
}

0 commit comments

Comments
 (0)