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 19 import android.text.TextUtils; 20 import android.util.Log; 21 22 import java.net.InetAddress; 23 import java.net.Inet4Address; 24 import java.net.UnknownHostException; 25 import java.util.ArrayList; 26 import java.util.Collection; 27 import java.util.Collections; 28 29 /** 30 * A simple object for retrieving the results of a DHCP request. 31 * Replaces (internally) the IPv4-only DhcpInfo class. 32 * @hide 33 */ 34 public class DhcpInfoInternal { 35 private final static String TAG = "DhcpInfoInternal"; 36 public String ipAddress; 37 public int prefixLength; 38 39 public String dns1; 40 public String dns2; 41 42 public String serverAddress; 43 public int leaseDuration; 44 45 private Collection<RouteInfo> mRoutes; 46 47 public DhcpInfoInternal() { 48 mRoutes = new ArrayList<RouteInfo>(); 49 } 50 51 public void addRoute(RouteInfo routeInfo) { 52 mRoutes.add(routeInfo); 53 } 54 55 public Collection<RouteInfo> getRoutes() { 56 return Collections.unmodifiableCollection(mRoutes); 57 } 58 59 private int convertToInt(String addr) { 60 if (addr != null) { 61 try { 62 InetAddress inetAddress = NetworkUtils.numericToInetAddress(addr); 63 if (inetAddress instanceof Inet4Address) { 64 return NetworkUtils.inetAddressToInt(inetAddress); 65 } 66 } catch (IllegalArgumentException e) {} 67 } 68 return 0; 69 } 70 71 public DhcpInfo makeDhcpInfo() { 72 DhcpInfo info = new DhcpInfo(); 73 info.ipAddress = convertToInt(ipAddress); 74 for (RouteInfo route : mRoutes) { 75 if (route.isDefaultRoute()) { 76 info.gateway = convertToInt(route.getGateway().getHostAddress()); 77 break; 78 } 79 } 80 try { 81 InetAddress inetAddress = NetworkUtils.numericToInetAddress(ipAddress); 82 info.netmask = NetworkUtils.prefixLengthToNetmaskInt(prefixLength); 83 } catch (IllegalArgumentException e) {} 84 info.dns1 = convertToInt(dns1); 85 info.dns2 = convertToInt(dns2); 86 info.serverAddress = convertToInt(serverAddress); 87 info.leaseDuration = leaseDuration; 88 return info; 89 } 90 91 public LinkAddress makeLinkAddress() { 92 if (TextUtils.isEmpty(ipAddress)) { 93 Log.e(TAG, "makeLinkAddress with empty ipAddress"); 94 return null; 95 } 96 return new LinkAddress(NetworkUtils.numericToInetAddress(ipAddress), prefixLength); 97 } 98 99 public LinkProperties makeLinkProperties() { 100 LinkProperties p = new LinkProperties(); 101 p.addLinkAddress(makeLinkAddress()); 102 for (RouteInfo route : mRoutes) { 103 p.addRoute(route); 104 } 105 //if empty, connectivity configures default DNS 106 if (TextUtils.isEmpty(dns1) == false) { 107 p.addDns(NetworkUtils.numericToInetAddress(dns1)); 108 } else { 109 Log.d(TAG, "makeLinkProperties with empty dns1!"); 110 } 111 if (TextUtils.isEmpty(dns2) == false) { 112 p.addDns(NetworkUtils.numericToInetAddress(dns2)); 113 } else { 114 Log.d(TAG, "makeLinkProperties with empty dns2!"); 115 } 116 return p; 117 } 118 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 141 public String toString() { 142 String routeString = ""; 143 for (RouteInfo route : mRoutes) routeString += route.toString() + " | "; 144 return "addr: " + ipAddress + "/" + prefixLength + 145 " mRoutes: " + routeString + 146 " dns: " + dns1 + "," + dns2 + 147 " dhcpServer: " + serverAddress + 148 " leaseDuration: " + leaseDuration; 149 } 150 } 151