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 | package org.onlab.packet.dhcp; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.onlab.packet.DHCP6; |
| 21 | import org.onlab.packet.DeserializationException; |
| 22 | import org.onlab.packet.Deserializer; |
| 23 | |
| 24 | import java.nio.ByteBuffer; |
| 25 | import java.util.List; |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | /** |
| 29 | * DHCPv6 Identity Association for Non-temporary Addresses Option. |
| 30 | * Based on RFC-3315 |
| 31 | */ |
| 32 | public final class Dhcp6IaNaOption extends Dhcp6Option { |
| 33 | public static final int DEFAULT_LEN = 12; |
| 34 | private int iaId; |
| 35 | private int t1; |
| 36 | private int t2; |
| 37 | private List<Dhcp6Option> options; |
| 38 | |
| 39 | @Override |
| 40 | public short getCode() { |
| 41 | return DHCP6.OptionCode.IA_NA.value(); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public short getLength() { |
| 46 | return (short) (DEFAULT_LEN + options.stream() |
| 47 | .mapToInt(opt -> (int) opt.getLength() + Dhcp6Option.DEFAULT_LEN) |
| 48 | .sum()); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Gets Identity Association ID. |
| 54 | * |
| 55 | * @return the Identity Association ID |
| 56 | */ |
| 57 | public int getIaId() { |
| 58 | return iaId; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Sets Identity Association ID. |
| 63 | * |
| 64 | * @param iaId the Identity Association ID. |
| 65 | */ |
| 66 | public void setIaId(int iaId) { |
| 67 | this.iaId = iaId; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets time 1. |
| 72 | * The time at which the client contacts the |
| 73 | * server from which the addresses in the IA_NA |
| 74 | * were obtained to extend the lifetimes of the |
| 75 | * addresses assigned to the IA_NA; T1 is a |
| 76 | * time duration relative to the current time |
| 77 | * expressed in units of seconds. |
| 78 | * |
| 79 | * @return the value of time 1 |
| 80 | */ |
| 81 | public int getT1() { |
| 82 | return t1; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Sets time 1. |
| 87 | * |
| 88 | * @param t1 the value of time 1 |
| 89 | */ |
| 90 | public void setT1(int t1) { |
| 91 | this.t1 = t1; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Gets time 2. |
| 96 | * The time at which the client contacts any |
| 97 | * available server to extend the lifetimes of |
| 98 | * the addresses assigned to the IA_NA; T2 is a |
| 99 | * time duration relative to the current time |
| 100 | * expressed in units of seconds. |
| 101 | * |
| 102 | * @return the value of time 2 |
| 103 | */ |
| 104 | public int getT2() { |
| 105 | return t2; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Sets time 2. |
| 110 | * |
| 111 | * @param t2 the value of time 2 |
| 112 | */ |
| 113 | public void setT2(int t2) { |
| 114 | this.t2 = t2; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Gets sub-options. |
| 119 | * |
| 120 | * @return sub-options of this option |
| 121 | */ |
| 122 | public List<Dhcp6Option> getOptions() { |
| 123 | return options; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Sets sub-options. |
| 128 | * |
| 129 | * @param options the sub-options of this option |
| 130 | */ |
| 131 | public void setOptions(List<Dhcp6Option> options) { |
| 132 | this.options = options; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Default constructor. |
| 137 | */ |
| 138 | public Dhcp6IaNaOption() { |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Constructs a DHCPv6 IA NA option with DHCPv6 option. |
| 143 | * |
| 144 | * @param dhcp6Option the DHCPv6 option |
| 145 | */ |
| 146 | public Dhcp6IaNaOption(Dhcp6Option dhcp6Option) { |
| 147 | super(dhcp6Option); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Gets deserializer. |
| 152 | * |
| 153 | * @return the deserializer |
| 154 | */ |
| 155 | public static Deserializer<Dhcp6Option> deserializer() { |
| 156 | return (data, offset, length) -> { |
| 157 | Dhcp6Option dhcp6Option = |
| 158 | Dhcp6Option.deserializer().deserialize(data, offset, length); |
| 159 | if (dhcp6Option.getLength() < DEFAULT_LEN) { |
| 160 | throw new DeserializationException("Invalid IA NA option data"); |
| 161 | } |
| 162 | Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption(dhcp6Option); |
| 163 | byte[] optionData = iaNaOption.getData(); |
| 164 | ByteBuffer bb = ByteBuffer.wrap(optionData); |
| 165 | iaNaOption.iaId = bb.getInt(); |
| 166 | iaNaOption.t1 = bb.getInt(); |
| 167 | iaNaOption.t2 = bb.getInt(); |
| 168 | |
| 169 | iaNaOption.options = Lists.newArrayList(); |
| 170 | while (bb.remaining() >= Dhcp6Option.DEFAULT_LEN) { |
| 171 | Dhcp6Option option; |
| 172 | ByteBuffer optByteBuffer = ByteBuffer.wrap(optionData, |
| 173 | bb.position(), |
| 174 | optionData.length - bb.position()); |
| 175 | short code = optByteBuffer.getShort(); |
| 176 | short len = optByteBuffer.getShort(); |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 177 | int optLen = UNSIGNED_SHORT_MASK & len; |
| 178 | byte[] subOptData = new byte[Dhcp6Option.DEFAULT_LEN + optLen]; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 179 | bb.get(subOptData); |
| 180 | |
| 181 | // TODO: put more sub-options? |
| 182 | if (code == DHCP6.OptionCode.IAADDR.value()) { |
| 183 | option = Dhcp6IaAddressOption.deserializer() |
| 184 | .deserialize(subOptData, 0, subOptData.length); |
| 185 | } else { |
| 186 | option = Dhcp6Option.deserializer() |
| 187 | .deserialize(subOptData, 0, subOptData.length); |
| 188 | } |
| 189 | iaNaOption.options.add(option); |
| 190 | } |
| 191 | return iaNaOption; |
| 192 | }; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public byte[] serialize() { |
| 197 | int payloadLen = DEFAULT_LEN + options.stream() |
| 198 | .mapToInt(opt -> (int) opt.getLength() + Dhcp6Option.DEFAULT_LEN) |
| 199 | .sum(); |
| 200 | int len = Dhcp6Option.DEFAULT_LEN + payloadLen; |
| 201 | ByteBuffer bb = ByteBuffer.allocate(len); |
| 202 | bb.putShort(DHCP6.OptionCode.IA_NA.value()); |
| 203 | bb.putShort((short) payloadLen); |
| 204 | bb.putInt(iaId); |
| 205 | bb.putInt(t1); |
| 206 | bb.putInt(t2); |
| 207 | |
| 208 | options.stream().map(Dhcp6Option::serialize).forEach(bb::put); |
| 209 | return bb.array(); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public int hashCode() { |
| 214 | return 31 * super.hashCode() + Objects.hash(iaId, t1, t2, options); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public boolean equals(Object obj) { |
| 219 | if (this == obj) { |
| 220 | return true; |
| 221 | } |
| 222 | if (obj == null || getClass() != obj.getClass()) { |
| 223 | return false; |
| 224 | } |
| 225 | if (!super.equals(obj)) { |
| 226 | return false; |
| 227 | } |
| 228 | final Dhcp6IaNaOption other = (Dhcp6IaNaOption) obj; |
| 229 | return Objects.equals(this.iaId, other.iaId) |
| 230 | && Objects.equals(this.t1, other.t1) |
| 231 | && Objects.equals(this.t2, other.t2) |
| 232 | && Objects.equals(this.options, other.options); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public String toString() { |
| 237 | return getToStringHelper() |
| 238 | .add("iaId", iaId) |
| 239 | .add("t1", t1) |
| 240 | .add("t2", t2) |
| 241 | .add("options", options) |
| 242 | .toString(); |
| 243 | } |
| 244 | } |