Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present 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; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Deserializer; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 20 | import org.onlab.packet.IPacket; |
| 21 | |
| 22 | import java.nio.ByteBuffer; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 23 | import java.util.List; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 24 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 26 | import static org.onlab.packet.PacketUtils.checkInput; |
| 27 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 28 | /** |
| 29 | * Implements ICMPv6 Router Solicitation packet format. (RFC 4861) |
| 30 | */ |
| 31 | public class RouterSolicitation extends BasePacket { |
| 32 | public static final byte HEADER_LENGTH = 4; // bytes |
| 33 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 34 | private final NeighborDiscoveryOptions options = new NeighborDiscoveryOptions(); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Gets the Neighbor Discovery Protocol packet options. |
| 38 | * |
| 39 | * @return the Neighbor Discovery Protocol packet options |
| 40 | */ |
| 41 | public List<NeighborDiscoveryOptions.Option> getOptions() { |
| 42 | return this.options.options(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Adds a Neighbor Discovery Protocol packet option. |
| 47 | * |
| 48 | * @param type the option type |
| 49 | * @param data the option data |
| 50 | * @return this |
| 51 | */ |
| 52 | public RouterSolicitation addOption(final byte type, final byte[] data) { |
| 53 | this.options.addOption(type, data); |
| 54 | return this; |
| 55 | } |
| 56 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 57 | @Override |
| 58 | public byte[] serialize() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 59 | byte[] optionsData = null; |
| 60 | if (this.options.hasOptions()) { |
| 61 | optionsData = this.options.serialize(); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 62 | } |
| 63 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 64 | int optionsLength = 0; |
| 65 | if (optionsData != null) { |
| 66 | optionsLength = optionsData.length; |
Charles M.C. Chan | 5c0b476 | 2015-01-10 18:38:37 +0800 | [diff] [blame] | 67 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 68 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 69 | final byte[] data = new byte[HEADER_LENGTH + optionsLength]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 70 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 71 | |
| 72 | bb.putInt(0); |
| 73 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 74 | if (optionsData != null) { |
| 75 | bb.put(optionsData); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return data; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public IPacket deserialize(byte[] data, int offset, int length) { |
| 83 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 84 | |
| 85 | bb.getInt(); |
| 86 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 87 | this.options.deserialize(data, bb.position(), |
| 88 | bb.limit() - bb.position()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 89 | |
| 90 | return this; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * (non-Javadoc) |
| 95 | * |
| 96 | * @see java.lang.Object#hashCode() |
| 97 | */ |
| 98 | @Override |
| 99 | public int hashCode() { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 100 | final int prime = 5807; |
| 101 | int result = super.hashCode(); |
| 102 | result = prime * result + this.options.hashCode(); |
| 103 | return result; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 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 RouterSolicitation)) { |
| 120 | return false; |
| 121 | } |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 122 | final RouterSolicitation other = (RouterSolicitation) obj; |
| 123 | if (!this.options.equals(other.options)) { |
| 124 | return false; |
| 125 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 126 | return true; |
| 127 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Deserializer function for router solicitation packets. |
| 131 | * |
| 132 | * @return deserializer function |
| 133 | */ |
| 134 | public static Deserializer<RouterSolicitation> deserializer() { |
| 135 | return (data, offset, length) -> { |
| 136 | checkInput(data, offset, length, HEADER_LENGTH); |
| 137 | |
| 138 | RouterSolicitation routerSolicitation = new RouterSolicitation(); |
| 139 | |
| 140 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 141 | |
| 142 | bb.getInt(); |
| 143 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 144 | if (bb.limit() - bb.position() > 0) { |
| 145 | NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer() |
| 146 | .deserialize(data, bb.position(), bb.limit() - bb.position()); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 147 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 148 | for (NeighborDiscoveryOptions.Option option : options.options()) { |
| 149 | routerSolicitation.addOption(option.type(), option.data()); |
| 150 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return routerSolicitation; |
| 154 | }; |
| 155 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 156 | |
| 157 | @Override |
| 158 | public String toString() { |
| 159 | return toStringHelper(getClass()) |
| 160 | .toString(); |
| 161 | // TODO: need to handle options |
| 162 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 163 | } |