Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -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 | ca34e1d | 2017-07-18 16:16:25 -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 | |
| 18 | package org.onlab.packet.dhcp; |
| 19 | |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 21 | import org.onlab.packet.BasePacket; |
| 22 | import org.onlab.packet.Deserializer; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 23 | |
| 24 | import java.nio.ByteBuffer; |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 25 | import java.util.Arrays; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 26 | |
| 27 | public 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 Chan | 29813b0 | 2018-04-13 14:03:13 -0400 | [diff] [blame] | 31 | private static final int DEFAULT_UUID_LEN = 2; |
| 32 | |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 33 | public enum DuidType { |
| 34 | DUID_LLT((short) 1), |
| 35 | DUID_EN((short) 2), |
Charles Chan | 29813b0 | 2018-04-13 14:03:13 -0400 | [diff] [blame] | 36 | DUID_LL((short) 3), |
| 37 | DUID_UUID((short) 4); // RFC6355 |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 38 | |
| 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 Chan | 29813b0 | 2018-04-13 14:03:13 -0400 | [diff] [blame] | 56 | case 4: |
| 57 | return DUID_UUID; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 58 | default: |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 59 | throw new IllegalArgumentException("Unknown type: " + type); |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 60 | } |
| 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 Chan | 29813b0 | 2018-04-13 14:03:13 -0400 | [diff] [blame] | 75 | // fields for DUID_UUID |
| 76 | private byte[] uuid; |
| 77 | |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 78 | 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 Chan | 29813b0 | 2018-04-13 14:03:13 -0400 | [diff] [blame] | 107 | this.linkLayerAddress = Arrays.copyOf(linkLayerAddress, linkLayerAddress.length); |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 108 | } |
| 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 Chan | 29813b0 | 2018-04-13 14:03:13 -0400 | [diff] [blame] | 123 | 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 Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 132 | } |
| 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 Chan | 29813b0 | 2018-04-13 14:03:13 -0400 | [diff] [blame] | 157 | case DUID_UUID: |
| 158 | byteBuffer = ByteBuffer.allocate(DEFAULT_UUID_LEN + uuid.length); |
| 159 | byteBuffer.putShort(duidType.value); |
Charles Chan | 79cc5e8 | 2018-04-23 10:24:21 -0700 | [diff] [blame] | 160 | byteBuffer.put(uuid); |
Charles Chan | 29813b0 | 2018-04-13 14:03:13 -0400 | [diff] [blame] | 161 | break; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 162 | default: |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 163 | throw new IllegalArgumentException("Unknown duidType: " + duidType.toString()); |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 164 | } |
| 165 | return byteBuffer.array(); |
| 166 | } |
| 167 | |
Ray Milkey | f0c4761 | 2017-09-28 11:29:38 -0700 | [diff] [blame] | 168 | |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 169 | |
| 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 Chan | 29813b0 | 2018-04-13 14:03:13 -0400 | [diff] [blame] | 194 | case DUID_UUID: |
| 195 | duid.uuid = new byte[length - DEFAULT_LL_LEN]; |
| 196 | byteBuffer.get(duid.uuid); |
| 197 | break; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 198 | default: |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 199 | throw new IllegalArgumentException("Unknown type: " + duidType); |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 200 | } |
| 201 | return duid; |
| 202 | }; |
| 203 | } |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 204 | |
| 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 Chan | 29813b0 | 2018-04-13 14:03:13 -0400 | [diff] [blame] | 226 | case DUID_UUID: |
| 227 | helper.add("type", "DUID_UUID"); |
| 228 | helper.add("uuid", Arrays.toString(uuid)); |
| 229 | break; |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 230 | default: |
| 231 | helper.add("type", "Unknown"); |
| 232 | } |
| 233 | return helper.toString(); |
| 234 | } |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 235 | } |