DhcpInfoInternal.java revision aa70f101e08098ed9cb190abe2d7f952561026b8
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 17package android.net; 18 19import android.text.TextUtils; 20import android.util.Log; 21 22import java.net.InetAddress; 23import java.net.Inet4Address; 24import java.net.UnknownHostException; 25import java.util.ArrayList; 26import java.util.Collection; 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 */ 33public class DhcpInfoInternal { 34 private final static String TAG = "DhcpInfoInternal"; 35 public String ipAddress; 36 public int prefixLength; 37 38 public String dns1; 39 public String dns2; 40 41 public String serverAddress; 42 public int leaseDuration; 43 44 private Collection<RouteInfo> routes; 45 46 public DhcpInfoInternal() { 47 routes = new ArrayList<RouteInfo>(); 48 } 49 50 public void addRoute(RouteInfo routeInfo) { 51 routes.add(routeInfo); 52 } 53 54 private int convertToInt(String addr) { 55 if (addr != null) { 56 try { 57 InetAddress inetAddress = NetworkUtils.numericToInetAddress(addr); 58 if (inetAddress instanceof Inet4Address) { 59 return NetworkUtils.inetAddressToInt(inetAddress); 60 } 61 } catch (IllegalArgumentException e) {} 62 } 63 return 0; 64 } 65 66 public DhcpInfo makeDhcpInfo() { 67 DhcpInfo info = new DhcpInfo(); 68 info.ipAddress = convertToInt(ipAddress); 69 for (RouteInfo route : routes) { 70 if (route.isDefaultRoute()) { 71 info.gateway = convertToInt(route.getGateway().getHostAddress()); 72 break; 73 } 74 } 75 try { 76 InetAddress inetAddress = NetworkUtils.numericToInetAddress(ipAddress); 77 info.netmask = NetworkUtils.prefixLengthToNetmaskInt(prefixLength); 78 } catch (IllegalArgumentException e) {} 79 info.dns1 = convertToInt(dns1); 80 info.dns2 = convertToInt(dns2); 81 info.serverAddress = convertToInt(serverAddress); 82 info.leaseDuration = leaseDuration; 83 return info; 84 } 85 86 public LinkAddress makeLinkAddress() { 87 if (TextUtils.isEmpty(ipAddress)) { 88 Log.e(TAG, "makeLinkAddress with empty ipAddress"); 89 return null; 90 } 91 return new LinkAddress(NetworkUtils.numericToInetAddress(ipAddress), prefixLength); 92 } 93 94 public LinkProperties makeLinkProperties() { 95 LinkProperties p = new LinkProperties(); 96 p.addLinkAddress(makeLinkAddress()); 97 for (RouteInfo route : routes) { 98 p.addRoute(route); 99 } 100 if (TextUtils.isEmpty(dns1) == false) { 101 p.addDns(NetworkUtils.numericToInetAddress(dns1)); 102 } else { 103 Log.d(TAG, "makeLinkProperties with empty dns1!"); 104 } 105 if (TextUtils.isEmpty(dns2) == false) { 106 p.addDns(NetworkUtils.numericToInetAddress(dns2)); 107 } else { 108 Log.d(TAG, "makeLinkProperties with empty dns2!"); 109 } 110 return p; 111 } 112 113 public String toString() { 114 String routeString = ""; 115 for (RouteInfo route : routes) routeString += route.toString() + " | "; 116 return "addr: " + ipAddress + "/" + prefixLength + 117 " routes: " + routeString + 118 " dns: " + dns1 + "," + dns2 + 119 " dhcpServer: " + serverAddress + 120 " leaseDuration: " + leaseDuration; 121 } 122} 123