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.InetAddress;
20import java.nio.ByteBuffer;
21
22/**
23 * This class implements the (unused) DHCP-INFORM packet.
24 */
25class DhcpInformPacket extends DhcpPacket {
26    /**
27     * Generates an INFORM packet with the specified parameters.
28     */
29    DhcpInformPacket(int transId, InetAddress clientIp, InetAddress yourIp,
30                     InetAddress nextIp, InetAddress relayIp,
31                     byte[] clientMac) {
32        super(transId, clientIp, yourIp, nextIp, relayIp, clientMac, false);
33    }
34
35    public String toString() {
36        String s = super.toString();
37        return s + " INFORM";
38    }
39
40    /**
41     * Builds an INFORM packet.
42     */
43    public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) {
44        ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH);
45
46        fillInPacket(encap, mClientIp, mYourIp, destUdp, srcUdp, result,
47            DHCP_BOOTREQUEST, false);
48        result.flip();
49        return result;
50    }
51
52    /**
53     * Adds additional parameters to the INFORM packet.
54     */
55    void finishPacket(ByteBuffer buffer) {
56        byte[] clientId = new byte[7];
57
58        clientId[0] = CLIENT_ID_ETHER;
59        System.arraycopy(mClientMac, 0, clientId, 1, 6);
60
61        addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_REQUEST);
62        addTlv(buffer, DHCP_PARAMETER_LIST, mRequestedParams);
63        addTlvEnd(buffer);
64    }
65
66    /**
67     * Informs the state machine of the arrival of an INFORM packet.  Not
68     * used currently.
69     */
70    public void doNextOp(DhcpStateMachine machine) {
71        InetAddress clientRequest =
72            mRequestedIp == null ? mClientIp : mRequestedIp;
73        machine.onInformReceived(mTransId, mClientMac, clientRequest,
74            mRequestedParams);
75    }
76}
77