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