Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 19 | package org.onlab.packet.ndp; |
| 20 | |
| 21 | import org.onlab.packet.BasePacket; |
| 22 | import org.onlab.packet.Data; |
| 23 | import org.onlab.packet.IPacket; |
| 24 | import org.onlab.packet.Ip6Address; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 25 | |
| 26 | import java.nio.ByteBuffer; |
| 27 | import java.util.Arrays; |
| 28 | |
| 29 | /** |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 30 | * Implements ICMPv6 Redirect packet format. (RFC 4861) |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 31 | */ |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 32 | public class Redirect extends BasePacket { |
| 33 | public static final byte HEADER_LENGTH = 36; // bytes |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 34 | |
| 35 | protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 36 | protected byte[] destinationAddress = new byte[Ip6Address.BYTE_LENGTH]; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 37 | |
| 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) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 54 | this.targetAddress = Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH); |
| 55 | return this; |
| 56 | } |
| 57 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 58 | /** |
| 59 | * Gets destination address. |
| 60 | * |
| 61 | * @return the destination IPv6 address |
| 62 | */ |
| 63 | public byte[] getDestinationAddress() { |
| 64 | return this.destinationAddress; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Sets destination address. |
| 69 | * |
| 70 | * @param destinationAddress the destination IPv6 address to set |
| 71 | * @return this |
| 72 | */ |
| 73 | public Redirect setDestinationAddress(final byte[] destinationAddress) { |
| 74 | this.destinationAddress = Arrays.copyOfRange(destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
| 75 | return this; |
| 76 | } |
| 77 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 78 | @Override |
| 79 | public byte[] serialize() { |
| 80 | byte[] payloadData = null; |
| 81 | if (this.payload != null) { |
| 82 | this.payload.setParent(this); |
| 83 | payloadData = this.payload.serialize(); |
| 84 | } |
| 85 | |
Charles M.C. Chan | 5c0b476 | 2015-01-10 18:38:37 +0800 | [diff] [blame] | 86 | int payloadLength = 0; |
| 87 | if (payloadData != null) { |
| 88 | payloadLength = payloadData.length; |
| 89 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 90 | |
| 91 | final byte[] data = new byte[HEADER_LENGTH + payloadLength]; |
| 92 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 93 | |
| 94 | bb.putInt(0); |
| 95 | bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 96 | bb.put(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
| 97 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 98 | if (payloadData != null) { |
| 99 | bb.put(payloadData); |
| 100 | } |
| 101 | |
| 102 | return data; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 107 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 108 | |
| 109 | bb.getInt(); |
| 110 | bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 111 | bb.get(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 112 | |
| 113 | this.payload = new Data(); |
| 114 | this.payload = this.payload.deserialize(data, bb.position(), bb.limit() |
| 115 | - bb.position()); |
| 116 | this.payload.setParent(this); |
| 117 | |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * (non-Javadoc) |
| 123 | * |
| 124 | * @see java.lang.Object#hashCode() |
| 125 | */ |
| 126 | @Override |
| 127 | public int hashCode() { |
| 128 | final int prime = 5807; |
| 129 | int result = super.hashCode(); |
| 130 | ByteBuffer bb; |
| 131 | bb = ByteBuffer.wrap(this.targetAddress); |
| 132 | for (int i = 0; i < 4; i++) { |
| 133 | result = prime * result + bb.getInt(); |
| 134 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 135 | bb = ByteBuffer.wrap(this.destinationAddress); |
| 136 | for (int i = 0; i < 4; i++) { |
| 137 | result = prime * result + bb.getInt(); |
| 138 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 139 | return result; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * (non-Javadoc) |
| 144 | * |
| 145 | * @see java.lang.Object#equals(java.lang.Object) |
| 146 | */ |
| 147 | @Override |
| 148 | public boolean equals(final Object obj) { |
| 149 | if (this == obj) { |
| 150 | return true; |
| 151 | } |
| 152 | if (!super.equals(obj)) { |
| 153 | return false; |
| 154 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 155 | if (!(obj instanceof Redirect)) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 156 | return false; |
| 157 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 158 | final Redirect other = (Redirect) obj; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 159 | if (!Arrays.equals(this.targetAddress, other.targetAddress)) { |
| 160 | return false; |
| 161 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 162 | if (!Arrays.equals(this.destinationAddress, other.destinationAddress)) { |
| 163 | return false; |
| 164 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | } |