Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 20 | import org.onlab.packet.DHCP6; |
| 21 | import org.onlab.packet.Data; |
| 22 | import org.onlab.packet.DeserializationException; |
| 23 | import org.onlab.packet.Deserializer; |
| 24 | import org.onlab.packet.IPacket; |
| 25 | import org.onlab.packet.Ip6Address; |
| 26 | |
| 27 | import java.nio.ByteBuffer; |
| 28 | import java.util.Objects; |
| 29 | |
| 30 | /** |
| 31 | * IA Address option for DHCPv6. |
| 32 | * Based on RFC-3633. |
| 33 | */ |
| 34 | public final class Dhcp6IaPrefixOption extends Dhcp6Option { |
| 35 | public static final int DEFAULT_LEN = 25; |
| 36 | |
| 37 | private Ip6Address ip6Prefix; |
| 38 | private byte prefixLength; |
| 39 | private int preferredLifetime; |
| 40 | private int validLifetime; |
| 41 | private IPacket options; |
| 42 | |
| 43 | @Override |
| 44 | public short getCode() { |
| 45 | return DHCP6.OptionCode.IAPREFIX.value(); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public short getLength() { |
| 50 | return (short) (options == null ? DEFAULT_LEN : DEFAULT_LEN + options.serialize().length); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Sets IPv6 prefix. |
| 55 | * |
| 56 | * @param ip6Prefix the IPv6 prefix |
| 57 | */ |
| 58 | public void setIp6Prefix(Ip6Address ip6Prefix) { |
| 59 | this.ip6Prefix = ip6Prefix; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Sets prefix length. |
| 64 | * |
| 65 | * @param prefixLength the prefix length |
| 66 | */ |
| 67 | public void setPrefixLength(byte prefixLength) { |
| 68 | this.prefixLength = prefixLength; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sets preferred lifetime. |
| 73 | * |
| 74 | * @param preferredLifetime the preferred lifetime |
| 75 | */ |
| 76 | public void setPreferredLifetime(int preferredLifetime) { |
| 77 | this.preferredLifetime = preferredLifetime; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Sets valid lifetime. |
| 82 | * |
| 83 | * @param validLifetime the valid lifetime |
| 84 | */ |
| 85 | public void setValidLifetime(int validLifetime) { |
| 86 | this.validLifetime = validLifetime; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sets options data. |
| 91 | * |
| 92 | * @param options the options data |
| 93 | */ |
| 94 | public void setOptions(IPacket options) { |
| 95 | this.options = options; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Gets IPv6 address. |
| 100 | * |
| 101 | * @return the IPv6 address |
| 102 | */ |
| 103 | public Ip6Address getIp6Prefix() { |
| 104 | return ip6Prefix; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Gets prefix length. |
| 109 | * |
| 110 | * @return the prefix length |
| 111 | */ |
| 112 | public byte getPrefixLength() { |
| 113 | return prefixLength; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Gets preferred lifetime. |
| 118 | * |
| 119 | * @return the preferred lifetime |
| 120 | */ |
| 121 | public int getPreferredLifetime() { |
| 122 | return preferredLifetime; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Gets valid lifetime. |
| 127 | * |
| 128 | * @return the valid lifetime |
| 129 | */ |
| 130 | public int getValidLifetime() { |
| 131 | return validLifetime; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Gets options of IA Address option. |
| 136 | * |
| 137 | * @return the options data |
| 138 | */ |
| 139 | public IPacket getOptions() { |
| 140 | return options; |
| 141 | } |
| 142 | |
| 143 | public static Deserializer<Dhcp6Option> deserializer() { |
| 144 | return (data, offset, length) -> { |
| 145 | Dhcp6IaPrefixOption iaPrefixOption = new Dhcp6IaPrefixOption(); |
| 146 | Dhcp6Option dhcp6Option = |
| 147 | Dhcp6Option.deserializer().deserialize(data, offset, length); |
| 148 | iaPrefixOption.setPayload(dhcp6Option.getPayload()); |
| 149 | if (dhcp6Option.getLength() < DEFAULT_LEN) { |
| 150 | throw new DeserializationException("Invalid length of IA prefix option"); |
| 151 | } |
| 152 | ByteBuffer bb = ByteBuffer.wrap(dhcp6Option.getData()); |
| 153 | iaPrefixOption.preferredLifetime = bb.getInt(); |
| 154 | iaPrefixOption.validLifetime = bb.getInt(); |
| 155 | iaPrefixOption.prefixLength = bb.get(); |
| 156 | byte[] ipv6Pref = new byte[Ip6Address.BYTE_LENGTH]; |
| 157 | bb.get(ipv6Pref); |
| 158 | iaPrefixOption.ip6Prefix = Ip6Address.valueOf(ipv6Pref); |
| 159 | |
| 160 | // options length of IA Address option |
| 161 | int optionsLen = dhcp6Option.getLength() - DEFAULT_LEN; |
| 162 | if (optionsLen > 0) { |
| 163 | byte[] optionsData = new byte[optionsLen]; |
| 164 | bb.get(optionsData); |
| 165 | iaPrefixOption.options = |
| 166 | Data.deserializer().deserialize(optionsData, 0, optionsLen); |
| 167 | } |
| 168 | return iaPrefixOption; |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public byte[] serialize() { |
| 174 | int payloadLen = options == null ? DEFAULT_LEN : DEFAULT_LEN + options.serialize().length; |
| 175 | ByteBuffer bb = ByteBuffer.allocate(payloadLen + Dhcp6Option.DEFAULT_LEN); |
| 176 | bb.putShort(DHCP6.OptionCode.IAPREFIX.value()); |
| 177 | bb.putShort((short) payloadLen); |
| 178 | bb.putInt(preferredLifetime); |
| 179 | bb.putInt(validLifetime); |
| 180 | bb.put(prefixLength); |
| 181 | bb.put(ip6Prefix.toOctets()); |
| 182 | if (options != null) { |
| 183 | bb.put(options.serialize()); |
| 184 | } |
| 185 | return bb.array(); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public int hashCode() { |
| 190 | return Objects.hash(super.hashCode(), ip6Prefix, prefixLength, preferredLifetime, |
| 191 | validLifetime, options); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public boolean equals(Object obj) { |
| 196 | if (this == obj) { |
| 197 | return true; |
| 198 | } |
| 199 | if (obj == null) { |
| 200 | return false; |
| 201 | } |
Yuta HIGUCHI | 6800ced | 2017-11-20 11:15:37 -0800 | [diff] [blame] | 202 | if (!(obj instanceof Dhcp6IaPrefixOption)) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | final Dhcp6IaPrefixOption other = (Dhcp6IaPrefixOption) obj; |
| 206 | |
| 207 | return Objects.equals(getCode(), other.getCode()) && |
| 208 | Objects.equals(getLength(), other.getLength()) && |
| 209 | Objects.equals(preferredLifetime, other.preferredLifetime) && |
| 210 | Objects.equals(validLifetime, other.validLifetime) && |
| 211 | Objects.equals(prefixLength, other.prefixLength) && |
| 212 | Objects.equals(ip6Prefix, other.ip6Prefix) && |
| 213 | Objects.equals(options, other.options); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public String toString() { |
| 218 | return getToStringHelper() |
| 219 | .add("preferredLifetime", preferredLifetime) |
| 220 | .add("validLifetime", validLifetime) |
| 221 | .add("prefixLength", prefixLength) |
| 222 | .add("ip6Address", ip6Prefix) |
| 223 | .add("options", options) |
| 224 | .toString(); |
| 225 | } |
| 226 | } |