• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.dhcp6;
18 
19 import static com.android.net.module.util.NetworkStackConstants.DHCP_MAX_LENGTH;
20 
21 import androidx.annotation.NonNull;
22 
23 import java.nio.ByteBuffer;
24 
25 /**
26  * DHCPv6 SOLICIT packet class, a client sends a Solicit message to locate DHCPv6 servers.
27  *
28  * https://tools.ietf.org/html/rfc8415#page-24
29  */
30 public class Dhcp6SolicitPacket extends Dhcp6Packet {
31     /**
32      * Generates a solicit packet with the specified parameters.
33      */
Dhcp6SolicitPacket(int transId, int elapsedTime, @NonNull final byte[] clientDuid, final byte[] iapd, boolean rapidCommit)34     Dhcp6SolicitPacket(int transId, int elapsedTime, @NonNull final byte[] clientDuid,
35             final byte[] iapd, boolean rapidCommit) {
36         super(transId, elapsedTime, clientDuid, null /* serverDuid */, iapd);
37         mRapidCommit = rapidCommit;
38     }
39 
40     /**
41      * Build a DHCPv6 Solicit message with the specific parameters.
42      */
buildPacket()43     public ByteBuffer buildPacket() {
44         final ByteBuffer packet = ByteBuffer.allocate(DHCP_MAX_LENGTH);
45         final int msgTypeAndTransId = (DHCP6_MESSAGE_TYPE_SOLICIT << 24) | mTransId;
46         packet.putInt(msgTypeAndTransId);
47 
48         addTlv(packet, DHCP6_ELAPSED_TIME, (short) (mElapsedTime & 0xFFFF));
49         addTlv(packet, DHCP6_CLIENT_IDENTIFIER, mClientDuid);
50         addTlv(packet, DHCP6_IA_PD, mIaPd);
51         addTlv(packet, DHCP6_OPTION_REQUEST_OPTION, DHCP6_SOL_MAX_RT);
52         if (mRapidCommit) {
53             addTlv(packet, DHCP6_RAPID_COMMIT);
54         }
55 
56         packet.flip();
57         return packet;
58     }
59 }
60