DhcpInfoInternal.java revision 0216e618198393bfd7ac0625fa6ad251d5ea682f
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 java.net.InetAddress;
20import java.net.Inet4Address;
21import java.net.UnknownHostException;
22
23/**
24 * A simple object for retrieving the results of a DHCP request.
25 * Replaces (internally) the IPv4-only DhcpInfo class.
26 * @hide
27 */
28public class DhcpInfoInternal {
29    public String ipAddress;
30    public String gateway;
31    public int prefixLength;
32
33    public String dns1;
34    public String dns2;
35
36    public String serverAddress;
37    public int leaseDuration;
38
39    public DhcpInfoInternal() {
40    }
41
42    private int convertToInt(String addr) {
43        try {
44            InetAddress inetAddress = NetworkUtils.numericToInetAddress(addr);
45            if (inetAddress instanceof Inet4Address) {
46                return NetworkUtils.inetAddressToInt(inetAddress);
47            }
48        } catch (IllegalArgumentException e) {}
49        return 0;
50    }
51
52    public DhcpInfo makeDhcpInfo() {
53        DhcpInfo info = new DhcpInfo();
54        info.ipAddress = convertToInt(ipAddress);
55        info.gateway = convertToInt(gateway);
56        try {
57            InetAddress inetAddress = NetworkUtils.numericToInetAddress(ipAddress);
58            info.netmask = NetworkUtils.prefixLengthToNetmaskInt(prefixLength);
59        } catch (IllegalArgumentException e) {}
60        info.dns1 = convertToInt(dns1);
61        info.dns2 = convertToInt(dns2);
62        info.serverAddress = convertToInt(serverAddress);
63        info.leaseDuration = leaseDuration;
64        return info;
65    }
66
67    public LinkAddress makeLinkAddress() {
68        return new LinkAddress(NetworkUtils.numericToInetAddress(ipAddress), prefixLength);
69    }
70
71    public LinkProperties makeLinkProperties() {
72        LinkProperties p = new LinkProperties();
73        p.addLinkAddress(makeLinkAddress());
74        p.setGateway(NetworkUtils.numericToInetAddress(gateway));
75        p.addDns(NetworkUtils.numericToInetAddress(dns1));
76        p.addDns(NetworkUtils.numericToInetAddress(dns2));
77        return p;
78    }
79
80    public String toString() {
81        return "addr: " + ipAddress + "/" + prefixLength +
82                " gateway: " + gateway +
83                " dns: " + dns1 + "," + dns2 +
84                " dhcpServer: " + serverAddress +
85                " leaseDuration: " + leaseDuration;
86    }
87}
88