blob: 76f768f7831814ee7b1dd4d3e69b1ba1e9482eaf [file] [log] [blame]
Yi Tsengc7403c22017-06-19 16:23:22 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengc7403c22017-06-19 16:23:22 -07003 *
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
17package org.onlab.packet.dhcp;
18
19import org.onlab.packet.BasePacket;
20import org.onlab.packet.DeserializationException;
21import org.onlab.packet.Deserializer;
22import org.onlab.packet.IPacket;
23import org.slf4j.Logger;
24
25import java.nio.ByteBuffer;
26import java.util.Arrays;
27import java.util.Objects;
28
29import static org.slf4j.LoggerFactory.getLogger;
30
31/**
32 * Default DHCP option.
33 */
34public class DhcpOption extends BasePacket {
35 public static final int OPT_CODE_LEN = 1;
36 public static final int DEFAULT_LEN = 2;
Yi Tsengb4fdb042017-08-07 13:32:32 -070037 protected static final int UNSIGNED_BYTE_MASK = 0xff;
Yi Tsengc7403c22017-06-19 16:23:22 -070038 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 Tsengb4fdb042017-08-07 13:32:32 -070080 int optionLen = UNSIGNED_BYTE_MASK & dhcpOption.length;
81 dhcpOption.data = new byte[optionLen];
Yi Tsengc7403c22017-06-19 16:23:22 -070082 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 Milkey5963bd42017-08-01 15:37:59 -0700146 return Objects.hash(code, length, Arrays.hashCode(data));
Yi Tsengc7403c22017-06-19 16:23:22 -0700147 }
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}