Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9a5255c authored by Lorenzo Colitti's avatar Lorenzo Colitti Committed by Android (Google) Code Review
Browse files

Merge changes I35b8c943,I5e9fee54,I8c5a7dc8 into qt-dev

* changes:
  Fix some checkstyle nits in DhcpPacketTest.
  Additional code for server name in DHCP packets.
  Parse the server host name field of the dhcp package
parents f8e4e75d a7af4126
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -64,6 +64,8 @@ public final class DhcpResults implements Parcelable {
    @UnsupportedAppUsage
    public int mtu;

    public String serverHostName;

    public DhcpResults() {
        super();
    }
@@ -97,6 +99,7 @@ public final class DhcpResults implements Parcelable {
            vendorInfo = source.vendorInfo;
            leaseDuration = source.leaseDuration;
            mtu = source.mtu;
            serverHostName = source.serverHostName;
        }
    }

@@ -129,6 +132,7 @@ public final class DhcpResults implements Parcelable {
        vendorInfo = null;
        leaseDuration = 0;
        mtu = 0;
        serverHostName = null;
    }

    @Override
@@ -139,6 +143,7 @@ public final class DhcpResults implements Parcelable {
        str.append(" Vendor info ").append(vendorInfo);
        str.append(" lease ").append(leaseDuration).append(" seconds");
        if (mtu != 0) str.append(" MTU ").append(mtu);
        str.append(" Servername ").append(serverHostName);

        return str.toString();
    }
@@ -154,6 +159,7 @@ public final class DhcpResults implements Parcelable {
        return toStaticIpConfiguration().equals(target.toStaticIpConfiguration())
                && Objects.equals(serverAddress, target.serverAddress)
                && Objects.equals(vendorInfo, target.vendorInfo)
                && Objects.equals(serverHostName, target.serverHostName)
                && leaseDuration == target.leaseDuration
                && mtu == target.mtu;
    }
@@ -179,6 +185,7 @@ public final class DhcpResults implements Parcelable {
        dest.writeInt(mtu);
        InetAddressUtils.parcelInetAddress(dest, serverAddress, flags);
        dest.writeString(vendorInfo);
        dest.writeString(serverHostName);
    }

    @Override
@@ -193,6 +200,7 @@ public final class DhcpResults implements Parcelable {
        dhcpResults.mtu = in.readInt();
        dhcpResults.serverAddress = (Inet4Address) InetAddressUtils.unparcelInetAddress(in);
        dhcpResults.vendorInfo = in.readString();
        dhcpResults.serverHostName = in.readString();
        return dhcpResults;
    }

