Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
Robert Greenwalt | 68e18d2 | 2011-01-26 15:01:16 -0800 | [diff] [blame] | 19 | import android.text.TextUtils; |
| 20 | import android.util.Log; |
| 21 | |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 22 | import java.net.Inet4Address; |
Jeff Sharkey | 77b87ba | 2012-04-18 21:55:14 -0700 | [diff] [blame] | 23 | import java.net.InetAddress; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 24 | import java.util.ArrayList; |
| 25 | import java.util.Collection; |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 26 | import java.util.Collections; |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * A simple object for retrieving the results of a DHCP request. |
| 30 | * Replaces (internally) the IPv4-only DhcpInfo class. |
| 31 | * @hide |
| 32 | */ |
| 33 | public class DhcpInfoInternal { |
Robert Greenwalt | 68e18d2 | 2011-01-26 15:01:16 -0800 | [diff] [blame] | 34 | private final static String TAG = "DhcpInfoInternal"; |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 35 | public String ipAddress; |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 36 | public int prefixLength; |
| 37 | |
| 38 | public String dns1; |
| 39 | public String dns2; |
| 40 | |
| 41 | public String serverAddress; |
| 42 | public int leaseDuration; |
| 43 | |
Jeff Sharkey | 77b87ba | 2012-04-18 21:55:14 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Vendor specific information (from RFC 2132). |
| 46 | */ |
| 47 | public String vendorInfo; |
| 48 | |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 49 | private Collection<RouteInfo> mRoutes; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 50 | |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 51 | public DhcpInfoInternal() { |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 52 | mRoutes = new ArrayList<RouteInfo>(); |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public void addRoute(RouteInfo routeInfo) { |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 56 | mRoutes.add(routeInfo); |
| 57 | } |
| 58 | |
| 59 | public Collection<RouteInfo> getRoutes() { |
| 60 | return Collections.unmodifiableCollection(mRoutes); |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private int convertToInt(String addr) { |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame] | 64 | 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 Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | public DhcpInfo makeDhcpInfo() { |
| 76 | DhcpInfo info = new DhcpInfo(); |
| 77 | info.ipAddress = convertToInt(ipAddress); |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 78 | for (RouteInfo route : mRoutes) { |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 79 | if (route.isDefaultRoute()) { |
| 80 | info.gateway = convertToInt(route.getGateway().getHostAddress()); |
| 81 | break; |
| 82 | } |
| 83 | } |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 84 | 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 Greenwalt | 68e18d2 | 2011-01-26 15:01:16 -0800 | [diff] [blame] | 96 | if (TextUtils.isEmpty(ipAddress)) { |
| 97 | Log.e(TAG, "makeLinkAddress with empty ipAddress"); |
| 98 | return null; |
| 99 | } |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 100 | return new LinkAddress(NetworkUtils.numericToInetAddress(ipAddress), prefixLength); |
| 101 | } |
| 102 | |
| 103 | public LinkProperties makeLinkProperties() { |
| 104 | LinkProperties p = new LinkProperties(); |
| 105 | p.addLinkAddress(makeLinkAddress()); |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 106 | for (RouteInfo route : mRoutes) { |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 107 | p.addRoute(route); |
Robert Greenwalt | 68e18d2 | 2011-01-26 15:01:16 -0800 | [diff] [blame] | 108 | } |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 109 | //if empty, connectivity configures default DNS |
Robert Greenwalt | 68e18d2 | 2011-01-26 15:01:16 -0800 | [diff] [blame] | 110 | if (TextUtils.isEmpty(dns1) == false) { |
| 111 | p.addDns(NetworkUtils.numericToInetAddress(dns1)); |
| 112 | } else { |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 113 | Log.d(TAG, "makeLinkProperties with empty dns1!"); |
Robert Greenwalt | 68e18d2 | 2011-01-26 15:01:16 -0800 | [diff] [blame] | 114 | } |
| 115 | if (TextUtils.isEmpty(dns2) == false) { |
| 116 | p.addDns(NetworkUtils.numericToInetAddress(dns2)); |
| 117 | } else { |
Robert Greenwalt | 992564e | 2011-02-09 13:56:06 -0800 | [diff] [blame] | 118 | Log.d(TAG, "makeLinkProperties with empty dns2!"); |
Robert Greenwalt | 68e18d2 | 2011-01-26 15:01:16 -0800 | [diff] [blame] | 119 | } |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 120 | return p; |
| 121 | } |
| 122 | |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 123 | /* 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 Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 145 | /** |
| 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 Sharkey | f166f48 | 2012-04-30 15:59:21 -0700 | [diff] [blame^] | 150 | if (vendorInfo != null) { |
| 151 | return vendorInfo.contains("ANDROID_METERED"); |
| 152 | } else { |
| 153 | return false; |
| 154 | } |
Jeff Sharkey | 9f6e4ba | 2012-04-19 23:01:08 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 157 | public String toString() { |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 158 | String routeString = ""; |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 159 | for (RouteInfo route : mRoutes) routeString += route.toString() + " | "; |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 160 | return "addr: " + ipAddress + "/" + prefixLength + |
Irfan Sheriff | 2c08ede | 2011-09-16 22:03:09 -0700 | [diff] [blame] | 161 | " mRoutes: " + routeString + |
Robert Greenwalt | 0216e61 | 2011-01-14 16:29:58 -0800 | [diff] [blame] | 162 | " dns: " + dns1 + "," + dns2 + |
| 163 | " dhcpServer: " + serverAddress + |
| 164 | " leaseDuration: " + leaseDuration; |
| 165 | } |
| 166 | } |