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-DISCOVER packet.
     25  */
     26 class DhcpDiscoverPacket extends DhcpPacket {
     27     /**
     28      * Generates a DISCOVER packet with the specified parameters.
     29      */
     30     DhcpDiscoverPacket(int transId, byte[] clientMac, boolean broadcast) {
     31         super(transId, Inet4Address.ANY, Inet4Address.ANY, Inet4Address.ANY,
     32             Inet4Address.ANY, clientMac, broadcast);
     33     }
     34 
     35     public String toString() {
     36         String s = super.toString();
     37         return s + " DISCOVER " +
     38                 (mBroadcast ? "broadcast " : "unicast ");
     39     }
     40 
     41     /**
     42      * Fills in a packet with the requested DISCOVER parameters.
     43      */
     44     public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) {
     45         ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH);
     46         InetAddress destIp = Inet4Address.ALL;
     47 
     48         fillInPacket(encap, Inet4Address.ALL, Inet4Address.ANY, destUdp, srcUdp,
     49             result, DHCP_BOOTREQUEST, true);
     50         result.flip();
     51         return result;
     52     }
     53 
     54     /**
     55      * Adds optional parameters to a DISCOVER packet.
     56      */
     57     void finishPacket(ByteBuffer buffer) {
     58         addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_DISCOVER);
     59         addTlv(buffer, DHCP_PARAMETER_LIST, mRequestedParams);
     60         addTlvEnd(buffer);
     61     }
     62 
     63     /**
     64      * Informs the state machine of the arrival of a DISCOVER packet.
     65      */
     66     public void doNextOp(DhcpStateMachine machine) {
     67         // currently omitted: host name
     68         machine.onDiscoverReceived(mBroadcast, mTransId, mClientMac,
     69             mRequestedParams);
     70     }
     71 }
     72