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
17package android.net.dhcp;
18
19import java.net.Inet4Address;
20import java.nio.ByteBuffer;
21
22/**
23 * This class implements the DHCP-DISCOVER packet.
24 */
25class DhcpDiscoverPacket extends DhcpPacket {
26    /**
27     * Generates a DISCOVER packet with the specified parameters.
28     */
29    DhcpDiscoverPacket(int transId, short secs, byte[] clientMac, boolean broadcast) {
30        super(transId, secs, INADDR_ANY, INADDR_ANY, INADDR_ANY, INADDR_ANY, clientMac, broadcast);
31    }
32
33    public String toString() {
34        String s = super.toString();
35        return s + " DISCOVER " +
36                (mBroadcast ? "broadcast " : "unicast ");
37    }
38
39    /**
40     * Fills in a packet with the requested DISCOVER parameters.
41     */
42    public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) {
43        ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH);
44        fillInPacket(encap, INADDR_BROADCAST, INADDR_ANY, destUdp,
45                srcUdp, result, DHCP_BOOTREQUEST, mBroadcast);
46        result.flip();
47        return result;
48    }
49
50    /**
51     * Adds optional parameters to a DISCOVER packet.
52     */
53    void finishPacket(ByteBuffer buffer) {
54        addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_DISCOVER);
55        addTlv(buffer, DHCP_CLIENT_IDENTIFIER, getClientId());
56        addCommonClientTlvs(buffer);
57        addTlv(buffer, DHCP_PARAMETER_LIST, mRequestedParams);
58        addTlvEnd(buffer);
59    }
60}
61