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-OFFER packet. 24 */ 25 public class DhcpOfferPacket extends DhcpPacket { 26 /** 27 * The IP address of the server which sent this packet. 28 */ 29 private final Inet4Address mSrcIp; 30 31 /** 32 * Generates a OFFER packet with the specified parameters. 33 */ DhcpOfferPacket(int transId, short secs, boolean broadcast, Inet4Address serverAddress, Inet4Address relayIp, Inet4Address clientIp, Inet4Address yourIp, byte[] clientMac)34 DhcpOfferPacket(int transId, short secs, boolean broadcast, Inet4Address serverAddress, 35 Inet4Address relayIp, Inet4Address clientIp, Inet4Address yourIp, byte[] clientMac) { 36 super(transId, secs, clientIp, yourIp, serverAddress, relayIp, clientMac, broadcast); 37 mSrcIp = serverAddress; 38 } 39 toString()40 public String toString() { 41 String s = super.toString(); 42 String dnsServers = ", DNS servers: "; 43 44 if (mDnsServers != null) { 45 for (Inet4Address dnsServer: mDnsServers) { 46 dnsServers += dnsServer + " "; 47 } 48 } 49 50 return s + " OFFER, ip " + mYourIp 51 + ", netmask " + mSubnetMask + dnsServers 52 + ", gateways " + mGateways 53 + ", lease time " + mLeaseTime 54 + ", domain " + mDomainName 55 + (mIpv6OnlyWaitTime != null ? ", V6ONLY_WAIT " + mIpv6OnlyWaitTime : ""); 56 } 57 58 /** 59 * Fills in a packet with the specified OFFER attributes. 60 */ buildPacket(int encap, short destUdp, short srcUdp)61 public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) { 62 ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH); 63 Inet4Address destIp = mBroadcast ? INADDR_BROADCAST : mYourIp; 64 Inet4Address srcIp = mBroadcast ? INADDR_ANY : mSrcIp; 65 66 fillInPacket(encap, destIp, srcIp, destUdp, srcUdp, result, 67 DHCP_BOOTREPLY, mBroadcast); 68 result.flip(); 69 return result; 70 } 71 72 /** 73 * Adds the optional parameters to the server-generated OFFER packet. 74 */ finishPacket(ByteBuffer buffer)75 void finishPacket(ByteBuffer buffer) { 76 addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_OFFER); 77 addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier); 78 79 addCommonServerTlvs(buffer); 80 addTlvEnd(buffer); 81 } 82 } 83