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 | |
| 19 | package org.onlab.packet; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.Arrays; |
| 23 | |
| 24 | /** |
| 25 | * Implements ICMPv6 Neighbor Solicitation packet format. (RFC 4861) |
| 26 | */ |
| 27 | public class NeighborSolicitation extends BasePacket { |
| 28 | public static final byte HEADER_LENGTH = 20; // bytes |
| 29 | |
| 30 | protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH]; |
| 31 | |
| 32 | /** |
| 33 | * Gets target address. |
| 34 | * |
| 35 | * @return the target IPv6 address |
| 36 | */ |
| 37 | public byte[] getTargetAddress() { |
| 38 | return this.targetAddress; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Sets target address. |
| 43 | * |
| 44 | * @param targetAddress the target IPv6 address to set |
| 45 | * @return this |
| 46 | */ |
| 47 | public NeighborSolicitation setTargetAddress(final byte[] targetAddress) { |
| 48 | this.targetAddress = Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH); |
| 49 | return this; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public byte[] serialize() { |
| 54 | byte[] payloadData = null; |
| 55 | if (this.payload != null) { |
| 56 | this.payload.setParent(this); |
| 57 | payloadData = this.payload.serialize(); |
| 58 | } |
| 59 | |
| 60 | int payloadLength = payloadData == null ? 0 : (short) payloadData.length; |
| 61 | |
| 62 | final byte[] data = new byte[HEADER_LENGTH + payloadLength]; |
| 63 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 64 | |
| 65 | bb.putInt(0); |
| 66 | bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
| 67 | if (payloadData != null) { |
| 68 | bb.put(payloadData); |
| 69 | } |
| 70 | |
| 71 | return data; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 76 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 77 | |
| 78 | bb.getInt(); |
| 79 | bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); |
| 80 | |
| 81 | this.payload = new Data(); |
| 82 | this.payload = this.payload.deserialize(data, bb.position(), bb.limit() |
| 83 | - bb.position()); |
| 84 | this.payload.setParent(this); |
| 85 | |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * (non-Javadoc) |
| 91 | * |
| 92 | * @see java.lang.Object#hashCode() |
| 93 | */ |
| 94 | @Override |
| 95 | public int hashCode() { |
| 96 | final int prime = 5807; |
| 97 | int result = super.hashCode(); |
| 98 | ByteBuffer bb; |
| 99 | bb = ByteBuffer.wrap(this.targetAddress); |
| 100 | for (int i = 0; i < 4; i++) { |
| 101 | result = prime * result + bb.getInt(); |
| 102 | } |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * (non-Javadoc) |
| 108 | * |
| 109 | * @see java.lang.Object#equals(java.lang.Object) |
| 110 | */ |
| 111 | @Override |
| 112 | public boolean equals(final Object obj) { |
| 113 | if (this == obj) { |
| 114 | return true; |
| 115 | } |
| 116 | if (!super.equals(obj)) { |
| 117 | return false; |
| 118 | } |
| 119 | if (!(obj instanceof NeighborSolicitation)) { |
| 120 | return false; |
| 121 | } |
| 122 | final NeighborSolicitation other = (NeighborSolicitation) obj; |
| 123 | if (!Arrays.equals(this.targetAddress, other.targetAddress)) { |
| 124 | return false; |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | } |