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