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