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