Mingguang Xu | 2d87c61 | 2021-10-29 00:18:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Mingguang Xu | 6914a16 | 2022-01-26 15:37:57 -0800 | [diff] [blame] | 21 | import android.annotation.SuppressLint; |
Mingguang Xu | 2d87c61 | 2021-10-29 00:18:55 -0700 | [diff] [blame] | 22 | import android.annotation.SystemApi; |
| 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | |
| 26 | /** |
| 27 | * A class representing an option in the DHCP protocol. |
| 28 | * |
| 29 | * @hide |
| 30 | */ |
| 31 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 32 | public final class DhcpOption implements Parcelable { |
| 33 | private final byte mType; |
| 34 | private final byte[] mValue; |
| 35 | |
| 36 | /** |
| 37 | * Constructs a DhcpOption object. |
| 38 | * |
Mingguang Xu | 6914a16 | 2022-01-26 15:37:57 -0800 | [diff] [blame] | 39 | * @param type the type of this option. For more information, see |
| 40 | * https://www.iana.org/assignments/bootp-dhcp-parameters/bootp-dhcp-parameters.xhtml. |
Mingguang Xu | 2d87c61 | 2021-10-29 00:18:55 -0700 | [diff] [blame] | 41 | * @param value the value of this option. If {@code null}, DHCP packets containing this option |
| 42 | * will include the option type in the Parameter Request List. Otherwise, DHCP |
| 43 | * packets containing this option will include the option in the options section. |
| 44 | */ |
Mingguang Xu | 6914a16 | 2022-01-26 15:37:57 -0800 | [diff] [blame] | 45 | public DhcpOption(@SuppressLint("NoByteOrShort") byte type, @Nullable byte[] value) { |
Mingguang Xu | 2d87c61 | 2021-10-29 00:18:55 -0700 | [diff] [blame] | 46 | mType = type; |
| 47 | mValue = value; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public int describeContents() { |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 57 | dest.writeByte(mType); |
| 58 | dest.writeByteArray(mValue); |
| 59 | } |
| 60 | |
| 61 | /** Implement the Parcelable interface */ |
| 62 | public static final @NonNull Creator<DhcpOption> CREATOR = |
| 63 | new Creator<DhcpOption>() { |
| 64 | public DhcpOption createFromParcel(Parcel in) { |
| 65 | return new DhcpOption(in.readByte(), in.createByteArray()); |
| 66 | } |
| 67 | |
| 68 | public DhcpOption[] newArray(int size) { |
| 69 | return new DhcpOption[size]; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | /** Get the type of DHCP option */ |
Mingguang Xu | 6914a16 | 2022-01-26 15:37:57 -0800 | [diff] [blame] | 74 | @SuppressLint("NoByteOrShort") |
Mingguang Xu | 2d87c61 | 2021-10-29 00:18:55 -0700 | [diff] [blame] | 75 | public byte getType() { |
| 76 | return mType; |
| 77 | } |
| 78 | |
| 79 | /** Get the value of DHCP option */ |
| 80 | @Nullable public byte[] getValue() { |
| 81 | return mValue == null ? null : mValue.clone(); |
| 82 | } |
| 83 | } |