DhcpInfoInternal.java revision 992564e481af13cbcb058ee801f9254a520c54a1
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;
25
26/**
27 * A simple object for retrieving the results of a DHCP request.
28 * Replaces (internally) the IPv4-only DhcpInfo class.
29 * @hide
30 */
31public class DhcpInfoInternal {
32    private final static String TAG = "DhcpInfoInternal";
33    public String ipAddress;
34    public String gateway;
35    public int prefixLength;
36
37    public String dns1;
38    public String dns2;
39
40    public String serverAddress;
41    public int leaseDuration;
42
43    public DhcpInfoInternal() {
44    }
45
46    private int convertToInt(String addr) {
47        if (addr != null) {
48            try {
49                InetAddress inetAddress = NetworkUtils.numericToInetAddress(addr);
50                if (inetAddress instanceof Inet4Address) {
51                    return NetworkUtils.inetAddressToInt(inetAddress);
52                }
53            } catch (IllegalArgumentException e) {}
54        }
55        return 0;
56    }
57
58    public DhcpInfo makeDhcpInfo() {
59        DhcpInfo info = new DhcpInfo();
60        info.ipAddress = convertToInt(ipAddress);
61        info.gateway = convertToInt(gateway);
62        try {
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;
70        return info;
71    }
72
73    public LinkAddress makeLinkAddress() {
74        if (TextUtils.isEmpty(ipAddress)) {
75            Log.e(TAG, "makeLinkAddress with empty ipAddress");
76            return null;
77        }
78        return new LinkAddress(NetworkUtils.numericToInetAddress(ipAddress), prefixLength);
79    }
80
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));
86        }
87        if (TextUtils.isEmpty(dns1) == false) {
88            p.addDns(NetworkUtils.numericToInetAddress(dns1));
89        } else {
90            Log.d(TAG, "makeLinkProperties with empty dns1!");
91        }
92        if (TextUtils.isEmpty(dns2) == false) {
93            p.addDns(NetworkUtils.numericToInetAddress(dns2));
94        } else {
95            Log.d(TAG, "makeLinkProperties with empty dns2!");
96        }
97        return p;
98    }
99
100    public String toString() {
101        return "addr: " + ipAddress + "/" + prefixLength +
102                " gateway: " + gateway +
103                " dns: " + dns1 + "," + dns2 +
104                " dhcpServer: " + serverAddress +
105                " leaseDuration: " + leaseDuration;
106    }
107}
108