Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [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 com.google.common.base.MoreObjects; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import org.onlab.packet.DHCP6; |
| 23 | import org.onlab.packet.Deserializer; |
| 24 | import org.onlab.packet.Ip6Address; |
| 25 | |
| 26 | import java.nio.ByteBuffer; |
| 27 | import java.util.List; |
| 28 | import java.util.Objects; |
| 29 | |
| 30 | /** |
| 31 | * DHCPv6 Lease Query Option. |
| 32 | */ |
| 33 | public final class Dhcp6LeaseQueryOption extends Dhcp6Option { |
| 34 | |
| 35 | public static final int DEFAULT_LEN = 1 + 16; |
| 36 | //public short QueryType; |
| 37 | public Ip6Address linkAddress; |
| 38 | private List<Dhcp6Option> options; |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 39 | public byte queryType; |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 40 | |
| 41 | public Dhcp6LeaseQueryOption(Dhcp6Option dhcp6Option) { |
| 42 | super(dhcp6Option); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 43 | options = Lists.newArrayList(); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public short getCode() { |
| 48 | return DHCP6.OptionCode.LEASE_QUERY.value(); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public short getLength() { |
| 53 | //return (short) payload.serialize().length; |
| 54 | return (short) (DEFAULT_LEN + options.stream() |
| 55 | .mapToInt(opt -> (int) opt.getLength() + Dhcp6Option.DEFAULT_LEN) |
| 56 | .sum()); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public byte[] getData() { |
| 61 | return payload.serialize(); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | public static Deserializer<Dhcp6Option> deserializer() { |
| 66 | return (data, offset, length) -> { |
| 67 | Dhcp6Option dhcp6Option = Dhcp6Option.deserializer().deserialize(data, offset, length); |
| 68 | Dhcp6LeaseQueryOption lQ6Option = new Dhcp6LeaseQueryOption(dhcp6Option); |
| 69 | |
| 70 | byte[] optionData = lQ6Option.getData(); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 71 | if (optionData.length >= DEFAULT_LEN) { |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 72 | ByteBuffer bb = ByteBuffer.wrap(optionData); |
| 73 | // fetch the Query type - just pop the byte from the byte buffer for subsequent parsing... |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 74 | lQ6Option.queryType = bb.get(); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 75 | byte[] ipv6Addr = new byte[16]; |
| 76 | bb.get(ipv6Addr); |
| 77 | lQ6Option.linkAddress = Ip6Address.valueOf(ipv6Addr); |
| 78 | //int optionsLen = dhcp6Option.getLength() - 1 - 16; // query type (1) + link address (16) |
| 79 | |
| 80 | lQ6Option.options = Lists.newArrayList(); |
| 81 | |
| 82 | while (bb.remaining() >= Dhcp6Option.DEFAULT_LEN) { |
| 83 | Dhcp6Option option; |
| 84 | ByteBuffer optByteBuffer = ByteBuffer.wrap(optionData, |
| 85 | bb.position(), |
| 86 | optionData.length - bb.position()); |
| 87 | short code = optByteBuffer.getShort(); |
| 88 | short len = optByteBuffer.getShort(); |
| 89 | int optLen = UNSIGNED_SHORT_MASK & len; |
| 90 | byte[] subOptData = new byte[Dhcp6Option.DEFAULT_LEN + optLen]; |
| 91 | bb.get(subOptData); |
| 92 | |
| 93 | // TODO: put more sub-options? |
| 94 | if (code == DHCP6.OptionCode.IAADDR.value()) { |
| 95 | option = Dhcp6IaAddressOption.deserializer() |
| 96 | .deserialize(subOptData, 0, subOptData.length); |
| 97 | } else if (code == DHCP6.OptionCode.ORO.value()) { |
| 98 | option = Dhcp6Option.deserializer() |
| 99 | .deserialize(subOptData, 0, subOptData.length); |
| 100 | } else { |
| 101 | option = Dhcp6Option.deserializer() |
| 102 | .deserialize(subOptData, 0, subOptData.length); |
| 103 | } |
| 104 | lQ6Option.options.add(option); |
| 105 | } |
| 106 | } |
| 107 | return lQ6Option; |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public byte[] serialize() { |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 113 | byte[] serializedPayload = payload.serialize(); |
| 114 | |
| 115 | ByteBuffer bb = ByteBuffer.allocate(serializedPayload.length + Dhcp6Option.DEFAULT_LEN); |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 116 | bb.putShort(getCode()); |
| 117 | bb.putShort(getLength()); |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 118 | bb.put(serializedPayload); |
| 119 | |
Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 120 | return bb.array(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public String toString() { |
| 125 | return MoreObjects.toStringHelper(getClass()) |
| 126 | .add("code", getCode()) |
| 127 | .add("length", getLength()) |
| 128 | .toString(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | @Override |
| 133 | public int hashCode() { |
| 134 | return Objects.hash(super.hashCode(), linkAddress, options); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public boolean equals(Object obj) { |
| 139 | if (this == obj) { |
| 140 | return true; |
| 141 | } |
| 142 | if (obj == null) { |
| 143 | return false; |
| 144 | } |
| 145 | if (!(obj instanceof Dhcp6LeaseQueryOption)) { |
| 146 | return false; |
| 147 | } |
| 148 | if (!super.equals(obj)) { |
| 149 | return false; |
| 150 | } |
| 151 | final Dhcp6LeaseQueryOption other = (Dhcp6LeaseQueryOption) obj; |
| 152 | |
| 153 | return Objects.equals(getCode(), other.getCode()) && |
| 154 | Objects.equals(getLength(), other.getLength()) && |
| 155 | Objects.equals(linkAddress, other.linkAddress) && |
| 156 | Objects.equals(options, other.options); |
| 157 | } |
| 158 | } |