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 17 package android.net.dhcp; 18 19 import java.net.InetAddress; 20 import java.net.Inet4Address; 21 import java.nio.ByteBuffer; 22 import java.util.List; 23 24 /** 25 * This class implements the DHCP-ACK packet. 26 */ 27 class DhcpAckPacket extends DhcpPacket { 28 29 /** 30 * The address of the server which sent this packet. 31 */ 32 private final InetAddress mSrcIp; 33 34 DhcpAckPacket(int transId, boolean broadcast, InetAddress serverAddress, 35 InetAddress clientIp, byte[] clientMac) { 36 super(transId, Inet4Address.ANY, clientIp, serverAddress, 37 Inet4Address.ANY, clientMac, broadcast); 38 mBroadcast = broadcast; 39 mSrcIp = serverAddress; 40 } 41 42 public String toString() { 43 String s = super.toString(); 44 String dnsServers = " DNS servers: "; 45 46 for (InetAddress dnsServer: mDnsServers) { 47 dnsServers += dnsServer.toString() + " "; 48 } 49 50 return s + " ACK: your new IP " + mYourIp + 51 ", netmask " + mSubnetMask + 52 ", gateway " + mGateway + dnsServers + 53 ", lease time " + mLeaseTime; 54 } 55 56 /** 57 * Fills in a packet with the requested ACK parameters. 58 */ 59 public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) { 60 ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH); 61 InetAddress destIp = mBroadcast ? Inet4Address.ALL : mYourIp; 62 InetAddress srcIp = mBroadcast ? Inet4Address.ANY : mSrcIp; 63 64 fillInPacket(encap, destIp, srcIp, destUdp, srcUdp, result, 65 DHCP_BOOTREPLY, mBroadcast); 66 result.flip(); 67 return result; 68 } 69 70 /** 71 * Adds the optional parameters to the client-generated ACK packet. 72 */ 73 void finishPacket(ByteBuffer buffer) { 74 addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_ACK); 75 addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier); 76 addTlv(buffer, DHCP_LEASE_TIME, mLeaseTime); 77 78 // the client should renew at 1/2 the lease-expiry interval 79 if (mLeaseTime != null) { 80 addTlv(buffer, DHCP_RENEWAL_TIME, 81 Integer.valueOf(mLeaseTime.intValue() / 2)); 82 } 83 84 addTlv(buffer, DHCP_SUBNET_MASK, mSubnetMask); 85 addTlv(buffer, DHCP_ROUTER, mGateway); 86 addTlv(buffer, DHCP_DOMAIN_NAME, mDomainName); 87 addTlv(buffer, DHCP_BROADCAST_ADDRESS, mBroadcastAddress); 88 addTlv(buffer, DHCP_DNS_SERVER, mDnsServers); 89 addTlvEnd(buffer); 90 } 91 92 /** 93 * Un-boxes an Integer, returning 0 if a null reference is supplied. 94 */ 95 private static final int getInt(Integer v) { 96 if (v == null) { 97 return 0; 98 } else { 99 return v.intValue(); 100 } 101 } 102 103 /** 104 * Notifies the specified state machine of the ACK packet parameters. 105 */ 106 public void doNextOp(DhcpStateMachine machine) { 107 machine.onAckReceived(mYourIp, mSubnetMask, mGateway, mDnsServers, 108 mServerIdentifier, getInt(mLeaseTime)); 109 } 110 } 111