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 23 /** 24 * This class implements the DHCP-NAK packet. 25 */ 26 class DhcpNakPacket extends DhcpPacket { 27 /** 28 * Generates a NAK packet with the specified parameters. 29 */ 30 DhcpNakPacket(int transId, InetAddress clientIp, InetAddress yourIp, 31 InetAddress nextIp, InetAddress relayIp, 32 byte[] clientMac) { 33 super(transId, Inet4Address.ANY, Inet4Address.ANY, nextIp, relayIp, 34 clientMac, false); 35 } 36 37 public String toString() { 38 String s = super.toString(); 39 return s + " NAK, reason " + (mMessage == null ? "(none)" : mMessage); 40 } 41 42 /** 43 * Fills in a packet with the requested NAK attributes. 44 */ 45 public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) { 46 ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH); 47 InetAddress destIp = mClientIp; 48 InetAddress srcIp = mYourIp; 49 50 fillInPacket(encap, destIp, srcIp, destUdp, srcUdp, result, 51 DHCP_BOOTREPLY, mBroadcast); 52 result.flip(); 53 return result; 54 } 55 56 /** 57 * Adds the optional parameters to the client-generated NAK packet. 58 */ 59 void finishPacket(ByteBuffer buffer) { 60 addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_NAK); 61 addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier); 62 addTlv(buffer, DHCP_MESSAGE, mMessage); 63 addTlvEnd(buffer); 64 } 65 66 /** 67 * Notifies the specified state machine of the newly-arrived NAK packet. 68 */ 69 public void doNextOp(DhcpStateMachine machine) { 70 machine.onNakReceived(); 71 } 72 } 73