14717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt/*
24717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * Copyright (C) 2012 The Android Open Source Project
34717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt *
44717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
54717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * you may not use this file except in compliance with the License.
64717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * You may obtain a copy of the License at
74717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt *
84717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
94717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt *
104717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * Unless required by applicable law or agreed to in writing, software
114717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
124717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * See the License for the specific language governing permissions and
144717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * limitations under the License.
154717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt */
164717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
174717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwaltpackage android.net;
184717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
190a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colittiimport android.net.NetworkUtils;
204717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwaltimport android.os.Parcelable;
214717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwaltimport android.os.Parcel;
223c97f944eb08a2720b43382d94a56383c2cd6b9bRobert Greenwaltimport android.text.TextUtils;
234717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwaltimport android.util.Log;
244717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
254717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwaltimport java.net.InetAddress;
260a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colittiimport java.net.Inet4Address;
270a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colittiimport java.util.Objects;
284717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
294717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt/**
304717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * A simple object for retrieving the results of a DHCP request.
314717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * Optimized (attempted) for that jni interface
324717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * TODO - remove when DhcpInfo is deprecated.  Move the remaining api to LinkProperties.
334717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt * @hide
344717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt */
350a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colittipublic class DhcpResults extends StaticIpConfiguration {
364717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    private static final String TAG = "DhcpResults";
374717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
384717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public InetAddress serverAddress;
394717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
400a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    /** Vendor specific information (from RFC 2132). */
414717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public String vendorInfo;
424717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
434717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public int leaseDuration;
444717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
454717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public DhcpResults() {
460a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        super();
470a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    }
480a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti
490a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    public DhcpResults(StaticIpConfiguration source) {
500a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        super(source);
514717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
524717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
534717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    /** copy constructor */
544717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public DhcpResults(DhcpResults source) {
550a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        super(source);
560a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti
574717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        if (source != null) {
580a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti            // All these are immutable, so no need to make copies.
594717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            serverAddress = source.serverAddress;
604717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            vendorInfo = source.vendorInfo;
610a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti            leaseDuration = source.leaseDuration;
624717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        }
634717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
644717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
654717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    /**
664717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt     * Updates the DHCP fields that need to be retained from
674717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt     * original DHCP request if the current renewal shows them
684717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt     * being empty.
694717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt     */
704717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public void updateFromDhcpRequest(DhcpResults orig) {
710a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        if (orig == null) return;
720a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        if (gateway == null) gateway = orig.gateway;
730a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        if (dnsServers.size() == 0) {
740a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti            dnsServers.addAll(orig.dnsServers);
754717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        }
764717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
774717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
784717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    /**
794717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt     * Test if this DHCP lease includes vendor hint that network link is
804717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt     * metered, and sensitive to heavy data transfers.
814717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt     */
824717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public boolean hasMeteredHint() {
834717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        if (vendorInfo != null) {
844717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            return vendorInfo.contains("ANDROID_METERED");
854717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        } else {
864717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            return false;
874717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        }
884717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
894717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
904717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public void clear() {
910a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        super.clear();
924717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        vendorInfo = null;
934717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        leaseDuration = 0;
944717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
954717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
964717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    @Override
974717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public String toString() {
980a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        StringBuffer str = new StringBuffer(super.toString());
994717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1004717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        str.append(" DHCP server ").append(serverAddress);
1014717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        str.append(" Vendor info ").append(vendorInfo);
1024717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        str.append(" lease ").append(leaseDuration).append(" seconds");
1034717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1044717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        return str.toString();
1054717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
1064717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1074717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    @Override
1084717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public boolean equals(Object obj) {
1094717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        if (this == obj) return true;
1104717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1114717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        if (!(obj instanceof DhcpResults)) return false;
1124717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1134717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        DhcpResults target = (DhcpResults)obj;
1144717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1150a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        return super.equals((StaticIpConfiguration) obj) &&
1160a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti                Objects.equals(serverAddress, target.serverAddress) &&
1170a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti                Objects.equals(vendorInfo, target.vendorInfo) &&
1180a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti                leaseDuration == target.leaseDuration;
1194717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
1204717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1214717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    /** Implement the Parcelable interface */
1224717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public static final Creator<DhcpResults> CREATOR =
1234717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        new Creator<DhcpResults>() {
1244717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            public DhcpResults createFromParcel(Parcel in) {
1250a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti                DhcpResults dhcpResults = new DhcpResults();
1260a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti                readFromParcel(dhcpResults, in);
1270a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti                return dhcpResults;
1284717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            }
1294717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1304717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            public DhcpResults[] newArray(int size) {
1314717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt                return new DhcpResults[size];
1324717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            }
1334717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        };
1344717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1350a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    /** Implement the Parcelable interface */
1360a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    public void writeToParcel(Parcel dest, int flags) {
1370a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        super.writeToParcel(dest, flags);
1380a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        dest.writeInt(leaseDuration);
1390a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        NetworkUtils.parcelInetAddress(dest, serverAddress, flags);
1400a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        dest.writeString(vendorInfo);
1410a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    }
1420a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti
1430a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    private static void readFromParcel(DhcpResults dhcpResults, Parcel in) {
1440a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        StaticIpConfiguration.readFromParcel(dhcpResults, in);
1450a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        dhcpResults.leaseDuration = in.readInt();
1460a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        dhcpResults.serverAddress = NetworkUtils.unparcelInetAddress(in);
1470a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        dhcpResults.vendorInfo = in.readString();
1484717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
1494717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1500a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    // Utils for jni population - false on success
1510a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    // Not part of the superclass because they're only used by the JNI iterface to the DHCP daemon.
1520a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    public boolean setIpAddress(String addrString, int prefixLength) {
1534717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        try {
1540a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti            Inet4Address addr = (Inet4Address) NetworkUtils.numericToInetAddress(addrString);
1550a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti            ipAddress = new LinkAddress(addr, prefixLength);
1560a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti        } catch (IllegalArgumentException|ClassCastException e) {
1570a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti            Log.e(TAG, "setIpAddress failed with addrString " + addrString + "/" + prefixLength);
1584717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            return true;
1594717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        }
1604717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        return false;
1614717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
1624717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1630a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti    public boolean setGateway(String addrString) {
1644717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        try {
1650a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti            gateway = NetworkUtils.numericToInetAddress(addrString);
1664717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        } catch (IllegalArgumentException e) {
1670a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti            Log.e(TAG, "setGateway failed with addrString " + addrString);
1684717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            return true;
1694717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        }
1704717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        return false;
1714717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
1724717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1734717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public boolean addDns(String addrString) {
1743c97f944eb08a2720b43382d94a56383c2cd6b9bRobert Greenwalt        if (TextUtils.isEmpty(addrString) == false) {
1753c97f944eb08a2720b43382d94a56383c2cd6b9bRobert Greenwalt            try {
1760a82e80073e193725a9d4c84a93db8a04b2456b9Lorenzo Colitti                dnsServers.add(NetworkUtils.numericToInetAddress(addrString));
1773c97f944eb08a2720b43382d94a56383c2cd6b9bRobert Greenwalt            } catch (IllegalArgumentException e) {
1783c97f944eb08a2720b43382d94a56383c2cd6b9bRobert Greenwalt                Log.e(TAG, "addDns failed with addrString " + addrString);
1793c97f944eb08a2720b43382d94a56383c2cd6b9bRobert Greenwalt                return true;
1803c97f944eb08a2720b43382d94a56383c2cd6b9bRobert Greenwalt            }
1814717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        }
1824717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        return false;
1834717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
1844717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1854717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public boolean setServerAddress(String addrString) {
1864717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        try {
1874717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            serverAddress = NetworkUtils.numericToInetAddress(addrString);
1884717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        } catch (IllegalArgumentException e) {
1894717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            Log.e(TAG, "setServerAddress failed with addrString " + addrString);
1904717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt            return true;
1914717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        }
1924717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        return false;
1934717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
1944717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1954717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public void setLeaseDuration(int duration) {
1964717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        leaseDuration = duration;
1974717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
1984717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
1994717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    public void setVendorInfo(String info) {
2004717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt        vendorInfo = info;
2014717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt    }
2024717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt
203c53113b37f33c7ed19660c8ec5bfd578e8bb5409Paul Jensen    public void setDomains(String newDomains) {
204c53113b37f33c7ed19660c8ec5bfd578e8bb5409Paul Jensen        domains = newDomains;
2058058f621891b41c6864b6004c1c47647436a0ac1Robert Greenwalt    }
2064717c261b2c670d5c0925e3527a864aa52db6ac0Robert Greenwalt}
207