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; |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 22 | import org.onlab.packet.DeserializationException; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Deserializer; |
| 24 | import org.onlab.packet.IPacket; |
| 25 | |
| 26 | import java.nio.ByteBuffer; |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 27 | import java.util.Arrays; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 28 | |
| 29 | public class Dhcp6Duid extends BasePacket { |
| 30 | private static final int DEFAULT_LLT_LEN = 8; |
| 31 | private static final int DEFAULT_EN_LEN = 6; |
| 32 | private static final int DEFAULT_LL_LEN = 4; |
| 33 | public enum DuidType { |
| 34 | DUID_LLT((short) 1), |
| 35 | DUID_EN((short) 2), |
| 36 | DUID_LL((short) 3); |
| 37 | |
| 38 | private short value; |
| 39 | DuidType(short value) { |
| 40 | this.value = value; |
| 41 | } |
| 42 | |
| 43 | public short getValue() { |
| 44 | return value; |
| 45 | } |
| 46 | |
| 47 | public static DuidType of(short type) { |
| 48 | switch (type) { |
| 49 | case 1: |
| 50 | return DUID_LLT; |
| 51 | case 2: |
| 52 | return DUID_EN; |
| 53 | case 3: |
| 54 | return DUID_LL; |
| 55 | default: |
| 56 | throw new RuntimeException("Unknown type: " + type); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | // general field |
| 61 | private DuidType duidType; |
| 62 | |
| 63 | // fields for DUID_LLT & DUID_LL |
| 64 | private short hardwareType; |
| 65 | private int duidTime; |
| 66 | private byte[] linkLayerAddress; |
| 67 | |
| 68 | // fields for DUID_EN |
| 69 | private int enterpriseNumber; |
| 70 | private byte[] identifier; |
| 71 | |
| 72 | public DuidType getDuidType() { |
| 73 | return duidType; |
| 74 | } |
| 75 | |
| 76 | public void setDuidType(DuidType duidType) { |
| 77 | this.duidType = duidType; |
| 78 | } |
| 79 | |
| 80 | public short getHardwareType() { |
| 81 | return hardwareType; |
| 82 | } |
| 83 | |
| 84 | public void setHardwareType(short hardwareType) { |
| 85 | this.hardwareType = hardwareType; |
| 86 | } |
| 87 | |
| 88 | public int getDuidTime() { |
| 89 | return duidTime; |
| 90 | } |
| 91 | |
| 92 | public void setDuidTime(int duidTime) { |
| 93 | this.duidTime = duidTime; |
| 94 | } |
| 95 | |
| 96 | public byte[] getLinkLayerAddress() { |
| 97 | return linkLayerAddress; |
| 98 | } |
| 99 | |
| 100 | public void setLinkLayerAddress(byte[] linkLayerAddress) { |
| 101 | this.linkLayerAddress = linkLayerAddress; |
| 102 | } |
| 103 | |
| 104 | public int getEnterpriseNumber() { |
| 105 | return enterpriseNumber; |
| 106 | } |
| 107 | |
| 108 | public void setEnterpriseNumber(int enterpriseNumber) { |
| 109 | this.enterpriseNumber = enterpriseNumber; |
| 110 | } |
| 111 | |
| 112 | public byte[] getIdentifier() { |
| 113 | return identifier; |
| 114 | } |
| 115 | |
| 116 | public void setIdentifier(byte[] identifier) { |
| 117 | this.identifier = identifier; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public byte[] serialize() { |
| 122 | ByteBuffer byteBuffer; |
| 123 | switch (duidType) { |
| 124 | case DUID_LLT: |
| 125 | byteBuffer = ByteBuffer.allocate(DEFAULT_LLT_LEN + linkLayerAddress.length); |
| 126 | byteBuffer.putShort(duidType.value); |
| 127 | byteBuffer.putShort(hardwareType); |
| 128 | byteBuffer.putInt(duidTime); |
| 129 | byteBuffer.put(linkLayerAddress); |
| 130 | break; |
| 131 | case DUID_EN: |
| 132 | byteBuffer = ByteBuffer.allocate(DEFAULT_EN_LEN + identifier.length); |
| 133 | byteBuffer.putShort(duidType.value); |
| 134 | byteBuffer.putInt(enterpriseNumber); |
| 135 | byteBuffer.put(identifier); |
| 136 | break; |
| 137 | case DUID_LL: |
| 138 | byteBuffer = ByteBuffer.allocate(DEFAULT_LL_LEN + linkLayerAddress.length); |
| 139 | byteBuffer.putShort(duidType.value); |
| 140 | byteBuffer.putShort(hardwareType); |
| 141 | byteBuffer.put(linkLayerAddress); |
| 142 | break; |
| 143 | default: |
| 144 | throw new RuntimeException("Unknown duidType: " + duidType.toString()); |
| 145 | } |
| 146 | return byteBuffer.array(); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public IPacket deserialize(byte[] data, int offset, int length) { |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 151 | try { |
| 152 | return deserializer().deserialize(data, offset, length); |
| 153 | } catch (DeserializationException e) { |
| 154 | throw new RuntimeException("Can't deserialize duid due to {}", e); |
| 155 | } |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | public static Deserializer<Dhcp6Duid> deserializer() { |
| 159 | return (data, offset, length) -> { |
| 160 | Dhcp6Duid duid = new Dhcp6Duid(); |
| 161 | ByteBuffer byteBuffer = ByteBuffer.wrap(data, offset, length); |
| 162 | |
| 163 | DuidType duidType = DuidType.of(byteBuffer.getShort()); |
| 164 | duid.setDuidType(duidType); |
| 165 | switch (duidType) { |
| 166 | case DUID_LLT: |
| 167 | duid.setHardwareType(byteBuffer.getShort()); |
| 168 | duid.setDuidTime(byteBuffer.getInt()); |
| 169 | duid.linkLayerAddress = new byte[length - DEFAULT_LLT_LEN]; |
| 170 | byteBuffer.get(duid.linkLayerAddress); |
| 171 | break; |
| 172 | case DUID_EN: |
| 173 | duid.setEnterpriseNumber(byteBuffer.getInt()); |
| 174 | duid.identifier = new byte[length - DEFAULT_EN_LEN]; |
| 175 | byteBuffer.get(duid.identifier); |
| 176 | break; |
| 177 | case DUID_LL: |
| 178 | duid.setHardwareType(byteBuffer.getShort()); |
| 179 | duid.linkLayerAddress = new byte[length - DEFAULT_LL_LEN]; |
| 180 | byteBuffer.get(duid.linkLayerAddress); |
| 181 | break; |
| 182 | default: |
| 183 | throw new RuntimeException("Unknown type: " + duidType); |
| 184 | } |
| 185 | return duid; |
| 186 | }; |
| 187 | } |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 188 | |
| 189 | @Override |
| 190 | public String toString() { |
| 191 | MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(getClass()); |
| 192 | |
| 193 | switch (duidType) { |
| 194 | case DUID_LLT: |
| 195 | helper.add("type", "DUID_LLT"); |
| 196 | helper.add("hardwareType", hardwareType); |
| 197 | helper.add("duidTime", duidTime); |
| 198 | helper.add("linkLayerAddress", Arrays.toString(linkLayerAddress)); |
| 199 | break; |
| 200 | case DUID_EN: |
| 201 | helper.add("type", "DUID_EN"); |
| 202 | helper.add("enterpriseNumber", enterpriseNumber); |
| 203 | helper.add("id", Arrays.toString(identifier)); |
| 204 | break; |
| 205 | case DUID_LL: |
| 206 | helper.add("type", "DUID_LL"); |
| 207 | helper.add("hardwareType", hardwareType); |
| 208 | helper.add("linkLayerAddress", Arrays.toString(linkLayerAddress)); |
| 209 | break; |
| 210 | default: |
| 211 | helper.add("type", "Unknown"); |
| 212 | } |
| 213 | return helper.toString(); |
| 214 | } |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 215 | } |