blob: 02700324b8c2ad0c4dc9b2957c0e411acaa368e8 [file] [log] [blame]
Kalhee Kim1b5094f2017-09-05 19:05:06 +00001/*
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;
19import org.onlab.packet.MacAddress;
20import com.google.common.base.MoreObjects;
21import org.onlab.packet.DHCP6;
22import org.onlab.packet.Deserializer;
23import org.onlab.packet.DeserializationException;
Kalhee Kimaa5172a2017-09-15 17:43:27 +000024import org.onlab.packet.VlanId;
Kalhee Kim1b5094f2017-09-05 19:05:06 +000025
26
27import java.nio.ByteBuffer;
28
29/**
30 * Relay option for DHCPv6.
31 * Based on RFC-3315.
32 */
33public final class Dhcp6InterfaceIdOption extends Dhcp6Option {
34 private MacAddress peerMacAddr;
35 private byte[] inPort;
Kalhee Kimaa5172a2017-09-15 17:43:27 +000036 private short vlanId;
37
Kalhee Kim1b5094f2017-09-05 19:05:06 +000038 @Override
39 public short getCode() {
40 return DHCP6.OptionCode.INTERFACE_ID.value();
41 }
42
43 @Override
44 public short getLength() {
45 return (short) payload.serialize().length;
46 }
47
48 @Override
49 public byte[] getData() {
50 return this.payload.serialize();
51 }
52
53 /**
54 * Default constructor.
55 */
56 public Dhcp6InterfaceIdOption() {
57 }
58
59 /**
60 * Constructs a DHCPv6 relay option with DHCPv6 option.
61 *
62 * @param dhcp6Option the DHCPv6 option
63 */
64 public Dhcp6InterfaceIdOption(Dhcp6Option dhcp6Option) {
65 super(dhcp6Option);
66 }
67
68 /**
69 * Sets MacAddress address.
70 *
71 * @param macAddress the client peer MacAddress
72 */
73 public void setMacAddress(MacAddress macAddress) {
74 this.peerMacAddr = macAddress;
75 }
76
Kalhee Kimaa5172a2017-09-15 17:43:27 +000077
Kalhee Kim1b5094f2017-09-05 19:05:06 +000078 /**
79 * Gets Mac address.
80 *
81 * @return the client peer mac address
82 */
83 public MacAddress getMacAddress() {
84 return peerMacAddr;
85 }
86
87 /**
88 * Sets inPort string.
89 *
90 * @param port the port from which client packet is received
91 */
92 public void setInPort(byte[] port) {
93 this.inPort = port;
94 }
95
96 /**
97 * Gets inPort string.
98 *
99 * @return the port from which client packet is received
100 */
101 public byte[] getInPort() {
102 return inPort;
103 }
104
105 /**
Kalhee Kimaa5172a2017-09-15 17:43:27 +0000106 * Sets the vlan id of interface id.
107 *
108 * @param vlanId the vlanid of client packet
109 */
110 public void setVlanId(short vlanId) {
111 this.vlanId = vlanId;
112 }
113
114 /**
115 * Gets the vlan id of interface id.
116 *
117 * @return the vlan id
118 *
119 */
120 public short getVlanId() {
121 return vlanId;
122 }
123
124 /**
Kalhee Kim1b5094f2017-09-05 19:05:06 +0000125 * Gets deserializer for DHCPv6 relay option.
126 *
127 * @return the deserializer
128 */
129 public static Deserializer<Dhcp6Option> deserializer() {
130 return (data, offset, len) -> {
131 Dhcp6Option dhcp6Option = Dhcp6Option.deserializer().deserialize(data, offset, len);
132 if (dhcp6Option.getLength() < DEFAULT_LEN) {
133 throw new DeserializationException("Invalid InterfaceIoption data");
134 }
135 Dhcp6InterfaceIdOption interfaceIdOption = new Dhcp6InterfaceIdOption(dhcp6Option);
136 byte[] optionData = interfaceIdOption.getData();
Kalhee Kimaa5172a2017-09-15 17:43:27 +0000137 if (optionData.length >= 31) {
Kalhee Kim1b5094f2017-09-05 19:05:06 +0000138 ByteBuffer bb = ByteBuffer.wrap(optionData);
139
140 byte[] macAddr = new byte[MacAddress.MAC_ADDRESS_LENGTH];
141 byte[] port = new byte[21];
Kalhee Kimaa5172a2017-09-15 17:43:27 +0000142 short vlan;
Kalhee Kim1b5094f2017-09-05 19:05:06 +0000143 bb.get(macAddr);
Kalhee Kimaa5172a2017-09-15 17:43:27 +0000144 bb.get(); // separator "-"
Kalhee Kim1b5094f2017-09-05 19:05:06 +0000145 bb.get(port);
Kalhee Kimaa5172a2017-09-15 17:43:27 +0000146 bb.get(); // separator ":"
147 vlan = bb.getShort();
Kalhee Kim1b5094f2017-09-05 19:05:06 +0000148 interfaceIdOption.setMacAddress(MacAddress.valueOf(macAddr));
149 interfaceIdOption.setInPort(port);
Kalhee Kimaa5172a2017-09-15 17:43:27 +0000150 interfaceIdOption.setVlanId(vlan > VlanId.MAX_VLAN ? VlanId.UNTAGGED : vlan);
Kalhee Kim1b5094f2017-09-05 19:05:06 +0000151 }
152 return interfaceIdOption;
153 };
154 }
155
156 @Override
157 public String toString() {
158 return MoreObjects.toStringHelper(getClass())
159 .add("code", getCode())
160 .add("length", getLength())
161 .add("data", payload.toString())
162 .toString();
163 }
164}