use of android.net.DhcpResults in project android_frameworks_base by ResurrectionRemix.
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());
}
use of android.net.DhcpResults in project android_frameworks_base by DirtyUnicorns.
the class DhcpPacketTest method testPadAndOverloadedOptionsOffer.
@SmallTest
public void testPadAndOverloadedOptionsOffer() throws Exception {
// A packet observed in the real world that is interesting for two reasons:
//
// 1. It uses pad bytes, which we previously didn't support correctly.
// 2. It uses DHCP option overloading, which we don't currently support (but it doesn't
// store any information in the overloaded fields).
//
// For now, we just check that it parses correctly.
final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(// Ethernet header.
"b4cef6000000e80462236e300800" + // IP header.
"4500014c00000000ff11741701010101ac119876" + // UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
"004300440138ae5a" + // BOOTP header.
"020106000fa0059f0000000000000000ac1198760000000000000000" + // MAC address.
"b4cef600000000000000000000000000" + // Server name.
"ff00000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // File.
"ff00000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // Options
"638253633501023604010101010104ffff000033040000a8c03401030304ac1101010604ac110101" + "0000000000000000000000000000000000000000000000ff000000"));
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);
}
use of android.net.DhcpResults in project android_frameworks_base by DirtyUnicorns.
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;
}
use of android.net.DhcpResults in project android_frameworks_base by DirtyUnicorns.
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;
}
use of android.net.DhcpResults in project android_frameworks_base by DirtyUnicorns.
the class DhcpPacketTest method testOffer2.
@SmallTest
public void testOffer2() throws Exception {
final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(// IP header.
"450001518d0600004011144dc0a82b01c0a82bf7" + // UDP header.
"00430044013d9ac7" + // BOOTP header.
"02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" + // MAC address.
"30766ff2a90c00000000000000000000" + // Server name.
"0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // File.
"0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // Options
"638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" + "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"));
assertEquals(337, packet.limit());
DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
// Implicitly checks it's non-null.
assertTrue(offerPacket instanceof DhcpOfferPacket);
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);
assertTrue(dhcpResults.hasMeteredHint());
}
Aggregations