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 android.util.Log;
     20 
     21 import java.net.Inet4Address;
     22 import java.nio.ByteBuffer;
     23 
     24 /**
     25  * This class implements the DHCP-REQUEST packet.
     26  */
     27 class DhcpRequestPacket extends DhcpPacket {
     28     /**
     29      * Generates a REQUEST packet with the specified parameters.
     30      */
     31     DhcpRequestPacket(int transId, short secs, Inet4Address clientIp, byte[] clientMac,
     32                       boolean broadcast) {
     33         super(transId, secs, clientIp, INADDR_ANY, INADDR_ANY, INADDR_ANY, clientMac, broadcast);
     34     }
     35 
     36     public String toString() {
     37         String s = super.toString();
     38         return s + " REQUEST, desired IP " + mRequestedIp + " from host '"
     39             + mHostName + "', param list length "
     40             + (mRequestedParams == null ? 0 : mRequestedParams.length);
     41     }
     42 
     43     /**
     44      * Fills in a packet with the requested REQUEST attributes.
     45      */
     46     public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) {
     47         ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH);
     48 
     49         fillInPacket(encap, INADDR_BROADCAST, INADDR_ANY, destUdp, srcUdp,
     50             result, DHCP_BOOTREQUEST, mBroadcast);
     51         result.flip();
     52         return result;
     53     }
     54 
     55     /**
     56      * Adds the optional parameters to the client-generated REQUEST packet.
     57      */
     58     void finishPacket(ByteBuffer buffer) {
     59         addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_REQUEST);
     60         addTlv(buffer, DHCP_CLIENT_IDENTIFIER, getClientId());
     61         if (!INADDR_ANY.equals(mRequestedIp)) {
     62             addTlv(buffer, DHCP_REQUESTED_IP, mRequestedIp);
     63         }
     64         if (!INADDR_ANY.equals(mServerIdentifier)) {
     65             addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier);
     66         }
     67         addCommonClientTlvs(buffer);
     68         addTlv(buffer, DHCP_PARAMETER_LIST, mRequestedParams);
     69         addTlvEnd(buffer);
     70     }
     71 }
     72