Search in sources :

Example 21 with DhcpResults

use of android.net.DhcpResults in project platform_frameworks_base by android.

the class DhcpPacketTest method testOffer1.

@SmallTest
public void testOffer1() throws Exception {
    // TODO: Turn all of these into golden files. This will probably require modifying
    // Android.mk appropriately, making this into an AndroidTestCase, and adding code to read
    // the golden files from the test APK's assets via mContext.getAssets().
    final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(// IP header.
    "451001480000000080118849c0a89003c0a89ff7" + // UDP header.
    "004300440134dcfa" + // BOOTP header.
    "02010600c997a63b0000000000000000c0a89ff70000000000000000" + // MAC address.
    "30766ff2a90c00000000000000000000" + // Server name.
    "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // File.
    "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // Options
    "638253633501023604c0a89003330400001c200104fffff0000304c0a89ffe06080808080808080404" + "3a0400000e103b040000189cff00000000000000000000"));
    DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
    // Implicitly checks it's non-null.
    assertTrue(offerPacket instanceof DhcpOfferPacket);
    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);
}
Also used : DhcpResults(android.net.DhcpResults) ByteBuffer(java.nio.ByteBuffer) DhcpPacket(android.net.dhcp.DhcpPacket) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 22 with DhcpResults

use of android.net.DhcpResults in project platform_frameworks_base by android.

the class DhcpPacketTest method assertLeaseTimeParses.

private void assertLeaseTimeParses(boolean expectValid, Integer rawLeaseTime, long leaseTimeMillis, byte[] leaseTimeBytes) throws Exception {
    TestDhcpPacket testPacket = new TestDhcpPacket(DHCP_MESSAGE_TYPE_OFFER);
    if (leaseTimeBytes != null) {
        testPacket.setLeaseTimeBytes(leaseTimeBytes);
    }
    ByteBuffer packet = testPacket.build();
    DhcpPacket offerPacket = null;
    if (!expectValid) {
        try {
            offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
            fail("Invalid packet parsed successfully: " + offerPacket);
        } catch (ParseException expected) {
        }
        return;
    }
    offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
    assertNotNull(offerPacket);
    assertEquals(rawLeaseTime, offerPacket.mLeaseTime);
    // Just check this doesn't crash.
    DhcpResults dhcpResults = offerPacket.toDhcpResults();
    assertEquals(leaseTimeMillis, offerPacket.getLeaseTimeMillis());
}
Also used : DhcpResults(android.net.DhcpResults) ByteBuffer(java.nio.ByteBuffer) DhcpPacket(android.net.dhcp.DhcpPacket)

Example 23 with DhcpResults

use of android.net.DhcpResults in project platform_frameworks_base by android.

the class DhcpPacket method toDhcpResults.

/**
     *  Construct a DhcpResults object from a DHCP reply packet.
     */
public DhcpResults toDhcpResults() {
    Inet4Address ipAddress = mYourIp;
    if (ipAddress.equals(Inet4Address.ANY)) {
        ipAddress = mClientIp;
        if (ipAddress.equals(Inet4Address.ANY)) {
            return null;
        }
    }
    int prefixLength;
    if (mSubnetMask != null) {
        try {
            prefixLength = NetworkUtils.netmaskToPrefixLength(mSubnetMask);
        } catch (IllegalArgumentException e) {
            // Non-contiguous netmask.
            return null;
        }
    } else {
        prefixLength = NetworkUtils.getImplicitNetmask(ipAddress);
    }
    DhcpResults results = new DhcpResults();
    try {
        results.ipAddress = new LinkAddress(ipAddress, prefixLength);
    } catch (IllegalArgumentException e) {
        return null;
    }
    if (mGateways.size() > 0) {
        results.gateway = mGateways.get(0);
    }
    results.dnsServers.addAll(mDnsServers);
    results.domains = mDomainName;
    results.serverAddress = mServerIdentifier;
    results.vendorInfo = mVendorInfo;
    results.leaseDuration = (mLeaseTime != null) ? mLeaseTime : INFINITE_LEASE;
    results.mtu = (mMtu != null && MIN_MTU <= mMtu && mMtu <= MAX_MTU) ? mMtu : 0;
    return results;
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) DhcpResults(android.net.DhcpResults)

Example 24 with DhcpResults

use of android.net.DhcpResults in project android_frameworks_base by crdroidandroid.

the class IpManager method startIPv4.

private boolean startIPv4() {
    // handle the result accordingly.
    if (mConfiguration.mStaticIpConfig != null) {
        if (setIPv4Address(mConfiguration.mStaticIpConfig.ipAddress)) {
            handleIPv4Success(new DhcpResults(mConfiguration.mStaticIpConfig));
        } else {
            return false;
        }
    } else {
        // Start DHCPv4.
        mDhcpClient = DhcpClient.makeDhcpClient(mContext, IpManager.this, mInterfaceName);
        mDhcpClient.registerForPreDhcpNotification();
        mDhcpClient.sendMessage(DhcpClient.CMD_START_DHCP);
    }
    return true;
}
Also used : DhcpResults(android.net.DhcpResults)

Example 25 with DhcpResults

use of android.net.DhcpResults in project android_frameworks_base by crdroidandroid.

the class DhcpPacketTest method assertLeaseTimeParses.

private void assertLeaseTimeParses(boolean expectValid, Integer rawLeaseTime, long leaseTimeMillis, byte[] leaseTimeBytes) throws Exception {
    TestDhcpPacket testPacket = new TestDhcpPacket(DHCP_MESSAGE_TYPE_OFFER);
    if (leaseTimeBytes != null) {
        testPacket.setLeaseTimeBytes(leaseTimeBytes);
    }
    ByteBuffer packet = testPacket.build();
    DhcpPacket offerPacket = null;
    if (!expectValid) {
        try {
            offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
            fail("Invalid packet parsed successfully: " + offerPacket);
        } catch (ParseException expected) {
        }
        return;
    }
    offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
    assertNotNull(offerPacket);
    assertEquals(rawLeaseTime, offerPacket.mLeaseTime);
    // Just check this doesn't crash.
    DhcpResults dhcpResults = offerPacket.toDhcpResults();
    assertEquals(leaseTimeMillis, offerPacket.getLeaseTimeMillis());
}
Also used : DhcpResults(android.net.DhcpResults) ByteBuffer(java.nio.ByteBuffer) DhcpPacket(android.net.dhcp.DhcpPacket)

Aggregations

DhcpResults (android.net.DhcpResults)55 DhcpPacket (android.net.dhcp.DhcpPacket)40 ByteBuffer (java.nio.ByteBuffer)36 SmallTest (android.test.suitebuilder.annotation.SmallTest)28 LinkAddress (android.net.LinkAddress)9 Inet4Address (java.net.Inet4Address)5 LinkProperties (android.net.LinkProperties)4 ProvisioningChange (android.net.LinkProperties.ProvisioningChange)4 DhcpInfo (android.net.DhcpInfo)1 NetworkInfo (android.net.NetworkInfo)1 RouteInfo (android.net.RouteInfo)1 Message (android.os.Message)1 InetAddress (java.net.InetAddress)1