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