Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +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 | ea5aa47 | 2015-01-03 13:40:39 +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 | |
| 21 | import java.nio.ByteBuffer; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 22 | import java.util.List; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Implements ICMPv6 Router Solicitation packet format. (RFC 4861) |
| 26 | */ |
| 27 | public class RouterSolicitation extends BasePacket { |
| 28 | public static final byte HEADER_LENGTH = 4; // bytes |
| 29 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 30 | private final NeighborDiscoveryOptions options = |
| 31 | new NeighborDiscoveryOptions(); |
| 32 | |
| 33 | /** |
| 34 | * Gets the Neighbor Discovery Protocol packet options. |
| 35 | * |
| 36 | * @return the Neighbor Discovery Protocol packet options |
| 37 | */ |
| 38 | public List<NeighborDiscoveryOptions.Option> getOptions() { |
| 39 | return this.options.options(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Adds a Neighbor Discovery Protocol packet option. |
| 44 | * |
| 45 | * @param type the option type |
| 46 | * @param data the option data |
| 47 | * @return this |
| 48 | */ |
| 49 | public RouterSolicitation addOption(final byte type, final byte[] data) { |
| 50 | this.options.addOption(type, data); |
| 51 | return this; |
| 52 | } |
| 53 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 54 | @Override |
| 55 | public byte[] serialize() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 56 | byte[] optionsData = null; |
| 57 | if (this.options.hasOptions()) { |
| 58 | optionsData = this.options.serialize(); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 59 | } |
| 60 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 61 | int optionsLength = 0; |
| 62 | if (optionsData != null) { |
| 63 | optionsLength = optionsData.length; |
Charles M.C. Chan | 5c0b476 | 2015-01-10 18:38:37 +0800 | [diff] [blame] | 64 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 65 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 66 | final byte[] data = new byte[HEADER_LENGTH + optionsLength]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 67 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 68 | |
| 69 | bb.putInt(0); |
| 70 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 71 | if (optionsData != null) { |
| 72 | bb.put(optionsData); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | return data; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 80 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 81 | |
| 82 | bb.getInt(); |
| 83 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 84 | this.options.deserialize(data, bb.position(), |
| 85 | bb.limit() - bb.position()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 86 | |
| 87 | return this; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * (non-Javadoc) |
| 92 | * |
| 93 | * @see java.lang.Object#hashCode() |
| 94 | */ |
| 95 | @Override |
| 96 | public int hashCode() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 97 | final int prime = 5807; |
| 98 | int result = super.hashCode(); |
| 99 | result = prime * result + this.options.hashCode(); |
| 100 | return result; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * (non-Javadoc) |
| 105 | * |
| 106 | * @see java.lang.Object#equals(java.lang.Object) |
| 107 | */ |
| 108 | @Override |
| 109 | public boolean equals(final Object obj) { |
| 110 | if (this == obj) { |
| 111 | return true; |
| 112 | } |
| 113 | if (!super.equals(obj)) { |
| 114 | return false; |
| 115 | } |
| 116 | if (!(obj instanceof RouterSolicitation)) { |
| 117 | return false; |
| 118 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 119 | final RouterSolicitation other = (RouterSolicitation) obj; |
| 120 | if (!this.options.equals(other.options)) { |
| 121 | return false; |
| 122 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | } |