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; |
| 24 | |
| 25 | |
| 26 | import java.nio.ByteBuffer; |
| 27 | |
| 28 | /** |
| 29 | * Relay option for DHCPv6. |
| 30 | * Based on RFC-3315. |
| 31 | */ |
| 32 | public final class Dhcp6InterfaceIdOption extends Dhcp6Option { |
| 33 | private MacAddress peerMacAddr; |
| 34 | private byte[] inPort; |
| 35 | @Override |
| 36 | public short getCode() { |
| 37 | return DHCP6.OptionCode.INTERFACE_ID.value(); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public short getLength() { |
| 42 | return (short) payload.serialize().length; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public byte[] getData() { |
| 47 | return this.payload.serialize(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Default constructor. |
| 52 | */ |
| 53 | public Dhcp6InterfaceIdOption() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Constructs a DHCPv6 relay option with DHCPv6 option. |
| 58 | * |
| 59 | * @param dhcp6Option the DHCPv6 option |
| 60 | */ |
| 61 | public Dhcp6InterfaceIdOption(Dhcp6Option dhcp6Option) { |
| 62 | super(dhcp6Option); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Sets MacAddress address. |
| 67 | * |
| 68 | * @param macAddress the client peer MacAddress |
| 69 | */ |
| 70 | public void setMacAddress(MacAddress macAddress) { |
| 71 | this.peerMacAddr = macAddress; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Gets Mac address. |
| 76 | * |
| 77 | * @return the client peer mac address |
| 78 | */ |
| 79 | public MacAddress getMacAddress() { |
| 80 | return peerMacAddr; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Sets inPort string. |
| 85 | * |
| 86 | * @param port the port from which client packet is received |
| 87 | */ |
| 88 | public void setInPort(byte[] port) { |
| 89 | this.inPort = port; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Gets inPort string. |
| 94 | * |
| 95 | * @return the port from which client packet is received |
| 96 | */ |
| 97 | public byte[] getInPort() { |
| 98 | return inPort; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Gets deserializer for DHCPv6 relay option. |
| 103 | * |
| 104 | * @return the deserializer |
| 105 | */ |
| 106 | public static Deserializer<Dhcp6Option> deserializer() { |
| 107 | return (data, offset, len) -> { |
| 108 | Dhcp6Option dhcp6Option = Dhcp6Option.deserializer().deserialize(data, offset, len); |
| 109 | if (dhcp6Option.getLength() < DEFAULT_LEN) { |
| 110 | throw new DeserializationException("Invalid InterfaceIoption data"); |
| 111 | } |
| 112 | Dhcp6InterfaceIdOption interfaceIdOption = new Dhcp6InterfaceIdOption(dhcp6Option); |
| 113 | byte[] optionData = interfaceIdOption.getData(); |
| 114 | if (optionData.length >= 28) { |
| 115 | ByteBuffer bb = ByteBuffer.wrap(optionData); |
| 116 | |
| 117 | byte[] macAddr = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
| 118 | byte[] port = new byte[21]; |
| 119 | bb.get(macAddr); |
| 120 | bb.get(); // separator |
| 121 | bb.get(port); |
| 122 | interfaceIdOption.setMacAddress(MacAddress.valueOf(macAddr)); |
| 123 | interfaceIdOption.setInPort(port); |
| 124 | } |
| 125 | return interfaceIdOption; |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String toString() { |
| 131 | return MoreObjects.toStringHelper(getClass()) |
| 132 | .add("code", getCode()) |
| 133 | .add("length", getLength()) |
| 134 | .add("data", payload.toString()) |
| 135 | .toString(); |
| 136 | } |
| 137 | } |