Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 1 | /* |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 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 | */ |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 16 | package org.onlab.packet.ndp; |
| 17 | |
| 18 | import org.onlab.packet.BasePacket; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 19 | import org.onlab.packet.IPacket; |
| 20 | import org.onlab.packet.Ip6Address; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | import java.util.Arrays; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 24 | import java.util.List; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 25 | |
| 26 | /** |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 27 | * Implements ICMPv6 Redirect packet format. (RFC 4861) |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 28 | */ |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 29 | public class Redirect extends BasePacket { |
| 30 | public static final byte HEADER_LENGTH = 36; // bytes |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 31 | |
| 32 | protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 33 | protected byte[] destinationAddress = new byte[Ip6Address.BYTE_LENGTH]; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 34 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 35 | private final NeighborDiscoveryOptions options = |
| 36 | new NeighborDiscoveryOptions(); |
| 37 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 38 | /** |
| 39 | * Gets target address. |
| 40 | * |
| 41 | * @return the target IPv6 address |
| 42 | */ |
| 43 | public byte[] getTargetAddress() { |
| 44 | return this.targetAddress; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Sets target address. |
| 49 | * |
| 50 | * @param targetAddress the target IPv6 address to set |
| 51 | * @return this |
| 52 | */ |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 53 | public Redirect setTargetAddress(final byte[] targetAddress) { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 54 | this.targetAddress = |
| 55 | Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 56 | return this; |
| 57 | } |
| 58 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 59 | /** |
| 60 | * Gets destination address. |
| 61 | * |
| 62 | * @return the destination IPv6 address |
| 63 | */ |
| 64 | public byte[] getDestinationAddress() { |
| 65 | return this.destinationAddress; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Sets destination address. |
| 70 | * |
| 71 | * @param destinationAddress the destination IPv6 address to set |
| 72 | * @return this |
| 73 | */ |
| 74 | public Redirect setDestinationAddress(final byte[] destinationAddress) { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 75 | this.destinationAddress = |
| 76 | Arrays.copyOfRange(destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
| 77 | return this; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Gets the Neighbor Discovery Protocol packet options. |
| 82 | * |
| 83 | * @return the Neighbor Discovery Protocol packet options |
| 84 | */ |
| 85 | public List<NeighborDiscoveryOptions.Option> getOptions() { |
| 86 | return this.options.options(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Adds a Neighbor Discovery Protocol packet option. |
| 91 | * |
| 92 | * @param type the option type |
| 93 | * @param data the option data |
| 94 | * @return this |
| 95 | */ |
| 96 | public Redirect addOption(final byte type, final byte[] data) { |
| 97 | this.options.addOption(type, data); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 98 | return this; |
| 99 | } |
| 100 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 101 | @Override |
| 102 | public byte[] serialize() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 103 | byte[] optionsData = null; |
| 104 | if (this.options.hasOptions()) { |
| 105 | optionsData = this.options.serialize(); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 106 | } |
| 107 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 108 | int optionsLength = 0; |
| 109 | if (optionsData != null) { |
| 110 | optionsLength = optionsData.length; |
Charles M.C. Chan | 5c0b476 | 2015-01-10 18:38:37 +0800 | [diff] [blame] | 111 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 112 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 113 | final byte[] data = new byte[HEADER_LENGTH + optionsLength]; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 114 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 115 | |
| 116 | bb.putInt(0); |
| 117 | bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 118 | bb.put(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 119 | if (optionsData != null) { |
| 120 | bb.put(optionsData); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return data; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 128 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 129 | |
| 130 | bb.getInt(); |
| 131 | bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 132 | bb.get(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 133 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 134 | this.options.deserialize(data, bb.position(), |
| 135 | bb.limit() - bb.position()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 136 | |
| 137 | return this; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * (non-Javadoc) |
| 142 | * |
| 143 | * @see java.lang.Object#hashCode() |
| 144 | */ |
| 145 | @Override |
| 146 | public int hashCode() { |
| 147 | final int prime = 5807; |
| 148 | int result = super.hashCode(); |
| 149 | ByteBuffer bb; |
| 150 | bb = ByteBuffer.wrap(this.targetAddress); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 151 | for (int i = 0; i < this.targetAddress.length / 4; i++) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 152 | result = prime * result + bb.getInt(); |
| 153 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 154 | bb = ByteBuffer.wrap(this.destinationAddress); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 155 | for (int i = 0; i < this.destinationAddress.length / 4; i++) { |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 156 | result = prime * result + bb.getInt(); |
| 157 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 158 | result = prime * result + this.options.hashCode(); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 159 | return result; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * (non-Javadoc) |
| 164 | * |
| 165 | * @see java.lang.Object#equals(java.lang.Object) |
| 166 | */ |
| 167 | @Override |
| 168 | public boolean equals(final Object obj) { |
| 169 | if (this == obj) { |
| 170 | return true; |
| 171 | } |
| 172 | if (!super.equals(obj)) { |
| 173 | return false; |
| 174 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 175 | if (!(obj instanceof Redirect)) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 176 | return false; |
| 177 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 178 | final Redirect other = (Redirect) obj; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 179 | if (!Arrays.equals(this.targetAddress, other.targetAddress)) { |
| 180 | return false; |
| 181 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 182 | if (!Arrays.equals(this.destinationAddress, |
| 183 | other.destinationAddress)) { |
| 184 | return false; |
| 185 | } |
| 186 | if (!this.options.equals(other.options)) { |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 187 | return false; |
| 188 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | } |