Home | History | Annotate | Download | only in dhcp
      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-OFFER packet.
     25  */
     26 class DhcpOfferPacket extends DhcpPacket {
     27     /**
     28      * The IP address of the server which sent this packet.
     29      */
     30     private final InetAddress mSrcIp;
     31 
     32     /**
     33      * Generates a OFFER packet with the specified parameters.
     34      */
     35     DhcpOfferPacket(int transId, boolean broadcast, InetAddress serverAddress,
     36                     InetAddress clientIp, byte[] clientMac) {
     37         super(transId, Inet4Address.ANY, clientIp, Inet4Address.ANY,
     38             Inet4Address.ANY, clientMac, broadcast);
     39         mSrcIp = serverAddress;
     40     }
     41 
     42     public String toString() {
     43         String s = super.toString();
     44         String dnsServers = ", DNS servers: ";
     45 
     46         if (mDnsServers != null) {
     47             for (InetAddress dnsServer: mDnsServers) {
     48                 dnsServers += dnsServer + " ";
     49             }
     50         }
     51 
     52         return s + " OFFER, ip " + mYourIp + ", mask " + mSubnetMask +
     53                 dnsServers + ", gateway " + mGateway +
     54                 " lease time " + mLeaseTime + ", domain " + mDomainName;
     55     }
     56 
     57     /**
     58      * Fills in a packet with the specified OFFER attributes.
     59      */
     60     public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) {
     61         ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH);
     62         InetAddress destIp = mBroadcast ? Inet4Address.ALL : mYourIp;
     63         InetAddress srcIp = mBroadcast ? Inet4Address.ANY : mSrcIp;
     64 
     65         fillInPacket(encap, destIp, srcIp, destUdp, srcUdp, result,
     66             DHCP_BOOTREPLY, mBroadcast);
     67         result.flip();
     68         return result;
     69     }
     70 
     71     /**
     72      * Adds the optional parameters to the server-generated OFFER packet.
     73      */
     74     void finishPacket(ByteBuffer buffer) {
     75         addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_OFFER);
     76         addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier);
     77         addTlv(buffer, DHCP_LEASE_TIME, mLeaseTime);
     78 
     79         // the client should renew at 1/2 the lease-expiry interval
     80         if (mLeaseTime != null) {
     81             addTlv(buffer, DHCP_RENEWAL_TIME,
     82                 Integer.valueOf(mLeaseTime.intValue() / 2));
     83         }
     84 
     85         addTlv(buffer, DHCP_SUBNET_MASK, mSubnetMask);
     86         addTlv(buffer, DHCP_ROUTER, mGateway);
     87         addTlv(buffer, DHCP_DOMAIN_NAME, mDomainName);
     88         addTlv(buffer, DHCP_BROADCAST_ADDRESS, mBroadcastAddress);
     89         addTlv(buffer, DHCP_DNS_SERVER, mDnsServers);
     90         addTlvEnd(buffer);
     91     }
     92 
     93     /**
     94      * Notifies the state machine of the OFFER packet parameters.
     95      */
     96     public void doNextOp(DhcpStateMachine machine) {
     97         machine.onOfferReceived(mBroadcast, mTransId, mClientMac, mYourIp,
     98             mServerIdentifier);
     99     }
    100 }
    101