blob: f3508c1105bbb9e6f245f21d77b1598cb34a79f8 [file] [log] [blame]
Robert Greenwalt0216e612011-01-14 16:29:58 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
Robert Greenwalt68e18d22011-01-26 15:01:16 -080019import android.text.TextUtils;
20import android.util.Log;
21
Robert Greenwalt0216e612011-01-14 16:29:58 -080022import java.net.Inet4Address;
Jeff Sharkey77b87ba2012-04-18 21:55:14 -070023import java.net.InetAddress;
Robert Greenwaltaa70f102011-04-28 14:28:50 -070024import java.util.ArrayList;
25import java.util.Collection;
Irfan Sheriff2c08ede2011-09-16 22:03:09 -070026import java.util.Collections;
Robert Greenwalt0216e612011-01-14 16:29:58 -080027
28/**
29 * A simple object for retrieving the results of a DHCP request.
30 * Replaces (internally) the IPv4-only DhcpInfo class.
31 * @hide
32 */
33public class DhcpInfoInternal {
Robert Greenwalt68e18d22011-01-26 15:01:16 -080034 private final static String TAG = "DhcpInfoInternal";
Robert Greenwalt0216e612011-01-14 16:29:58 -080035 public String ipAddress;
Robert Greenwalt0216e612011-01-14 16:29:58 -080036 public int prefixLength;
37
38 public String dns1;
39 public String dns2;
40
41 public String serverAddress;
42 public int leaseDuration;
43
Jeff Sharkey77b87ba2012-04-18 21:55:14 -070044 /**
45 * Vendor specific information (from RFC 2132).
46 */
47 public String vendorInfo;
48
Irfan Sheriff2c08ede2011-09-16 22:03:09 -070049 private Collection<RouteInfo> mRoutes;
Robert Greenwaltaa70f102011-04-28 14:28:50 -070050
Robert Greenwalt0216e612011-01-14 16:29:58 -080051 public DhcpInfoInternal() {
Irfan Sheriff2c08ede2011-09-16 22:03:09 -070052 mRoutes = new ArrayList<RouteInfo>();
Robert Greenwaltaa70f102011-04-28 14:28:50 -070053 }
54
55 public void addRoute(RouteInfo routeInfo) {
Irfan Sheriff2c08ede2011-09-16 22:03:09 -070056 mRoutes.add(routeInfo);
57 }
58
59 public Collection<RouteInfo> getRoutes() {
60 return Collections.unmodifiableCollection(mRoutes);
Robert Greenwalt0216e612011-01-14 16:29:58 -080061 }
62
63 private int convertToInt(String addr) {
Robert Greenwalt992564e2011-02-09 13:56:06 -080064 if (addr != null) {
65 try {
66 InetAddress inetAddress = NetworkUtils.numericToInetAddress(addr);
67 if (inetAddress instanceof Inet4Address) {
68 return NetworkUtils.inetAddressToInt(inetAddress);
69 }
70 } catch (IllegalArgumentException e) {}
71 }
Robert Greenwalt0216e612011-01-14 16:29:58 -080072 return 0;
73 }
74
75 public DhcpInfo makeDhcpInfo() {
76 DhcpInfo info = new DhcpInfo();
77 info.ipAddress = convertToInt(ipAddress);
Irfan Sheriff2c08ede2011-09-16 22:03:09 -070078 for (RouteInfo route : mRoutes) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -070079 if (route.isDefaultRoute()) {
80 info.gateway = convertToInt(route.getGateway().getHostAddress());
81 break;
82 }
83 }
Robert Greenwalt0216e612011-01-14 16:29:58 -080084 try {
85 InetAddress inetAddress = NetworkUtils.numericToInetAddress(ipAddress);
86 info.netmask = NetworkUtils.prefixLengthToNetmaskInt(prefixLength);
87 } catch (IllegalArgumentException e) {}
88 info.dns1 = convertToInt(dns1);
89 info.dns2 = convertToInt(dns2);
90 info.serverAddress = convertToInt(serverAddress);
91 info.leaseDuration = leaseDuration;
92 return info;
93 }
94
95 public LinkAddress makeLinkAddress() {
Robert Greenwalt68e18d22011-01-26 15:01:16 -080096 if (TextUtils.isEmpty(ipAddress)) {
97 Log.e(TAG, "makeLinkAddress with empty ipAddress");
98 return null;
99 }
Robert Greenwalt0216e612011-01-14 16:29:58 -0800100 return new LinkAddress(NetworkUtils.numericToInetAddress(ipAddress), prefixLength);
101 }
102
103 public LinkProperties makeLinkProperties() {
104 LinkProperties p = new LinkProperties();
105 p.addLinkAddress(makeLinkAddress());
Irfan Sheriff2c08ede2011-09-16 22:03:09 -0700106 for (RouteInfo route : mRoutes) {
Robert Greenwaltaa70f102011-04-28 14:28:50 -0700107 p.addRoute(route);
Robert Greenwalt68e18d22011-01-26 15:01:16 -0800108 }
Irfan Sheriff2c08ede2011-09-16 22:03:09 -0700109 //if empty, connectivity configures default DNS
Robert Greenwalt68e18d22011-01-26 15:01:16 -0800110 if (TextUtils.isEmpty(dns1) == false) {
111 p.addDns(NetworkUtils.numericToInetAddress(dns1));
112 } else {
Irfan Sheriff2c08ede2011-09-16 22:03:09 -0700113 Log.d(TAG, "makeLinkProperties with empty dns1!");
Robert Greenwalt68e18d22011-01-26 15:01:16 -0800114 }
115 if (TextUtils.isEmpty(dns2) == false) {
116 p.addDns(NetworkUtils.numericToInetAddress(dns2));
117 } else {
Robert Greenwalt992564e2011-02-09 13:56:06 -0800118 Log.d(TAG, "makeLinkProperties with empty dns2!");
Robert Greenwalt68e18d22011-01-26 15:01:16 -0800119 }
Robert Greenwalt0216e612011-01-14 16:29:58 -0800120 return p;
121 }
122
Irfan Sheriff2c08ede2011-09-16 22:03:09 -0700123 /* Updates the DHCP fields that need to be retained from
124 * original DHCP request if the DHCP renewal shows them as
125 * being empty
126 */
127 public void updateFromDhcpRequest(DhcpInfoInternal orig) {
128 if (orig == null) return;
129
130 if (TextUtils.isEmpty(dns1)) {
131 dns1 = orig.dns1;
132 }
133
134 if (TextUtils.isEmpty(dns2)) {
135 dns2 = orig.dns2;
136 }
137
138 if (mRoutes.size() == 0) {
139 for (RouteInfo route : orig.getRoutes()) {
140 addRoute(route);
141 }
142 }
143 }
144
Jeff Sharkey9f6e4ba2012-04-19 23:01:08 -0700145 /**
146 * Test if this DHCP lease includes vendor hint that network link is
147 * metered, and sensitive to heavy data transfers.
148 */
149 public boolean hasMeteredHint() {
Jeff Sharkeyf166f482012-04-30 15:59:21 -0700150 if (vendorInfo != null) {
151 return vendorInfo.contains("ANDROID_METERED");
152 } else {
153 return false;
154 }
Jeff Sharkey9f6e4ba2012-04-19 23:01:08 -0700155 }
156
Robert Greenwalt0216e612011-01-14 16:29:58 -0800157 public String toString() {
Robert Greenwaltaa70f102011-04-28 14:28:50 -0700158 String routeString = "";
Irfan Sheriff2c08ede2011-09-16 22:03:09 -0700159 for (RouteInfo route : mRoutes) routeString += route.toString() + " | ";
Robert Greenwalt0216e612011-01-14 16:29:58 -0800160 return "addr: " + ipAddress + "/" + prefixLength +
Irfan Sheriff2c08ede2011-09-16 22:03:09 -0700161 " mRoutes: " + routeString +
Robert Greenwalt0216e612011-01-14 16:29:58 -0800162 " dns: " + dns1 + "," + dns2 +
163 " dhcpServer: " + serverAddress +
164 " leaseDuration: " + leaseDuration;
165 }
166}