blob: 63be1f0a4f50d30d27c06703323e15d94eaf4847 [file] [log] [blame]
Yi Tsengca34e1d2017-07-18 16:16:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengca34e1d2017-07-18 16:16:25 -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 */
17
18package org.onlab.packet.dhcp;
19
Yi Tsengb4fdb042017-08-07 13:32:32 -070020import com.google.common.base.MoreObjects;
Yi Tsengca34e1d2017-07-18 16:16:25 -070021import org.onlab.packet.BasePacket;
22import org.onlab.packet.Deserializer;
Yi Tsengca34e1d2017-07-18 16:16:25 -070023
24import java.nio.ByteBuffer;
Yi Tsengb4fdb042017-08-07 13:32:32 -070025import java.util.Arrays;
Yi Tsengca34e1d2017-07-18 16:16:25 -070026
27public class Dhcp6Duid extends BasePacket {
28 private static final int DEFAULT_LLT_LEN = 8;
29 private static final int DEFAULT_EN_LEN = 6;
30 private static final int DEFAULT_LL_LEN = 4;
Charles Chan29813b02018-04-13 14:03:13 -040031 private static final int DEFAULT_UUID_LEN = 2;
32
Yi Tsengca34e1d2017-07-18 16:16:25 -070033 public enum DuidType {
34 DUID_LLT((short) 1),
35 DUID_EN((short) 2),
Charles Chan29813b02018-04-13 14:03:13 -040036 DUID_LL((short) 3),
37 DUID_UUID((short) 4); // RFC6355
Yi Tsengca34e1d2017-07-18 16:16:25 -070038
39 private short value;
40 DuidType(short value) {
41 this.value = value;
42 }
43
44 public short getValue() {
45 return value;
46 }
47
48 public static DuidType of(short type) {
49 switch (type) {
50 case 1:
51 return DUID_LLT;
52 case 2:
53 return DUID_EN;
54 case 3:
55 return DUID_LL;
Charles Chan29813b02018-04-13 14:03:13 -040056 case 4:
57 return DUID_UUID;
Yi Tsengca34e1d2017-07-18 16:16:25 -070058 default:
Ray Milkey986a47a2018-01-25 11:38:51 -080059 throw new IllegalArgumentException("Unknown type: " + type);
Yi Tsengca34e1d2017-07-18 16:16:25 -070060 }
61 }
62 }
63 // general field
64 private DuidType duidType;
65
66 // fields for DUID_LLT & DUID_LL
67 private short hardwareType;
68 private int duidTime;
69 private byte[] linkLayerAddress;
70
71 // fields for DUID_EN
72 private int enterpriseNumber;
73 private byte[] identifier;
74
Charles Chan29813b02018-04-13 14:03:13 -040075 // fields for DUID_UUID
76 private byte[] uuid;
77
Yi Tsengca34e1d2017-07-18 16:16:25 -070078 public DuidType getDuidType() {
79 return duidType;
80 }
81
82 public void setDuidType(DuidType duidType) {
83 this.duidType = duidType;
84 }
85
86 public short getHardwareType() {
87 return hardwareType;
88 }
89
90 public void setHardwareType(short hardwareType) {
91 this.hardwareType = hardwareType;
92 }
93
94 public int getDuidTime() {
95 return duidTime;
96 }
97
98 public void setDuidTime(int duidTime) {
99 this.duidTime = duidTime;
100 }
101
102 public byte[] getLinkLayerAddress() {
103 return linkLayerAddress;
104 }
105
106 public void setLinkLayerAddress(byte[] linkLayerAddress) {
Charles Chan29813b02018-04-13 14:03:13 -0400107 this.linkLayerAddress = Arrays.copyOf(linkLayerAddress, linkLayerAddress.length);
Yi Tsengca34e1d2017-07-18 16:16:25 -0700108 }
109
110 public int getEnterpriseNumber() {
111 return enterpriseNumber;
112 }
113
114 public void setEnterpriseNumber(int enterpriseNumber) {
115 this.enterpriseNumber = enterpriseNumber;
116 }
117
118 public byte[] getIdentifier() {
119 return identifier;
120 }
121
122 public void setIdentifier(byte[] identifier) {
Charles Chan29813b02018-04-13 14:03:13 -0400123 this.identifier = Arrays.copyOf(identifier, identifier.length);
124 }
125
126 public byte[] getUuid() {
127 return uuid;
128 }
129
130 public void setUuid(byte[] uuid) {
131 this.uuid = Arrays.copyOf(uuid, uuid.length);
Yi Tsengca34e1d2017-07-18 16:16:25 -0700132 }
133
134 @Override
135 public byte[] serialize() {
136 ByteBuffer byteBuffer;
137 switch (duidType) {
138 case DUID_LLT:
139 byteBuffer = ByteBuffer.allocate(DEFAULT_LLT_LEN + linkLayerAddress.length);
140 byteBuffer.putShort(duidType.value);
141 byteBuffer.putShort(hardwareType);
142 byteBuffer.putInt(duidTime);
143 byteBuffer.put(linkLayerAddress);
144 break;
145 case DUID_EN:
146 byteBuffer = ByteBuffer.allocate(DEFAULT_EN_LEN + identifier.length);
147 byteBuffer.putShort(duidType.value);
148 byteBuffer.putInt(enterpriseNumber);
149 byteBuffer.put(identifier);
150 break;
151 case DUID_LL:
152 byteBuffer = ByteBuffer.allocate(DEFAULT_LL_LEN + linkLayerAddress.length);
153 byteBuffer.putShort(duidType.value);
154 byteBuffer.putShort(hardwareType);
155 byteBuffer.put(linkLayerAddress);
156 break;
Charles Chan29813b02018-04-13 14:03:13 -0400157 case DUID_UUID:
158 byteBuffer = ByteBuffer.allocate(DEFAULT_UUID_LEN + uuid.length);
159 byteBuffer.putShort(duidType.value);
160 byteBuffer.put(linkLayerAddress);
161 break;
Yi Tsengca34e1d2017-07-18 16:16:25 -0700162 default:
Ray Milkey986a47a2018-01-25 11:38:51 -0800163 throw new IllegalArgumentException("Unknown duidType: " + duidType.toString());
Yi Tsengca34e1d2017-07-18 16:16:25 -0700164 }
165 return byteBuffer.array();
166 }
167
Ray Milkeyf0c47612017-09-28 11:29:38 -0700168
Yi Tsengca34e1d2017-07-18 16:16:25 -0700169
170 public static Deserializer<Dhcp6Duid> deserializer() {
171 return (data, offset, length) -> {
172 Dhcp6Duid duid = new Dhcp6Duid();
173 ByteBuffer byteBuffer = ByteBuffer.wrap(data, offset, length);
174
175 DuidType duidType = DuidType.of(byteBuffer.getShort());
176 duid.setDuidType(duidType);
177 switch (duidType) {
178 case DUID_LLT:
179 duid.setHardwareType(byteBuffer.getShort());
180 duid.setDuidTime(byteBuffer.getInt());
181 duid.linkLayerAddress = new byte[length - DEFAULT_LLT_LEN];
182 byteBuffer.get(duid.linkLayerAddress);
183 break;
184 case DUID_EN:
185 duid.setEnterpriseNumber(byteBuffer.getInt());
186 duid.identifier = new byte[length - DEFAULT_EN_LEN];
187 byteBuffer.get(duid.identifier);
188 break;
189 case DUID_LL:
190 duid.setHardwareType(byteBuffer.getShort());
191 duid.linkLayerAddress = new byte[length - DEFAULT_LL_LEN];
192 byteBuffer.get(duid.linkLayerAddress);
193 break;
Charles Chan29813b02018-04-13 14:03:13 -0400194 case DUID_UUID:
195 duid.uuid = new byte[length - DEFAULT_LL_LEN];
196 byteBuffer.get(duid.uuid);
197 break;
Yi Tsengca34e1d2017-07-18 16:16:25 -0700198 default:
Ray Milkey986a47a2018-01-25 11:38:51 -0800199 throw new IllegalArgumentException("Unknown type: " + duidType);
Yi Tsengca34e1d2017-07-18 16:16:25 -0700200 }
201 return duid;
202 };
203 }
Yi Tsengb4fdb042017-08-07 13:32:32 -0700204
205 @Override
206 public String toString() {
207 MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(getClass());
208
209 switch (duidType) {
210 case DUID_LLT:
211 helper.add("type", "DUID_LLT");
212 helper.add("hardwareType", hardwareType);
213 helper.add("duidTime", duidTime);
214 helper.add("linkLayerAddress", Arrays.toString(linkLayerAddress));
215 break;
216 case DUID_EN:
217 helper.add("type", "DUID_EN");
218 helper.add("enterpriseNumber", enterpriseNumber);
219 helper.add("id", Arrays.toString(identifier));
220 break;
221 case DUID_LL:
222 helper.add("type", "DUID_LL");
223 helper.add("hardwareType", hardwareType);
224 helper.add("linkLayerAddress", Arrays.toString(linkLayerAddress));
225 break;
Charles Chan29813b02018-04-13 14:03:13 -0400226 case DUID_UUID:
227 helper.add("type", "DUID_UUID");
228 helper.add("uuid", Arrays.toString(uuid));
229 break;
Yi Tsengb4fdb042017-08-07 13:32:32 -0700230 default:
231 helper.add("type", "Unknown");
232 }
233 return helper.toString();
234 }
Yi Tsengca34e1d2017-07-18 16:16:25 -0700235}