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