Kalhee Kim | 1b5094f | 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 | import org.onlab.packet.MacAddress; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import org.onlab.packet.DHCP6; |
| 22 | import org.onlab.packet.Deserializer; |
| 23 | import org.onlab.packet.DeserializationException; |
Kalhee Kim | aa5172a | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
Kalhee Kim | 1b5094f | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | import java.nio.ByteBuffer; |
| 28 | |
| 29 | /** |
| 30 | * Relay option for DHCPv6. |
| 31 | * Based on RFC-3315. |
| 32 | */ |
| 33 | public final class Dhcp6InterfaceIdOption extends Dhcp6Option { |
| 34 | private MacAddress peerMacAddr; |
| 35 | private byte[] inPort; |
Kalhee Kim | aa5172a | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 36 | private short vlanId; |
| 37 | |
Kalhee Kim | 1b5094f | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 38 | @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 Kim | aa5172a | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 77 | |
Kalhee Kim | 1b5094f | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 78 | /** |
| 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 Kim | aa5172a | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 106 | * 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 Kim | 1b5094f | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 125 | * 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 Kim | aa5172a | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 137 | if (optionData.length >= 31) { |
Kalhee Kim | 1b5094f | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 138 | ByteBuffer bb = ByteBuffer.wrap(optionData); |
| 139 | |
| 140 | byte[] macAddr = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 141 | byte[] port = new byte[21]; |
Kalhee Kim | aa5172a | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 142 | short vlan; |
Kalhee Kim | 1b5094f | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 143 | bb.get(macAddr); |
Kalhee Kim | aa5172a | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 144 | bb.get(); // separator "-" |
Kalhee Kim | 1b5094f | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 145 | bb.get(port); |
Kalhee Kim | aa5172a | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 146 | bb.get(); // separator ":" |
| 147 | vlan = bb.getShort(); |
Kalhee Kim | 1b5094f | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 148 | interfaceIdOption.setMacAddress(MacAddress.valueOf(macAddr)); |
| 149 | interfaceIdOption.setInPort(port); |
Kalhee Kim | aa5172a | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 150 | interfaceIdOption.setVlanId(vlan > VlanId.MAX_VLAN ? VlanId.UNTAGGED : vlan); |
Kalhee Kim | 1b5094f | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 151 | } |
| 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 | } |