Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 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 org.onlab.packet.dhcp; |
| 18 | |
| 19 | import org.onlab.packet.BasePacket; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 20 | import org.onlab.packet.Deserializer; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 21 | import org.slf4j.Logger; |
| 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.Arrays; |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | /** |
| 30 | * Default DHCP option. |
| 31 | */ |
| 32 | public class DhcpOption extends BasePacket { |
| 33 | public static final int OPT_CODE_LEN = 1; |
| 34 | public static final int DEFAULT_LEN = 2; |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 35 | protected static final int UNSIGNED_BYTE_MASK = 0xff; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 36 | private final Logger log = getLogger(getClass()); |
| 37 | protected byte code; |
| 38 | protected byte length; |
| 39 | protected byte[] data; |
| 40 | |
| 41 | @Override |
| 42 | public byte[] serialize() { |
| 43 | ByteBuffer byteBuffer; |
| 44 | if (data != null) { |
| 45 | byteBuffer = ByteBuffer.allocate(DEFAULT_LEN + data.length); |
| 46 | byteBuffer.put(code); |
| 47 | byteBuffer.put(length); |
| 48 | byteBuffer.put(data); |
| 49 | } else { |
| 50 | byteBuffer = ByteBuffer.allocate(OPT_CODE_LEN); |
| 51 | byteBuffer.put(code); |
| 52 | } |
| 53 | return byteBuffer.array(); |
| 54 | } |
| 55 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Deserializer function for DHCP option. |
| 59 | * |
| 60 | * @return deserializer function |
| 61 | */ |
| 62 | public static Deserializer<DhcpOption> deserializer() { |
| 63 | return (data, offset, length) -> { |
| 64 | DhcpOption dhcpOption = new DhcpOption(); |
| 65 | ByteBuffer byteBuffer = ByteBuffer.wrap(data, offset, length); |
| 66 | dhcpOption.code = byteBuffer.get(); |
| 67 | if (byteBuffer.hasRemaining()) { |
| 68 | dhcpOption.length = byteBuffer.get(); |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 69 | int optionLen = UNSIGNED_BYTE_MASK & dhcpOption.length; |
| 70 | dhcpOption.data = new byte[optionLen]; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 71 | byteBuffer.get(dhcpOption.data); |
| 72 | } else { |
| 73 | dhcpOption.length = 0; |
| 74 | dhcpOption.data = null; |
| 75 | } |
| 76 | return dhcpOption; |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return the code |
| 82 | */ |
| 83 | public byte getCode() { |
| 84 | return this.code; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param code the code to set |
| 89 | * @return this |
| 90 | */ |
| 91 | public DhcpOption setCode(final byte code) { |
| 92 | this.code = code; |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return the length |
| 98 | */ |
| 99 | public byte getLength() { |
| 100 | return this.length; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param length the length to set |
| 105 | * @return this |
| 106 | */ |
| 107 | public DhcpOption setLength(final byte length) { |
| 108 | this.length = length; |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @return the data |
| 114 | */ |
| 115 | public byte[] getData() { |
| 116 | return this.data; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param data the data to set |
| 121 | * @return this |
| 122 | */ |
| 123 | public DhcpOption setData(final byte[] data) { |
| 124 | this.data = data; |
| 125 | return this; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * (non-Javadoc) |
| 130 | * |
| 131 | * @see java.lang.Object#hashCode() |
| 132 | */ |
| 133 | @Override |
| 134 | public int hashCode() { |
Ray Milkey | 5963bd4 | 2017-08-01 15:37:59 -0700 | [diff] [blame] | 135 | return Objects.hash(code, length, Arrays.hashCode(data)); |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* |
| 139 | * (non-Javadoc) |
| 140 | * |
| 141 | * @see java.lang.Object#equals(java.lang.Object) |
| 142 | */ |
| 143 | @Override |
| 144 | public boolean equals(final Object obj) { |
| 145 | if (this == obj) { |
| 146 | return true; |
| 147 | } |
| 148 | if (obj == null) { |
| 149 | return false; |
| 150 | } |
| 151 | if (!(obj instanceof DhcpOption)) { |
| 152 | return false; |
| 153 | } |
| 154 | final DhcpOption other = (DhcpOption) obj; |
| 155 | return Objects.equals(this.code, other.code) && |
| 156 | Objects.equals(this.length, other.length) && |
| 157 | Arrays.equals(this.data, other.data); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * (non-Javadoc) |
| 162 | * |
| 163 | * @see java.lang.Object#toString() |
| 164 | */ |
| 165 | @Override |
| 166 | public String toString() { |
| 167 | return "DhcpOption [code=" + this.code + ", length=" + this.length |
| 168 | + ", data=" + Arrays.toString(this.data) + "]"; |
| 169 | } |
| 170 | } |