1 /*
2  * Copyright (C) 2018 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  * Implements DHCP-RELEASE
24  */
25 public class DhcpReleasePacket extends DhcpPacket {
26 
27     final Inet4Address mClientAddr;
28 
29     /**
30      * Generates a RELEASE packet with the specified parameters.
31      */
DhcpReleasePacket(int transId, Inet4Address serverId, Inet4Address clientAddr, Inet4Address relayIp, byte[] clientMac)32     public DhcpReleasePacket(int transId, Inet4Address serverId, Inet4Address clientAddr,
33             Inet4Address relayIp, byte[] clientMac) {
34         super(transId, (short)0, clientAddr, INADDR_ANY /* yourIp */, INADDR_ANY /* nextIp */,
35                 relayIp, clientMac, false /* broadcast */);
36         mServerIdentifier = serverId;
37         mClientAddr = clientAddr;
38     }
39 
40 
41     @Override
buildPacket(int encap, short destUdp, short srcUdp)42     public ByteBuffer buildPacket(int encap, short destUdp, short srcUdp) {
43         ByteBuffer result = ByteBuffer.allocate(MAX_LENGTH);
44         fillInPacket(encap, mServerIdentifier /* destIp */, mClientIp /* srcIp */, destUdp, srcUdp,
45                 result, DHCP_BOOTREPLY, mBroadcast);
46         result.flip();
47         return result;
48     }
49 
50     @Override
finishPacket(ByteBuffer buffer)51     void finishPacket(ByteBuffer buffer) {
52         addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_RELEASE);
53         addTlv(buffer, DHCP_CLIENT_IDENTIFIER, getClientId());
54         addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier);
55         addCommonClientTlvs(buffer);
56         addTlvEnd(buffer);
57     }
58 }
59