Skip to content

Commit 04e3bb6

Browse files
isheriffAndroid (Google) Code Review
authored andcommitted
Merge "Retain DNS information from DHCP request"
2 parents bfb9a9a + 2c08ede commit 04e3bb6

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

core/java/android/net/DhcpInfoInternal.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.net.UnknownHostException;
2525
import java.util.ArrayList;
2626
import java.util.Collection;
27+
import java.util.Collections;
2728

2829
/**
2930
* A simple object for retrieving the results of a DHCP request.
@@ -41,14 +42,18 @@ public class DhcpInfoInternal {
4142
public String serverAddress;
4243
public int leaseDuration;
4344

44-
private Collection<RouteInfo> routes;
45+
private Collection<RouteInfo> mRoutes;
4546

4647
public DhcpInfoInternal() {
47-
routes = new ArrayList<RouteInfo>();
48+
mRoutes = new ArrayList<RouteInfo>();
4849
}
4950

5051
public void addRoute(RouteInfo routeInfo) {
51-
routes.add(routeInfo);
52+
mRoutes.add(routeInfo);
53+
}
54+
55+
public Collection<RouteInfo> getRoutes() {
56+
return Collections.unmodifiableCollection(mRoutes);
5257
}
5358

5459
private int convertToInt(String addr) {
@@ -66,7 +71,7 @@ private int convertToInt(String addr) {
6671
public DhcpInfo makeDhcpInfo() {
6772
DhcpInfo info = new DhcpInfo();
6873
info.ipAddress = convertToInt(ipAddress);
69-
for (RouteInfo route : routes) {
74+
for (RouteInfo route : mRoutes) {
7075
if (route.isDefaultRoute()) {
7176
info.gateway = convertToInt(route.getGateway().getHostAddress());
7277
break;
@@ -94,14 +99,14 @@ public LinkAddress makeLinkAddress() {
9499
public LinkProperties makeLinkProperties() {
95100
LinkProperties p = new LinkProperties();
96101
p.addLinkAddress(makeLinkAddress());
97-
for (RouteInfo route : routes) {
102+
for (RouteInfo route : mRoutes) {
98103
p.addRoute(route);
99104
}
105+
//if empty, connectivity configures default DNS
100106
if (TextUtils.isEmpty(dns1) == false) {
101107
p.addDns(NetworkUtils.numericToInetAddress(dns1));
102108
} else {
103-
p.addDns(NetworkUtils.numericToInetAddress(serverAddress));
104-
Log.d(TAG, "empty dns1, use dhcp server as dns1!");
109+
Log.d(TAG, "makeLinkProperties with empty dns1!");
105110
}
106111
if (TextUtils.isEmpty(dns2) == false) {
107112
p.addDns(NetworkUtils.numericToInetAddress(dns2));
@@ -111,11 +116,33 @@ public LinkProperties makeLinkProperties() {
111116
return p;
112117
}
113118

119+
/* Updates the DHCP fields that need to be retained from
120+
* original DHCP request if the DHCP renewal shows them as
121+
* being empty
122+
*/
123+
public void updateFromDhcpRequest(DhcpInfoInternal orig) {
124+
if (orig == null) return;
125+
126+
if (TextUtils.isEmpty(dns1)) {
127+
dns1 = orig.dns1;
128+
}
129+
130+
if (TextUtils.isEmpty(dns2)) {
131+
dns2 = orig.dns2;
132+
}
133+
134+
if (mRoutes.size() == 0) {
135+
for (RouteInfo route : orig.getRoutes()) {
136+
addRoute(route);
137+
}
138+
}
139+
}
140+
114141
public String toString() {
115142
String routeString = "";
116-
for (RouteInfo route : routes) routeString += route.toString() + " | ";
143+
for (RouteInfo route : mRoutes) routeString += route.toString() + " | ";
117144
return "addr: " + ipAddress + "/" + prefixLength +
118-
" routes: " + routeString +
145+
" mRoutes: " + routeString +
119146
" dns: " + dns1 + "," + dns2 +
120147
" dhcpServer: " + serverAddress +
121148
" leaseDuration: " + leaseDuration;

core/java/android/net/DhcpStateMachine.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public class DhcpStateMachine extends StateMachine {
6363
private PowerManager.WakeLock mDhcpRenewWakeLock;
6464
private static final String WAKELOCK_TAG = "DHCP";
6565

66+
//Remember DHCP configuration from first request
67+
private DhcpInfoInternal mDhcpInfo;
68+
6669
private static final int DHCP_RENEW = 0;
6770
private static final String ACTION_DHCP_RENEW = "android.net.wifi.DHCP_RENEW";
6871

@@ -335,9 +338,11 @@ private boolean runDhcp(DhcpAction dhcpAction) {
335338
if (dhcpAction == DhcpAction.START) {
336339
Log.d(TAG, "DHCP request on " + mInterfaceName);
337340
success = NetworkUtils.runDhcp(mInterfaceName, dhcpInfoInternal);
341+
mDhcpInfo = dhcpInfoInternal;
338342
} else if (dhcpAction == DhcpAction.RENEW) {
339343
Log.d(TAG, "DHCP renewal on " + mInterfaceName);
340344
success = NetworkUtils.runDhcpRenew(mInterfaceName, dhcpInfoInternal);
345+
dhcpInfoInternal.updateFromDhcpRequest(mDhcpInfo);
341346
}
342347

343348
if (success) {

0 commit comments

Comments
 (0)