2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.text.TextUtils;
20 import android.util.Log;
22 import java.net.InetAddress;
23 import java.net.Inet4Address;
24 import java.net.UnknownHostException;
27 * A simple object for retrieving the results of a DHCP request.
28 * Replaces (internally) the IPv4-only DhcpInfo class.
31 public class DhcpInfoInternal {
32 private final static String TAG = "DhcpInfoInternal";
33 public String ipAddress;
34 public String gateway;
35 public int prefixLength;
40 public String serverAddress;
41 public int leaseDuration;
43 public DhcpInfoInternal() {
46 private int convertToInt(String addr) {
49 InetAddress inetAddress = NetworkUtils.numericToInetAddress(addr);
50 if (inetAddress instanceof Inet4Address) {
51 return NetworkUtils.inetAddressToInt(inetAddress);
53 } catch (IllegalArgumentException e) {}
58 public DhcpInfo makeDhcpInfo() {
59 DhcpInfo info = new DhcpInfo();
60 info.ipAddress = convertToInt(ipAddress);
61 info.gateway = convertToInt(gateway);
63 InetAddress inetAddress = NetworkUtils.numericToInetAddress(ipAddress);
64 info.netmask = NetworkUtils.prefixLengthToNetmaskInt(prefixLength);
65 } catch (IllegalArgumentException e) {}
66 info.dns1 = convertToInt(dns1);
67 info.dns2 = convertToInt(dns2);
68 info.serverAddress = convertToInt(serverAddress);
69 info.leaseDuration = leaseDuration;
73 public LinkAddress makeLinkAddress() {
74 if (TextUtils.isEmpty(ipAddress)) {
75 Log.e(TAG, "makeLinkAddress with empty ipAddress");
78 return new LinkAddress(NetworkUtils.numericToInetAddress(ipAddress), prefixLength);
81 public LinkProperties makeLinkProperties() {
82 LinkProperties p = new LinkProperties();
83 p.addLinkAddress(makeLinkAddress());
84 if (TextUtils.isEmpty(gateway) == false) {
85 p.addGateway(NetworkUtils.numericToInetAddress(gateway));
87 if (TextUtils.isEmpty(dns1) == false) {
88 p.addDns(NetworkUtils.numericToInetAddress(dns1));
90 Log.d(TAG, "makeLinkProperties with empty dns1!");
92 if (TextUtils.isEmpty(dns2) == false) {
93 p.addDns(NetworkUtils.numericToInetAddress(dns2));
95 Log.d(TAG, "makeLinkProperties with empty dns2!");
100 public String toString() {
101 return "addr: " + ipAddress + "/" + prefixLength +
102 " gateway: " + gateway +
103 " dns: " + dns1 + "," + dns2 +
104 " dhcpServer: " + serverAddress +
105 " leaseDuration: " + leaseDuration;