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.Inet4Address; 20 import java.nio.ByteBuffer; 21 22 /** 23 * This class implements the DHCP-ACK packet. 24 */ 25 public class DhcpAckPacket extends DhcpPacket { 26 27 /** 28 * The address of the server which sent this packet. 29 */ 30 private final Inet4Address mSrcIp; 31 DhcpAckPacket(int transId, short secs, boolean broadcast, Inet4Address serverAddress, Inet4Address relayIp, Inet4Address clientIp, Inet4Address yourIp, byte[] clientMac, boolean rapidCommit)32 DhcpAckPacket(int transId, short secs, boolean broadcast, Inet4Address serverAddress, 33 Inet4Address relayIp, Inet4Address clientIp, Inet4Address yourIp, byte[] clientMac, 34 boolean rapidCommit) { 35 super(transId, secs, clientIp, yourIp, serverAddress, relayIp, clientMac, broadcast); 36 mBroadcast = broadcast; 37 mSrcIp = serverAddress; 38 mRapidCommit = rapidCommit; 39 } 40 toString()41 public String toString() { 42 String s = super.toString(); 43 String dnsServers = " DNS servers: "; 44 45 for (Inet4Address dnsServer: mDnsServers) { 46 dnsServers += dnsServer.toString() + " "; 47 } 48 49 return s + " ACK: your new IP " + mYourIp 50 + ", netmask " + mSubnetMask 51 + ", gateways " + mGateways + dnsServers 52 + ", lease time " + mLeaseTime 53 + (mIpv6OnlyWaitTime != null ? ", V6ONLY_WAIT " + mIpv6OnlyWaitTime : ""); 54 } 55 56 /** 57 * Fills in a packet with the requested ACK parameters. 58 */ buildPacket(int encap, short destUdp, short srcUdp)59 public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) { 60 ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH); 61 Inet4Address destIp = mBroadcast ? INADDR_BROADCAST : mYourIp; 62 Inet4Address srcIp = mBroadcast ? INADDR_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 */ finishPacket(ByteBuffer buffer)73 void finishPacket(ByteBuffer buffer) { 74 addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_ACK); 75 addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier); 76 addCommonServerTlvs(buffer); 77 if (mRapidCommit) { 78 addTlv(buffer, DHCP_RAPID_COMMIT); 79 } 80 addTlvEnd(buffer); 81 } 82 83 /** 84 * Un-boxes an Integer, returning 0 if a null reference is supplied. 85 */ getInt(Integer v)86 private static final int getInt(Integer v) { 87 if (v == null) { 88 return 0; 89 } else { 90 return v.intValue(); 91 } 92 } 93 } 94