+33 −3
Original line number Diff line number Diff line
@@ -194,6 +194,18 @@ public abstract class DhcpPacket {
     */
    public static final String VENDOR_INFO_ANDROID_METERED = "ANDROID_METERED";

    /**
     * DHCP Optional Type: Option overload option
     */
    protected static final byte DHCP_OPTION_OVERLOAD = 52;

    /**
     * Possible values of the option overload option.
     */
    private static final byte OPTION_OVERLOAD_FILE = 1;
    private static final byte OPTION_OVERLOAD_SNAME = 2;
    private static final byte OPTION_OVERLOAD_BOTH = 3;

    /**
     * DHCP Optional Type: DHCP Requested IP Address
     */
@@ -308,6 +320,11 @@ public abstract class DhcpPacket {
     */
    protected final byte[] mClientMac;

    /**
     * The server host name from server.
     */
    protected String mServerHostName;

    /**
     * Asks the packet object to create a ByteBuffer serialization of
     * the packet for transmission.
@@ -848,6 +865,8 @@ public abstract class DhcpPacket {
        Inet4Address ipDst = null;
        Inet4Address bcAddr = null;
        Inet4Address requestedIp = null;
        String serverHostName;
        byte optionOverload = 0;

        // The following are all unsigned integers. Internally we store them as signed integers of
        // the same length because that way we're guaranteed that they can't be out of the range of
@@ -989,9 +1008,9 @@ public abstract class DhcpPacket {
        packet.get(clientMac);

        // skip over address padding (16 octets allocated)
        packet.position(packet.position() + (16 - addrLen)
                        + 64    // skip server host name (64 chars)
                        + 128); // skip boot file name (128 chars)
        packet.position(packet.position() + (16 - addrLen));
        serverHostName = readAsciiString(packet, 64, false);
        packet.position(packet.position() + 128);

        // Ensure this is a DHCP packet with a magic cookie, and not BOOTP. http://b/31850211
        if (packet.remaining() < 4) {
@@ -1102,6 +1121,11 @@ public abstract class DhcpPacket {
                            // Embedded nulls are safe as this does not get passed to netd.
                            vendorInfo = readAsciiString(packet, optionLen, true);
                            break;
                        case DHCP_OPTION_OVERLOAD:
                            expectedLen = 1;
                            optionOverload = packet.get();
                            optionOverload &= OPTION_OVERLOAD_BOTH;
                            break;
                        default:
                            // ignore any other parameters
                            for (int i = 0; i < optionLen; i++) {
@@ -1192,6 +1216,11 @@ public abstract class DhcpPacket {
        newPacket.mT2 = T2;
        newPacket.mVendorId = vendorId;
        newPacket.mVendorInfo = vendorInfo;
        if ((optionOverload & OPTION_OVERLOAD_SNAME) == 0) {
            newPacket.mServerHostName = serverHostName;
        } else {
            newPacket.mServerHostName = "";
        }
        return newPacket;
    }

@@ -1251,6 +1280,7 @@ public abstract class DhcpPacket {
        results.vendorInfo = mVendorInfo;
        results.leaseDuration = (mLeaseTime != null) ? mLeaseTime : INFINITE_LEASE;
        results.mtu = (mMtu != null && MIN_MTU <= mMtu && mMtu <= MAX_MTU) ? mMtu : 0;
        results.serverHostName = mServerHostName;

        return results;
    }
+36 −13
Original line number Diff line number Diff line
@@ -302,8 +302,9 @@ public class DhcpPacketTest {
    }

    private void assertDhcpResults(String ipAddress, String gateway, String dnsServersString,
            String domains, String serverAddress, String vendorInfo, int leaseDuration,
            boolean hasMeteredHint, int mtu, DhcpResults dhcpResults) throws Exception {
            String domains, String serverAddress, String serverHostName, String vendorInfo,
            int leaseDuration, boolean hasMeteredHint, int mtu, DhcpResults dhcpResults)
                    throws Exception {
        assertEquals(new LinkAddress(ipAddress), dhcpResults.ipAddress);
        assertEquals(v4Address(gateway), dhcpResults.gateway);

@@ -316,6 +317,7 @@ public class DhcpPacketTest {

        assertEquals(domains, dhcpResults.domains);
        assertEquals(v4Address(serverAddress), dhcpResults.serverAddress);
        assertEquals(serverHostName, dhcpResults.serverHostName);
        assertEquals(vendorInfo, dhcpResults.vendorInfo);
        assertEquals(leaseDuration, dhcpResults.leaseDuration);
        assertEquals(hasMeteredHint, dhcpResults.hasMeteredHint());
@@ -327,6 +329,7 @@ public class DhcpPacketTest {
        // TODO: Turn all of these into golden files. This will probably require using
        // androidx.test.InstrumentationRegistry for obtaining a Context object
        // to read such golden files, along with an appropriate Android.mk.
        // CHECKSTYLE:OFF Generated code
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // IP header.
            "451001480000000080118849c0a89003c0a89ff7" +
@@ -347,16 +350,18 @@ public class DhcpPacketTest {
            // Options
            "638253633501023604c0a89003330400001c200104fffff0000304c0a89ffe06080808080808080404" +
            "3a0400000e103b040000189cff00000000000000000000"));
        // CHECKSTYLE:ON Generated code

        DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
        assertTrue(offerPacket instanceof DhcpOfferPacket);  // Implicitly checks it's non-null.
        DhcpResults dhcpResults = offerPacket.toDhcpResults();
        assertDhcpResults("192.168.159.247/20", "192.168.159.254", "8.8.8.8,8.8.4.4",
                null, "192.168.144.3", null, 7200, false, 0, dhcpResults);
                null, "192.168.144.3", "", null, 7200, false, 0, dhcpResults);
    }

    @Test
    public void testOffer2() throws Exception {
        // CHECKSTYLE:OFF Generated code
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // IP header.
            "450001518d0600004011144dc0a82b01c0a82bf7" +
@@ -366,9 +371,9 @@ public class DhcpPacketTest {
            "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" +
            // MAC address.
            "30766ff2a90c00000000000000000000" +
            // Server name.
            "0000000000000000000000000000000000000000000000000000000000000000" +
            "0000000000000000000000000000000000000000000000000000000000000000" +
            // Server name ("dhcp.android.com" plus invalid "AAAA" after null terminator).
            "646863702e616e64726f69642e636f6d00000000000000000000000000000000" +
            "0000000000004141414100000000000000000000000000000000000000000000" +
            // File.
            "0000000000000000000000000000000000000000000000000000000000000000" +
            "0000000000000000000000000000000000000000000000000000000000000000" +
@@ -377,13 +382,15 @@ public class DhcpPacketTest {
            // Options
            "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
            "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"));
        // CHECKSTYLE:ON Generated code

        assertEquals(337, packet.limit());
        DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
        assertTrue(offerPacket instanceof DhcpOfferPacket);  // Implicitly checks it's non-null.
        DhcpResults dhcpResults = offerPacket.toDhcpResults();
        assertDhcpResults("192.168.43.247/24", "192.168.43.1", "192.168.43.1",
                null, "192.168.43.1", "ANDROID_METERED", 3600, true, 0, dhcpResults);
                null, "192.168.43.1", "dhcp.android.com", "ANDROID_METERED", 3600, true, 0,
                dhcpResults);
        assertTrue(dhcpResults.hasMeteredHint());
    }

@@ -588,11 +595,12 @@ public class DhcpPacketTest {
        assertTrue(offerPacket instanceof DhcpOfferPacket);  // Implicitly checks it's non-null.
        DhcpResults dhcpResults = offerPacket.toDhcpResults();
        assertDhcpResults("192.168.159.247/20", "192.168.159.254", "8.8.8.8,8.8.4.4",
                null, "192.168.144.3", null, 7200, false, expectedMtu, dhcpResults);
                null, "192.168.144.3", "", null, 7200, false, expectedMtu, dhcpResults);
    }

    @Test
    public void testMtu() throws Exception {
        // CHECKSTYLE:OFF Generated code
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // IP header.
            "451001480000000080118849c0a89003c0a89ff7" +
@@ -613,6 +621,7 @@ public class DhcpPacketTest {
            // Options
            "638253633501023604c0a89003330400001c200104fffff0000304c0a89ffe06080808080808080404" +
            "3a0400000e103b040000189cff00000000"));
        // CHECKSTYLE:ON Generated code

        checkMtu(packet, 0, null);
        checkMtu(packet, 0, mtuBytes(1501));
@@ -629,6 +638,7 @@ public class DhcpPacketTest {

    @Test
    public void testBadHwaddrLength() throws Exception {
        // CHECKSTYLE:OFF Generated code
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // IP header.
            "450001518d0600004011144dc0a82b01c0a82bf7" +
@@ -649,6 +659,7 @@ public class DhcpPacketTest {
            // Options
            "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
            "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"));
        // CHECKSTYLE:ON Generated code
        String expectedClientMac = "30766FF2A90C";

        final int hwAddrLenOffset = 20 + 8 + 2;
@@ -705,6 +716,7 @@ public class DhcpPacketTest {
        //    store any information in the overloaded fields).
        //
        // For now, we just check that it parses correctly.
        // CHECKSTYLE:OFF Generated code
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // Ethernet header.
            "b4cef6000000e80462236e300800" +
@@ -727,16 +739,18 @@ public class DhcpPacketTest {
            // Options
            "638253633501023604010101010104ffff000033040000a8c03401030304ac1101010604ac110101" +
            "0000000000000000000000000000000000000000000000ff000000"));
        // CHECKSTYLE:ON Generated code

        DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
        assertTrue(offerPacket instanceof DhcpOfferPacket);
        DhcpResults dhcpResults = offerPacket.toDhcpResults();
        assertDhcpResults("172.17.152.118/16", "172.17.1.1", "172.17.1.1",
                null, "1.1.1.1", null, 43200, false, 0, dhcpResults);
                null, "1.1.1.1", "", null, 43200, false, 0, dhcpResults);
    }

    @Test
    public void testBug2111() throws Exception {
        // CHECKSTYLE:OFF Generated code
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // IP header.
            "4500014c00000000ff119beac3eaf3880a3f5d04" +
@@ -757,16 +771,18 @@ public class DhcpPacketTest {
            // Options.
            "638253633501023604c00002fe33040000bfc60104fffff00003040a3f50010608c0000201c0000202" +
            "0f0f646f6d61696e3132332e636f2e756b0000000000ff00000000"));
        // CHECKSTYLE:ON Generated code

        DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
        assertTrue(offerPacket instanceof DhcpOfferPacket);
        DhcpResults dhcpResults = offerPacket.toDhcpResults();
        assertDhcpResults("10.63.93.4/20", "10.63.80.1", "192.0.2.1,192.0.2.2",
                "domain123.co.uk", "192.0.2.254", null, 49094, false, 0, dhcpResults);
                "domain123.co.uk", "192.0.2.254", "", null, 49094, false, 0, dhcpResults);
    }

    @Test
    public void testBug2136() throws Exception {
        // CHECKSTYLE:OFF Generated code
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // Ethernet header.
            "bcf5ac000000d0c7890000000800" +
@@ -789,17 +805,19 @@ public class DhcpPacketTest {
            // Options.
            "6382536335010236040a20ff80330400001c200104fffff00003040a20900106089458413494584135" +
            "0f0b6c616e63732e61632e756b000000000000000000ff00000000"));
        // CHECKSTYLE:ON Generated code

        DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
        assertTrue(offerPacket instanceof DhcpOfferPacket);
        assertEquals("BCF5AC000000", HexDump.toHexString(offerPacket.getClientMac()));
        DhcpResults dhcpResults = offerPacket.toDhcpResults();
        assertDhcpResults("10.32.158.205/20", "10.32.144.1", "148.88.65.52,148.88.65.53",
                "lancs.ac.uk", "10.32.255.128", null, 7200, false, 0, dhcpResults);
                "lancs.ac.uk", "10.32.255.128", "", null, 7200, false, 0, dhcpResults);
    }

    @Test
    public void testUdpServerAnySourcePort() throws Exception {
        // CHECKSTYLE:OFF Generated code
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // Ethernet header.
            "9cd917000000001c2e0000000800" +
@@ -823,6 +841,7 @@ public class DhcpPacketTest {
            // Options.
            "6382536335010236040a0169fc3304000151800104ffff000003040a0fc817060cd1818003d1819403" +
            "d18180060f0777766d2e6564751c040a0fffffff000000"));
        // CHECKSTYLE:ON Generated code

        DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
        assertTrue(offerPacket instanceof DhcpOfferPacket);
@@ -830,11 +849,12 @@ public class DhcpPacketTest {
        DhcpResults dhcpResults = offerPacket.toDhcpResults();
        assertDhcpResults("10.15.122.242/16", "10.15.200.23",
                "209.129.128.3,209.129.148.3,209.129.128.6",
                "wvm.edu", "10.1.105.252", null, 86400, false, 0, dhcpResults);
                "wvm.edu", "10.1.105.252", "", null, 86400, false, 0, dhcpResults);
    }

    @Test
    public void testUdpInvalidDstPort() throws Exception {
        // CHECKSTYLE:OFF Generated code
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // Ethernet header.
            "9cd917000000001c2e0000000800" +
@@ -858,6 +878,7 @@ public class DhcpPacketTest {
            // Options.
            "6382536335010236040a0169fc3304000151800104ffff000003040a0fc817060cd1818003d1819403" +
            "d18180060f0777766d2e6564751c040a0fffffff000000"));
        // CHECKSTYLE:ON Generated code

        try {
            DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
@@ -867,6 +888,7 @@ public class DhcpPacketTest {

    @Test
    public void testMultipleRouters() throws Exception {
        // CHECKSTYLE:OFF Generated code
        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
            // Ethernet header.
            "fc3d93000000" + "081735000000" + "0800" +
@@ -889,13 +911,14 @@ public class DhcpPacketTest {
            // Options.
            "638253633501023604c0abbd023304000070803a04000038403b04000062700104ffffff00" +
            "0308c0a8bd01ffffff0006080808080808080404ff000000000000"));
        // CHECKSTYLE:ON Generated code

        DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
        assertTrue(offerPacket instanceof DhcpOfferPacket);
        assertEquals("FC3D93000000", HexDump.toHexString(offerPacket.getClientMac()));
        DhcpResults dhcpResults = offerPacket.toDhcpResults();
        assertDhcpResults("192.168.189.49/24", "192.168.189.1", "8.8.8.8,8.8.4.4",
                null, "192.171.189.2", null, 28800, false, 0, dhcpResults);
                null, "192.171.189.2", "", null, 28800, false, 0, dhcpResults);
    }

    @Test
+2 −1
Original line number Diff line number Diff line
@@ -24,4 +24,5 @@ parcelable DhcpResultsParcelable {
    int mtu;
    String serverAddress;
    String vendorInfo;
    String serverHostName;
}
+2 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ public final class IpConfigurationParcelableUtil {
        p.mtu = results.mtu;
        p.serverAddress = parcelAddress(results.serverAddress);
        p.vendorInfo = results.vendorInfo;
        p.serverHostName = results.serverHostName;
        return p;
    }

@@ -54,6 +55,7 @@ public final class IpConfigurationParcelableUtil {
        results.mtu = p.mtu;
        results.serverAddress = (Inet4Address) unparcelAddress(p.serverAddress);
        results.vendorInfo = p.vendorInfo;
        results.serverHostName = p.serverHostName;
        return results;
    }

Loading