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